KeyboardFollower.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using UnityEngine;
  2. namespace SC.XR.Unity.Module_Keyboard
  3. {
  4. public class KeyboardFollower : FollowerBase
  5. {
  6. [Header("Following Settings")]
  7. public bool LinearFollowing;//Whether is linear movement.
  8. public Vector2 border_x; //x-axis boundary
  9. public Vector2 border_y; //y-axis boundary
  10. private Transform _slamHead;
  11. Vector2 initViewPoint;//The midpoint of the camera frame, the coordinates are (0.5f, 0.5f).
  12. Vector3 viewPoint = Vector3.zero;// Panel coordinates are relative to the coordinates of the camera screen.
  13. private Vector3 velocity = Vector3.zero;
  14. bool isFollower = false;
  15. Vector3 originPos = Vector3.zero;
  16. Vector3 desPos = Vector3.zero;
  17. float journeyLength = 0f;
  18. Vector3 offsetPos;
  19. Quaternion offsetRotation;
  20. protected override void OnEnable()
  21. {
  22. base.OnEnable();
  23. }
  24. private void Start()
  25. {
  26. InitTrans();
  27. }
  28. protected override void LateUpdate()
  29. {
  30. if (StopFollower == false)
  31. {
  32. if (IsFollower())
  33. {
  34. if (LinearFollowing)
  35. {
  36. LinearFollow();
  37. }
  38. else
  39. {
  40. Follow();
  41. }
  42. }
  43. }
  44. }
  45. void InitTrans()
  46. {
  47. if (!this.StopFollower && (Camera.main != null))
  48. {
  49. _slamHead = Camera.main.transform;
  50. transform.position = CalculateWindowPosition(Camera.main.transform);
  51. transform.rotation = CalculateWindowRotation(Camera.main.transform);
  52. initViewPoint = Camera.main.WorldToViewportPoint(_slamHead.position + (_slamHead.forward * WindowDistance));//The coordinates of the midpoint of the camera screen.
  53. }
  54. }
  55. protected bool IsFollower()
  56. {
  57. if ( Camera.main == null)
  58. {
  59. return false;
  60. }
  61. viewPoint = Camera.main.WorldToViewportPoint(transform.position);// The coordinates of the panel on the camera screen.
  62. if (viewPoint.x > initViewPoint.x + border_x.x + CalculateWindowOffsetX() || viewPoint.x < initViewPoint.x + border_x.y + CalculateWindowOffsetX() || viewPoint.y < initViewPoint.y + border_y.y + CalculateWindowOffsetY() || viewPoint.y > initViewPoint.y + border_y.x + CalculateWindowOffsetY())// Boundary judgment
  63. {
  64. isFollower = true;
  65. }
  66. else if (Mathf.Abs(viewPoint.x - initViewPoint.x - CalculateWindowOffsetX()) < 0.05f && Mathf.Abs(viewPoint.y - initViewPoint.y - CalculateWindowOffsetY()) < 0.09f)
  67. {
  68. isFollower = false;
  69. }
  70. return isFollower;
  71. }
  72. //Nonlinear Following by default
  73. protected override void Follow()
  74. {
  75. transform.position = Vector3.Lerp(transform.position, CalculateWindowPosition(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  76. //transform.position = Vector3.SmoothDamp(transform.position, CalculateWindowPosition(Camera.main.transform), ref velocity, WindowFollowSpeed);
  77. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), WindowFollowSpeed * Time.deltaTime);
  78. }
  79. //For Linear Following, turn down the WindowFollowSpeed to around 0.5 at distance = 1 for best experience
  80. protected void LinearFollow()
  81. {
  82. originPos = transform.position;
  83. desPos = CalculateWindowPosition(Camera.main.transform);
  84. journeyLength = Vector3.Distance(originPos, desPos);
  85. transform.position = Vector3.Lerp(originPos, desPos, (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  86. transform.rotation = Quaternion.Slerp(transform.rotation, CalculateWindowRotation(Camera.main.transform), (Time.fixedDeltaTime) / journeyLength * WindowFollowSpeed);
  87. }
  88. protected override Vector3 CalculateWindowPosition(Transform cameraTransform)
  89. {
  90. Vector3 position = cameraTransform.position + (Vector3.ProjectOnPlane(cameraTransform.forward, Vector3.up).normalized * WindowDistance);
  91. Vector3 horizontalOffset = cameraTransform.right * windowOffset.x;
  92. Vector3 verticalOffset = cameraTransform.up * windowOffset.y;
  93. switch (windowAnchor)
  94. {
  95. case TextAnchor.UpperLeft: position += verticalOffset - horizontalOffset; break;
  96. case TextAnchor.UpperCenter: position += verticalOffset; break;
  97. case TextAnchor.UpperRight: position += verticalOffset + horizontalOffset; break;
  98. case TextAnchor.MiddleLeft: position -= horizontalOffset; break;
  99. case TextAnchor.MiddleRight: position += horizontalOffset; break;
  100. case TextAnchor.MiddleCenter: position += horizontalOffset + verticalOffset; break;
  101. case TextAnchor.LowerLeft: position -= verticalOffset + horizontalOffset; break;
  102. case TextAnchor.LowerCenter: position -= verticalOffset; break;
  103. case TextAnchor.LowerRight: position -= verticalOffset - horizontalOffset; break;
  104. }
  105. return position;
  106. }
  107. protected override Quaternion CalculateWindowRotation(Transform cameraTransform)
  108. {
  109. Quaternion rotation = Quaternion.Euler(0, cameraTransform.rotation.eulerAngles.y, 0);
  110. switch (windowAnchor)
  111. {
  112. case TextAnchor.UpperLeft: rotation *= windowHorizontalRotationInverse * windowVerticalRotationInverse; break;
  113. case TextAnchor.UpperCenter: rotation *= windowHorizontalRotationInverse; break;
  114. case TextAnchor.UpperRight: rotation *= windowHorizontalRotationInverse * windowVerticalRotation; break;
  115. case TextAnchor.MiddleLeft: rotation *= windowVerticalRotationInverse; break;
  116. case TextAnchor.MiddleRight: rotation *= windowVerticalRotation; break;
  117. case TextAnchor.LowerLeft: rotation *= windowHorizontalRotation * windowVerticalRotationInverse; break;
  118. case TextAnchor.LowerCenter: rotation *= windowHorizontalRotation; break;
  119. case TextAnchor.LowerRight: rotation *= windowHorizontalRotation * windowVerticalRotation; break;
  120. }
  121. return rotation;
  122. }
  123. }
  124. }