RayVisual.cs 14 KB

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