PXR_HandTracking.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*******************************************************************************
  2. Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved.
  3. NOTICE:All information contained herein is, and remains the property of
  4. PICO Technology Co., Ltd. The intellectual and technical concepts
  5. contained herein are proprietary to PICO Technology Co., Ltd. and may be
  6. covered by patents, patents in process, and are protected by trade secret or
  7. copyright law. Dissemination of this information or reproduction of this
  8. material is strictly forbidden unless prior written permission is obtained from
  9. PICO Technology Co., Ltd.
  10. *******************************************************************************/
  11. using System.Runtime.InteropServices;
  12. using UnityEngine;
  13. namespace Unity.XR.PXR
  14. {
  15. public enum HandType
  16. {
  17. HandLeft = 0,
  18. HandRight = 1,
  19. }
  20. /// <summary>
  21. /// The current active input device.
  22. /// </summary>
  23. public enum ActiveInputDevice
  24. {
  25. /// <summary>
  26. /// HMD
  27. /// </summary>
  28. HeadActive = 0,
  29. /// <summary>
  30. /// Controllers
  31. /// </summary>
  32. ControllerActive = 1,
  33. /// <summary>
  34. /// Hands
  35. /// </summary>
  36. HandTrackingActive = 2,
  37. }
  38. public struct Vector3f
  39. {
  40. public float x;
  41. public float y;
  42. public float z;
  43. public Vector3 ToVector3()
  44. {
  45. return new Vector3() { x = x, y = y, z = -z };
  46. }
  47. }
  48. public struct Quatf
  49. {
  50. public float x;
  51. public float y;
  52. public float z;
  53. public float w;
  54. public Quaternion ToQuat()
  55. {
  56. return new Quaternion() { x = x, y = y, z = -z, w = -w };
  57. }
  58. }
  59. /// <summary>
  60. /// The location of hand joint.
  61. /// </summary>
  62. public struct Posef
  63. {
  64. /// <summary>
  65. /// The orientation of hand joint.
  66. /// </summary>
  67. public Quatf Orientation;
  68. /// <summary>
  69. /// The position of hand joint.
  70. /// </summary>
  71. public Vector3f Position;
  72. public override string ToString()
  73. {
  74. return string.Format("Orientation :{0}, {1}, {2}, {3} Position: {4}, {5}, {6}",
  75. Orientation.x, Orientation.y, Orientation.z, Orientation.w,
  76. Position.x, Position.y, Position.z);
  77. }
  78. }
  79. /// <summary>
  80. /// The status of ray and fingers.
  81. /// </summary>
  82. public enum HandAimStatus : ulong
  83. {
  84. /// <summary>
  85. /// Whether the data is valid.
  86. /// </summary>
  87. AimComputed = 0x00000001,
  88. /// <summary>
  89. /// Whether the ray appears.
  90. /// </summary>
  91. AimRayValid = 0x00000002,
  92. /// <summary>
  93. /// Whether the index finger pinches.
  94. /// </summary>
  95. AimIndexPinching = 0x00000004,
  96. /// <summary>
  97. /// Whether the middle finger pinches.
  98. /// </summary>
  99. AimMiddlePinching = 0x00000008,
  100. /// <summary>
  101. /// Whether the ring finger pinches.
  102. /// </summary>
  103. AimRingPinching = 0x00000010,
  104. /// <summary>
  105. /// Whether the little finger pinches.
  106. /// </summary>
  107. AimLittlePinching = 0x00000020,
  108. /// <summary>
  109. /// Whether the ray touches.
  110. /// </summary>
  111. AimRayTouched = 0x00000200
  112. }
  113. /// <summary>
  114. /// The data about the poses of ray and fingers.
  115. /// </summary>
  116. public struct HandAimState
  117. {
  118. /// <summary>
  119. /// The status of hand tracking. If it is not `tracked`, confidence will be `0`.
  120. /// </summary>
  121. public HandAimStatus aimStatus;
  122. /// <summary>
  123. /// The pose of the ray.
  124. /// </summary>
  125. public Posef aimRayPose;
  126. /// <summary>
  127. /// The strength of index finger's pinch.
  128. /// </summary>
  129. private float pinchStrengthIndex;
  130. /// <summary>
  131. /// The strength of middle finger's pinch.
  132. /// </summary>
  133. private float pinchStrengthMiddle;
  134. /// <summary>
  135. /// The strength of ring finger's pinch.
  136. /// </summary>
  137. private float pinchStrengthRing;
  138. /// <summary>
  139. /// The strength of little finger's pinch.
  140. /// </summary>
  141. private float pinchStrengthLittle;
  142. /// <summary>
  143. /// The strength of ray's touch.
  144. /// </summary>
  145. public float touchStrengthRay;
  146. }
  147. /// <summary>
  148. /// The data about the status of hand joint location.
  149. /// </summary>
  150. public enum HandLocationStatus : ulong
  151. {
  152. /// <summary>
  153. /// Whether the joint's orientation is valid.
  154. /// </summary>
  155. OrientationValid = 0x00000001,
  156. /// <summary>
  157. /// Whether the joint's position is valid.
  158. /// </summary>
  159. PositionValid = 0x00000002,
  160. /// <summary>
  161. /// Whether the joint's orientation is being tracked.
  162. /// </summary>
  163. OrientationTracked = 0x00000004,
  164. /// <summary>
  165. /// Whether the joint's position is being tracked.
  166. /// </summary>
  167. PositionTracked = 0x00000008
  168. }
  169. public enum HandJoint
  170. {
  171. JointPalm = 0,
  172. JointWrist = 1,
  173. JointThumbMetacarpal = 2,
  174. JointThumbProximal = 3,
  175. JointThumbDistal = 4,
  176. JointThumbTip = 5,
  177. JointIndexMetacarpal = 6,
  178. JointIndexProximal = 7,
  179. JointIndexIntermediate = 8,
  180. JointIndexDistal = 9,
  181. JointIndexTip = 10,
  182. JointMiddleMetacarpal = 11,
  183. JointMiddleProximal = 12,
  184. JointMiddleIntermediate = 13,
  185. JointMiddleDistal = 14,
  186. JointMiddleTip = 15,
  187. JointRingMetacarpal = 16,
  188. JointRingProximal = 17,
  189. JointRingIntermediate = 18,
  190. JointRingDistal = 19,
  191. JointRingTip = 20,
  192. JointLittleMetacarpal = 21,
  193. JointLittleProximal = 22,
  194. JointLittleIntermediate = 23,
  195. JointLittleDistal = 24,
  196. JointLittleTip = 25,
  197. JointMax = 26
  198. }
  199. /// <summary>
  200. /// The data about the location of hand joint.
  201. /// </summary>
  202. public struct HandJointLocation
  203. {
  204. /// <summary>
  205. /// The status of hand joint location.
  206. /// </summary>
  207. public HandLocationStatus locationStatus;
  208. /// <summary>
  209. /// The orientation and position of hand joint.
  210. /// </summary>
  211. public Posef pose;
  212. /// <summary>
  213. /// The radius of hand joint.
  214. /// </summary>
  215. public float radius;
  216. }
  217. /// <summary>
  218. /// The data about hand tracking.
  219. /// </summary>
  220. public struct HandJointLocations
  221. {
  222. /// <summary>
  223. /// The quality level of hand tracking:
  224. /// `0`: low
  225. /// `1`: high
  226. /// </summary>
  227. public uint isActive;
  228. /// <summary>
  229. /// The number of hand joints that the SDK supports. Currenty returns `26`.
  230. /// </summary>
  231. public uint jointCount;
  232. /// <summary>
  233. /// The scale of the hand.
  234. /// </summary>
  235. public float handScale;
  236. /// <summary>
  237. /// The locations (orientation and position) of hand joints.
  238. /// </summary>
  239. [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)HandJoint.JointMax)]
  240. public HandJointLocation[] jointLocations;
  241. }
  242. public enum HandFinger
  243. {
  244. Thumb = 0,
  245. Index = 1,
  246. Middle = 2,
  247. Ring = 3,
  248. Pinky = 4
  249. }
  250. public static class PXR_HandTracking
  251. {
  252. /// <summary>Gets whether hand tracking is enabled or disabled.</summary>
  253. /// <returns>
  254. /// * `true`: enabled
  255. /// * `false`: disabled
  256. /// </returns>
  257. public static bool GetSettingState()
  258. {
  259. return PXR_Plugin.HandTracking.UPxr_GetHandTrackerSettingState();
  260. }
  261. /// <summary>Gets the current active input device.</summary>
  262. /// <returns>The current active input device:
  263. /// * `HeadActive`: HMD
  264. /// * `ControllerActive`: controllers
  265. /// * `HandTrackingActive`: hands
  266. /// </returns>
  267. public static ActiveInputDevice GetActiveInputDevice()
  268. {
  269. return PXR_Plugin.HandTracking.UPxr_GetHandTrackerActiveInputType();
  270. }
  271. /// <summary>Gets the data about the pose of a specified hand, including the status of the ray and fingers, the strength of finger pinch and ray touch.</summary>
  272. /// <param name="hand">The hand to get data for:
  273. /// * `HandLeft`: left hand
  274. /// * `HandRight`: right hand
  275. /// </param>
  276. /// <param name="aimState">`HandAimState` contains the data about the poses of ray and fingers.
  277. /// If you use PICO hand prefabs without changing any of their default settings, you will get the following data:
  278. /// ```csharp
  279. /// public class PXR_Hand
  280. /// {
  281. /// // Whether the data is valid.
  282. /// public bool Computed { get; private set; }
  283. /// // The ray pose.
  284. /// public Posef RayPose { get; private set; }
  285. /// // Whether the ray was displayed.
  286. /// public bool RayValid { get; private set; }
  287. /// // Whether the ray pinched.
  288. /// public bool Pinch { get; private set; }
  289. /// // The strength of ray pinch.
  290. /// public float PinchStrength { get; private set; }
  291. /// ```
  292. /// </param>
  293. /// <returns>
  294. /// * `true`: success
  295. /// * `false`: failure
  296. /// </returns>
  297. public static bool GetAimState(HandType hand, ref HandAimState aimState)
  298. {
  299. if (!PXR_ProjectSetting.GetProjectConfig().handTracking) return false;
  300. return PXR_Plugin.HandTracking.UPxr_GetHandTrackerAimState(hand, ref aimState);
  301. }
  302. /// <summary>Gets the locations of joints for a specified hand.</summary>
  303. /// <param name="hand">The hand to get joint locations for:
  304. /// * `HandLeft`: left hand
  305. /// * `HandRight`: right hand
  306. /// </param>
  307. /// <param name="jointLocations">Contains data about the locations of the joints in the specified hand.</param>
  308. /// <returns>
  309. /// * `true`: success
  310. /// * `false`: failure
  311. /// </returns>
  312. public static bool GetJointLocations(HandType hand, ref HandJointLocations jointLocations)
  313. {
  314. if (!PXR_ProjectSetting.GetProjectConfig().handTracking) return false;
  315. return PXR_Plugin.HandTracking.UPxr_GetHandTrackerJointLocations(hand, ref jointLocations);
  316. }
  317. }
  318. }
粤ICP备19079148号