NRHandSimpleVisual.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. public class NRHandSimpleVisual : MonoBehaviour
  15. {
  16. public HandEnum handEnum;
  17. public GameObject jointPrefab;
  18. protected readonly Dictionary<HandJointID, Transform> joints = new Dictionary<HandJointID, Transform>();
  19. private void OnEnable()
  20. {
  21. NRInput.Hands.OnHandStatesUpdated += OnHandStatesUpdated;
  22. NRInput.Hands.OnHandTrackingStopped += OnHandTrackingStopped;
  23. }
  24. private void OnDisable()
  25. {
  26. NRInput.Hands.OnHandStatesUpdated -= OnHandStatesUpdated;
  27. NRInput.Hands.OnHandTrackingStopped += OnHandTrackingStopped;
  28. }
  29. private void OnHandStatesUpdated()
  30. {
  31. UpdateHandVisual();
  32. }
  33. private void OnHandTrackingStopped()
  34. {
  35. OnHandTrackingLost();
  36. }
  37. private void UpdateHandVisual()
  38. {
  39. var handState = NRInput.Hands.GetHandState(handEnum);
  40. if (handState != null && handState.isTracked)
  41. {
  42. foreach (var jointID in handState.jointsPoseDict.Keys)
  43. {
  44. Transform jointTransform = null;
  45. if (joints.TryGetValue(jointID, out jointTransform))
  46. {
  47. jointTransform.gameObject.SetActive(true);
  48. //Debug.LogError("jointTransform position = " + jointTransform.position.ToString("f2"));
  49. }
  50. else
  51. {
  52. GameObject jointObj = CreateJointObj(jointID);
  53. if(jointObj == null)
  54. {
  55. Debug.LogError("Create joint failed, joint ID = " + jointID);
  56. continue;
  57. }
  58. jointTransform = jointObj.transform;
  59. jointTransform.name = jointID.ToString() + " Transform";
  60. jointTransform.SetParent(transform);
  61. jointTransform.localScale = Vector3.one * 0.01f;
  62. joints.Add(jointID, jointTransform);
  63. }
  64. if (jointTransform)
  65. {
  66. jointTransform.position = handState.jointsPoseDict[jointID].position;
  67. jointTransform.rotation = handState.jointsPoseDict[jointID].rotation;
  68. }
  69. }
  70. }
  71. else
  72. {
  73. OnHandTrackingLost();
  74. }
  75. }
  76. private void OnHandTrackingLost()
  77. {
  78. foreach (Transform jointTransform in joints.Values)
  79. {
  80. if (jointTransform)
  81. {
  82. jointTransform.gameObject.SetActive(false);
  83. }
  84. }
  85. }
  86. private GameObject CreateJointObj(HandJointID handJointID)
  87. {
  88. return Instantiate(jointPrefab);
  89. }
  90. public void SetHandCollidersEnabled(bool isEnabled)
  91. {
  92. var collidersInChildren = transform.GetComponentsInChildren<Collider>(true);
  93. if(collidersInChildren != null)
  94. {
  95. for (int i = 0; i < collidersInChildren.Length; i++)
  96. {
  97. collidersInChildren[i].enabled = isEnabled;
  98. }
  99. }
  100. }
  101. }
  102. }