HandVisual.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using Rokid.UXR.Utility;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. public class HandVisual : MonoBehaviour
  7. {
  8. /// <summary>
  9. /// HandType
  10. /// </summary>
  11. [SerializeField]
  12. private HandType hand;
  13. /// <summary>
  14. /// Bone Coordinate Axis Transformation"
  15. /// </summary>
  16. [SerializeField]
  17. private Vector3 axisRot = Vector3.zero;
  18. /// <summary>
  19. /// The root joint of the hand is generally the "Wrist."
  20. /// </summary>
  21. [SerializeField]
  22. private Transform handRootSkeleton;
  23. /// <summary>
  24. /// Hand MeshRender
  25. /// </summary>
  26. [SerializeField]
  27. private SkinnedMeshRenderer handMesh;
  28. /// <summary>
  29. /// Hand Skeleton
  30. /// </summary>
  31. /// <typeparam name="Transform"></typeparam>
  32. /// <returns></returns>
  33. [SerializeField]
  34. private List<Transform> handSkeletons = new List<Transform>();
  35. private bool pokeSelect = false;
  36. private Vector3 pokeClosePoint = Vector3.zero;
  37. #if UNITY_EDITOR
  38. private float index_mcp_pip_dis;
  39. private float middle_mcp_pip_dis;
  40. private float ring_mcp_pip_dis;
  41. private float pinky_mcp_pip_dis;
  42. private float index_dip_pip_dis;
  43. private float middle_dip_pip_dis;
  44. private float ring_dip_pip_dis;
  45. private float pinky_dip_pip_dis;
  46. private float index_dip_tip_dis;
  47. private float middle_dip_tip_dis;
  48. private float ring_dip_tip_dis;
  49. private float pinky_dip_tip_dis;
  50. private float wrist_middle_cmp_dis;
  51. [SerializeField]
  52. private float[] boneDis = new float[13];
  53. #endif
  54. private void Start()
  55. {
  56. if (!Utils.IsAndroidPlatfrom())
  57. {
  58. if (Utils.IsAndroidPlatfrom())
  59. {
  60. RKLog.Error("====HandVisual==== Error Destory Switch Active!!!");
  61. }
  62. Destroy(GetComponent<InputModuleSwitchActive>());
  63. this.gameObject.SetActive(false);
  64. }
  65. InteractorStateChange.OnPokeSelectUpdate += OnPokeSelectUpdate;
  66. InteractorStateChange.OnPokeUnSelectUpdate += OnPokeUnSelectUpdate;
  67. }
  68. private void OnDestroy()
  69. {
  70. InteractorStateChange.OnPokeSelectUpdate -= OnPokeSelectUpdate;
  71. InteractorStateChange.OnPokeUnSelectUpdate -= OnPokeUnSelectUpdate;
  72. }
  73. private void OnEnable()
  74. {
  75. if (Utils.IsAndroidPlatfrom())
  76. GesEventInput.OnRenderHand += OnRenderHand;
  77. }
  78. private void OnDisable()
  79. {
  80. if (Utils.IsAndroidPlatfrom())
  81. GesEventInput.OnRenderHand -= OnRenderHand;
  82. }
  83. private void OnPokeUnSelectUpdate(HandType handType)
  84. {
  85. if (handType == this.hand)
  86. {
  87. pokeSelect = false;
  88. }
  89. }
  90. private void OnPokeSelectUpdate(HandType handType, Vector3 pokeClosePoint)
  91. {
  92. if (handType == this.hand)
  93. {
  94. this.pokeClosePoint = pokeClosePoint;
  95. pokeSelect = true;
  96. }
  97. }
  98. #if UNITY_EDITOR
  99. [ContextMenu("AutoSetSkeletons")]
  100. private void AutoSetSkeletons()
  101. {
  102. handSkeletons.Add(handRootSkeleton);
  103. for (int i = 0; i < handRootSkeleton.childCount; i++)
  104. {
  105. AutoSetSkeletons(handRootSkeleton.GetChild(i), handSkeletons);
  106. }
  107. index_mcp_pip_dis = Vector3.Distance(handSkeletons[5].position, handSkeletons[6].position);
  108. middle_mcp_pip_dis = Vector3.Distance(handSkeletons[9].position, handSkeletons[10].position);
  109. ring_mcp_pip_dis = Vector3.Distance(handSkeletons[13].position, handSkeletons[14].position);
  110. pinky_mcp_pip_dis = Vector3.Distance(handSkeletons[17].position, handSkeletons[18].position);
  111. index_dip_pip_dis = Vector3.Distance(handSkeletons[7].position, handSkeletons[6].position);
  112. middle_dip_pip_dis = Vector3.Distance(handSkeletons[11].position, handSkeletons[10].position);
  113. ring_dip_pip_dis = Vector3.Distance(handSkeletons[15].position, handSkeletons[14].position);
  114. pinky_dip_pip_dis = Vector3.Distance(handSkeletons[19].position, handSkeletons[18].position);
  115. index_dip_tip_dis = Vector3.Distance(handSkeletons[7].position, handSkeletons[8].position);
  116. middle_dip_tip_dis = Vector3.Distance(handSkeletons[11].position, handSkeletons[12].position);
  117. ring_dip_tip_dis = Vector3.Distance(handSkeletons[15].position, handSkeletons[16].position);
  118. pinky_dip_tip_dis = Vector3.Distance(handSkeletons[19].position, handSkeletons[20].position);
  119. wrist_middle_cmp_dis = Vector3.Distance(handSkeletons[0].position, handSkeletons[9].position);
  120. boneDis[0] = wrist_middle_cmp_dis;
  121. boneDis[1] = index_mcp_pip_dis;
  122. boneDis[2] = middle_mcp_pip_dis;
  123. boneDis[3] = ring_mcp_pip_dis;
  124. boneDis[4] = pinky_mcp_pip_dis;
  125. boneDis[5] = index_dip_pip_dis;
  126. boneDis[6] = middle_dip_pip_dis;
  127. boneDis[7] = ring_dip_pip_dis;
  128. boneDis[8] = pinky_dip_pip_dis;
  129. boneDis[9] = index_dip_tip_dis;
  130. boneDis[10] = middle_dip_tip_dis;
  131. boneDis[11] = ring_dip_tip_dis;
  132. boneDis[12] = pinky_dip_tip_dis;
  133. }
  134. /// <summary>
  135. /// auto find skeletons
  136. /// </summary>
  137. /// <param name="skeleton"></param>
  138. /// <param name="handSkeletons"></param>
  139. private void AutoSetSkeletons(Transform skeleton, List<Transform> handSkeletons)
  140. {
  141. handSkeletons.Add(skeleton);
  142. if (skeleton.childCount > 0)
  143. {
  144. for (int i = 0; i < skeleton.childCount; i++)
  145. {
  146. AutoSetSkeletons(skeleton.GetChild(i), handSkeletons);
  147. }
  148. }
  149. }
  150. #endif
  151. private void OnRenderHand(HandType hand, GestureBean data)
  152. {
  153. Quaternion[] rotations = data.skeletonsRot;
  154. if (hand == this.hand)
  155. {
  156. for (int i = 0; i < handSkeletons.Count; i++)
  157. {
  158. handSkeletons[i].rotation = rotations[i] * Quaternion.Euler(axisRot);
  159. Vector3 offset = pokeClosePoint - GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_TIP, hand).position;
  160. if (pokeSelect)
  161. {
  162. handSkeletons[i].position = data.skeletons[i] + offset;
  163. }
  164. else
  165. {
  166. handSkeletons[i].position = data.skeletons[i];
  167. }
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// Update hand mesh materials
  173. /// </summary>
  174. /// <param name="materials"></param>
  175. public void UpdateHandMeshMaterials(Material[] materials)
  176. {
  177. handMesh.materials = materials;
  178. }
  179. public void SetHandMeshActive(bool active)
  180. {
  181. handMesh.gameObject.SetActive(active);
  182. }
  183. }
  184. }