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.

161 lines
5.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Area
{
LeftWall,
RightWall,
TopWall,
Floor
}
public class FrozenScreenToWorldSpace : MonoBehaviour {
public static FrozenScreenToWorldSpace Instance {
get {
/*
if (instacnce == null && GameObject.Find("FrozenScreenToWorldSpace").GetComponent<FrozenScreenToWorldSpace>() != null)
instacnce = GameObject.Find("FrozenScreenToWorldSpace").GetComponent<FrozenScreenToWorldSpace>();
else
instacnce = new GameObject("FrozenScreenToWorldSpace").AddComponent<FrozenScreenToWorldSpace>();
*/
return instacnce;
}
}
private static FrozenScreenToWorldSpace instacnce;
public float width = 8;
public float length = 5;
public float height = 3;
public float blackLength = 0.89f;
public float PixelsByMeter = 360;
public float finalPixelsByMeter = 90;
[System.Serializable]
public class RenderTextureROI
{
public float X;
public float Y;
public float ROI_X;
public float ROI_Y;
public RenderTextureROI(float x, float y, float roi_x, float roi_y)
{
X = x;
Y = y;
ROI_X = roi_x;
ROI_Y = roi_y;
}
public RenderTextureROI() { }
}
[SerializeField]
RenderTextureROI LeftWall = new RenderTextureROI();
[SerializeField]
RenderTextureROI TopWall = new RenderTextureROI();
[SerializeField]
RenderTextureROI RightWall = new RenderTextureROI();
[SerializeField]
RenderTextureROI Floor = new RenderTextureROI();
Dictionary<Area, RenderTextureROI> RenderTextureROIList = new Dictionary<Area, RenderTextureROI>();
Dictionary<Area, Plane> wallPlaneList = new Dictionary<Area, Plane>();
private float totalLength;
private float totalWidth;
private void Awake()
{
instacnce = this;
InitailROISetting();
}
void InitailROISetting()
{
totalLength = height * 2 + width;
totalWidth = height + length + blackLength;
LeftWall = new RenderTextureROI(0, height / totalWidth, height / totalLength, length / totalWidth);
RightWall = new RenderTextureROI((height + width) / totalLength, height / totalWidth, height / totalLength, length / totalWidth);
TopWall = new RenderTextureROI((height) / totalLength, 0, width / totalLength, height / totalWidth);
Floor = new RenderTextureROI(height / totalLength, height / totalWidth, width / totalLength, length / totalWidth);
RenderTextureROIList.Add(Area.LeftWall, LeftWall);
RenderTextureROIList.Add(Area.RightWall, RightWall);
RenderTextureROIList.Add(Area.TopWall, TopWall);
RenderTextureROIList.Add(Area.Floor, Floor);
wallPlaneList.Add(Area.LeftWall, new Plane(Vector3.right, new Vector3(-width * 0.5f, 0, 0)));
wallPlaneList.Add(Area.RightWall, new Plane(Vector3.left, new Vector3(width * 0.5f, 0, 0)));
wallPlaneList.Add(Area.TopWall, new Plane(Vector3.back, new Vector3(0, 0, length * 0.5f)));
}
public RenderTextureROI GetROI(Area area)
{
if (RenderTextureROIList.ContainsKey(area))
return RenderTextureROIList[area];
return null;
}
public Plane GetWallPlane(Area area)
{
if (wallPlaneList.ContainsKey(area))
return wallPlaneList[area];
return new Plane();
}
public Vector2 GetFinalScreenPos(Area area, Vector2 wallROI)
{
if(!RenderTextureROIList.ContainsKey(area))
return Vector2.zero;
RenderTextureROI textureROI = GetROI(area);
Vector2 currenPos = Vector2.zero;
if (area == Area.LeftWall)
{
float x = textureROI.X + (1 - wallROI.y) * textureROI.ROI_X;
float y = textureROI.Y + (1 - wallROI.x) * textureROI.ROI_Y;
currenPos = new Vector2(x, y);
}
if (area == Area.RightWall)
{
float x = textureROI.X + wallROI.y * textureROI.ROI_X;
float y = textureROI.Y + (1 - wallROI.x) * textureROI.ROI_Y;
currenPos = new Vector2(x, y);
}
if (area == Area.TopWall)
{
float x = textureROI.X + wallROI.x * textureROI.ROI_X;
float y = textureROI.Y + (1 - wallROI.y) * textureROI.ROI_Y;
currenPos = new Vector2(x, y);
}
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 + length * 0.5f) / length;
roi.y = position.y / height;
}
break;
case Area.RightWall:
{
roi.x = (position.z + length * 0.5f) / length;
roi.y = position.y / height;
}
break;
}
return roi;
}
public Vector3 Position { get { return transform.position; } }
}