HandsVisualizationForModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using EZXR.Glass.Core;
  2. using SuperSystems.ImageEffects;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace EZXR.Glass.Inputs
  9. {
  10. /// <summary>
  11. /// 可视化手部信息:关节、关节点等等
  12. /// </summary>
  13. [ScriptExecutionOrder(-8)]
  14. public class HandsVisualizationForModel : MonoBehaviour
  15. {
  16. #region singleton
  17. private static HandsVisualizationForModel _instance_Left;
  18. private static HandsVisualizationForModel _instance_Right;
  19. public static HandsVisualizationForModel Left
  20. {
  21. get
  22. {
  23. return _instance_Left;
  24. }
  25. }
  26. public static HandsVisualizationForModel Right
  27. {
  28. get
  29. {
  30. return _instance_Right;
  31. }
  32. }
  33. #endregion
  34. //public bool bone = true;
  35. public bool outline = false;
  36. public bool isLeft;
  37. bool visibility = true;
  38. bool lastVisibility = true;
  39. public Transform handRoot;
  40. public Transform wrist;
  41. /// <summary>
  42. /// 和HandJointType顺序一致
  43. /// </summary>
  44. public List<Transform> bones = new List<Transform>();
  45. /// <summary>
  46. /// 针对每个
  47. /// </summary>
  48. Vector3 corrction;
  49. HandInfo handInfo;
  50. #region 变量声明,灰度相关
  51. /// <summary>
  52. /// 左灰度相机的左手关节点的信息
  53. /// </summary>
  54. RectTransform[] keyPoints2D_LeftFish_LeftHand = new RectTransform[21];
  55. /// <summary>
  56. /// 左灰度相机的右手关节点的信息
  57. /// </summary>
  58. RectTransform[] keyPoints2D_LeftFish_RightHand = new RectTransform[21];
  59. /// <summary>
  60. /// 右灰度相机的左手关节点的信息
  61. /// </summary>
  62. RectTransform[] keyPoints2D_RightFish_LeftHand = new RectTransform[21];
  63. /// <summary>
  64. /// 右灰度相机的右手关节点的信息
  65. /// </summary>
  66. RectTransform[] keyPoints2D_RightFish_RightHand = new RectTransform[21];
  67. /// <summary>
  68. /// 左灰度相机的左手关节线的信息
  69. /// </summary>
  70. Dictionary<int, RectTransform> fingerBones2D_LeftFish_LeftHand = new Dictionary<int, RectTransform>();
  71. /// <summary>
  72. /// 左灰度相机的右手关节线的信息
  73. /// </summary>
  74. Dictionary<int, RectTransform> fingerBones2D_LeftFish_RightHand = new Dictionary<int, RectTransform>();
  75. /// <summary>
  76. /// 右灰度相机的左手关节线的信息
  77. /// </summary>
  78. Dictionary<int, RectTransform> fingerBones2D_RightFish_LeftHand = new Dictionary<int, RectTransform>();
  79. /// <summary>
  80. /// 右灰度相机的右手关节线的信息
  81. /// </summary>
  82. Dictionary<int, RectTransform> fingerBones2D_RightFish_RightHand = new Dictionary<int, RectTransform>();
  83. /// <summary>
  84. /// 左灰度相机图像上用于绘制关节点和关节线的物体
  85. /// </summary>
  86. Transform canvasRoot_Left;
  87. /// <summary>
  88. /// 右灰度相机图像上用于绘制关节点和关节线的物体
  89. /// </summary>
  90. Transform canvasRoot_Right;
  91. #endregion
  92. public Vector3 ModelFingerPointing = new Vector3(0, 0, 0);
  93. public Vector3 ModelPalmFacing = new Vector3(0, 0, 0);
  94. float width;
  95. float height;
  96. /// <summary>
  97. /// 0是默认都开启,1是禁用了手的长宽缩放,2是禁用了手的长宽缩放和关节点位置
  98. /// </summary>
  99. static int mode = 0;
  100. Vector3 defaultScale;
  101. public Vector3 defaultPos;
  102. public Quaternion defaultRot;
  103. public List<Vector3> defaultBonesPos = new List<Vector3>();
  104. public List<Quaternion> defaultBonesRot = new List<Quaternion>();
  105. public Renderer handRenderer;
  106. /// <summary>
  107. /// 0是模型手,1是轮廓手
  108. /// </summary>
  109. public Material[] materials;
  110. public static int Mode
  111. {
  112. get
  113. {
  114. return mode;
  115. }
  116. set
  117. {
  118. Debug.Log("Mode:a");
  119. mode = value;
  120. Left.wrist.localScale = Left.defaultScale;
  121. Right.wrist.localScale = Right.defaultScale;
  122. Left.wrist.localPosition = Left.defaultPos;
  123. Right.wrist.localPosition = Right.defaultPos;
  124. Left.wrist.localRotation = Left.defaultRot;
  125. Right.wrist.localRotation = Right.defaultRot;
  126. Debug.Log("Mode:b");
  127. Debug.Log("Mode:Left.bones.Count: " + Left.bones.Count + " , Left.defaultBonesPos: " + Left.defaultBonesPos.Count);
  128. foreach (HandJointType key in Left.handInfo.jointsPose.Keys)
  129. {
  130. int id = (int)key;
  131. if (id < Left.bones.Count)
  132. {
  133. Left.bones[id].localPosition = Left.defaultBonesPos[id];
  134. Left.bones[id].localRotation = Left.defaultBonesRot[id];
  135. }
  136. }
  137. Debug.Log("Mode:Right.bones.Count: " + Right.bones.Count + " , Right.defaultBonesPos: " + Right.defaultBonesPos.Count);
  138. foreach (HandJointType key in Right.handInfo.jointsPose.Keys)
  139. {
  140. int id = (int)key;
  141. if (id < Right.bones.Count)
  142. {
  143. Right.bones[id].localPosition = Right.defaultBonesPos[id];
  144. Right.bones[id].localRotation = Right.defaultBonesRot[id];
  145. }
  146. }
  147. }
  148. }
  149. private void Awake()
  150. {
  151. //编辑器内手动禁用此组件的时候需要将对应的模型手一并禁用
  152. handRoot.gameObject.SetActive(enabled);
  153. if (isLeft)
  154. {
  155. _instance_Left = this;
  156. }
  157. else
  158. {
  159. _instance_Right = this;
  160. }
  161. width = Vector3.Distance(bones[4].position, bones[16].position);
  162. height = Vector3.Distance(bones[8].position, wrist.position);
  163. handInfo = GetComponent<HandInfo>();
  164. string _handType = isLeft ? "L" : "R";
  165. corrction = isLeft ? new Vector3(-90, 0, 0) : new Vector3(-90, 0, 0);
  166. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Thumb" + _handType + "_JNT1"));
  167. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Thumb" + _handType + "_JNT1" + "/" + "Thumb" + _handType + "_JNT2"));
  168. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Thumb" + _handType + "_JNT1" + "/" + "Thumb" + _handType + "_JNT2" + "/" + "Thumb" + _handType + "_JNT3"));
  169. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Thumb" + _handType + "_JNT1" + "/" + "Thumb" + _handType + "_JNT2" + "/" + "Thumb" + _handType + "_JNT3"));
  170. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT1"));
  171. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT1" + "/" + "Point" + _handType + "_JNT2"));
  172. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT1" + "/" + "Point" + _handType + "_JNT2" + "/" + "Point" + _handType + "_JNT3"));
  173. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT" + "/" + "Point" + _handType + "_JNT1" + "/" + "Point" + _handType + "_JNT2" + "/" + "Point" + _handType + "_JNT3"));
  174. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT1"));
  175. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT1" + "/" + "Middle" + _handType + "_JNT2"));
  176. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT1" + "/" + "Middle" + _handType + "_JNT2" + "/" + "Middle" + _handType + "_JNT3"));
  177. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT" + "/" + "Middle" + _handType + "_JNT1" + "/" + "Middle" + _handType + "_JNT2" + "/" + "Middle" + _handType + "_JNT3"));
  178. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT1"));
  179. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT1" + "/" + "Ring" + _handType + "_JNT2"));
  180. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT1" + "/" + "Ring" + _handType + "_JNT2" + "/" + "Ring" + _handType + "_JNT3"));
  181. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT" + "/" + "Ring" + _handType + "_JNT1" + "/" + "Ring" + _handType + "_JNT2" + "/" + "Ring" + _handType + "_JNT3"));
  182. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT"));
  183. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT1"));
  184. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT1" + "/" + "Pinky" + _handType + "_JNT2"));
  185. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT1" + "/" + "Pinky" + _handType + "_JNT2" + "/" + "Pinky" + _handType + "_JNT3"));
  186. //bones.Add(handRoot.Find("Wrist" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT" + "/" + "Pinky" + _handType + "_JNT1" + "/" + "Pinky" + _handType + "_JNT2" + "/" + "Pinky" + _handType + "_JNT3"));
  187. ////bones.Add(temp);
  188. ////bones.Add(temp);
  189. ////bones.Add(temp);
  190. ////bones.Add(handRoot.Find("Wrist" + _handType + "_JNT"));
  191. }
  192. private void Start()
  193. {
  194. defaultScale = wrist.localScale;
  195. defaultPos = wrist.localPosition;
  196. defaultRot = wrist.localRotation;
  197. for (int id = 0; id < Left.bones.Count; id++)
  198. {
  199. if (id < bones.Count)
  200. {
  201. Left.defaultBonesPos.Add(Left.bones[id].localPosition);
  202. Left.defaultBonesRot.Add(Left.bones[id].localRotation);
  203. }
  204. }
  205. for (int id = 0; id < Right.bones.Count; id++)
  206. {
  207. if (id < bones.Count)
  208. {
  209. Right.defaultBonesPos.Add(Right.bones[id].localPosition);
  210. Right.defaultBonesRot.Add(Right.bones[id].localRotation);
  211. }
  212. }
  213. }
  214. void LateUpdate()
  215. {
  216. if (lastVisibility != visibility)
  217. {
  218. lastVisibility = visibility;
  219. handRoot.gameObject.SetActive(visibility);
  220. }
  221. if (handInfo.Exist)
  222. {
  223. wrist.position = handInfo.GetJointData(HandJointType.Wrist_Middle).position;
  224. wrist.rotation = handInfo.GetJointData(HandJointType.Wrist_Middle).rotation * Quaternion.Euler(corrction) * Reorientation();
  225. //Debug.Log("handInfo.handTrackingData.model_scale: " + handInfo.handTrackingData.model_scale);
  226. //Debug.Log("handInfo.handTrackingData.fingler_curl: " + handInfo.handTrackingData.fingler_curl[0] + ", " + handInfo.handTrackingData.fingler_curl[1] + ", " + handInfo.handTrackingData.fingler_curl[2] + ", " + handInfo.handTrackingData.fingler_curl[3] + ", " + handInfo.handTrackingData.fingler_curl[4]);
  227. if (Mode == 0)
  228. {
  229. //float curWidth = Vector3.Distance(bones[4].position, bones[16].position);
  230. //float curHeight = Vector3.Distance(bones[8].position, wrist.position);
  231. //float ratioWidth = curWidth / width;
  232. //float ratioHeight = curHeight / height;
  233. #if UNITY_EDITOR
  234. wrist.localScale = Vector3.one;
  235. #else
  236. wrist.localScale = new Vector3(handInfo.handTrackingData.model_scale, handInfo.handTrackingData.model_scale, handInfo.handTrackingData.model_scale);
  237. #endif
  238. }
  239. else
  240. {
  241. wrist.localScale = Vector3.one;
  242. }
  243. foreach (HandJointType key in handInfo.jointsPose.Keys)
  244. {
  245. int id = (int)key;
  246. if (id < bones.Count)
  247. {
  248. if (Mode == 0 || Mode == 1)
  249. {
  250. bones[id].position = handInfo.GetJointData(key).position;
  251. }
  252. bones[id].rotation = handInfo.GetJointData(key).rotation * Quaternion.Euler(corrction) * Reorientation();
  253. }
  254. }
  255. //handInfo.Visibility = bone;
  256. if (outline)
  257. {
  258. if (handRenderer.material != materials[1])
  259. {
  260. handRenderer.material = materials[1];
  261. }
  262. }
  263. else
  264. {
  265. if (handRenderer.material != materials[0])
  266. {
  267. handRenderer.material = materials[0];
  268. }
  269. }
  270. }
  271. }
  272. public bool Visibility
  273. {
  274. get
  275. {
  276. return visibility;
  277. }
  278. set
  279. {
  280. visibility = value;
  281. //这里直接调用Show会出现keyPoints数组虽然长度为25,但是每个元素都是"null",非常神奇的bug,只要从其他脚本中调用Visibility中的Show来隐藏球棍手就一定会出现这个问题,所以改为了在此脚本的update中调用Show
  282. //Show(value);
  283. }
  284. }
  285. private Quaternion Reorientation()
  286. {
  287. return Quaternion.Inverse(Quaternion.LookRotation(ModelFingerPointing, -ModelPalmFacing));
  288. }
  289. }
  290. }