HandModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using UnityEngine;
  3. namespace Leap.Unity
  4. {
  5. public abstract class HandModel : IHandModel
  6. {
  7. public override Chirality Handedness
  8. {
  9. get
  10. {
  11. return this.handedness;
  12. }
  13. set
  14. {
  15. this.handedness = value;
  16. }
  17. }
  18. public abstract override ModelType HandModelType { get; }
  19. public Vector3 GetPalmPosition()
  20. {
  21. return this.hand_.PalmPosition.ToVector3();
  22. }
  23. public Quaternion GetPalmRotation()
  24. {
  25. if (this.hand_ != null)
  26. {
  27. return this.hand_.Basis.CalculateRotation();
  28. }
  29. if (this.palm)
  30. {
  31. return this.palm.rotation;
  32. }
  33. return Quaternion.identity;
  34. }
  35. public Vector3 GetPalmDirection()
  36. {
  37. if (this.hand_ != null)
  38. {
  39. return this.hand_.Direction.ToVector3();
  40. }
  41. if (this.palm)
  42. {
  43. return this.palm.forward;
  44. }
  45. return Vector3.forward;
  46. }
  47. public Vector3 GetPalmNormal()
  48. {
  49. if (this.hand_ != null)
  50. {
  51. return this.hand_.PalmNormal.ToVector3();
  52. }
  53. if (this.palm)
  54. {
  55. return -this.palm.up;
  56. }
  57. return -Vector3.up;
  58. }
  59. public Vector3 GetArmDirection()
  60. {
  61. if (this.hand_ != null)
  62. {
  63. return this.hand_.Arm.Direction.ToVector3();
  64. }
  65. if (this.forearm)
  66. {
  67. return this.forearm.forward;
  68. }
  69. return Vector3.forward;
  70. }
  71. public Vector3 GetArmCenter()
  72. {
  73. if (this.hand_ != null)
  74. {
  75. Vector vector = 0.5f * (this.hand_.Arm.WristPosition + this.hand_.Arm.ElbowPosition);
  76. return vector.ToVector3();
  77. }
  78. if (this.forearm)
  79. {
  80. return this.forearm.position;
  81. }
  82. return Vector3.zero;
  83. }
  84. public float GetArmLength()
  85. {
  86. return (this.hand_.Arm.WristPosition - this.hand_.Arm.ElbowPosition).Magnitude;
  87. }
  88. public float GetArmWidth()
  89. {
  90. return this.hand_.Arm.Width;
  91. }
  92. public Vector3 GetElbowPosition()
  93. {
  94. if (this.hand_ != null)
  95. {
  96. return this.hand_.Arm.ElbowPosition.ToVector3();
  97. }
  98. if (this.elbowJoint)
  99. {
  100. return this.elbowJoint.position;
  101. }
  102. return Vector3.zero;
  103. }
  104. public Vector3 GetWristPosition()
  105. {
  106. if (this.hand_ != null)
  107. {
  108. return this.hand_.Arm.WristPosition.ToVector3();
  109. }
  110. if (this.wristJoint)
  111. {
  112. return this.wristJoint.position;
  113. }
  114. return Vector3.zero;
  115. }
  116. public Quaternion GetArmRotation()
  117. {
  118. if (this.hand_ != null)
  119. {
  120. return this.hand_.Arm.Rotation.ToQuaternion();
  121. }
  122. if (this.forearm)
  123. {
  124. return this.forearm.rotation;
  125. }
  126. return Quaternion.identity;
  127. }
  128. public override Hand GetLeapHand()
  129. {
  130. return this.hand_;
  131. }
  132. public override void SetLeapHand(Hand hand)
  133. {
  134. this.hand_ = hand;
  135. for (int i = 0; i < this.fingers.Length; i++)
  136. {
  137. if (this.fingers[i] != null)
  138. {
  139. this.fingers[i].SetLeapHand(this.hand_);
  140. }
  141. }
  142. }
  143. public override void InitHand()
  144. {
  145. for (int i = 0; i < this.fingers.Length; i++)
  146. {
  147. if (this.fingers[i] != null)
  148. {
  149. this.fingers[i].fingerType = (Finger.FingerType)i;
  150. this.fingers[i].InitFinger();
  151. }
  152. }
  153. }
  154. public int LeapID()
  155. {
  156. if (this.hand_ != null)
  157. {
  158. return this.hand_.Id;
  159. }
  160. return -1;
  161. }
  162. public abstract override void UpdateHand();
  163. [SerializeField]
  164. private Chirality handedness;
  165. private ModelType handModelType;
  166. public const int NUM_FINGERS = 5;
  167. public float handModelPalmWidth = 0.085f;
  168. public FingerModel[] fingers = new FingerModel[5];
  169. public Transform palm;
  170. public Transform forearm;
  171. public Transform wristJoint;
  172. public Transform elbowJoint;
  173. protected Hand hand_;
  174. }
  175. }