HandTrackingInfo.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Collections;
  5. using System;
  6. using Unity.Jobs;
  7. namespace Ximmerse.XR.InputSystems
  8. {
  9. /// <summary>
  10. /// State the left / right hand
  11. /// </summary>
  12. public enum HandnessType : byte
  13. {
  14. Left = 0, Right = 1,
  15. }
  16. /// <summary>
  17. /// hand tracking info.
  18. /// </summary>
  19. [Serializable]
  20. public struct HandTrackingInfo : IDisposable
  21. {
  22. /// <summary>
  23. /// The hand tracking frame time stamp.
  24. /// </summary>
  25. public long Timestamp;
  26. /// <summary>
  27. /// is currently valid tracking state ?
  28. /// </summary>
  29. public bool IsValid;
  30. /// <summary>
  31. /// Currently tracking left or right hand.
  32. /// For T3D, one hand per frame.
  33. /// </summary>
  34. public HandnessType Handness;
  35. /// <summary>
  36. /// Palm center world position
  37. /// </summary>
  38. public Vector3 PalmPosition;
  39. /// <summary>
  40. /// The palm delta position in world space.
  41. /// </summary>
  42. public Vector3 PalmDeltaPosition;
  43. /// <summary>
  44. /// Palm velocity.
  45. /// </summary>
  46. public Vector3 PalmVelocity;
  47. /// <summary>
  48. /// Palm surface world normal ray
  49. /// </summary>
  50. public Vector3 PalmNormal;
  51. /// <summary>
  52. /// Palm scale.
  53. /// </summary>
  54. public Vector3 PalmScale;
  55. /// <summary>
  56. /// Palm world rotation
  57. /// </summary>
  58. public Quaternion PalmRotation;
  59. /// <summary>
  60. /// The palm's local position relative to main camera's parent transform.
  61. /// </summary>
  62. public Vector3 PalmLocalPosition;
  63. /// <summary>
  64. /// The palm's local rotation relative to main camera's parent transform.
  65. /// </summary>
  66. public Quaternion PalmLocalRotation;
  67. /// <summary>
  68. /// The palm's local normal relative to main camera's parent transform.
  69. /// </summary>
  70. public Vector3 PalmLocalNormal;
  71. /// <summary>
  72. /// Thumb tracking info
  73. /// </summary>
  74. public RawFingerTrackingInfo ThumbFinger;
  75. /// <summary>
  76. /// Index tracking info
  77. /// </summary>
  78. public RawFingerTrackingInfo IndexFinger;
  79. /// <summary>
  80. /// Middle tracking info
  81. /// </summary>
  82. public RawFingerTrackingInfo MiddleFinger;
  83. /// <summary>
  84. /// Ring tracking info
  85. /// </summary>
  86. public RawFingerTrackingInfo RingFinger;
  87. /// <summary>
  88. /// Little finger info
  89. /// </summary>
  90. public RawFingerTrackingInfo LittleFinger;
  91. /// <summary>
  92. /// The current gesture type of open hand/fist.
  93. /// </summary>
  94. public GestureType_Fist_OpenHand gestureFistOpenHand;
  95. /// <summary>
  96. /// The current gesture type of grisp
  97. /// </summary>
  98. public GestureType_Grisp gestureGrisp;
  99. /// <summary>
  100. /// The gesture type from native plugin.
  101. /// -1 for invalid status.
  102. /// </summary>
  103. public int NativeGestureType;
  104. public bool IsTracking;
  105. internal void UpdateProperties()
  106. {
  107. ThumbFinger.UpdateInternalProperties();
  108. IndexFinger.UpdateInternalProperties();
  109. MiddleFinger.UpdateInternalProperties();
  110. RingFinger.UpdateInternalProperties();
  111. LittleFinger.UpdateInternalProperties();
  112. }
  113. public void Dispose()
  114. {
  115. ThumbFinger.Dispose();
  116. IndexFinger.Dispose();
  117. MiddleFinger.Dispose();
  118. RingFinger.Dispose();
  119. LittleFinger.Dispose();
  120. }
  121. internal void CopyFrom (HandTrackingInfo other)
  122. {
  123. Timestamp = other.Timestamp;
  124. IsValid = other.IsValid;
  125. Handness = other.Handness;
  126. PalmPosition = other.PalmPosition;
  127. PalmDeltaPosition = other.PalmDeltaPosition;
  128. PalmNormal = other.PalmNormal;
  129. PalmScale = other.PalmScale;
  130. PalmRotation = other.PalmRotation;
  131. PalmLocalPosition = other.PalmLocalPosition;
  132. PalmLocalRotation = other.PalmLocalRotation;
  133. PalmLocalNormal = other.PalmLocalNormal;
  134. ThumbFinger.CopyFrom(other.ThumbFinger);
  135. IndexFinger.CopyFrom(other.IndexFinger);
  136. MiddleFinger.CopyFrom(other.MiddleFinger);
  137. RingFinger.CopyFrom(other.RingFinger);
  138. LittleFinger.CopyFrom(other.LittleFinger);
  139. gestureFistOpenHand = other.gestureFistOpenHand;
  140. gestureGrisp = other.gestureGrisp;
  141. NativeGestureType = other.NativeGestureType;
  142. }
  143. }
  144. /// <summary>
  145. /// The raw finger data.
  146. /// </summary>
  147. [Serializable]
  148. public struct RawFingerTrackingInfo : IDisposable
  149. {
  150. /// <summary>
  151. /// Position nodes of each finger joint.
  152. /// </summary>
  153. public NativeArray<Vector3> Positions;
  154. /// <summary>
  155. /// Local position of the each finger joint.
  156. /// </summary>
  157. public NativeArray<Vector3> LocalPositions;
  158. /// <summary>
  159. /// Finger straightness factor.
  160. ///
  161. /// 1 means straight, 0 means bended.
  162. ///
  163. /// </summary>
  164. public float straightness;
  165. /// <summary>
  166. /// finger bendess factor.
  167. /// </summary>
  168. public float bendness;
  169. internal float bendnessRangeMin, bendnessRangeMax;
  170. internal void UpdateInternalProperties()
  171. {
  172. if (Positions.Length == 3)
  173. {
  174. Vector3 dir21 = (Positions[2] - Positions[1]).normalized;
  175. Vector3 dir10 = (Positions[1] - Positions[0]).normalized;
  176. this.straightness = Mathf.Abs(Vector3.Dot(dir21, dir10));
  177. }
  178. else if (Positions.Length == 4)
  179. {
  180. Vector3 dir32 = (Positions[3] - Positions[2]).normalized;
  181. Vector3 dir21 = (Positions[2] - Positions[1]).normalized;
  182. Vector3 dir10 = (Positions[1] - Positions[0]).normalized;
  183. this.straightness = Mathf.Abs(Vector3.Dot(dir32, dir21)) * Mathf.Abs(Vector3.Dot(dir21, dir10));
  184. }
  185. this.bendness = Mathf.Clamp01((1 - Mathf.InverseLerp(bendnessRangeMin, bendnessRangeMax, straightness) - bendnessRangeMin) / (bendnessRangeMax - bendnessRangeMin));
  186. }
  187. internal RawFingerTrackingInfo(int positionCapacity)
  188. {
  189. bendnessRangeMin = bendnessRangeMax = 0;
  190. straightness = 0;
  191. bendness = 0;
  192. Positions = new NativeArray<Vector3>(positionCapacity, Allocator.Persistent);
  193. LocalPositions = new NativeArray<Vector3>(positionCapacity, Allocator.Persistent);
  194. }
  195. public void Dispose()
  196. {
  197. if (Positions.IsCreated)
  198. {
  199. Positions.Dispose();
  200. }
  201. if(LocalPositions.IsCreated)
  202. {
  203. LocalPositions.Dispose();
  204. }
  205. }
  206. public void CopyFrom (RawFingerTrackingInfo other)
  207. {
  208. Positions.CopyFrom(other.Positions);
  209. LocalPositions.CopyFrom(other.LocalPositions);
  210. straightness = other.straightness;
  211. bendness = other.bendness;
  212. bendnessRangeMin = other.bendnessRangeMin;
  213. bendnessRangeMax = other.bendnessRangeMax;
  214. }
  215. }
  216. }