RKHandWatch.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using Rokid.UXR.Utility;
  5. namespace Rokid.UXR.Interaction
  6. {
  7. /// <summary>
  8. /// RKWatch
  9. /// </summary>
  10. public class RKHandWatch : MonoBehaviour
  11. {
  12. [SerializeField, Tooltip("跟随的手")]
  13. private HandType followhand = HandType.LeftHand;
  14. [SerializeField, Tooltip("跟随手的骨骼")]
  15. private SkeletonIndexFlag followSkeletonIndex = SkeletonIndexFlag.WRIST;
  16. private bool active = false;
  17. private bool handActive = false;
  18. private bool dragging = false;
  19. #region Event
  20. public static Action<HandType, bool> OnActiveWatch;
  21. public static Action<HandType, Pose> OnWatchPoseUpdate;
  22. #endregion
  23. private void Start()
  24. {
  25. GesEventInput.OnTrackedFailed += OnTrackedFailed;
  26. GesEventInput.OnTrackedSuccess += OnTrackedSuccess;
  27. RKPointerLisener.OnPointerDragBegin += OnPointerDragBegin;
  28. RKPointerLisener.OnPointerDragEnd += OnPointerDragEnd;
  29. }
  30. private void OnPointerDragEnd(PointerEventData data)
  31. {
  32. dragging = false;
  33. }
  34. private void OnPointerDragBegin(PointerEventData data)
  35. {
  36. dragging = true;
  37. }
  38. private void OnDestroy()
  39. {
  40. GesEventInput.OnTrackedFailed -= OnTrackedFailed;
  41. GesEventInput.OnTrackedSuccess -= OnTrackedSuccess;
  42. RKPointerLisener.OnPointerDragBegin -= OnPointerDragBegin;
  43. RKPointerLisener.OnPointerDragEnd -= OnPointerDragEnd;
  44. }
  45. private void OnTrackedFailed(HandType hand)
  46. {
  47. if (hand == followhand || hand == HandType.None)
  48. {
  49. handActive = false;
  50. if (active)
  51. {
  52. active = false;
  53. OnActiveWatch?.Invoke(followhand, false);
  54. }
  55. }
  56. }
  57. private void OnTrackedSuccess(HandType hand)
  58. {
  59. if (hand == followhand)
  60. {
  61. handActive = true;
  62. }
  63. }
  64. private void Update()
  65. {
  66. if (handActive)
  67. {
  68. Pose pose = GesEventInput.Instance.GetHandPose(followhand);
  69. Pose skeletonPose = GesEventInput.Instance.GetSkeletonPose(followSkeletonIndex, followhand);
  70. //Process Enable Logic
  71. if (!active)
  72. {
  73. if (Utils.InCameraView(skeletonPose.position, MainCameraCache.mainCamera) && !IsPalm() && GesEventInput.Instance.GetHandPress(followhand, false))
  74. {
  75. active = true;
  76. OnActiveWatch?.Invoke(followhand, true);
  77. }
  78. }
  79. //Process Disable Logic
  80. if (active)
  81. {
  82. OnWatchPoseUpdate?.Invoke(followhand, new Pose(skeletonPose.position, pose.rotation));
  83. if (!Utils.InCameraView(skeletonPose.position, MainCameraCache.mainCamera) || (GesEventInput.Instance.GetHandOrientation(followhand) == HandOrientation.Palm && IsPalm()))
  84. {
  85. active = false;
  86. OnActiveWatch?.Invoke(followhand, false);
  87. }
  88. }
  89. }
  90. }
  91. private bool IsPalm()
  92. {
  93. Vector3 handForward = (GesEventInput.Instance.GetHandPose(followhand).rotation * Vector3.forward).normalized;
  94. Vector3 cameraForward = MainCameraCache.mainCamera.transform.forward;
  95. return Vector3.Dot(handForward, cameraForward) < -0.7f;
  96. }
  97. }
  98. }