You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.5 KiB

using System.Collections;
using System.Collections.Generic;
using UltraCombos.Utility.Attributes;
using UnityEngine;
public enum Area
{
LeftWall,
RightWall,
TopWall,
Floor
}
public class FrozenScreenToWorldSpace : MonoBehaviour
{
public static FrozenScreenToWorldSpace Instance
{
get
{
return instacnce;
}
}
private static FrozenScreenToWorldSpace instacnce;
public float width = 8;
public float depth = 5;
public float height = 3;
public float blackLength = 0.89f;
public float PixelsByMeter = 360;
public float finalPixelsByMeter = 90;
[Header("Debug")]
[SerializeField, ReadOnly]
Rect LeftWall;
[SerializeField, ReadOnly]
Rect TopWall;
[SerializeField, ReadOnly]
Rect RightWall;
[SerializeField, ReadOnly]
Rect Floor;
Dictionary<Area, Rect> RenderTextureROIList = new Dictionary<Area, Rect>();
private void Awake()
{
instacnce = this;
InitailROISetting();
}
void InitailROISetting()
{
var totalWidth = height * 2 + width;
var totalHeight = height + depth + blackLength;
var nWidth = width / totalWidth;//normalized
var nWidth2 = height / totalWidth;
var nHeight = height / totalHeight;
var nHeight2 = depth / totalHeight;
TopWall = new Rect(nWidth2, 0, nWidth, nHeight);
LeftWall = new Rect(0, nHeight, nWidth2, nHeight2);
Floor = new Rect(nWidth2, nHeight, nWidth, nHeight2);
RightWall = new Rect(nWidth + nWidth2, nHeight, nWidth2, nHeight2);
RenderTextureROIList = new Dictionary<Area, Rect>()
{
[Area.LeftWall] = LeftWall,
[Area.RightWall] = RightWall,
[Area.TopWall] = TopWall,
[Area.Floor] = Floor,
};
}
public Vector2 GetFinalScreenPos(Area area, Vector2 wallROI)
{
if (!RenderTextureROIList.ContainsKey(area))
return Vector2.zero;
Rect textureROI = RenderTextureROIList[area];
Vector2 currenPos = Vector2.zero;
switch (area)
{
case Area.LeftWall:
currenPos = Rect.NormalizedToPoint(textureROI, new Vector2(1 - wallROI.y, 1 - wallROI.x));
break;
case Area.RightWall:
currenPos = Rect.NormalizedToPoint(textureROI, new Vector2(wallROI.y, 1 - wallROI.x));
break;
case Area.TopWall:
currenPos = Rect.NormalizedToPoint(textureROI, new Vector2(wallROI.x, 1 - wallROI.y));
break;
}
currenPos = new Vector2(currenPos.x * Screen.width, (1 - currenPos.y) * Screen.height);
return currenPos;
}
public Vector2 GetWallRoiFromPosition(Area area, Vector3 position)
{
position -= transform.position;
Vector2 roi = Vector2.zero;
switch (area)
{
case Area.TopWall:
roi.x = (position.x + width * 0.5f) / width;
roi.y = position.y / height;
break;
case Area.LeftWall:
roi.x = (position.z + depth * 0.5f) / depth;
roi.y = position.y / height;
break;
case Area.RightWall:
roi.x = (position.z + depth * 0.5f) / depth;
roi.y = position.y / height;
break;
}
// Debug.Log($"{area} {position.ToString()} {roi.ToString()}");
return roi;
}
public Vector3 Position { get { return transform.position; } }
}