ZwnAmlogicHand.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * File Name: ZwnAmlogicHand
  3. * Author: Guan B
  4. * Version: 1.0.0
  5. * Data: 2022.04.08
  6. * Des: 调用SDK接口Demo
  7. * Other: Zeewain
  8. */
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. public class ZwnAmlogicHand : MonoBehaviour
  13. {
  14. private AndroidJavaClass javaClass;
  15. public GameObject showPrefabs;
  16. public Transform showPointParent;
  17. public GameObject showRectfabs;
  18. List<GameObject> showHandList = new List<GameObject>();
  19. List<GameObject> showHandListTwo = new List<GameObject>();
  20. List<GameObject> showHandRectList = new List<GameObject>();
  21. List<GameObject> showHandOriginaltList = new List<GameObject>();
  22. List<GameObject> showHandOriginaltListTwo = new List<GameObject>();
  23. private int handCount = 21;
  24. Dictionary<int, Vector3[]> handData = new Dictionary<int, Vector3[]>();
  25. Dictionary<int, Vector3[]> handOriginalData = new Dictionary<int, Vector3[]>();
  26. Dictionary<int, Vector2> handWeightHeightData = new Dictionary<int, Vector2>();
  27. Dictionary<int, Vector3> handRectPointData = new Dictionary<int, Vector3>();
  28. Vector3 default_vec = new Vector3(-Screen.width, -Screen.height, 0);
  29. Vector3[] handChangeData;
  30. // Start is called before the first frame update
  31. void Start()
  32. {
  33. javaClass = new AndroidJavaClass("com.zee.unity.UnityHelper");
  34. ZwnAmLogicSDK.InitAndroidJavaClass(javaClass);
  35. handChangeData = new Vector3[21];
  36. }
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. if (ZwnAmlogicHelperData.isCanOpenCamera)
  41. {
  42. ZwnAmlogicHelperData.isCanOpenCamera = false;
  43. ZwnAmLogicSDK.OpenCamera(ZwnAmlogicHelperData.cameraId);
  44. }
  45. if (ZwnAmlogicHelperData.isOpenCamera)
  46. {
  47. ZwnAmlogicHelperData.initPersonPoseNum = ZwnAmLogicSDK.InitHand(ZwnAmlogicHelperData.landmarkType, ZwnAmlogicHelperData.deviceType, ZwnAmlogicHelperData.threadNum);
  48. if (ZwnAmlogicHelperData.initPersonPoseNum == 0)
  49. {
  50. ZwnAmLogicSDK.SetHandLandmarkStatus(true);
  51. ZwnAmLogicSDK.SetHandMaxNum(2, 0);
  52. ZwnAmLogicSDK.SetHandScoreThreshold(0.5f);
  53. ZwnAmLogicSDK.SetHandSmoothnessStatus(true);
  54. }
  55. }
  56. if (ZwnAmlogicHelperData.initPersonPoseNum == 0)
  57. {
  58. ZwnAmlogicHelperData.isOpenCamera = false;
  59. GetHandData();
  60. GetHandRectData();
  61. //GetHandOriginalData();
  62. }
  63. ShowChangeHandPoint();
  64. ShowHandRectPoint();
  65. //ShowHandOriginalData();
  66. }
  67. /// <summary>
  68. /// 获取转换后的手部数据
  69. /// </summary>
  70. void GetHandData()
  71. {
  72. handData = ZwnAmLogicSDK.GetHandInfo();
  73. }
  74. /// <summary>
  75. /// 获取转换后的手部框位置和大小
  76. /// </summary>
  77. void GetHandRectData()
  78. {
  79. handRectPointData = ZwnAmLogicSDK.GetHandRectPointInfo();
  80. handWeightHeightData = ZwnAmLogicSDK.GetHandRectWeightHeightInfo();
  81. }
  82. /// <summary>
  83. /// 获取手部原始点的数据
  84. /// </summary>
  85. void GetHandOriginalData()
  86. {
  87. handOriginalData = ZwnAmLogicSDK.GetOriginalHandInfo();
  88. }
  89. /// <summary>
  90. /// 显示转换后点的位置
  91. /// </summary>
  92. public void ShowChangeHandPoint()
  93. {
  94. if (handData.Count > 0 && handData != null)
  95. {
  96. if (handData.Count == 1)
  97. {
  98. for (int i = 0; i < handCount; i++)
  99. {
  100. if (i >= showHandList.Count)
  101. {
  102. GameObject go = Instantiate(showPrefabs);
  103. showHandList.Add(go);
  104. go.transform.SetParent(showPointParent);
  105. go.transform.localScale = new Vector3(1, 1, 1);
  106. }
  107. showHandList[i].transform.localPosition = handData[0][i];
  108. }
  109. }
  110. else if (handData.Count == 2)
  111. {
  112. for (int i = 0; i < handCount; i++)
  113. {
  114. if (i >= showHandList.Count)
  115. {
  116. GameObject go = Instantiate(showPrefabs);
  117. showHandList.Add(go);
  118. go.transform.SetParent(showPointParent);
  119. go.transform.localScale = new Vector3(1, 1, 1);
  120. }
  121. showHandList[i].transform.localPosition = handData[0][i];
  122. }
  123. for (int i = 0; i < handCount; i++)
  124. {
  125. if (i >= showHandListTwo.Count)
  126. {
  127. GameObject go = Instantiate(showPrefabs);
  128. showHandListTwo.Add(go);
  129. go.transform.SetParent(showPointParent);
  130. go.transform.localScale = new Vector3(1, 1, 1);
  131. }
  132. showHandListTwo[i].transform.localPosition = handData[1][i];
  133. }
  134. }
  135. }
  136. else
  137. {
  138. if (showHandList.Count > 0)
  139. {
  140. for (int i = 0; i < showHandList.Count; i++)
  141. {
  142. showHandList[i].transform.localPosition = default_vec;
  143. }
  144. }
  145. if (showHandListTwo.Count > 0)
  146. {
  147. for (int i = 0; i < showHandListTwo.Count; i++)
  148. {
  149. showHandListTwo[i].transform.localPosition = default_vec;
  150. }
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 显示手部框的位置和大小
  156. /// </summary>
  157. public void ShowHandRectPoint()
  158. {
  159. if (handRectPointData.Count > 0 && handWeightHeightData.Count > 0)
  160. {
  161. for (int i = 0; i < handRectPointData.Count; i++)
  162. {
  163. if (i >= showHandRectList.Count)
  164. {
  165. GameObject go = Instantiate(showRectfabs);
  166. showHandRectList.Add(go);
  167. go.transform.SetParent(showPointParent);
  168. go.transform.localScale = new Vector3(1, 1, 1);
  169. }
  170. showHandRectList[i].transform.localPosition = handRectPointData[i];
  171. showHandRectList[i].GetComponent<RectTransform>().sizeDelta = handWeightHeightData[i];
  172. }
  173. }
  174. else
  175. {
  176. if (showHandRectList.Count > 0)
  177. {
  178. for (int i = 0; i < showHandRectList.Count; i++)
  179. {
  180. showHandRectList[i].transform.localPosition = default_vec;
  181. }
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// 显示手部原始点数据
  187. /// </summary>
  188. public void ShowHandOriginalData()
  189. {
  190. if (handOriginalData.Count > 0 && handOriginalData != null)
  191. {
  192. if (handOriginalData.Count == 1)
  193. {
  194. for (int i = 0; i < handCount; i++)
  195. {
  196. if (i >= showHandOriginaltList.Count)
  197. {
  198. GameObject go = Instantiate(showPrefabs);
  199. showHandOriginaltList.Add(go);
  200. go.transform.SetParent(showPointParent);
  201. go.transform.localScale = new Vector3(1, 1, 1);
  202. }
  203. showHandOriginaltList[i].transform.localPosition = handOriginalData[0][i];
  204. }
  205. }
  206. else if (handOriginalData.Count == 2)
  207. {
  208. for (int i = 0; i < handCount; i++)
  209. {
  210. if (i >= showHandOriginaltList.Count)
  211. {
  212. GameObject go = Instantiate(showPrefabs);
  213. showHandOriginaltList.Add(go);
  214. go.transform.SetParent(showPointParent);
  215. go.transform.localScale = new Vector3(1, 1, 1);
  216. }
  217. showHandOriginaltList[i].transform.localPosition = handOriginalData[0][i];
  218. }
  219. for (int i = 0; i < handCount; i++)
  220. {
  221. if (i >= showHandOriginaltListTwo.Count)
  222. {
  223. GameObject go = Instantiate(showPrefabs);
  224. showHandOriginaltListTwo.Add(go);
  225. go.transform.SetParent(showPointParent);
  226. go.transform.localScale = new Vector3(1, 1, 1);
  227. }
  228. showHandOriginaltListTwo[i].transform.localPosition = handOriginalData[1][i];
  229. }
  230. }
  231. }
  232. else
  233. {
  234. if (showHandOriginaltList.Count > 0)
  235. {
  236. for (int i = 0; i < showHandOriginaltList.Count; i++)
  237. {
  238. showHandOriginaltList[i].transform.localPosition = default_vec;
  239. }
  240. }
  241. if (showHandOriginaltListTwo.Count > 0)
  242. {
  243. for (int i = 0; i < showHandOriginaltListTwo.Count; i++)
  244. {
  245. showHandOriginaltListTwo[i].transform.localPosition = default_vec;
  246. }
  247. }
  248. }
  249. }
  250. private void OnDestroy()
  251. {
  252. ZwnAmLogicSDK.ReleaseZeePose();
  253. ZwnAmLogicSDK.CloseCamera();
  254. }
  255. }