GestureSimpleTip.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace NRKernal.NRExamples
  6. {
  7. public class GestureSimpleTip : MonoBehaviour
  8. {
  9. public class GestureName
  10. {
  11. public const string Gesture_Open_Hand = "Open Hand";
  12. public const string Gesture_Grab = "Grab";
  13. public const string Gesture_Pinch = "Pinch";
  14. public const string Gesture_Point = "Point";
  15. public const string Gesture_Victory = "Victory";
  16. public const string Gesture_Call = "Call";
  17. public const string Gesture_System = "System";
  18. }
  19. public HandEnum handEnum;
  20. public Transform tipAnchor;
  21. public Text gestureTxt;
  22. private const string RIGHT_HAND_LABEL = "R:";
  23. private const string LEFT_HAND_LABEL = "L:";
  24. private void Update()
  25. {
  26. UpdateGestureTip();
  27. }
  28. private void UpdateGestureTip()
  29. {
  30. var handState = NRInput.Hands.GetHandState(handEnum);
  31. if (handState == null)
  32. return;
  33. switch (handState.currentGesture)
  34. {
  35. case HandGesture.OpenHand:
  36. gestureTxt.text = GetHandEnumLabel() + GestureName.Gesture_Open_Hand;
  37. break;
  38. case HandGesture.Grab:
  39. gestureTxt.text = GetHandEnumLabel() + GestureName.Gesture_Grab;
  40. break;
  41. case HandGesture.Pinch:
  42. gestureTxt.text = GetHandEnumLabel() + GestureName.Gesture_Pinch;
  43. break;
  44. case HandGesture.Point:
  45. gestureTxt.text = GetHandEnumLabel() + GestureName.Gesture_Point;
  46. break;
  47. case HandGesture.Victory:
  48. gestureTxt.text = GetHandEnumLabel() + GestureName.Gesture_Victory;
  49. break;
  50. case HandGesture.Call:
  51. gestureTxt.text = GetHandEnumLabel() + GestureName.Gesture_Call;
  52. break;
  53. case HandGesture.System:
  54. gestureTxt.text = GetHandEnumLabel() + GestureName.Gesture_System;
  55. break;
  56. default:
  57. gestureTxt.text = string.Empty;
  58. break;
  59. }
  60. if (handState.isTracked)
  61. {
  62. Pose palmPose;
  63. if(handState.jointsPoseDict.TryGetValue(HandJointID.Palm, out palmPose))
  64. {
  65. UpdateAnchorTransform(palmPose.position);
  66. }
  67. tipAnchor.gameObject.SetActive(!string.IsNullOrEmpty(gestureTxt.text));
  68. }
  69. else
  70. {
  71. tipAnchor.gameObject.SetActive(false);
  72. }
  73. }
  74. private string GetHandEnumLabel()
  75. {
  76. switch (handEnum)
  77. {
  78. case HandEnum.RightHand:
  79. return RIGHT_HAND_LABEL;
  80. case HandEnum.LeftHand:
  81. return LEFT_HAND_LABEL;
  82. default:
  83. break;
  84. }
  85. return string.Empty;
  86. }
  87. private void UpdateAnchorTransform(Vector3 jointPos)
  88. {
  89. var vec_from_head = jointPos - Camera.main.transform.position;
  90. var vec_horizontal = Vector3.Cross(Vector3.down, vec_from_head).normalized;
  91. tipAnchor.position = jointPos + Vector3.up * 0.08f - vec_horizontal * 0.015f;
  92. tipAnchor.rotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(vec_from_head, Vector3.up), Vector3.up);
  93. }
  94. }
  95. }