123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using UnityEngine;
- using System.Collections;
- public class CameraView
- {
- //距离摄像机8.5米 用黄色表示
- public float upperDistance = 8.5f;
- //距离摄像机12米 用红色表示
- public float lowerDistance = 12.0f;
- private Transform tx;
- void Start()
- {
- }
- void Update()
- {
- // FindUpperCorners();
- //FindLowerCorners();
- }
- public static Vector3 PosChange(float a, float b,float dic,Transform cam)
- {
- Vector3[] v3List = GetCorners(dic, cam);
- Vector3 posX = Vector3.zero;
- float juli = Vector3.Distance(v3List[1],v3List[0]);
- Vector3 vector = (v3List[1] - v3List[0]).normalized; //向量单位化
- float rand = juli*a;//随机距离
- posX = vector * rand; //得到新坐标
- posX += v3List[0];
- Vector3[] v3ListY = GetCorners(1f, cam);
- Vector3 posY = Vector3.zero;
- float juliY = Vector3.Distance(v3List[2], v3List[0]);
- Vector3 vectorY = (v3List[0] - v3List[2]).normalized; //向量单位化
- float randY = b * juliY;//随机距离
- posY = vectorY * randY; //得到新坐标
- posY += v3List[2];
- Vector3[] v3ListZ = GetCorners(1.5f, cam);
- Vector3 posZ = Vector3.zero;
- float juliZ = Vector3.Distance(v3List[2], v3List[0]);
- Vector3 vectorZ = (v3List[0] - v3List[2]).normalized; //向量单位化
- float randZ = b * juliZ;//随机距离
- posZ = vectorZ * randZ; //得到新坐标
- posZ += v3List[2];
- return cam.TransformPoint(new Vector3(posX.x, posY.y, posZ.z));
- }
- void FindUpperCorners()
- {
- // Vector3[] corners = GetCorners(upperDistance);
- // for debugging
- // Debug.DrawLine(corners[0], corners[1], Color.yellow); // UpperLeft -> UpperRight
- // Debug.DrawLine(corners[1], corners[3], Color.yellow); // UpperRight -> LowerRight
- // Debug.DrawLine(corners[3], corners[2], Color.yellow); // LowerRight -> LowerLeft
- // Debug.DrawLine(corners[2], corners[0], Color.yellow); // LowerLeft -> UpperLeft
- }
- void FindLowerCorners()
- {
- // Vector3[] corners = GetCorners(lowerDistance);
- // for debugging
- // Debug.DrawLine(corners[0], corners[1], Color.red);
- // Debug.DrawLine(corners[1], corners[3], Color.red);
- // Debug.DrawLine(corners[3], corners[2], Color.red);
- // Debug.DrawLine(corners[2], corners[0], Color.red);
- }
- public static Vector3[] GetCorners(float distance,Transform cam)
- {
- Vector3[] corners = new Vector3[4];
- float halfFOV = (RemoteRtc.Instance.cam.fieldOfView * 0.5f) * Mathf.Deg2Rad;
- float aspect = RemoteRtc.Instance.cam.aspect;
- float height = distance * Mathf.Tan(halfFOV);
- float width = height * aspect;
- // UpperLeft
- corners[0] = RemoteRtc.Instance.cam.transform.position - (RemoteRtc.Instance.cam.transform.right * width);
- corners[0] += RemoteRtc.Instance.cam.transform.up * height;
- corners[0] += RemoteRtc.Instance.cam.transform.forward * distance;
- corners[0] = cam.InverseTransformPoint(corners[0]);
- // UpperRight
- corners[1] = RemoteRtc.Instance.cam.transform.position + (RemoteRtc.Instance.cam.transform.right * width);
- corners[1] += RemoteRtc.Instance.cam.transform.up * height;
- corners[1] += RemoteRtc.Instance.cam.transform.forward * distance;
- corners[1] = cam.InverseTransformPoint(corners[1]);
- // LowerLeft
- corners[2] = RemoteRtc.Instance.cam.transform.position - (RemoteRtc.Instance.cam.transform.right * width);
- corners[2] -= RemoteRtc.Instance.cam.transform.up * height;
- corners[2] += RemoteRtc.Instance.cam.transform.forward * distance;
- corners[2] = cam.InverseTransformPoint(corners[2]);
- // LowerRight
- corners[3] = RemoteRtc.Instance.cam.transform.position + (RemoteRtc.Instance.cam.transform.right * width);
- corners[3] -= RemoteRtc.Instance.cam.transform.up * height;
- corners[3] += RemoteRtc.Instance.cam.transform.forward * distance;
- corners[3] = cam.InverseTransformPoint(corners[3]);
- return corners;
- }
- }
|