SnapPose.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Rokid.UXR.Utility;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class SnapPose : MonoBehaviour
  6. {
  7. [Tooltip("手的类型")]
  8. [SerializeField]
  9. private HandType hand;
  10. private bool trackedSuccess;
  11. private Vector3 offsetToCamera = new Vector3(0, 10000, 0);
  12. private void Start()
  13. {
  14. GesEventInput.OnTrackedSuccess += OnTrackedSuccess;
  15. GesEventInput.OnTrackedFailed += OnTrackedFailed;
  16. trackedSuccess = false;
  17. }
  18. private void OnTrackedSuccess(HandType hand)
  19. {
  20. if (this.hand == hand)
  21. {
  22. trackedSuccess = true;
  23. }
  24. }
  25. private void OnTrackedFailed(HandType handType)
  26. {
  27. if (this.hand == handType || handType == HandType.None)
  28. {
  29. trackedSuccess = false;
  30. }
  31. }
  32. private void OnDestroy()
  33. {
  34. GesEventInput.OnTrackedSuccess -= OnTrackedSuccess;
  35. GesEventInput.OnTrackedFailed -= OnTrackedFailed;
  36. }
  37. private void Update()
  38. {
  39. #if !UNITY_EDITOR
  40. if (trackedSuccess)
  41. {
  42. Pose pose = PoseAdd(GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.THUMB_IP, hand), GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_TIP, hand));
  43. transform.SetPose(new Pose(pose.position, GesEventInput.Instance.GetHandPose(hand).rotation));
  44. }
  45. else
  46. {
  47. this.transform.position = MainCameraCache.mainCamera.transform.position + offsetToCamera;
  48. }
  49. #endif
  50. }
  51. private Pose PoseAdd(Pose pose0, Pose pose1)
  52. {
  53. Pose pose = Pose.identity;
  54. pose.position = (pose0.position + pose1.position) / 2;
  55. pose.rotation = Quaternion.LookRotation((pose0.forward + pose1.forward) / 2);
  56. return pose;
  57. }
  58. }
  59. }