HandJointColliderEntity.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace NRKernal.NRExamples
  5. {
  6. public class HandJointColliderEntity : MonoBehaviour
  7. {
  8. public HandEnum handEnum;
  9. public HandJointID handJoint;
  10. public HandGesture activeByHandGesture = HandGesture.None;
  11. private HandState m_RelatedHandState;
  12. private Collider m_JointCollider;
  13. private MeshRenderer m_MeshRenderer;
  14. public bool IsColliderActive { get; private set; }
  15. private void Start()
  16. {
  17. m_JointCollider = GetComponent<Collider>();
  18. m_JointCollider.enabled = false;
  19. m_MeshRenderer = GetComponent<MeshRenderer>();
  20. m_MeshRenderer.enabled = false;
  21. }
  22. private void Update()
  23. {
  24. m_RelatedHandState = NRInput.Hands.GetHandState(handEnum);
  25. IsColliderActive = ShouldActive();
  26. m_MeshRenderer.enabled = m_JointCollider.enabled = IsColliderActive;
  27. if (IsColliderActive)
  28. {
  29. UpdateJointPose();
  30. }
  31. }
  32. private void OnDisable()
  33. {
  34. IsColliderActive = false;
  35. }
  36. private bool ShouldActive()
  37. {
  38. if (!NRInput.Hands.IsRunning)
  39. return false;
  40. if (m_RelatedHandState == null || !m_RelatedHandState.isTracked)
  41. return false;
  42. if (activeByHandGesture == HandGesture.None)
  43. return true;
  44. if (m_RelatedHandState.currentGesture == activeByHandGesture)
  45. return true;
  46. return false;
  47. }
  48. private void UpdateJointPose()
  49. {
  50. if (m_RelatedHandState == null)
  51. return;
  52. var jointPose = m_RelatedHandState.GetJointPose(handJoint);
  53. transform.position = jointPose.position;
  54. transform.rotation = jointPose.rotation;
  55. }
  56. }
  57. }