GesImplementation.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. using System.Threading;
  2. using UnityEngine;
  3. using UnityEngine.Android;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6. using Rokid.UXR.Native;
  7. using Rokid.UXR.Module;
  8. using Rokid.UXR.Utility;
  9. using System;
  10. namespace Rokid.UXR.Interaction
  11. {
  12. /// <summary>
  13. /// Gesture logic implementation class,
  14. /// the interfaces provided by this class are not recommended
  15. /// </summary>
  16. public class GesImplementation : MonoBehaviour
  17. {
  18. /// <summary>
  19. /// Left-hand cached data structure
  20. /// </summary>
  21. [SerializeField]
  22. private Gesture leftGesture;
  23. /// <summary>
  24. /// Right-hand cached data structure
  25. /// </summary>
  26. [SerializeField]
  27. private Gesture rightGesture;
  28. [SerializeField]
  29. private GestureType preLeftGesType = GestureType.None;
  30. [SerializeField]
  31. private GestureType preRightGesType = GestureType.None;
  32. /// <summary>
  33. /// Gesture type cache
  34. /// </summary>
  35. [SerializeField]
  36. protected GestureType[] leftGestureTypeCache;
  37. /// <summary>
  38. /// Gesture type cache
  39. /// </summary>
  40. [SerializeField]
  41. protected GestureType[] rightGestureTypeCache;
  42. /// <summary>
  43. /// Cache the number of frames for gestures
  44. /// </summary>
  45. [SerializeField]
  46. protected int gesCacheCount = 30;
  47. /// <summary>
  48. /// Determine the threshold for changing the gesture
  49. /// </summary>
  50. [SerializeField]
  51. protected int changeGesThreshold = 10;
  52. /// <summary>
  53. /// Whether to enable gesture type recording
  54. /// </summary>
  55. [SerializeField]
  56. protected bool useGesFrameRecord = true;
  57. /// <summary>
  58. /// Internal log text
  59. /// </summary>
  60. [SerializeField]
  61. private Text logText;
  62. /// <summary>
  63. /// Whether to display log
  64. /// </summary>
  65. [SerializeField]
  66. private bool showDebugLog;
  67. [Tooltip("Left-handed interaction type, default Far")]
  68. [SerializeField]
  69. private InteractorType rightHandInteractorType = InteractorType.Far;
  70. [Tooltip("Right-handed interaction type, default Far")]
  71. [SerializeField]
  72. private InteractorType leftHandInteractorType = InteractorType.Far;
  73. /// <summary>s
  74. /// Gesture data array
  75. /// </summary>
  76. [SerializeField]
  77. private GestureBean[] gestureData = new GestureBean[2] { new GestureBean(), new GestureBean() };
  78. /// <summary>s
  79. /// Cache data from the previous frame
  80. /// </summary>
  81. private GestureBean preLeftGesData = new GestureBean();
  82. private GestureBean preRightGesData = new GestureBean();
  83. /// <summary>
  84. /// Gesture frame
  85. /// </summary>
  86. protected int rightGesFrame = 0;
  87. /// <summary>
  88. /// Gesture frame
  89. /// </summary>
  90. protected int leftGesFrame = 0;
  91. private float clickTime = 1.25f;
  92. private float leftClickTime = 0;
  93. private float rightClickTime = 0;
  94. /// <summary>
  95. /// Initialize gesture module
  96. /// </summary>
  97. private bool initGesModule = false;
  98. #region Algorithm data caching
  99. /// <summary>
  100. /// Left hand joint data
  101. /// </summary>
  102. private float[] leftSkeletons;
  103. /// <summary>
  104. /// Right hand joint data
  105. /// </summary>
  106. private float[] rightSkeletons;
  107. /// <summary>
  108. /// Left hand rotation
  109. /// </summary>
  110. private float[] leftRootRotation;
  111. /// <summary>
  112. /// Right hand rotation
  113. /// </summary>
  114. private float[] rightRootRotation;
  115. /// <summary>
  116. /// Left skeletons rotation
  117. /// </summary>
  118. private float[] leftSkeletonRotation;
  119. /// <summary>
  120. /// Right skeletons rotation
  121. /// </summary>
  122. private float[] rightSkeletonRotation;
  123. /// <summary>
  124. /// Right hand coordinate axis point information
  125. /// </summary>
  126. private float[] rightRootAxis;
  127. /// <summary>
  128. /// Left hand coordinate axis point information
  129. /// </summary>
  130. private float[] leftRootAxis;
  131. private Quaternion[] leftSkeletonsRot;
  132. private Quaternion[] rightSkeletonsRot;
  133. private Vector3[] leftSkeletonsPos;
  134. private Vector3[] rightSkeletonsPos;
  135. /// <summary>
  136. /// Retrieve the number of tracked hands
  137. /// </summary>
  138. private int handNum;
  139. #endregion
  140. private Vector3 preLeftHandPos;
  141. private Vector3 preRightHandPos;
  142. private Vector3 preHandPos;
  143. /// <summary>
  144. /// Cache a Vector3 array to reduce garbage collection (GC).
  145. /// </summary>
  146. /// <typeparam name="int"></typeparam>
  147. /// <typeparam name="Vector3[]"></typeparam>
  148. /// <returns></returns>
  149. private Dictionary<string, Vector3[]> vector3Dict = new Dictionary<string, Vector3[]>();
  150. /// <summary>
  151. /// Cache a quaternion to reduce garbage collection (GC).
  152. /// </summary>
  153. private Quaternion rotation = Quaternion.identity;
  154. /// <summary>
  155. /// Cache a vector to reduce garbage collection (GC)
  156. /// </summary>
  157. private Vector3 op = Vector3.zero;
  158. [SerializeField, Tooltip("手被主动过滤后是否弹出提示")]
  159. private bool showToastOnHandLost = true;
  160. [SerializeField, Tooltip("手被过滤的阈值")]
  161. private float handLostThreshold = 0.1f;
  162. private float lostTimeThreshold = 1.0f;
  163. private float elapsedTime = 0;
  164. private long gesPreImageTimeStamp = 0;
  165. private Pose cameraPose = Pose.identity;
  166. /// <summary>
  167. /// user height
  168. /// </summary>
  169. private float height = 170;
  170. private Thread getDataThread;
  171. public void Initialze()
  172. {
  173. if (Utils.IsAndroidPlatfrom() && !FuncDeviceCheck.CheckFunc(FuncDeviceCheck.FuncEnum.HandTracking))
  174. {
  175. return;
  176. }
  177. InitCache();
  178. InitGesture();
  179. GesEventInput.OnTrackedFailed += OnTrackedFailed;
  180. }
  181. private void InitCache()
  182. {
  183. //初始化缓存...
  184. leftGestureTypeCache = new GestureType[gesCacheCount];
  185. rightGestureTypeCache = new GestureType[gesCacheCount];
  186. leftGesture = new Gesture(HandType.LeftHand);
  187. rightGesture = new Gesture(HandType.RightHand);
  188. leftSkeletons = new float[21 * 3];
  189. rightSkeletons = new float[21 * 3];
  190. leftRootAxis = new float[12];
  191. rightRootAxis = new float[12];
  192. leftRootRotation = new float[4];
  193. rightRootRotation = new float[4];
  194. leftSkeletonRotation = new float[189];
  195. rightSkeletonRotation = new float[189];
  196. leftSkeletonsPos = new Vector3[21];
  197. rightSkeletonsPos = new Vector3[21];
  198. leftSkeletonsRot = new Quaternion[21];
  199. rightSkeletonsRot = new Quaternion[21];
  200. }
  201. private void OnTrackedFailed(HandType hand)
  202. {
  203. if (hand == HandType.LeftHand)
  204. {
  205. if (preLeftGesType == GestureType.Grip || preLeftGesType == GestureType.Pinch)
  206. {
  207. preLeftGesData.gesture_type = (int)GestureType.None;
  208. ProcessGesData(preLeftGesData);
  209. }
  210. }
  211. else if (hand == HandType.RightHand)
  212. {
  213. if (preRightGesType == GestureType.Grip || preRightGesType == GestureType.Pinch)
  214. {
  215. preRightGesData.gesture_type = (int)GestureType.None;
  216. ProcessGesData(preRightGesData);
  217. }
  218. }
  219. }
  220. protected void OnDestroy()
  221. {
  222. Release();
  223. GesEventInput.OnTrackedFailed -= OnTrackedFailed;
  224. }
  225. /// <summary>
  226. /// Initialize logic
  227. /// </summary>
  228. private void InitGesture()
  229. {
  230. if (initGesModule == false && Utils.IsAndroidPlatfrom())
  231. {
  232. initGesModule = true;
  233. RKLog.KeyInfo("====GesImplementation==== Initialize");
  234. if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
  235. {
  236. Permission.RequestUserPermission(Permission.Camera);
  237. }
  238. NativeInterface.NativeAPI.InitGesture();
  239. getDataThread = new Thread(() =>
  240. {
  241. while (initGesModule && getDataThread.ThreadState != ThreadState.Aborted && getDataThread.ThreadState != ThreadState.Stopped)
  242. {
  243. GetData();
  244. Thread.Sleep(10);
  245. }
  246. });
  247. getDataThread.Start();
  248. }
  249. }
  250. private void Release()
  251. {
  252. if (initGesModule == true)
  253. {
  254. initGesModule = false;
  255. if (getDataThread.ThreadState != ThreadState.Aborted && getDataThread.ThreadState != ThreadState.Stopped)
  256. getDataThread.Abort();
  257. NativeInterface.NativeAPI.ReleaseGesure();
  258. RKLog.KeyInfo("====GesImplementation==== Release");
  259. // 释放事件
  260. if (leftGesture.trackingSuccess)
  261. {
  262. RKLog.KeyInfo($"====GesImplementation====: tracking failed {HandType.LeftHand}");
  263. leftGesture.trackingSuccess = false;
  264. GesEventInput.OnTrackedFailed?.Invoke(HandType.LeftHand);
  265. }
  266. if (rightGesture.trackingSuccess)
  267. {
  268. RKLog.KeyInfo($"====GesImplementation====: tracking failed {HandType.RightHand}");
  269. rightGesture.trackingSuccess = false;
  270. GesEventInput.OnTrackedFailed?.Invoke(HandType.RightHand);
  271. }
  272. }
  273. }
  274. private void OnApplicationPause(bool pause)
  275. {
  276. if (pause)
  277. {
  278. Release();
  279. }
  280. else
  281. {
  282. InitGesture();
  283. }
  284. }
  285. #region LogHandTrackCount
  286. private float logTime = 5;
  287. private float logElapsedTime = 0;
  288. private void LogHandKeyInfo()
  289. {
  290. logElapsedTime += Time.deltaTime;
  291. if (logElapsedTime > logTime)
  292. {
  293. logElapsedTime = 0;
  294. RKLog.KeyInfo($"====GesImplementation==== TrackingHandNum: {NativeInterface.NativeAPI.GetTrackingHandNum()}");
  295. }
  296. }
  297. #endregion
  298. public void Update()
  299. {
  300. if (initGesModule)
  301. {
  302. ProcessData(gestureData, handNum);
  303. LogHandKeyInfo();
  304. }
  305. }
  306. private void GetData()
  307. {
  308. long timeStamp = NativeInterface.NativeAPI.GetCurrentFrameTimeStamp();
  309. if (gesPreImageTimeStamp == timeStamp)
  310. {
  311. return;
  312. }
  313. gesPreImageTimeStamp = timeStamp;
  314. handNum = Mathf.Clamp(NativeInterface.NativeAPI.GetTrackingHandNum(), 0, 2);
  315. if (handNum > 0)
  316. {
  317. cameraPose = NativeInterface.NativeAPI.GetHistoryCameraPhysicsPose(timeStamp);
  318. if (float.IsNaN(cameraPose.position.x) || float.IsNaN(cameraPose.rotation.x))
  319. {
  320. RKLog.Error($"====GesImplementation==== History Pose Data Error: {cameraPose.position},{cameraPose.rotation.eulerAngles},{timeStamp} ");
  321. return;
  322. }
  323. for (int i = 0; i < handNum; i++)
  324. {
  325. //0:左手 1:右手
  326. int handType = NativeInterface.NativeAPI.GetTrackingHandLrHand(i);
  327. gestureData[i].hand_type = handType == 0 ? 2 : handType;
  328. int gesType = NativeInterface.NativeAPI.GetTrackingHandCombineGestureType(i);
  329. gestureData[i].gesture_type = gesType == 0 ? -1 : gesType;
  330. gestureData[i].hand_orientation = NativeInterface.NativeAPI.GetTrackingHandOrientation(i);
  331. if (gestureData[i].hand_type == 2)
  332. {
  333. //左手(骨骼数据和mesh数据)
  334. NativeInterface.NativeAPI.GetTrackingHandSkeletonCAM(leftSkeletons, i);
  335. gestureData[i].skeletons = GetVertices(leftSkeletons, false, cameraPose);
  336. NativeInterface.NativeAPI.GetTrackingHandRootRotationAxisCAM(leftRootAxis, i);
  337. gestureData[i].axis = GetVertices(leftRootAxis, false, cameraPose);
  338. NativeInterface.NativeAPI.GetTrackingHandRootRotation(leftRootRotation, i);
  339. gestureData[i].rotation = HandUtils.GetQuaternion(leftRootRotation, false, cameraPose);
  340. NativeInterface.NativeAPI.GetTrackingHandSkeletonRotationAll(leftSkeletonRotation, i, 0);
  341. gestureData[i].skeletonsRot = HandUtils.GetSkeletonsQuaternion(leftSkeletonRotation, false, leftSkeletonsRot, cameraPose);
  342. gestureData[i].skeletonsRot = HandUtils.AdjustSkeletonsRot(gestureData[i].skeletons, gestureData[i].skeletonsRot, Vector3.up, leftSkeletonsRot);
  343. gestureData[i].skeletons = HandUtils.AdjustSkeletonsPos(gestureData[i].skeletons, leftSkeletonsPos);
  344. }
  345. else
  346. {
  347. //右手(骨骼数据和mesh数据)
  348. NativeInterface.NativeAPI.GetTrackingHandSkeletonCAM(rightSkeletons, i);
  349. gestureData[i].skeletons = GetVertices(rightSkeletons, true, cameraPose);
  350. NativeInterface.NativeAPI.GetTrackingHandRootRotationAxisCAM(rightRootAxis, i);
  351. gestureData[i].axis = GetVertices(rightRootAxis, true, cameraPose);
  352. NativeInterface.NativeAPI.GetTrackingHandRootRotation(rightRootRotation, i);
  353. gestureData[i].rotation = HandUtils.GetQuaternion(rightRootRotation, true, cameraPose);
  354. NativeInterface.NativeAPI.GetTrackingHandSkeletonRotationAll(rightSkeletonRotation, i, 0);
  355. gestureData[i].skeletonsRot = HandUtils.GetSkeletonsQuaternion(rightSkeletonRotation, true, rightSkeletonsRot, cameraPose);
  356. gestureData[i].skeletonsRot = HandUtils.AdjustSkeletonsRot(gestureData[i].skeletons, gestureData[i].skeletonsRot, Vector3.up, rightSkeletonsRot);
  357. gestureData[i].skeletons = HandUtils.AdjustSkeletonsPos(gestureData[i].skeletons, rightSkeletonsPos);
  358. }
  359. // 使用坐标轴的原点作为手心的位置
  360. gestureData[i].position = gestureData[i].axis[3];
  361. }
  362. //将数组内的第二个数据置空
  363. if (handNum == 1)
  364. gestureData[1].hand_type = (int)HandType.None;
  365. //排除两只手,类型相同的情况
  366. if (handNum == 2 && gestureData[0].hand_type == gestureData[1].hand_type)
  367. {
  368. handNum = 1;
  369. gestureData[1].hand_type = (int)HandType.None;
  370. }
  371. }
  372. }
  373. /// <summary>
  374. /// Process data for 3D gestures
  375. /// </summary>
  376. /// <param name="beans"></param>
  377. public void ProcessData(GestureBean[] beans, int handNum)
  378. {
  379. if (showDebugLog && logText != null)
  380. {
  381. logText.text = $" handNum: {handNum}";
  382. logText.text += "\n" + $" PhysicalCameraPose: {cameraPose.position},{cameraPose.rotation.eulerAngles}";
  383. logText.text += "\n" + $" MainCameraPose: {MainCameraCache.mainCamera.transform.position},{MainCameraCache.mainCamera.transform.rotation.eulerAngles}";
  384. }
  385. int startIndex = 0;
  386. #if !UNITY_EDITOR
  387. //增加手势过滤的阈值
  388. for (int i = 0; i < handNum; i++)
  389. {
  390. Vector3 handPosInCamera = MainCameraCache.mainCamera.transform.InverseTransformPoint(beans[i].position);
  391. if (handPosInCamera.z < handLostThreshold)
  392. {
  393. RKLog.Info($"====GesImplementation==== 过滤手势,手距离相机过近:{handPosInCamera},{handNum},{beans[i].ThreeGesKeyInfo()}");
  394. if (showToastOnHandLost)
  395. {
  396. elapsedTime += Time.deltaTime;
  397. if (elapsedTime > lostTimeThreshold)
  398. {
  399. HandType hand = (HandType)beans[i].hand_type;
  400. GesEventInput.OnHandLostInCameraSpace?.Invoke(hand);
  401. }
  402. }
  403. startIndex++;
  404. }
  405. else
  406. {
  407. elapsedTime = 0;
  408. }
  409. }
  410. #endif
  411. if (beans != null && handNum - startIndex > 0)
  412. {
  413. if (handNum - startIndex == 1)
  414. {
  415. HandType handType = (HandType)beans[0].hand_type;
  416. if (handType == HandType.RightHand)
  417. {
  418. if (leftGesture.trackingSuccess)
  419. {
  420. RKLog.KeyInfo($"====GesImplementation====: tracking failed {HandType.LeftHand}");
  421. leftGesture.trackingSuccess = false;
  422. GesEventInput.OnTrackedFailed?.Invoke(HandType.LeftHand);
  423. }
  424. }
  425. else if (handType == HandType.LeftHand)
  426. {
  427. if (rightGesture.trackingSuccess)
  428. {
  429. RKLog.KeyInfo($"====GesImplementation====: tracking failed {HandType.RightHand}");
  430. rightGesture.trackingSuccess = false;
  431. GesEventInput.OnTrackedFailed?.Invoke(HandType.RightHand);
  432. }
  433. }
  434. }
  435. for (int i = startIndex; i < handNum; i++)
  436. {
  437. HandType handType = (HandType)beans[i].hand_type;
  438. switch (handType)
  439. {
  440. case HandType.LeftHand:
  441. if (leftGesture.trackingSuccess == false)
  442. {
  443. RKLog.KeyInfo($"====GesImplementation====: tracking success {HandType.LeftHand}");
  444. leftGesture.trackingSuccess = true;
  445. GesEventInput.OnTrackedSuccess?.Invoke(HandType.LeftHand);
  446. }
  447. break;
  448. case HandType.RightHand:
  449. if (rightGesture.trackingSuccess == false)
  450. {
  451. RKLog.KeyInfo($"====GesImplementation====: tracking success {HandType.RightHand}");
  452. rightGesture.trackingSuccess = true;
  453. GesEventInput.OnTrackedSuccess?.Invoke(HandType.RightHand);
  454. }
  455. break;
  456. }
  457. ProcessGesData(beans[i]);
  458. }
  459. }
  460. else
  461. {
  462. //两只手丢失的情况
  463. if (leftGesture.trackingSuccess)
  464. {
  465. RKLog.KeyInfo($"====GesImplementation====: tracking failed {HandType.LeftHand}");
  466. leftGesture.trackingSuccess = false;
  467. GesEventInput.OnTrackedFailed?.Invoke(HandType.LeftHand);
  468. }
  469. if (rightGesture.trackingSuccess)
  470. {
  471. RKLog.KeyInfo($"====GesImplementation====: tracking failed {HandType.RightHand}");
  472. rightGesture.trackingSuccess = false;
  473. GesEventInput.OnTrackedFailed?.Invoke(HandType.RightHand);
  474. }
  475. }
  476. }
  477. /// <summary>
  478. /// Process gesture data
  479. /// </summary>
  480. /// <param name="gesData">gesture data</param>
  481. protected void ProcessGesData(GestureBean gesData)
  482. {
  483. if (showDebugLog && logText != null)
  484. {
  485. logText.text += gesData.ThreeGesKeyInfo();
  486. }
  487. HandType handType = (HandType)gesData.hand_type;
  488. GestureType gesType = (GestureType)gesData.gesture_type;
  489. //优化手势类型的判定
  490. if (JudgeGesType(handType) == GestureType.OpenPinch)
  491. {
  492. gesType = GestureType.OpenPinch;
  493. }
  494. //手表模式的特殊过滤
  495. if (InputModuleManager.Instance.GetWatchModuleActive(handType) && JudgeGesType(handType, GestureType.Pinch) == GestureType.Grip)
  496. {
  497. gesType = GestureType.Grip;
  498. }
  499. if (handType == HandType.RightHand)
  500. {
  501. if (rightGesFrame < rightGestureTypeCache.Length)
  502. {
  503. rightGestureTypeCache[rightGesFrame] = gesType;
  504. rightGesFrame++;
  505. }
  506. else
  507. {
  508. rightGesFrame = 0;
  509. }
  510. //处理拖拽过程中Grip和Pinch互相误识别的问题
  511. if (preRightGesType == GestureType.Grip)
  512. {
  513. if (gesType == GestureType.Pinch)
  514. {
  515. gesType = GestureType.Grip;
  516. }
  517. if (gesType == GestureType.None && !CanChangeToGesType(GestureType.None, HandType.RightHand))
  518. {
  519. gesType = GestureType.Grip;
  520. }
  521. }
  522. //处理特殊角度手势Pinch和Grip误识别
  523. if (HasGesType(GestureType.OpenPinch, 20, HandType.RightHand) && gesType == GestureType.Grip)
  524. {
  525. gesType = GestureType.Pinch;
  526. }
  527. }
  528. else if (handType == HandType.LeftHand)
  529. {
  530. if (leftGesFrame < leftGestureTypeCache.Length)
  531. {
  532. leftGestureTypeCache[leftGesFrame] = gesType;
  533. leftGesFrame++;
  534. }
  535. else
  536. {
  537. leftGesFrame = 0;
  538. }
  539. //处理拖拽过程中Grip和Pinch互相误识别的问题
  540. if (preLeftGesType == GestureType.Grip)
  541. {
  542. if (gesType == GestureType.Pinch)
  543. {
  544. gesType = GestureType.Grip;
  545. }
  546. if (gesType == GestureType.None && !CanChangeToGesType(GestureType.None, HandType.LeftHand))
  547. {
  548. gesType = GestureType.Grip;
  549. }
  550. }
  551. //处理特殊角度手势Pinch和Grip误识别
  552. if (HasGesType(GestureType.OpenPinch, 20, HandType.LeftHand) && gesType == GestureType.Grip)
  553. {
  554. gesType = GestureType.Pinch;
  555. }
  556. }
  557. if (showDebugLog && logText != null)
  558. logText.text += $"\n FilterGesData {handType},{gesType}";
  559. //初始化数据
  560. if (handType == HandType.LeftHand)
  561. {
  562. leftGesture.Reset();
  563. leftGesture.gesType = gesType;
  564. leftGesture.position = gesData.position;
  565. leftGesture.deltaPos = leftGesture.position - preLeftHandPos;
  566. preLeftHandPos = leftGesture.position;
  567. leftGesture.handOrientation = gesData.hand_orientation == 0 ? HandOrientation.Palm : HandOrientation.Back;
  568. //处理手心手背的判定
  569. if (Vector3.Dot(gesData.rotation * Vector3.forward, MainCameraCache.mainCamera.transform.forward) < 0 && Vector3.Dot(gesData.rotation * Vector3.right, MainCameraCache.mainCamera.transform.right) > 0)
  570. {
  571. leftGesture.handOrientation = HandOrientation.Back;
  572. }
  573. GesEventInput.OnHandOrientationUpdate?.Invoke(HandType.LeftHand, leftGesture.handOrientation);
  574. }
  575. else
  576. {
  577. rightGesture.Reset();
  578. rightGesture.gesType = gesType;
  579. rightGesture.position = gesData.position;
  580. rightGesture.deltaPos = rightGesture.position - preRightHandPos;
  581. preRightHandPos = rightGesture.position;
  582. rightGesture.handOrientation = gesData.hand_orientation == 0 ? HandOrientation.Palm : HandOrientation.Back;
  583. if (Vector3.Dot(gesData.rotation * Vector3.forward, MainCameraCache.mainCamera.transform.forward) < 0 && Vector3.Dot(gesData.rotation * Vector3.right, MainCameraCache.mainCamera.transform.right) > 0)
  584. {
  585. rightGesture.handOrientation = HandOrientation.Back;
  586. }
  587. GesEventInput.OnHandOrientationUpdate?.Invoke(HandType.RightHand, rightGesture.handOrientation);
  588. }
  589. if (gesType == GestureType.Pinch || gesType == GestureType.Grip)
  590. {
  591. if (handType == HandType.LeftHand)
  592. {
  593. leftClickTime += Time.deltaTime;
  594. leftGesture.handPress = true;
  595. }
  596. else
  597. {
  598. rightClickTime += Time.deltaTime;
  599. rightGesture.handPress = true;
  600. }
  601. GesEventInput.OnHandPress?.Invoke(handType);
  602. }
  603. else
  604. {
  605. if (handType == HandType.LeftHand)
  606. {
  607. leftGesture.handPress = false;
  608. }
  609. else
  610. {
  611. rightGesture.handPress = false;
  612. }
  613. GesEventInput.OnHandRelease?.Invoke(handType);
  614. }
  615. switch (gesType)
  616. {
  617. case GestureType.Grip:
  618. if (handType == HandType.LeftHand && preLeftGesType != GestureType.Grip)
  619. {
  620. leftGesture.handDown = true;
  621. }
  622. if (handType == HandType.RightHand && preRightGesType != GestureType.Grip)
  623. {
  624. rightGesture.handDown = true;
  625. }
  626. break;
  627. case GestureType.Pinch:
  628. if (handType == HandType.LeftHand && preLeftGesType != GestureType.Pinch)
  629. {
  630. leftGesture.handDown = true;
  631. }
  632. if (handType == HandType.RightHand && preRightGesType != GestureType.Pinch)
  633. {
  634. rightGesture.handDown = true;
  635. }
  636. break;
  637. default:
  638. if (handType == HandType.LeftHand &&
  639. (preLeftGesType == GestureType.Pinch &&
  640. gesType != GestureType.Grip) ||
  641. (leftHandInteractorType == InteractorType.Near ?
  642. (preLeftGesType == GestureType.Grip && gesType == GestureType.Palm) :
  643. preLeftGesType == GestureType.Grip && gesType != GestureType.Grip))
  644. {
  645. RKLog.Info($"====GesImplementation====: Trigger LeftHandUp {preLeftGesType},{gesType}");
  646. leftGesture.handUp = true;
  647. }
  648. if (handType == HandType.RightHand &&
  649. (preRightGesType == GestureType.Pinch &&
  650. gesType != GestureType.Grip) ||
  651. (rightHandInteractorType == InteractorType.Near ?
  652. (preRightGesType == GestureType.Grip && gesType == GestureType.Palm) :
  653. preRightGesType == GestureType.Grip && gesType != GestureType.Grip))
  654. {
  655. RKLog.Info($"====GesImplementation====: Trigger RightHandUp {preRightGesType},{gesType}");
  656. rightGesture.handUp = true;
  657. }
  658. break;
  659. }
  660. if (handType == HandType.LeftHand)
  661. {
  662. preLeftGesType = gesType;
  663. preLeftGesData = gesData;
  664. if (leftClickTime > 0 && leftClickTime < clickTime && leftGesture.handUp && leftGesture.gesType != GestureType.Palm)
  665. {
  666. RKLog.Info("====GesImplementation====: OnLeftHandClick");
  667. GesEventInput.OnGesClick?.Invoke(handType);
  668. leftGesture.handClick = true;
  669. leftClickTime = 0;
  670. }
  671. else if (leftGesture.handUp)
  672. {
  673. leftClickTime = 0;
  674. }
  675. }
  676. else
  677. {
  678. preRightGesType = gesType;
  679. preRightGesData = gesData;
  680. if (rightClickTime > 0 && rightClickTime < clickTime && rightGesture.handUp && rightGesture.gesType != GestureType.Palm)
  681. {
  682. RKLog.Info("====GesImplementation====: OnRightHandClick");
  683. GesEventInput.OnGesClick?.Invoke(handType);
  684. rightGesture.handClick = true;
  685. rightClickTime = 0;
  686. }
  687. else if (rightGesture.handUp)
  688. {
  689. rightClickTime = 0;
  690. }
  691. }
  692. GesEventInput.OnRenderHand?.Invoke(handType, gesData);
  693. GesEventInput.OnProcessGesData?.Invoke(handType, gesData);
  694. Vector3 pinchCenterOri = (GetSkeletonPose(SkeletonIndexFlag.THUMB_MCP, handType).position + GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_MCP, handType).position) / 2;
  695. Vector3 handCenter = gesData.position;
  696. GesEventInput.OnRayPoseUpdate?.Invoke(handType, GetSkeletonPose(SkeletonIndexFlag.WRIST, handType).position, handCenter, pinchCenterOri, pinchCenterOri);
  697. }
  698. public Vector3[] GetVertices(float[] data, bool right, Pose cameraPose)
  699. {
  700. Vector3[] vertices;
  701. string key = right ? "right:" + data.Length / 3 : "left:" + data.Length / 3;
  702. if (vector3Dict.ContainsKey(key))
  703. {
  704. vertices = vector3Dict[key];
  705. }
  706. else
  707. {
  708. vertices = new Vector3[data.Length / 3];
  709. vector3Dict.Add(key, vertices);
  710. }
  711. for (int i = 0; i < vertices.Length; i++)
  712. {
  713. op[0] = data[3 * i] / 1000.0f;
  714. op[1] = -data[3 * i + 1] / 1000.0f;
  715. op[2] = data[3 * i + 2] / 1000.0f;
  716. op += new Vector3(0, 0, ((height - 170) / 5.0f) * 0.01f);
  717. vertices[i] = cameraPose.rotation * op;
  718. vertices[i] += cameraPose.position;
  719. }
  720. return vertices;
  721. }
  722. public bool GetHandDown(HandType type, bool isPinch)
  723. {
  724. if (type == HandType.RightHand)
  725. {
  726. return isPinch ? GetHandDown(HandType.RightHand) && rightGesture.gesType == GestureType.Pinch : GetHandDown(HandType.RightHand) && rightGesture.gesType == GestureType.Grip;
  727. }
  728. else if (type == HandType.LeftHand)
  729. {
  730. return isPinch ? GetHandDown(HandType.LeftHand) && leftGesture.gesType == GestureType.Pinch : GetHandDown(HandType.LeftHand) && leftGesture.gesType == GestureType.Grip;
  731. }
  732. else
  733. {
  734. return false;
  735. }
  736. }
  737. public bool GetHandUp(HandType type, bool isPinch)
  738. {
  739. if (type == HandType.RightHand)
  740. {
  741. return isPinch ? GetHandUp(HandType.RightHand) : GetHandUp(HandType.RightHand) && rightGesture.gesType == GestureType.Palm;
  742. }
  743. else if (type == HandType.LeftHand)
  744. {
  745. return isPinch ? GetHandUp(HandType.LeftHand) : GetHandUp(HandType.LeftHand) && leftGesture.gesType == GestureType.Palm;
  746. }
  747. else
  748. {
  749. return false;
  750. }
  751. }
  752. public bool GetHandUp(HandType type)
  753. {
  754. if (type == HandType.RightHand)
  755. {
  756. return rightGesture.handUp;
  757. }
  758. else if (type == HandType.LeftHand)
  759. {
  760. return leftGesture.handUp;
  761. }
  762. else
  763. {
  764. return false;
  765. }
  766. }
  767. public bool GetHandPress(HandType type)
  768. {
  769. if (type == HandType.RightHand)
  770. {
  771. return rightGesture.handPress;
  772. }
  773. else if (type == HandType.LeftHand)
  774. {
  775. return leftGesture.handPress;
  776. }
  777. else
  778. {
  779. return false;
  780. }
  781. }
  782. public void SetUserHeight(float height)
  783. {
  784. this.height = height;
  785. }
  786. public bool GetHandPress(HandType type, bool isPinch)
  787. {
  788. if (type == HandType.LeftHand)
  789. {
  790. return isPinch ? GetHandPress(HandType.LeftHand) && leftGesture.gesType == GestureType.Pinch : GetHandPress(HandType.LeftHand) && leftGesture.gesType == GestureType.Grip;
  791. }
  792. else if (type == HandType.RightHand)
  793. {
  794. return isPinch ? GetHandPress(HandType.RightHand) && rightGesture.gesType == GestureType.Pinch : GetHandPress(HandType.RightHand) && rightGesture.gesType == GestureType.Grip;
  795. }
  796. else
  797. {
  798. return false;
  799. }
  800. }
  801. public Gesture GetGesture(HandType type)
  802. {
  803. if (type == HandType.RightHand)
  804. {
  805. return rightGesture;
  806. }
  807. else
  808. {
  809. return leftGesture;
  810. }
  811. }
  812. public GestureType GetGestureType(HandType hand)
  813. {
  814. return GetGesture(hand).gesType;
  815. }
  816. public Vector3 GetHandPos(HandType handType)
  817. {
  818. switch (handType)
  819. {
  820. case HandType.LeftHand:
  821. return leftGesture.position;
  822. case HandType.RightHand:
  823. return rightGesture.position;
  824. case HandType.None:
  825. return Vector3.zero;
  826. }
  827. return Vector3.zero;
  828. }
  829. public Vector3 GetHandDeltaPos(HandType hand)
  830. {
  831. switch (hand)
  832. {
  833. case HandType.LeftHand:
  834. return leftGesture.deltaPos;
  835. case HandType.RightHand:
  836. return rightGesture.deltaPos;
  837. }
  838. return Vector3.zero;
  839. }
  840. public Pose GetSkeletonPose(SkeletonIndexFlag flag, HandType type)
  841. {
  842. Pose pose = new Pose();
  843. if (gestureData.Length > 0)
  844. {
  845. for (int i = 0; i < gestureData.Length; i++)
  846. {
  847. if (gestureData[i].hand_type == (int)type && gestureData[i].skeletons != null && gestureData[i].skeletons.Length > 0)
  848. {
  849. pose.position = gestureData[i].skeletons[(int)flag];
  850. pose.rotation = gestureData[i].skeletonsRot[(int)flag];
  851. }
  852. }
  853. }
  854. return pose;
  855. }
  856. /// <summary>
  857. /// Determine the filtered gesture types
  858. /// </summary>
  859. /// <param name="hand">左右手类型</param>
  860. /// <param name="gesType">输入的手势类型</param>
  861. /// <returns></returns>
  862. private GestureType JudgeGesType(HandType hand, GestureType gesType = GestureType.None)
  863. {
  864. //食指方向
  865. Vector3 indexForward = (GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_TIP, hand).position - GetSkeletonPose(SkeletonIndexFlag.INDEX_FINGER_MCP, hand).position).normalized;
  866. //中指方向
  867. Vector3 middleForward = (GetSkeletonPose(SkeletonIndexFlag.MIDDLE_FINGER_TIP, hand).position - GetSkeletonPose(SkeletonIndexFlag.MIDDLE_FINGER_MCP, hand).position).normalized;
  868. //无名指方向
  869. Vector3 ringFingerForward = (GetSkeletonPose(SkeletonIndexFlag.RING_FINGER_TIP, hand).position - GetSkeletonPose(SkeletonIndexFlag.RING_FINGER_MCP, hand).position).normalized;
  870. //小拇指方向
  871. Vector3 pinkyForward = (GetSkeletonPose(SkeletonIndexFlag.PINKY_TIP, hand).position - GetSkeletonPose(SkeletonIndexFlag.PINKY_MCP, hand).position).normalized;
  872. //手方向
  873. Vector3 handForward = (GetSkeletonPose(SkeletonIndexFlag.MIDDLE_FINGER_MCP, hand).position - GetSkeletonPose(SkeletonIndexFlag.WRIST, hand).position).normalized;
  874. float dotHandIndex = Vector3.Dot(handForward, indexForward);
  875. float dotHandMiddle = Vector3.Dot(handForward, middleForward);
  876. float dotHandRing = Vector3.Dot(handForward, ringFingerForward);
  877. float dotHandPinky = Vector3.Dot(handForward, pinkyForward);
  878. if (showDebugLog && logText != null)
  879. logText.text += "\n" + $"JudgeGesType => dotHandIndex:{dotHandIndex},dotHandMiddle:{dotHandMiddle},dotHandRing:{dotHandRing},dotHandPinky:{dotHandPinky}";
  880. if (dotHandIndex > 0.5f && dotHandMiddle < 0f && dotHandRing < 0f && dotHandPinky < 0f)
  881. {
  882. return GestureType.OpenPinch;
  883. }
  884. if (dotHandIndex < 0.8f && dotHandMiddle < 0.8f && dotHandRing < 0.8f && dotHandPinky < 0.8f && gesType == GestureType.Pinch)
  885. {
  886. return GestureType.Grip;
  887. }
  888. return GestureType.None;
  889. }
  890. /// <summary>
  891. /// Determine if there is a target gesture type within the previous frame
  892. /// </summary>
  893. /// <param name="targetGes"></param>
  894. /// <param name="preFrame"></param>
  895. /// <returns></returns>
  896. private bool HasGesType(GestureType targetGes, int preFrame, HandType hand)
  897. {
  898. if (hand == HandType.None)
  899. return false;
  900. GestureType[] gestureTypeCache = hand == HandType.LeftHand ? leftGestureTypeCache : rightGestureTypeCache;
  901. for (int i = 1; i <= preFrame; i++)
  902. {
  903. int index = hand == HandType.LeftHand ? leftGesFrame - i : rightGesFrame - i;
  904. if (index < 0)
  905. {
  906. index = gestureTypeCache.Length + index;
  907. }
  908. if (gestureTypeCache[index] == targetGes)
  909. {
  910. return true;
  911. }
  912. }
  913. return false;
  914. }
  915. /// <summary>
  916. /// Retrieve the hand pose
  917. /// </summary>
  918. /// <param name="type"></param>
  919. /// <returns></returns>
  920. public Pose GetHandPose(HandType type)
  921. {
  922. for (int i = 0; i < gestureData.Length; i++)
  923. {
  924. if (gestureData[i].hand_type == (int)type)
  925. {
  926. return new Pose(gestureData[i].position, gestureData[i].rotation);
  927. }
  928. }
  929. return new Pose();
  930. }
  931. public InteractorType GetInteractorType(HandType hand)
  932. {
  933. switch (hand)
  934. {
  935. case HandType.LeftHand:
  936. return leftHandInteractorType;
  937. case HandType.RightHand:
  938. return rightHandInteractorType;
  939. default:
  940. return InteractorType.None;
  941. }
  942. }
  943. public HandOrientation GetHandOrientation(HandType hand)
  944. {
  945. switch (hand)
  946. {
  947. case HandType.LeftHand:
  948. return leftGesture.handOrientation;
  949. case HandType.RightHand:
  950. return rightGesture.handOrientation;
  951. }
  952. return default(HandOrientation);
  953. }
  954. public void SetInteractorType(InteractorType type, HandType hand)
  955. {
  956. switch (hand)
  957. {
  958. case HandType.LeftHand:
  959. leftHandInteractorType = type;
  960. break;
  961. case HandType.RightHand:
  962. rightHandInteractorType = type;
  963. break;
  964. }
  965. }
  966. public bool GetHandDown(HandType type)
  967. {
  968. if (type == HandType.RightHand)
  969. {
  970. return rightGesture.handDown;
  971. }
  972. else if (type == HandType.LeftHand)
  973. {
  974. return leftGesture.handDown;
  975. }
  976. else
  977. {
  978. return false;
  979. }
  980. }
  981. /// <summary>
  982. /// Can switch to a specific gesture
  983. /// </summary>
  984. /// <param name="targetGes"></param>
  985. /// <returns></returns>
  986. public virtual bool CanChangeToGesType(GestureType targetGes, HandType hand)
  987. {
  988. GestureType[] gestureTypeCache = hand == HandType.LeftHand ? leftGestureTypeCache : rightGestureTypeCache;
  989. int count = 0;
  990. for (int i = 0; i < gestureTypeCache.Length; i++)
  991. {
  992. if (targetGes == gestureTypeCache[i])
  993. {
  994. count++;
  995. }
  996. }
  997. //超过百分之50%激活...
  998. return count > changeGesThreshold;
  999. }
  1000. }
  1001. }