IKSolverFABRIKRoot.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. namespace RootMotion.FinalIK {
  5. /// <summary>
  6. /// %IK system for multiple branched %FABRIK chains.
  7. /// </summary>
  8. [System.Serializable]
  9. public class IKSolverFABRIKRoot : IKSolver {
  10. #region Main Interface
  11. /// <summary>
  12. /// Solver iterations.
  13. /// </summary>
  14. public int iterations = 4;
  15. /// <summary>
  16. /// The weight of all chains being pinned to root position.
  17. /// </summary>
  18. [Range(0f, 1f)]
  19. public float rootPin = 0f;
  20. /// <summary>
  21. /// The %FABRIK chains.
  22. /// </summary>
  23. public FABRIKChain[] chains = new FABRIKChain[0];
  24. public override bool IsValid(ref string message) {
  25. if (chains.Length == 0) {
  26. message = "IKSolverFABRIKRoot contains no chains.";
  27. return false;
  28. }
  29. foreach (FABRIKChain chain in chains) {
  30. if (!chain.IsValid(ref message)) return false;
  31. }
  32. for (int i = 0; i < chains.Length; i++) {
  33. for (int c = 0; c < chains.Length; c++) {
  34. if (i != c && chains[i].ik == chains[c].ik) {
  35. message = chains[i].ik.name + " is represented more than once in IKSolverFABRIKRoot chain.";
  36. return false;
  37. }
  38. }
  39. }
  40. // Check the children
  41. for (int i = 0; i < chains.Length; i++) {
  42. for (int c = 0; c < chains[i].children.Length; c++) {
  43. int childIndex = chains[i].children[c];
  44. if (childIndex < 0) {
  45. message = chains[i].ik.name + "IKSolverFABRIKRoot chain at index " + i + " has invalid children array. Child index is < 0.";
  46. return false;
  47. }
  48. if (childIndex == i) {
  49. message = chains[i].ik.name + "IKSolverFABRIKRoot chain at index " + i + " has invalid children array. Child index is referencing to itself.";
  50. return false;
  51. }
  52. if (childIndex >= chains.Length) {
  53. message = chains[i].ik.name + "IKSolverFABRIKRoot chain at index " + i + " has invalid children array. Child index > number of chains";
  54. return false;
  55. }
  56. // Check if the child chain doesn't have this chain among it's children
  57. for (int o = 0; o < chains.Length; o++) {
  58. if (childIndex == o) {
  59. for (int n = 0; n < chains[o].children.Length; n++) {
  60. if (chains[o].children[n] == i) {
  61. message = "Circular parenting. " + chains[o].ik.name + " already has " + chains[i].ik.name + " listed as it's child.";
  62. return false;
  63. }
  64. }
  65. }
  66. }
  67. // Check for duplicates
  68. for (int n = 0; n < chains[i].children.Length; n++) {
  69. if (c != n && chains[i].children[n] == childIndex) {
  70. message = "Chain number " + childIndex + " is represented more than once in the children of " + chains[i].ik.name;
  71. return false;
  72. }
  73. }
  74. }
  75. }
  76. return true;
  77. }
  78. public override void StoreDefaultLocalState() {
  79. rootDefaultPosition = root.localPosition;
  80. for (int i = 0; i < chains.Length; i++) chains[i].ik.solver.StoreDefaultLocalState();
  81. }
  82. public override void FixTransforms() {
  83. if (!initiated) return;
  84. root.localPosition = rootDefaultPosition;
  85. for (int i = 0; i < chains.Length; i++) chains[i].ik.solver.FixTransforms();
  86. }
  87. #endregion Main Interface
  88. private bool zeroWeightApplied;
  89. private bool[] isRoot;
  90. private Vector3 rootDefaultPosition;
  91. protected override void OnInitiate() {
  92. for (int i = 0; i < chains.Length; i++) chains[i].Initiate();
  93. isRoot = new bool[chains.Length];
  94. for (int i = 0; i < chains.Length; i++) {
  95. isRoot[i] = IsRoot(i);
  96. }
  97. }
  98. // Is the chain at index a root chain (not parented by any other chains)?
  99. private bool IsRoot(int index) {
  100. for (int i = 0; i < chains.Length; i++) {
  101. for (int c = 0; c < chains[i].children.Length; c++) {
  102. if (chains[i].children[c] == index) return false;
  103. }
  104. }
  105. return true;
  106. }
  107. protected override void OnUpdate() {
  108. if (IKPositionWeight <= 0 && zeroWeightApplied) return;
  109. IKPositionWeight = Mathf.Clamp(IKPositionWeight, 0f, 1f);
  110. // Set weight of all IK solvers
  111. for (int i = 0; i < chains.Length; i++) chains[i].ik.solver.IKPositionWeight = IKPositionWeight;
  112. if (IKPositionWeight <= 0) {
  113. zeroWeightApplied = true;
  114. return;
  115. }
  116. zeroWeightApplied = false;
  117. for (int i = 0; i < iterations; i++) {
  118. // Solve trees from their targets
  119. for (int c = 0; c < chains.Length; c++) {
  120. if (isRoot[c]) chains[c].Stage1(chains);
  121. }
  122. // Get centroid of all tree roots
  123. Vector3 centroid = GetCentroid();
  124. root.position = centroid;
  125. // Start all trees from the centroid
  126. for (int c = 0; c < chains.Length; c++) {
  127. if (isRoot[c]) chains[c].Stage2(centroid, chains);
  128. }
  129. }
  130. }
  131. public override IKSolver.Point[] GetPoints() {
  132. IKSolver.Point[] array = new IKSolver.Point[0];
  133. for (int i = 0; i < chains.Length; i++) AddPointsToArray(ref array, chains[i]);
  134. return array;
  135. }
  136. public override IKSolver.Point GetPoint(Transform transform) {
  137. IKSolver.Point p = null;
  138. for (int i = 0; i < chains.Length; i++) {
  139. p = chains[i].ik.solver.GetPoint(transform);
  140. if (p != null) return p;
  141. }
  142. return null;
  143. }
  144. private void AddPointsToArray(ref IKSolver.Point[] array, FABRIKChain chain) {
  145. IKSolver.Point[] chainArray = chain.ik.solver.GetPoints();
  146. Array.Resize(ref array, array.Length + chainArray.Length);
  147. int a = 0;
  148. for (int i = array.Length - chainArray.Length; i < array.Length; i++) {
  149. array[i] = chainArray[a];
  150. a++;
  151. }
  152. }
  153. /*
  154. * Gets the centroid position of all chains respective of their pull weights
  155. * */
  156. private Vector3 GetCentroid() {
  157. Vector3 centroid = root.position;
  158. if (rootPin >= 1) return centroid;
  159. float pullSum = 0f;
  160. for (int i = 0; i < chains.Length; i++) {
  161. if (isRoot[i]) pullSum += chains[i].pull;
  162. }
  163. for (int i = 0; i < chains.Length; i++) {
  164. if (isRoot[i] && pullSum > 0) centroid += (chains[i].ik.solver.bones[0].solverPosition - root.position) * (chains[i].pull / Mathf.Clamp(pullSum, 1f, pullSum));
  165. }
  166. return Vector3.Lerp(centroid, root.position, rootPin);
  167. }
  168. }
  169. }