FingerModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using UnityEngine;
  3. namespace Leap.Unity
  4. {
  5. public abstract class FingerModel : MonoBehaviour
  6. {
  7. public void SetLeapHand(Hand hand)
  8. {
  9. this.hand_ = hand;
  10. if (this.hand_ != null)
  11. {
  12. this.finger_ = hand.Fingers[(int)this.fingerType];
  13. }
  14. }
  15. public Hand GetLeapHand()
  16. {
  17. return this.hand_;
  18. }
  19. public Finger GetLeapFinger()
  20. {
  21. return this.finger_;
  22. }
  23. public virtual void InitFinger()
  24. {
  25. this.UpdateFinger();
  26. }
  27. public abstract void UpdateFinger();
  28. public Vector3 GetTipPosition()
  29. {
  30. if (this.finger_ != null)
  31. {
  32. return this.finger_.Bone(Bone.BoneType.TYPE_DISTAL).NextJoint.ToVector3();
  33. }
  34. if (this.bones[3] && this.joints[1])
  35. {
  36. return 2f * this.bones[3].position - this.joints[1].position;
  37. }
  38. return Vector3.zero;
  39. }
  40. public Vector3 GetJointPosition(int joint)
  41. {
  42. if (joint >= 4)
  43. {
  44. return this.GetTipPosition();
  45. }
  46. if (this.finger_ != null)
  47. {
  48. return this.finger_.Bone((Bone.BoneType)joint).PrevJoint.ToVector3();
  49. }
  50. if (this.joints[joint])
  51. {
  52. return this.joints[joint].position;
  53. }
  54. return Vector3.zero;
  55. }
  56. public Ray GetRay()
  57. {
  58. Ray result = new Ray(this.GetTipPosition(), this.GetBoneDirection(3));
  59. return result;
  60. }
  61. public Vector3 GetBoneCenter(int bone_type)
  62. {
  63. if (this.finger_ != null)
  64. {
  65. Bone bone = this.finger_.Bone((Bone.BoneType)bone_type);
  66. return bone.Center.ToVector3();
  67. }
  68. if (this.bones[bone_type])
  69. {
  70. return this.bones[bone_type].position;
  71. }
  72. return Vector3.zero;
  73. }
  74. public Vector3 GetBoneDirection(int bone_type)
  75. {
  76. if (this.finger_ != null)
  77. {
  78. return (this.GetJointPosition(bone_type + 1) - this.GetJointPosition(bone_type)).normalized;
  79. }
  80. if (this.bones[bone_type])
  81. {
  82. return this.bones[bone_type].forward;
  83. }
  84. return Vector3.forward;
  85. }
  86. public Quaternion GetBoneRotation(int bone_type)
  87. {
  88. if (this.finger_ != null)
  89. {
  90. return this.finger_.Bone((Bone.BoneType)bone_type).Rotation.ToQuaternion();
  91. }
  92. if (this.bones[bone_type])
  93. {
  94. return this.bones[bone_type].rotation;
  95. }
  96. return Quaternion.identity;
  97. }
  98. public float GetBoneLength(int bone_type)
  99. {
  100. return this.finger_.Bone((Bone.BoneType)bone_type).Length;
  101. }
  102. public float GetBoneWidth(int bone_type)
  103. {
  104. return this.finger_.Bone((Bone.BoneType)bone_type).Width;
  105. }
  106. public float GetFingerJointStretchMecanim(int joint_type)
  107. {
  108. Quaternion quaternion = Quaternion.identity;
  109. if (this.finger_ != null)
  110. {
  111. quaternion = Quaternion.Inverse(this.finger_.Bone((Bone.BoneType)joint_type).Rotation.ToQuaternion()) * this.finger_.Bone(joint_type + Bone.BoneType.TYPE_PROXIMAL).Rotation.ToQuaternion();
  112. }
  113. else if (this.bones[joint_type] && this.bones[joint_type + 1])
  114. {
  115. quaternion = Quaternion.Inverse(this.GetBoneRotation(joint_type)) * this.GetBoneRotation(joint_type + 1);
  116. }
  117. float num = -quaternion.eulerAngles.x;
  118. if (num <= -180f)
  119. {
  120. num += 360f;
  121. }
  122. return num;
  123. }
  124. public float GetFingerJointSpreadMecanim()
  125. {
  126. Quaternion quaternion = Quaternion.identity;
  127. if (this.finger_ != null)
  128. {
  129. quaternion = Quaternion.Inverse(this.finger_.Bone(Bone.BoneType.TYPE_METACARPAL).Rotation.ToQuaternion()) * this.finger_.Bone(Bone.BoneType.TYPE_PROXIMAL).Rotation.ToQuaternion();
  130. }
  131. else if (this.bones[0] && this.bones[1])
  132. {
  133. quaternion = Quaternion.Inverse(this.GetBoneRotation(0)) * this.GetBoneRotation(1);
  134. }
  135. float num = 0f;
  136. Finger.FingerType fingerType = this.fingerType;
  137. if (this.finger_ != null)
  138. {
  139. this.fingerType = this.finger_.Type;
  140. }
  141. if (fingerType == Finger.FingerType.TYPE_INDEX || fingerType == Finger.FingerType.TYPE_MIDDLE)
  142. {
  143. num = quaternion.eulerAngles.y;
  144. if (num > 180f)
  145. {
  146. num -= 360f;
  147. }
  148. }
  149. if (fingerType == Finger.FingerType.TYPE_THUMB || fingerType == Finger.FingerType.TYPE_RING || fingerType == Finger.FingerType.TYPE_PINKY)
  150. {
  151. num = -quaternion.eulerAngles.y;
  152. if (num <= -180f)
  153. {
  154. num += 360f;
  155. }
  156. }
  157. return num;
  158. }
  159. public const int NUM_BONES = 4;
  160. public const int NUM_JOINTS = 3;
  161. public Finger.FingerType fingerType = Finger.FingerType.TYPE_INDEX;
  162. public Transform[] bones = new Transform[4];
  163. public Transform[] joints = new Transform[3];
  164. protected Hand hand_;
  165. protected Finger finger_;
  166. }
  167. }