SkeletonCacheExtensions.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal static class SkeletonCacheExtensions
  7. {
  8. public static void RotateBones(this SkeletonCache skeleton, BoneCache[] bones, float deltaAngle)
  9. {
  10. Debug.Assert(skeleton != null);
  11. foreach (var bone in bones)
  12. {
  13. Debug.Assert(bone != null);
  14. Debug.Assert(skeleton.Contains(bone));
  15. bone.localRotation *= Quaternion.AngleAxis(deltaAngle, Vector3.forward);
  16. }
  17. }
  18. public static void MoveBones(this SkeletonCache skeleton, BoneCache[] bones, Vector3 deltaPosition)
  19. {
  20. Debug.Assert(skeleton != null);
  21. foreach (var bone in bones)
  22. {
  23. Debug.Assert(bone != null);
  24. Debug.Assert(skeleton.Contains(bone));
  25. bone.position += deltaPosition;
  26. if (bone.parentBone != null && bone.parentBone.chainedChild == bone)
  27. bone.parentBone.OrientToChainedChild(false);
  28. }
  29. }
  30. public static void FreeMoveBones(this SkeletonCache skeleton, BoneCache[] bones, Vector3 deltaPosition)
  31. {
  32. Debug.Assert(skeleton != null);
  33. foreach (var bone in bones)
  34. {
  35. Debug.Assert(bone != null);
  36. Debug.Assert(skeleton.Contains(bone));
  37. var childrenWorldPose = bone.GetChildrenWoldPose();
  38. if (bone.chainedChild != null && ArrayUtility.Contains(bones, bone.chainedChild) == false)
  39. bone.chainedChild = null;
  40. if (bone.parentBone != null && bone.parentBone.chainedChild == bone && ArrayUtility.Contains(bones, bone.parentBone) == false)
  41. bone.parentBone.chainedChild = null;
  42. bone.position += deltaPosition;
  43. bone.SetChildrenWorldPose(childrenWorldPose);
  44. }
  45. }
  46. public static void MoveJoints(this SkeletonCache skeleton, BoneCache[] bones, Vector3 deltaPosition)
  47. {
  48. Debug.Assert(skeleton != null);
  49. foreach (var bone in bones)
  50. {
  51. Debug.Assert(bone != null);
  52. Debug.Assert(skeleton.Contains(bone));
  53. var childrenWorldPose = bone.GetChildrenWoldPose();
  54. var endPosition = bone.endPosition;
  55. bone.position += deltaPosition;
  56. if (bone.localLength > 0f)
  57. bone.endPosition = endPosition;
  58. if (bone.parentBone != null && bone.parentBone.chainedChild == bone)
  59. bone.parentBone.OrientToChainedChild(true);
  60. bone.SetChildrenWorldPose(childrenWorldPose);
  61. if (bone.chainedChild != null)
  62. bone.OrientToChainedChild(true);
  63. }
  64. }
  65. public static void SetEndPosition(this SkeletonCache skeleton, BoneCache bone, Vector3 endPosition)
  66. {
  67. Debug.Assert(skeleton != null);
  68. Debug.Assert(bone != null);
  69. Debug.Assert(skeleton.Contains(bone));
  70. var childrenStorage = bone.GetChildrenWoldPose();
  71. bone.endPosition = endPosition;
  72. bone.SetChildrenWorldPose(childrenStorage);
  73. }
  74. public static BoneCache SplitBone(this SkeletonCache skeleton, BoneCache boneToSplit, float splitLength, string name)
  75. {
  76. Debug.Assert(skeleton.Contains(boneToSplit));
  77. Debug.Assert(boneToSplit.length > splitLength);
  78. var endPosition = boneToSplit.endPosition;
  79. var chainedChild = boneToSplit.chainedChild;
  80. var splitPosition = boneToSplit.position + boneToSplit.right * splitLength;
  81. boneToSplit.length = splitLength;
  82. var bone = skeleton.CreateBone(boneToSplit, splitPosition, endPosition, true, name);
  83. if (chainedChild != null)
  84. {
  85. chainedChild.SetParent(bone);
  86. bone.chainedChild = chainedChild;
  87. }
  88. return bone;
  89. }
  90. public static BoneCache CreateBone(this SkeletonCache skeleton, BoneCache parentBone, Vector3 position, Vector3 endPosition, bool isChained, string name)
  91. {
  92. Debug.Assert(skeleton != null);
  93. if (parentBone != null)
  94. Debug.Assert(skeleton.Contains(parentBone));
  95. var bone = skeleton.skinningCache.CreateCache<BoneCache>();
  96. bone.SetParent(parentBone);
  97. bone.name = name;
  98. bone.bindPoseColor = ModuleUtility.CalculateNiceColor(skeleton.BoneCount, 6);
  99. bone.position = position;
  100. bone.endPosition = endPosition;
  101. if (isChained && parentBone != null)
  102. parentBone.chainedChild = bone;
  103. skeleton.AddBone(bone);
  104. return bone;
  105. }
  106. public static void SetBones(this SkeletonCache skeleton, BoneCache[] bones)
  107. {
  108. skeleton.SetBones(bones, true);
  109. }
  110. public static void SetBones(this SkeletonCache skeleton, BoneCache[] bones, bool worldPositionStays)
  111. {
  112. skeleton.Clear();
  113. skeleton.AddBones(bones, worldPositionStays);
  114. skeleton.SetDefaultPose();
  115. }
  116. public static void AddBones(this SkeletonCache skeleton, BoneCache[] bones)
  117. {
  118. skeleton.AddBones(bones, true);
  119. }
  120. public static void AddBones(this SkeletonCache skeleton, BoneCache[] bones, bool worldPositionStays)
  121. {
  122. foreach (var bone in bones)
  123. skeleton.AddBone(bone, worldPositionStays);
  124. }
  125. public static void DestroyBones(this SkeletonCache skeleton, BoneCache[] bones)
  126. {
  127. Debug.Assert(skeleton != null);
  128. foreach (var bone in bones)
  129. {
  130. Debug.Assert(bone != null);
  131. Debug.Assert(skeleton.Contains(bone));
  132. skeleton.DestroyBone(bone);
  133. }
  134. }
  135. }
  136. }