IKChain2D.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using UnityEngine.Scripting.APIUpdating;
  3. using UnityEngine.Serialization;
  4. namespace UnityEngine.U2D.IK
  5. {
  6. /// <summary>
  7. /// Class for storing data for a 2D IK Chain.
  8. /// </summary>
  9. [MovedFrom("UnityEngine.Experimental.U2D.IK")]
  10. [Serializable]
  11. public class IKChain2D
  12. {
  13. [SerializeField][FormerlySerializedAs("m_Target")]
  14. private Transform m_EffectorTransform;
  15. [SerializeField][FormerlySerializedAs("m_Effector")]
  16. private Transform m_TargetTransform;
  17. [SerializeField]
  18. private int m_TransformCount;
  19. [SerializeField]
  20. private Transform[] m_Transforms;
  21. [SerializeField]
  22. private Quaternion[] m_DefaultLocalRotations;
  23. [SerializeField]
  24. private Quaternion[] m_StoredLocalRotations;
  25. protected float[] m_Lengths;
  26. /// <summary>
  27. /// Get Set the Unity Transform used as IK Effector.
  28. /// </summary>
  29. public Transform effector
  30. {
  31. get { return m_EffectorTransform; }
  32. set { m_EffectorTransform = value; }
  33. }
  34. /// <summary>
  35. /// Get Set the Unity Transform used as IK Target.
  36. /// </summary>
  37. public Transform target
  38. {
  39. get { return m_TargetTransform; }
  40. set { m_TargetTransform = value; }
  41. }
  42. /// <summary>
  43. /// Get the Unity Transforms that are in the IK Chain.
  44. /// </summary>
  45. public Transform[] transforms
  46. {
  47. get { return m_Transforms; }
  48. }
  49. /// <summary>
  50. /// Get the root Unity Transform for the IK Chain.
  51. /// </summary>
  52. public Transform rootTransform
  53. {
  54. get
  55. {
  56. if (m_Transforms != null && transformCount > 0 && m_Transforms.Length == transformCount)
  57. return m_Transforms[0];
  58. return null;
  59. }
  60. }
  61. private Transform lastTransform
  62. {
  63. get
  64. {
  65. if (m_Transforms != null && transformCount > 0 && m_Transforms.Length == transformCount)
  66. return m_Transforms[transformCount - 1];
  67. return null;
  68. }
  69. }
  70. /// <summary>
  71. /// Get and Set the number of Unity Transforms in the IK Chain.
  72. /// </summary>
  73. public int transformCount
  74. {
  75. get { return m_TransformCount; }
  76. set { m_TransformCount = Mathf.Max(0, value); }
  77. }
  78. /// <summary>
  79. /// Returns true if the IK Chain is valid. False otherwise.
  80. /// </summary>
  81. public bool isValid
  82. {
  83. get { return Validate(); }
  84. }
  85. /// <summary>
  86. /// Gets the length of the IK Chain.
  87. /// </summary>
  88. public float[] lengths
  89. {
  90. get
  91. {
  92. if(isValid)
  93. {
  94. PrepareLengths();
  95. return m_Lengths;
  96. }
  97. return null;
  98. }
  99. }
  100. private bool Validate()
  101. {
  102. if (effector == null)
  103. return false;
  104. if (transformCount == 0)
  105. return false;
  106. if (m_Transforms == null || m_Transforms.Length != transformCount)
  107. return false;
  108. if (m_DefaultLocalRotations == null || m_DefaultLocalRotations.Length != transformCount)
  109. return false;
  110. if (m_StoredLocalRotations == null || m_StoredLocalRotations.Length != transformCount)
  111. return false;
  112. if (rootTransform == null)
  113. return false;
  114. if (lastTransform != effector)
  115. return false;
  116. if (target && IKUtility.IsDescendentOf(target, rootTransform))
  117. return false;
  118. return true;
  119. }
  120. /// <summary>
  121. /// Initialize the IK Chain.
  122. /// </summary>
  123. public void Initialize()
  124. {
  125. if (effector == null || transformCount == 0 || IKUtility.GetAncestorCount(effector) < transformCount - 1)
  126. return;
  127. m_Transforms = new Transform[transformCount];
  128. m_DefaultLocalRotations = new Quaternion[transformCount];
  129. m_StoredLocalRotations = new Quaternion[transformCount];
  130. var currentTransform = effector;
  131. int index = transformCount - 1;
  132. while (currentTransform && index >= 0)
  133. {
  134. m_Transforms[index] = currentTransform;
  135. m_DefaultLocalRotations[index] = currentTransform.localRotation;
  136. currentTransform = currentTransform.parent;
  137. --index;
  138. }
  139. }
  140. private void PrepareLengths()
  141. {
  142. var currentTransform = effector;
  143. int index = transformCount - 1;
  144. if (m_Lengths == null || m_Lengths.Length != transformCount - 1)
  145. m_Lengths = new float[transformCount - 1];
  146. while (currentTransform && index >= 0)
  147. {
  148. if (currentTransform.parent && index > 0)
  149. m_Lengths[index - 1] = (currentTransform.position - currentTransform.parent.position).magnitude;
  150. currentTransform = currentTransform.parent;
  151. --index;
  152. }
  153. }
  154. /// <summary>
  155. /// Restores IK Chain to it's default pose.
  156. /// </summary>
  157. /// <param name="targetRotationIsConstrained">True to constrain the target rotation. False otherwise.</param>
  158. public void RestoreDefaultPose(bool targetRotationIsConstrained)
  159. {
  160. var count = targetRotationIsConstrained ? transformCount : transformCount-1;
  161. for (int i = 0; i < count; ++i)
  162. m_Transforms[i].localRotation = m_DefaultLocalRotations[i];
  163. }
  164. /// <summary>
  165. /// Explicitly stores the local rotation
  166. /// </summary>
  167. public void StoreLocalRotations()
  168. {
  169. for (int i = 0; i < m_Transforms.Length; ++i)
  170. m_StoredLocalRotations[i] = m_Transforms[i].localRotation;
  171. }
  172. /// <summary>
  173. /// Blend between Forward Kinematics and Inverse Kinematics.
  174. /// </summary>
  175. /// <param name="finalWeight">Weight for blend</param>
  176. /// <param name="targetRotationIsConstrained">True to constrain target rotation. False otherwise.</param>
  177. public void BlendFkToIk(float finalWeight, bool targetRotationIsConstrained)
  178. {
  179. var count = targetRotationIsConstrained ? transformCount : transformCount-1;
  180. for (int i = 0; i < count; ++i)
  181. m_Transforms[i].localRotation = Quaternion.Slerp(m_StoredLocalRotations[i], m_Transforms[i].localRotation, finalWeight);
  182. }
  183. }
  184. }