A game about forced loneliness, made by TACStudios
at master 179 lines 6.1 kB view raw
1using UnityEngine; 2 3namespace UnityEditor.U2D.Animation 4{ 5 internal static class SkeletonCacheExtensions 6 { 7 public static void RotateBones(this SkeletonCache skeleton, BoneCache[] bones, float deltaAngle) 8 { 9 Debug.Assert(skeleton != null); 10 11 foreach (var bone in bones) 12 { 13 Debug.Assert(bone != null); 14 Debug.Assert(skeleton.Contains(bone)); 15 16 bone.localRotation *= Quaternion.AngleAxis(deltaAngle, Vector3.forward); 17 } 18 } 19 20 public static void MoveBones(this SkeletonCache skeleton, BoneCache[] bones, Vector3 deltaPosition) 21 { 22 Debug.Assert(skeleton != null); 23 24 foreach (var bone in bones) 25 { 26 Debug.Assert(bone != null); 27 Debug.Assert(skeleton.Contains(bone)); 28 29 bone.position += deltaPosition; 30 31 if (bone.parentBone != null && bone.parentBone.chainedChild == bone) 32 bone.parentBone.OrientToChainedChild(false); 33 } 34 } 35 36 public static void FreeMoveBones(this SkeletonCache skeleton, BoneCache[] bones, Vector3 deltaPosition) 37 { 38 Debug.Assert(skeleton != null); 39 40 foreach (var bone in bones) 41 { 42 Debug.Assert(bone != null); 43 Debug.Assert(skeleton.Contains(bone)); 44 45 var childrenWorldPose = bone.GetChildrenWoldPose(); 46 47 if (bone.chainedChild != null && ArrayUtility.Contains(bones, bone.chainedChild) == false) 48 bone.chainedChild = null; 49 50 if (bone.parentBone != null && bone.parentBone.chainedChild == bone && ArrayUtility.Contains(bones, bone.parentBone) == false) 51 bone.parentBone.chainedChild = null; 52 53 bone.position += deltaPosition; 54 55 bone.SetChildrenWorldPose(childrenWorldPose); 56 } 57 } 58 59 public static void MoveJoints(this SkeletonCache skeleton, BoneCache[] bones, Vector3 deltaPosition) 60 { 61 Debug.Assert(skeleton != null); 62 63 foreach (var bone in bones) 64 { 65 Debug.Assert(bone != null); 66 Debug.Assert(skeleton.Contains(bone)); 67 68 var childrenWorldPose = bone.GetChildrenWoldPose(); 69 var endPosition = bone.endPosition; 70 71 bone.position += deltaPosition; 72 73 if (bone.localLength > 0f) 74 bone.endPosition = endPosition; 75 76 if (bone.parentBone != null && bone.parentBone.chainedChild == bone) 77 bone.parentBone.OrientToChainedChild(true); 78 79 bone.SetChildrenWorldPose(childrenWorldPose); 80 81 if (bone.chainedChild != null) 82 bone.OrientToChainedChild(true); 83 } 84 } 85 86 public static void SetEndPosition(this SkeletonCache skeleton, BoneCache bone, Vector3 endPosition) 87 { 88 Debug.Assert(skeleton != null); 89 Debug.Assert(bone != null); 90 Debug.Assert(skeleton.Contains(bone)); 91 92 var childrenStorage = bone.GetChildrenWoldPose(); 93 bone.endPosition = endPosition; 94 bone.SetChildrenWorldPose(childrenStorage); 95 } 96 97 public static BoneCache SplitBone(this SkeletonCache skeleton, BoneCache boneToSplit, float splitLength, string name) 98 { 99 Debug.Assert(skeleton.Contains(boneToSplit)); 100 Debug.Assert(boneToSplit.length > splitLength); 101 102 var endPosition = boneToSplit.endPosition; 103 var chainedChild = boneToSplit.chainedChild; 104 var splitPosition = boneToSplit.position + boneToSplit.right * splitLength; 105 106 boneToSplit.length = splitLength; 107 108 var bone = skeleton.CreateBone(boneToSplit, splitPosition, endPosition, true, name); 109 110 if (chainedChild != null) 111 { 112 chainedChild.SetParent(bone); 113 bone.chainedChild = chainedChild; 114 } 115 116 return bone; 117 } 118 119 public static BoneCache CreateBone(this SkeletonCache skeleton, BoneCache parentBone, Vector3 position, Vector3 endPosition, bool isChained, string name) 120 { 121 Debug.Assert(skeleton != null); 122 123 if (parentBone != null) 124 Debug.Assert(skeleton.Contains(parentBone)); 125 126 var bone = skeleton.skinningCache.CreateCache<BoneCache>(); 127 128 bone.SetParent(parentBone); 129 bone.name = name; 130 bone.bindPoseColor = ModuleUtility.CalculateNiceColor(skeleton.boneCount, 6); 131 bone.position = position; 132 bone.endPosition = endPosition; 133 bone.guid = GUID.Generate().ToString(); 134 if (isChained && parentBone != null) 135 parentBone.chainedChild = bone; 136 137 skeleton.AddBone(bone); 138 139 140 return bone; 141 } 142 143 public static void SetBones(this SkeletonCache skeleton, BoneCache[] bones) 144 { 145 skeleton.SetBones(bones, true); 146 } 147 148 public static void SetBones(this SkeletonCache skeleton, BoneCache[] bones, bool worldPositionStays) 149 { 150 skeleton.Clear(); 151 skeleton.AddBones(bones, worldPositionStays); 152 skeleton.SetDefaultPose(); 153 } 154 155 public static void AddBones(this SkeletonCache skeleton, BoneCache[] bones) 156 { 157 skeleton.AddBones(bones, true); 158 } 159 160 public static void AddBones(this SkeletonCache skeleton, BoneCache[] bones, bool worldPositionStays) 161 { 162 foreach (var bone in bones) 163 skeleton.AddBone(bone, worldPositionStays); 164 } 165 166 public static void DestroyBones(this SkeletonCache skeleton, BoneCache[] bones) 167 { 168 Debug.Assert(skeleton != null); 169 170 foreach (var bone in bones) 171 { 172 Debug.Assert(bone != null); 173 Debug.Assert(skeleton.Contains(bone)); 174 175 skeleton.DestroyBone(bone); 176 } 177 } 178 } 179}