CameraFollowerEdustry.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public class CameraFollowerEdustry : FollowerBase
  7. {
  8. [Header("Following Settings")]
  9. public bool InstantFollowing;//是否无延迟跟随
  10. public bool LinearFollowing;//是否线性移动
  11. public Vector2 menu_size;//面板的长宽,默认面板的position在中心。
  12. private Transform _slamHead;
  13. Vector2 initViewPoint;//相机画面的中点,坐标为(0.5f,0.5f)
  14. Vector3 viewPoint = Vector3.zero;// 面板坐标相对于相机画面的坐标。
  15. Vector3 viewPoint_right = Vector3.zero;// 面板的上下左右端相对于相机画面的坐标。
  16. Vector3 viewPoint_left = Vector3.zero;
  17. Vector3 viewPoint_top = Vector3.zero;
  18. Vector3 viewPoint_bot = Vector3.zero;
  19. bool isFollower = false;
  20. Vector3 originPos = Vector3.zero;
  21. Vector3 desPos = Vector3.zero;
  22. float journeyLength = 0f;
  23. public Camera Cam;
  24. void Start()
  25. {
  26. _slamHead = Cam.transform;
  27. initViewPoint = Cam.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));
  28. InstantFollow();
  29. }
  30. //Nonlinear Following by default
  31. protected override void Follow()
  32. {
  33. if (Cam == null)
  34. {
  35. return;
  36. }
  37. transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Cam.transform), WindowFollowSpeed * Time.deltaTime);
  38. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Cam.transform), WindowFollowSpeed * Time.deltaTime);
  39. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  40. }
  41. //Following camera without latency
  42. protected void InstantFollow()
  43. {
  44. transform.position = CalculateWindowPosition(Cam.transform);
  45. transform.rotation = CalculateWindowRotation(Cam.transform);
  46. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  47. }
  48. //For Linear Following, turn down the WindowFollowSpeed to around 0.5 at distance = 1 for best experience
  49. protected void LinearFollow()
  50. {
  51. originPos = transform.position;
  52. desPos = CalculateWindowPosition(Cam.transform);
  53. journeyLength = Vector3.Distance(originPos, desPos);
  54. transform.position = Vector3.Lerp(originPos, desPos, (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  55. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Cam.transform), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  56. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  57. }
  58. protected override void LateUpdate()
  59. {
  60. if ((StopFollower == false)&& !InstantFollowing)
  61. {
  62. if (IsFollower() && !LinearFollowing)
  63. {
  64. Follow();
  65. }
  66. else if (IsFollower() && LinearFollowing)
  67. {
  68. LinearFollow();
  69. }
  70. else
  71. {
  72. Cam.transform.SetParent(null);
  73. Cam.transform.position = OpenXRCamera.Instance.head.position;
  74. Cam.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.transform.eulerAngles.y, 0);
  75. }
  76. }
  77. else if ((StopFollower == false) && InstantFollowing)
  78. {
  79. InstantFollow();
  80. }else
  81. {
  82. Cam.transform.SetParent(null);
  83. Cam.transform.position = OpenXRCamera.Instance.head.position;
  84. Cam.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.transform.eulerAngles.y, 0);
  85. }
  86. }
  87. protected bool IsFollower()
  88. {
  89. if (Cam == null || Cam == null)
  90. {
  91. return false;
  92. }
  93. viewPoint = Cam.WorldToViewportPoint(transform.position);// 面板在相机画面上的坐标
  94. viewPoint_right = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(menu_size.x/2, 0, 0)));// 面板边界在相机画面上的坐标
  95. viewPoint_left = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(-menu_size.x/2, 0, 0)));
  96. viewPoint_top = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, menu_size.y/2, 0)));
  97. viewPoint_bot = Cam.WorldToViewportPoint(transform.TransformPoint(new Vector3(0, -menu_size.y/2, 0)));
  98. if (viewPoint_right.x > 1 || viewPoint_left.x < 0)//边界判断// || viewPoint_bot.y < 0 || viewPoint_top.y > 1
  99. {
  100. isFollower = true;
  101. }
  102. else if (Mathf.Abs(viewPoint.x - initViewPoint.x) < 0.05f && Mathf.Abs(viewPoint.y - initViewPoint.y) < 0.05f)//停止跟随的边界判断
  103. {
  104. isFollower = false;
  105. }
  106. return isFollower;
  107. }
  108. }
  109. }