CameraView.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using UnityEngine;
  2. using System.Collections;
  3. public class CameraView
  4. {
  5. //距离摄像机8.5米 用黄色表示
  6. public float upperDistance = 8.5f;
  7. //距离摄像机12米 用红色表示
  8. public float lowerDistance = 12.0f;
  9. private Transform tx;
  10. void Start()
  11. {
  12. }
  13. void Update()
  14. {
  15. // FindUpperCorners();
  16. //FindLowerCorners();
  17. }
  18. public static Vector3 PosChange(float a, float b,float dic,Transform cam)
  19. {
  20. Vector3[] v3List = GetCorners(dic, cam);
  21. Vector3 posX = Vector3.zero;
  22. float juli = Vector3.Distance(v3List[1],v3List[0]);
  23. Vector3 vector = (v3List[1] - v3List[0]).normalized; //向量单位化
  24. float rand = juli*a;//随机距离
  25. posX = vector * rand; //得到新坐标
  26. posX += v3List[0];
  27. Vector3[] v3ListY = GetCorners(1f, cam);
  28. Vector3 posY = Vector3.zero;
  29. float juliY = Vector3.Distance(v3List[2], v3List[0]);
  30. Vector3 vectorY = (v3List[0] - v3List[2]).normalized; //向量单位化
  31. float randY = b * juliY;//随机距离
  32. posY = vectorY * randY; //得到新坐标
  33. posY += v3List[2];
  34. Vector3[] v3ListZ = GetCorners(1.5f, cam);
  35. Vector3 posZ = Vector3.zero;
  36. float juliZ = Vector3.Distance(v3List[2], v3List[0]);
  37. Vector3 vectorZ = (v3List[0] - v3List[2]).normalized; //向量单位化
  38. float randZ = b * juliZ;//随机距离
  39. posZ = vectorZ * randZ; //得到新坐标
  40. posZ += v3List[2];
  41. return cam.TransformPoint(new Vector3(posX.x, posY.y, posZ.z));
  42. }
  43. void FindUpperCorners()
  44. {
  45. // Vector3[] corners = GetCorners(upperDistance);
  46. // for debugging
  47. // Debug.DrawLine(corners[0], corners[1], Color.yellow); // UpperLeft -> UpperRight
  48. // Debug.DrawLine(corners[1], corners[3], Color.yellow); // UpperRight -> LowerRight
  49. // Debug.DrawLine(corners[3], corners[2], Color.yellow); // LowerRight -> LowerLeft
  50. // Debug.DrawLine(corners[2], corners[0], Color.yellow); // LowerLeft -> UpperLeft
  51. }
  52. void FindLowerCorners()
  53. {
  54. // Vector3[] corners = GetCorners(lowerDistance);
  55. // for debugging
  56. // Debug.DrawLine(corners[0], corners[1], Color.red);
  57. // Debug.DrawLine(corners[1], corners[3], Color.red);
  58. // Debug.DrawLine(corners[3], corners[2], Color.red);
  59. // Debug.DrawLine(corners[2], corners[0], Color.red);
  60. }
  61. public static Vector3[] GetCorners(float distance,Transform cam)
  62. {
  63. Vector3[] corners = new Vector3[4];
  64. float halfFOV = (RemoteRtc.Instance.cam.fieldOfView * 0.5f) * Mathf.Deg2Rad;
  65. float aspect = RemoteRtc.Instance.cam.aspect;
  66. float height = distance * Mathf.Tan(halfFOV);
  67. float width = height * aspect;
  68. // UpperLeft
  69. corners[0] = RemoteRtc.Instance.cam.transform.position - (RemoteRtc.Instance.cam.transform.right * width);
  70. corners[0] += RemoteRtc.Instance.cam.transform.up * height;
  71. corners[0] += RemoteRtc.Instance.cam.transform.forward * distance;
  72. corners[0] = cam.InverseTransformPoint(corners[0]);
  73. // UpperRight
  74. corners[1] = RemoteRtc.Instance.cam.transform.position + (RemoteRtc.Instance.cam.transform.right * width);
  75. corners[1] += RemoteRtc.Instance.cam.transform.up * height;
  76. corners[1] += RemoteRtc.Instance.cam.transform.forward * distance;
  77. corners[1] = cam.InverseTransformPoint(corners[1]);
  78. // LowerLeft
  79. corners[2] = RemoteRtc.Instance.cam.transform.position - (RemoteRtc.Instance.cam.transform.right * width);
  80. corners[2] -= RemoteRtc.Instance.cam.transform.up * height;
  81. corners[2] += RemoteRtc.Instance.cam.transform.forward * distance;
  82. corners[2] = cam.InverseTransformPoint(corners[2]);
  83. // LowerRight
  84. corners[3] = RemoteRtc.Instance.cam.transform.position + (RemoteRtc.Instance.cam.transform.right * width);
  85. corners[3] -= RemoteRtc.Instance.cam.transform.up * height;
  86. corners[3] += RemoteRtc.Instance.cam.transform.forward * distance;
  87. corners[3] = cam.InverseTransformPoint(corners[3]);
  88. return corners;
  89. }
  90. }