No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SkeletonCacheExtensions.cs 6.0KB

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