PokePose.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Rokid.UXR.Utility;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class PokePose : MonoBehaviour
  6. {
  7. [Tooltip("手的类型")]
  8. [SerializeField]
  9. private HandType hand;
  10. private bool trackedSuccess;
  11. private Vector3 offsetToCamera = new Vector3(0, 10000, 0);
  12. #if UNITY_EDITOR
  13. public float maxDistanceInEditor = 10.0f;
  14. private float raycastDistance = -1.0f;
  15. #endif
  16. private void Start()
  17. {
  18. GesEventInput.OnTrackedSuccess += OnTrackedSuccess;
  19. GesEventInput.OnTrackedFailed += OnTrackedFailed;
  20. trackedSuccess = false;
  21. }
  22. private void OnTrackedSuccess(HandType hand)
  23. {
  24. if (this.hand == hand)
  25. {
  26. trackedSuccess = true;
  27. }
  28. }
  29. private void OnTrackedFailed(HandType handType)
  30. {
  31. if (this.hand == handType || handType == HandType.None)
  32. {
  33. trackedSuccess = false;
  34. }
  35. }
  36. private void OnDestroy()
  37. {
  38. GesEventInput.OnTrackedSuccess -= OnTrackedSuccess;
  39. GesEventInput.OnTrackedFailed -= OnTrackedFailed;
  40. }
  41. private void Update()
  42. {
  43. #if !UNITY_EDITOR
  44. if (trackedSuccess)
  45. {
  46. Gesture gesture = GesEventInput.Instance.GetGesture(hand);
  47. if (gesture.handOrientation == HandOrientation.Palm)
  48. {
  49. //放在距离相机天涯海角的距离
  50. this.transform.position = MainCameraCache.mainCamera.transform.position + offsetToCamera;
  51. }
  52. else
  53. {
  54. Pose pose = GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_TIP, hand);
  55. Pose pose1 = GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_DIP, hand);
  56. this.transform.position = pose.position;
  57. this.transform.rotation = Quaternion.FromToRotation(Vector3.forward, pose.position - pose1.position);
  58. }
  59. }
  60. else
  61. {
  62. this.transform.position = MainCameraCache.mainCamera.transform.position + offsetToCamera;
  63. }
  64. #else
  65. UpdateInEditor();
  66. #endif
  67. }
  68. protected virtual void UpdateInEditor()
  69. {
  70. #if UNITY_EDITOR
  71. if (GesEventInput.Instance.GetInteractorType(hand) == InteractorType.Near)
  72. {
  73. Ray ray = MainCameraCache.mainCamera.ScreenPointToRay(Input.mousePosition);
  74. Vector3 oldPosition = transform.position;
  75. transform.position = MainCameraCache.mainCamera.transform.forward * (-1000.0f);
  76. RaycastHit raycastHit;
  77. if (Physics.Raycast(ray, out raycastHit, maxDistanceInEditor))
  78. {
  79. if (Input.GetMouseButton(0))
  80. {
  81. if (Input.GetKey(KeyCode.B))
  82. {
  83. //模拟近场的Pinch拖拽
  84. transform.position = raycastHit.point + raycastHit.normal * 0.02f;
  85. }
  86. else
  87. {
  88. //模拟近场的Poke点击以及Poke拖拽逻辑
  89. transform.position = raycastHit.point - raycastHit.normal * 0.05f;
  90. }
  91. }
  92. else
  93. {
  94. transform.position = raycastHit.point + raycastHit.normal * 0.02f;
  95. }
  96. raycastDistance = Mathf.Min(maxDistanceInEditor, raycastHit.distance);
  97. }
  98. else if (raycastDistance > 0.0f)
  99. {
  100. transform.position = ray.origin + Mathf.Min(maxDistanceInEditor, raycastDistance) * ray.direction;
  101. }
  102. else
  103. {
  104. transform.position = oldPosition;
  105. }
  106. }
  107. else
  108. {
  109. //放在距离相机天涯海角的距离
  110. this.transform.position = MainCameraCache.mainCamera.transform.position + offsetToCamera;
  111. }
  112. #endif
  113. }
  114. }
  115. }