HandRender.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Rokid.UXR.Config;
  4. using Rokid.UXR.Utility;
  5. namespace Rokid.UXR.Interaction
  6. {
  7. public class HandRender : AutoInjectBehaviour
  8. {
  9. /// <summary>
  10. /// Is handtype
  11. /// </summary>
  12. public HandType handType;
  13. /// <summary>
  14. /// Is draw handmesh
  15. /// </summary>
  16. public bool drawMesh = false;
  17. /// <summary>
  18. /// Is draw skeleton
  19. /// </summary>
  20. public bool drawSkeleton = true;
  21. /// <summary>
  22. /// Is draw skeleton line only drawSkeleton is true will draw
  23. /// </summary>
  24. public bool drawSkeletonLine = false;
  25. /// <summary>
  26. /// Is draw hand root axis
  27. /// </summary>
  28. public bool drawHandRootAxis = false;
  29. /// <summary>
  30. /// Is show debugtext
  31. /// </summary>
  32. public bool showDebugText = false;
  33. /// <summary>
  34. /// Bone node templates are used to generate bone
  35. /// </summary>
  36. public GameObject skeletonNode;
  37. /// <summary>
  38. /// The bone alignment template is used to create the lines before the bone
  39. /// </summary>
  40. public LineRenderer skeletonLine;
  41. /// <summary>
  42. /// Coordinate axis
  43. /// </summary>s
  44. [SerializeField, Autowrited("HandRootAxis")]
  45. private Transform handRootAxis;
  46. /// <summary>
  47. /// 手的渲染
  48. /// </summary>
  49. [SerializeField, Autowrited("RKHandVisual")]
  50. private HandVisual handVisual;
  51. /// <summary>
  52. /// 调试模式的手
  53. /// </summary>
  54. [SerializeField, Autowrited("HandGestureInEditor")]
  55. private Transform handVisualInEditor;
  56. private TextMesh debugText;
  57. private Dictionary<string, LineRenderer> skeletonLineDict = new Dictionary<string, LineRenderer>();
  58. private Dictionary<SkeletonIndexFlag, GameObject> skeletonDict = new Dictionary<SkeletonIndexFlag, GameObject>();
  59. private void Start()
  60. {
  61. GesEventInput.OnTrackedFailed += OnTrackedFailed;
  62. Init();
  63. #if UNITY_EDITOR
  64. handVisualInEditor?.gameObject.SetActive(true);
  65. #else
  66. Destroy(handVisualInEditor?.gameObject);
  67. #endif
  68. }
  69. private void OnEnable()
  70. {
  71. GesEventInput.OnRenderHand += OnRenderHand;
  72. }
  73. private void OnDisable()
  74. {
  75. GesEventInput.OnRenderHand -= OnRenderHand;
  76. }
  77. private void Init()
  78. {
  79. if (Utils.IsAndroidPlatfrom() && drawMesh == false)
  80. {
  81. Destroy(handVisual.GetComponent<InputModuleSwitchActive>());
  82. handVisual.gameObject.SetActive(false);
  83. }
  84. if (UXRSDKConfig.Instance.URPActive)
  85. {
  86. handVisual.UpdateHandMeshMaterials(new Material[]{
  87. Resources.Load<Material>("Materials/URP/RokidHand"), Resources.Load<Material>("Materials/URP/HandMesh_Finger")
  88. });
  89. }
  90. if (showDebugText)
  91. {
  92. if (debugText == null)
  93. {
  94. debugText = new GameObject("_debug_text").AddComponent<TextMesh>();
  95. debugText.fontSize = 120;
  96. debugText.characterSize = 0.001f;
  97. debugText.transform.parent = transform;
  98. debugText.color = Color.green;
  99. debugText.transform.localScale = Vector3.one;
  100. debugText.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
  101. }
  102. debugText.gameObject.SetActive(false);
  103. }
  104. }
  105. private void OnDestroy()
  106. {
  107. GesEventInput.OnTrackedFailed -= OnTrackedFailed;
  108. }
  109. private void OnTrackedFailed(HandType handType)
  110. {
  111. if (handType == this.handType || handType == HandType.None)
  112. {
  113. if (drawHandRootAxis)
  114. handRootAxis.gameObject.SetActive(false);
  115. if (showDebugText)
  116. debugText.gameObject.SetActive(false);
  117. }
  118. }
  119. private void OnRenderHand(HandType handType, GestureBean bean)
  120. {
  121. if (handType == this.handType)
  122. {
  123. if (bean.skeletons != null && bean.skeletons.Length > 0)
  124. {
  125. if (drawSkeleton)
  126. DrawSkeleton(bean.skeletons, bean.skeletonsRot);
  127. if (drawHandRootAxis)
  128. {
  129. handRootAxis.gameObject.SetActive(true);
  130. handRootAxis.transform.SetPose(GesEventInput.Instance.GetHandPose(handType));
  131. }
  132. }
  133. }
  134. }
  135. void DrawSkeleton(Vector3[] skeletons, Quaternion[] rotations)
  136. {
  137. if (skeletons.Length == 0 || drawSkeleton == false)
  138. {
  139. return;
  140. }
  141. //绘制骨骼点
  142. for (int i = 0; i < skeletons.Length; i++)
  143. {
  144. GameObject go = null;
  145. SkeletonIndexFlag indexFlag = (SkeletonIndexFlag)i;
  146. if (skeletonDict.ContainsKey(indexFlag))
  147. {
  148. go = skeletonDict[indexFlag];
  149. }
  150. else
  151. {
  152. go = GameObject.Instantiate(skeletonNode);
  153. go.GetComponent<MeshRenderer>().enabled = true;
  154. go.name = indexFlag.ToString();
  155. go.transform.SetParent(transform);
  156. skeletonDict.Add(indexFlag, go);
  157. }
  158. go.gameObject.SetActive(true);
  159. go.transform.position = skeletons[i];
  160. go.transform.rotation = rotations[i];
  161. }
  162. if (drawSkeletonLine)
  163. {
  164. for (int i = 0; i < 5; i++)
  165. {
  166. LineRenderer skeletonLine = null;
  167. string lineName = handType == HandType.RightHand ? "right_skeletonLine" + i : "left_skeletonLine" + i;
  168. if (skeletonLineDict.ContainsKey(lineName))
  169. {
  170. skeletonLine = skeletonLineDict[lineName];
  171. }
  172. else
  173. {
  174. skeletonLine = GameObject.Instantiate<LineRenderer>(this.skeletonLine);
  175. skeletonLine.name = lineName;
  176. skeletonLineDict.Add(lineName, skeletonLine);
  177. }
  178. skeletonLine.gameObject.SetActive(true);
  179. skeletonLine.transform.SetParent(transform);
  180. for (int j = 0; j < 5; j++)
  181. {
  182. int index = j == 0 ? 0 : i * 4 + j;
  183. skeletonLine.SetPosition(j, skeletons[index]);
  184. }
  185. }
  186. }
  187. }
  188. private void Update()
  189. {
  190. if (showDebugText)
  191. {
  192. debugText.gameObject.SetActive(true);
  193. debugText.transform.SetPose(GesEventInput.Instance.GetHandPose(handType));
  194. if (handType == HandType.RightHand)
  195. {
  196. debugText.transform.position += new Vector3(-0.05f, 0.1f, 0.0f);
  197. debugText.alignment = TextAlignment.Right;
  198. debugText.anchor = TextAnchor.UpperRight;
  199. }
  200. else
  201. {
  202. debugText.transform.position += new Vector3(0.05f, 0.1f, 0.0f);
  203. debugText.alignment = TextAlignment.Left;
  204. debugText.anchor = TextAnchor.UpperLeft;
  205. }
  206. debugText.text = string.Format(
  207. "WristPos: {0}\n" +
  208. "GestureType: {1}\n" +
  209. "HandType: {2}\n" +
  210. "HandOrientation: {3}\n" +
  211. "HandRotation: {4}\n",
  212. GesEventInput.Instance.GetSkeletonPose(SkeletonIndexFlag.WRIST, handType).position.ToString("0.000"),
  213. GesEventInput.Instance.GetGesture(handType).gesType.ToString(),
  214. handType.ToString(),
  215. GesEventInput.Instance.GetGesture(handType).handOrientation == 0 ? HandOrientation.Palm : HandOrientation.Back,
  216. GesEventInput.Instance.GetHandPose(handType).rotation.eulerAngles.ToString("0.000"));
  217. }
  218. else
  219. {
  220. if (debugText != null)
  221. {
  222. Destroy(debugText.gameObject);
  223. }
  224. }
  225. }
  226. }
  227. }