GestureDrag.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using Rokid.UXR.Utility;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace Rokid.UXR.Interaction
  6. {
  7. [Obsolete("Use Draggable Instead")]
  8. public class GestureDrag : MonoBehaviour, IGesDrag, IGesBeginDrag, IGesEndDrag, IBezierCurveDrag
  9. {
  10. private float smoothSpeed = 8;
  11. private Vector3 allDelta;
  12. private Vector3 oriPos;
  13. [SerializeField]
  14. private bool lookAtCamera = true;
  15. private Vector3 preCameraPos;
  16. private Vector3 cameraPosDelta;
  17. [SerializeField]
  18. private bool UseBezierCurve = false;
  19. [SerializeField]
  20. private float moveLerpTime = 0.05f;
  21. private Vector3 HitLocalPos;
  22. private bool IsDraging;
  23. private void Start()
  24. {
  25. preCameraPos = MainCameraCache.mainCamera.transform.position;
  26. }
  27. private void Update()
  28. {
  29. cameraPosDelta = MainCameraCache.mainCamera.transform.position - preCameraPos;
  30. preCameraPos = MainCameraCache.mainCamera.transform.position;
  31. // RKLog.Info("cameraPosDelta.sqrMagnitude:" + cameraPosDelta.sqrMagnitude);
  32. }
  33. public void OnGesBeginDrag(PointerEventData eventData)
  34. {
  35. IsDraging = true;
  36. HitLocalPos = transform.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
  37. RKLog.Info($"{gameObject.name}: Begin Drag, DragHitLocalPos={HitLocalPos}");
  38. allDelta = Vector3.zero;
  39. oriPos = transform.position;
  40. }
  41. #region BezierCurve
  42. public bool IsEnablePinchBezierCurve()
  43. {
  44. return false;
  45. }
  46. public bool IsEnableGripBezierCurve()
  47. {
  48. return UseBezierCurve;
  49. }
  50. public bool IsInBezierCurveDragging()
  51. {
  52. return IsDraging;
  53. }
  54. public Vector3 GetBezierCurveEndPoint()
  55. {
  56. return transform.TransformPoint(HitLocalPos);
  57. }
  58. #endregion
  59. public void OnGesDrag(Vector3 delta)
  60. {
  61. float disToCamera = Vector3.Distance(MainCameraCache.mainCamera.transform.position, transform.position);
  62. if (disToCamera > 0.5f)
  63. {
  64. //远场拖拽
  65. preFilter(ref delta);
  66. allDelta += delta;
  67. Vector3 targetPos = oriPos + allDelta;
  68. smoothSpeed = 10;
  69. filter(ref targetPos, ref allDelta, ref oriPos, delta);
  70. if (lookAtCamera)
  71. {
  72. if (GetComponent<CanvasRenderer>())
  73. {
  74. Vector3 director = targetPos - MainCameraCache.mainCamera.transform.position;
  75. transform.localRotation = Quaternion.LookRotation(director);
  76. }
  77. else
  78. {
  79. //模型
  80. Vector3 director = MainCameraCache.mainCamera.transform.position - targetPos;
  81. director.y = 0;
  82. transform.localRotation = Quaternion.LookRotation(director);
  83. }
  84. }
  85. if (UseBezierCurve)
  86. {
  87. transform.position = Vector3.Lerp(transform.position, targetPos, (moveLerpTime == 0f) ? 1f : 1f - Mathf.Pow(moveLerpTime, Time.deltaTime));
  88. }
  89. else
  90. {
  91. transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * smoothSpeed);
  92. }
  93. }
  94. else
  95. {
  96. //近场拖拽
  97. filter01(ref delta);
  98. allDelta += delta;
  99. Vector3 targetPos = oriPos + allDelta;
  100. if (lookAtCamera)
  101. {
  102. if (GetComponent<CanvasRenderer>())
  103. {
  104. Vector3 director = targetPos - MainCameraCache.mainCamera.transform.position;
  105. transform.localRotation = Quaternion.LookRotation(director);
  106. }
  107. else
  108. {
  109. //模型
  110. Vector3 director = MainCameraCache.mainCamera.transform.position - targetPos;
  111. director.y = 0;
  112. transform.localRotation = Quaternion.LookRotation(director);
  113. }
  114. }
  115. transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * smoothSpeed * 2);
  116. }
  117. }
  118. private void preFilter(ref Vector3 delta)
  119. {
  120. if (cameraPosDelta.sqrMagnitude < 0.0001f)
  121. {
  122. Vector3 oriDelta = new Vector3(delta.x, delta.y, delta.z);
  123. float disToCamera = Vector3.Distance(MainCameraCache.mainCamera.transform.position, transform.position);
  124. delta *= disToCamera * 2;
  125. // 0.判断delta在相机空间中的分量
  126. Vector3 cameraSpaceDelta = MainCameraCache.mainCamera.transform.InverseTransformDirection(delta);
  127. if (cameraSpaceDelta.z * cameraSpaceDelta.z > (cameraSpaceDelta.x * cameraSpaceDelta.x + cameraSpaceDelta.y * cameraSpaceDelta.y))
  128. {
  129. //修改拖拽的朝向
  130. delta = Vector3.Normalize(MainCameraCache.mainCamera.transform.position - transform.position) * -(MainCameraCache.mainCamera.transform.InverseTransformDirection(oriDelta).z) * 5;
  131. }
  132. else
  133. {
  134. delta = MainCameraCache.mainCamera.transform.rotation * cameraSpaceDelta;
  135. }
  136. }
  137. }
  138. private void filter(ref Vector3 targetPos, ref Vector3 allDelta, ref Vector3 oriPos, Vector3 delta)
  139. {
  140. float disToCamera = Vector3.Distance(MainCameraCache.mainCamera.transform.position, transform.position);
  141. Vector3 cameraSpaceDelta = MainCameraCache.mainCamera.transform.InverseTransformDirection(delta);
  142. if (disToCamera < 0.5f && cameraSpaceDelta.z < 0)
  143. {
  144. targetPos = MainCameraCache.mainCamera.transform.position + MainCameraCache.mainCamera.transform.forward * 0.5f;
  145. allDelta = Vector3.zero;
  146. oriPos = transform.position;
  147. smoothSpeed = 5;
  148. }
  149. }
  150. private void filter01(ref Vector3 delta)
  151. {
  152. Vector3 cameraSpaceDelta = MainCameraCache.mainCamera.transform.InverseTransformDirection(delta);
  153. if (cameraSpaceDelta.z > 0)
  154. {
  155. cameraSpaceDelta.z *= 3;
  156. }
  157. delta = MainCameraCache.mainCamera.transform.rotation * cameraSpaceDelta;
  158. }
  159. public void OnGesEndDrag()
  160. {
  161. RKLog.Info($"{gameObject.name}: End Drag");
  162. IsDraging = false;
  163. }
  164. public Vector3 GetBezierCurveEndNormal()
  165. {
  166. return Vector3.forward;
  167. }
  168. }
  169. }