RayVisual.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class RayVisual : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private RayInteractor rayInteractor;
  9. [SerializeField]
  10. private LineRenderer rayRenderer;
  11. [Tooltip("射线最小长度")]
  12. [SerializeField]
  13. protected float minRayLength;
  14. [Tooltip("射线最长长度")]
  15. [SerializeField]
  16. protected float maxRayLength;
  17. [Tooltip("射线默认长度")]
  18. [SerializeField]
  19. protected float normalRayLength;
  20. [SerializeField]
  21. private Transform rayOrigin;
  22. private Material rayMat;
  23. private bool dragging;
  24. [SerializeField]
  25. private HandType hand;
  26. private void Start()
  27. {
  28. rayInteractor.WhenPostprocessed += UpdateVisual;
  29. rayInteractor.WhenStateChanged += HandleStateChanged;
  30. RKPointerLisener.OnPointerDragEnd += OnPointerDragEnd;
  31. RKPointerLisener.OnPointerDragBegin += OnPointerDragBegin;
  32. InteractorStateChange.OnHandDragStatusChanged += OnHandDragStatusChanged;
  33. rayMat = rayRenderer.material;
  34. }
  35. private void OnDestroy()
  36. {
  37. rayInteractor.WhenPostprocessed -= UpdateVisual;
  38. rayInteractor.WhenStateChanged -= HandleStateChanged;
  39. RKPointerLisener.OnPointerDragEnd -= OnPointerDragEnd;
  40. RKPointerLisener.OnPointerDragBegin -= OnPointerDragBegin;
  41. InteractorStateChange.OnHandDragStatusChanged -= OnHandDragStatusChanged;
  42. }
  43. private void OnHandDragStatusChanged(HandType hand, bool dragging)
  44. {
  45. if (hand == this.hand)
  46. {
  47. if (dragging)
  48. {
  49. this.dragging = true;
  50. if (rayInteractor.Interactable != null)
  51. {
  52. allCanDragObjs = rayInteractor.Interactable.GetComponentsInChildren<IBezierCurveDrag>();
  53. curentDragObj = GetCurrentDragObj();
  54. }
  55. }
  56. else
  57. {
  58. allCanDragObjs = null;
  59. curentDragObj = null;
  60. this.dragging = false;
  61. }
  62. }
  63. }
  64. private void OnEnable()
  65. {
  66. RKPointerLisener.OnGraphicPointerEnter += OnPointerEnter;
  67. RKPointerLisener.OnGraphicPointerExit += OnPointerExit;
  68. RKPointerLisener.OnGraphicPointerHover += OnPointerHover;
  69. }
  70. private void OnDisable()
  71. {
  72. RKPointerLisener.OnGraphicPointerEnter -= OnPointerEnter;
  73. RKPointerLisener.OnGraphicPointerExit -= OnPointerExit;
  74. RKPointerLisener.OnGraphicPointerHover -= OnPointerHover;
  75. IsInHover = false;
  76. }
  77. private void OnPointerDragBegin(PointerEventData pointerEventData)
  78. {
  79. if (hand == HandType.None)
  80. {
  81. dragging = true;
  82. if (rayInteractor.Interactable != null)
  83. {
  84. allCanDragObjs = rayInteractor.Interactable.GetComponentsInChildren<IBezierCurveDrag>();
  85. curentDragObj = GetCurrentDragObj();
  86. }
  87. }
  88. }
  89. private void OnPointerDragEnd(PointerEventData pointerEventData)
  90. {
  91. if (hand == HandType.None)
  92. {
  93. allCanDragObjs = null;
  94. curentDragObj = null;
  95. dragging = false;
  96. }
  97. }
  98. private void HandleStateChanged(InteractorStateChangeArgs args)
  99. {
  100. UpdateVisual();
  101. }
  102. private bool IsBezierCurveDragging()
  103. {
  104. return dragging && curentDragObj != null && curentDragObj.IsInBezierCurveDragging() && (curentDragObj.IsEnablePinchBezierCurve() || curentDragObj.IsEnableGripBezierCurve());
  105. }
  106. private void UpdateVisual()
  107. {
  108. LogHandRayInfo();
  109. rayRenderer.enabled = true;
  110. if (IsBezierCurveDragging())
  111. {
  112. SetLineEndPoints(curentDragObj.GetBezierCurveEndPoint());
  113. DrawBezierRay();
  114. return;
  115. }
  116. else if (rayInteractor.State == InteractorState.Disabled)
  117. {
  118. rayRenderer.enabled = false;
  119. return;
  120. }
  121. switch (rayInteractor.State)
  122. {
  123. case InteractorState.Normal:
  124. DrawRay(normalRayLength, false);
  125. break;
  126. case InteractorState.Hover:
  127. if (IsInHover && HoverPosition != Vector3.zero && HoverNormal != Vector3.zero)
  128. {
  129. DrawRay(transform.InverseTransformPoint(HoverPosition), false);
  130. }
  131. else
  132. {
  133. if (rayInteractor.CollisionInfo != null)
  134. {
  135. DrawRay(transform.InverseTransformPoint(rayInteractor.End).z, false);
  136. }
  137. else
  138. {
  139. DrawRay(normalRayLength, false);
  140. }
  141. }
  142. break;
  143. case InteractorState.Select:
  144. if (IsInHover && HoverPosition != Vector3.zero && HoverNormal != Vector3.zero)
  145. {
  146. DrawRay(transform.InverseTransformPoint(HoverPosition), true);
  147. }
  148. else
  149. {
  150. DrawRay(transform.InverseTransformPoint(rayInteractor.End).z, true);
  151. }
  152. break;
  153. }
  154. }
  155. #region LogHandRayInfo
  156. private float logTime = 5;
  157. private float logElapsedTime = 0;
  158. private void LogHandRayInfo()
  159. {
  160. logElapsedTime += Time.deltaTime;
  161. if (logElapsedTime > logTime)
  162. {
  163. logElapsedTime = 0;
  164. RKLog.KeyInfo($"====RayVisual==== KeyInfo handType: {hand}, renderPosition: {rayRenderer.GetPosition(0)},{rayRenderer.GetPosition(1)}, rayVisualPosition:{transform.position},dragging:{dragging},IsBezierCurveDragging: {IsBezierCurveDragging()},InteractorState:{rayInteractor.State} ");
  165. }
  166. }
  167. #endregion
  168. protected void DrawRay(float length, bool isPress)
  169. {
  170. rayRenderer.positionCount = 2;
  171. rayRenderer.useWorldSpace = false;
  172. rayRenderer.textureMode = LineTextureMode.RepeatPerSegment;
  173. rayRenderer.SetPosition(0, rayOrigin != null ? rayOrigin.localPosition : new Vector3(0, 0, 0));
  174. rayRenderer.SetPosition(1, new Vector3(0, 0, length > 0 ? length : 0));
  175. rayMat?.SetFloat("_Length", length);
  176. rayMat?.SetFloat("_IsPress", isPress ? 1 : 0);
  177. }
  178. protected void DrawRay(Vector3 endPosition, bool isPress)
  179. {
  180. float length = Vector3.Distance(rayOrigin != null ? rayOrigin.localPosition : new Vector3(0, 0, 0), endPosition);
  181. rayRenderer.positionCount = 2;
  182. rayRenderer.useWorldSpace = false;
  183. rayRenderer.textureMode = LineTextureMode.RepeatPerSegment;
  184. rayRenderer.SetPosition(0, rayOrigin != null ? rayOrigin.localPosition : new Vector3(0, 0, 0));
  185. rayRenderer.SetPosition(1, length > 0 ? endPosition : rayOrigin != null ? rayOrigin.localPosition : new Vector3(0, 0, 0));
  186. rayMat?.SetFloat("_Length", length);
  187. rayMat?.SetFloat("_IsPress", isPress ? 1 : 0);
  188. }
  189. #region DrawBezierCurve
  190. [SerializeField]
  191. private float lineStartClamp = 0.0001f;
  192. [SerializeField]
  193. private float lineEndClamp = 0.8028384f;
  194. private float startPointLerp = 0.33f;
  195. private float endPointLerp = 0.66f;
  196. private Vector3[] Points = new Vector3[4];
  197. private Vector3[] positions;
  198. private int lineStepCount = 16;
  199. private int LineStepCount
  200. {
  201. get => lineStepCount;
  202. set => lineStepCount = Mathf.Clamp(value, 2, 2048);
  203. }
  204. private IBezierCurveDrag[] allCanDragObjs;
  205. private IBezierCurveDrag curentDragObj;
  206. private IBezierCurveDrag GetCurrentDragObj()
  207. {
  208. if (allCanDragObjs == null || allCanDragObjs.Length == 0)
  209. {
  210. return null;
  211. }
  212. foreach (var Drag in allCanDragObjs)
  213. {
  214. if (Drag.IsInBezierCurveDragging())
  215. {
  216. return Drag;
  217. }
  218. }
  219. return null;
  220. }
  221. private int PointCount { get { return 4; } }
  222. public Vector3 FirstPoint
  223. {
  224. get => GetPoint(0);
  225. set => SetPoint(0, value);
  226. }
  227. public Vector3 LastPoint
  228. {
  229. get => GetPoint(PointCount - 1);
  230. set => SetPoint(PointCount - 1, value);
  231. }
  232. private void SetLinePoints(Vector3 startPoint, Vector3 endPoint)
  233. {
  234. FirstPoint = startPoint;
  235. LastPoint = endPoint;
  236. }
  237. private void SetLineEndPoints(Vector3 endPoint)
  238. {
  239. LastPoint = endPoint;
  240. }
  241. private Vector3 GetPoint(int pointIndex)
  242. {
  243. if (pointIndex < 0 || pointIndex >= PointCount)
  244. {
  245. Debug.LogError("Invalid point index");
  246. return Vector3.zero;
  247. }
  248. Vector3 point = Points[pointIndex];
  249. point = transform.TransformPoint(point);
  250. return point;
  251. }
  252. public Vector3 GetPoint(float normalizedLength)
  253. {
  254. normalizedLength = Mathf.Lerp(lineStartClamp, lineEndClamp, Mathf.Clamp01(normalizedLength));
  255. Vector3 point = GetPointInternal(normalizedLength);
  256. point = transform.TransformPoint(point);
  257. return point;
  258. }
  259. private void SetPoint(int pointIndex, Vector3 point)
  260. {
  261. if (pointIndex < 0 || pointIndex >= PointCount)
  262. {
  263. Debug.LogError("Invalid point index");
  264. return;
  265. }
  266. point = transform.InverseTransformPoint(point);
  267. Points[pointIndex] = point;
  268. }
  269. private Vector3 InterpolateBezierPoints(Vector3 point1, Vector3 point2, Vector3 point3, Vector3 point4, float normalizedLength)
  270. {
  271. float invertedDistance = 1f - normalizedLength;
  272. return invertedDistance * invertedDistance * invertedDistance * point1 +
  273. 3f * invertedDistance * invertedDistance * normalizedLength * point2 +
  274. 3f * invertedDistance * normalizedLength * normalizedLength * point3 +
  275. normalizedLength * normalizedLength * normalizedLength * point4;
  276. }
  277. private Vector3 GetPointInternal(float normalizedDistance)
  278. {
  279. return InterpolateBezierPoints(Points[0], Points[1], Points[2], Points[3], normalizedDistance);
  280. }
  281. private void DrawBezierRay()
  282. {
  283. float distance = Vector3.Distance(transform.position, LastPoint);
  284. Vector3 startPoint = FirstPoint;
  285. Vector3 expectedPoint = startPoint + transform.forward * distance;
  286. SetPoint(1, Vector3.Lerp(startPoint, expectedPoint, startPointLerp));
  287. expectedPoint = Vector3.Lerp(expectedPoint, LastPoint, endPointLerp);
  288. SetPoint(2, Vector3.Lerp(startPoint, expectedPoint, endPointLerp));
  289. rayRenderer.positionCount = LineStepCount;
  290. rayRenderer.useWorldSpace = false;
  291. rayRenderer.textureMode = LineTextureMode.Stretch;
  292. if (positions == null || positions.Length != rayRenderer.positionCount)
  293. {
  294. positions = new Vector3[rayRenderer.positionCount];
  295. }
  296. for (int i = 0; i < positions.Length; i++)
  297. {
  298. float normalizedDistance = (1f / (LineStepCount - 1)) * i;
  299. positions[i] = transform.InverseTransformPoint(GetPoint(normalizedDistance));
  300. }
  301. // Set positions
  302. rayRenderer.positionCount = positions.Length;
  303. rayRenderer.SetPositions(positions);
  304. rayMat?.SetFloat("_IsPress", 1);
  305. }
  306. #endregion
  307. #region ForRayHoverPose
  308. private bool IsInHover;
  309. private Vector3 HoverPosition;
  310. private Vector3 HoverNormal;
  311. private void OnPointerEnter(PointerEventData eventData)
  312. {
  313. if (RayInteractor.GetHandTypeByIdentifier(eventData.pointerId) == hand && eventData.pointerCurrentRaycast.gameObject != null)
  314. {
  315. IsInHover = true;
  316. HoverPosition = Vector3.zero;
  317. HoverNormal = Vector3.zero;
  318. }
  319. }
  320. private void OnPointerExit(PointerEventData eventData)
  321. {
  322. if (RayInteractor.GetHandTypeByIdentifier(eventData.pointerId) == hand)
  323. {
  324. IsInHover = false;
  325. HoverPosition = Vector3.zero;
  326. HoverNormal = Vector3.zero;
  327. }
  328. }
  329. private void OnPointerHover(PointerEventData eventData)
  330. {
  331. if (IsInHover && RayInteractor.GetHandTypeByIdentifier(eventData.pointerId) == hand)
  332. {
  333. HoverPosition = eventData.pointerCurrentRaycast.worldPosition;
  334. HoverNormal = eventData.pointerCurrentRaycast.worldNormal;
  335. }
  336. }
  337. #endregion
  338. }
  339. }