SkeletonExtensions.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using UnityEngine;
  30. namespace Spine.Unity {
  31. public static class SkeletonExtensions {
  32. #region Colors
  33. const float ByteToFloat = 1f / 255f;
  34. public static Color GetColor (this Skeleton s) { return new Color(s.r, s.g, s.b, s.a); }
  35. public static Color GetColor (this RegionAttachment a) { return new Color(a.r, a.g, a.b, a.a); }
  36. public static Color GetColor (this MeshAttachment a) { return new Color(a.r, a.g, a.b, a.a); }
  37. public static Color GetColor (this Slot s) { return new Color(s.r, s.g, s.b, s.a); }
  38. public static Color GetColorTintBlack (this Slot s) { return new Color(s.r2, s.g2, s.b2, 1f); }
  39. public static void SetColor (this Skeleton skeleton, Color color) {
  40. skeleton.A = color.a;
  41. skeleton.R = color.r;
  42. skeleton.G = color.g;
  43. skeleton.B = color.b;
  44. }
  45. public static void SetColor (this Skeleton skeleton, Color32 color) {
  46. skeleton.A = color.a * ByteToFloat;
  47. skeleton.R = color.r * ByteToFloat;
  48. skeleton.G = color.g * ByteToFloat;
  49. skeleton.B = color.b * ByteToFloat;
  50. }
  51. public static void SetColor (this Slot slot, Color color) {
  52. slot.A = color.a;
  53. slot.R = color.r;
  54. slot.G = color.g;
  55. slot.B = color.b;
  56. }
  57. public static void SetColor (this Slot slot, Color32 color) {
  58. slot.A = color.a * ByteToFloat;
  59. slot.R = color.r * ByteToFloat;
  60. slot.G = color.g * ByteToFloat;
  61. slot.B = color.b * ByteToFloat;
  62. }
  63. public static void SetColor (this RegionAttachment attachment, Color color) {
  64. attachment.A = color.a;
  65. attachment.R = color.r;
  66. attachment.G = color.g;
  67. attachment.B = color.b;
  68. }
  69. public static void SetColor (this RegionAttachment attachment, Color32 color) {
  70. attachment.A = color.a * ByteToFloat;
  71. attachment.R = color.r * ByteToFloat;
  72. attachment.G = color.g * ByteToFloat;
  73. attachment.B = color.b * ByteToFloat;
  74. }
  75. public static void SetColor (this MeshAttachment attachment, Color color) {
  76. attachment.A = color.a;
  77. attachment.R = color.r;
  78. attachment.G = color.g;
  79. attachment.B = color.b;
  80. }
  81. public static void SetColor (this MeshAttachment attachment, Color32 color) {
  82. attachment.A = color.a * ByteToFloat;
  83. attachment.R = color.r * ByteToFloat;
  84. attachment.G = color.g * ByteToFloat;
  85. attachment.B = color.b * ByteToFloat;
  86. }
  87. #endregion
  88. #region Skeleton
  89. /// <summary>Sets the Skeleton's local scale using a UnityEngine.Vector2. If only individual components need to be set, set Skeleton.ScaleX or Skeleton.ScaleY.</summary>
  90. public static void SetLocalScale (this Skeleton skeleton, Vector2 scale) {
  91. skeleton.ScaleX = scale.x;
  92. skeleton.ScaleY = scale.y;
  93. }
  94. /// <summary>Gets the internal bone matrix as a Unity bonespace-to-skeletonspace transformation matrix.</summary>
  95. public static Matrix4x4 GetMatrix4x4 (this Bone bone) {
  96. return new Matrix4x4 {
  97. m00 = bone.a,
  98. m01 = bone.b,
  99. m03 = bone.worldX,
  100. m10 = bone.c,
  101. m11 = bone.d,
  102. m13 = bone.worldY,
  103. m33 = 1
  104. };
  105. }
  106. #endregion
  107. #region Bone
  108. /// <summary>Sets the bone's (local) X and Y according to a Vector2</summary>
  109. public static void SetLocalPosition (this Bone bone, Vector2 position) {
  110. bone.X = position.x;
  111. bone.Y = position.y;
  112. }
  113. /// <summary>Sets the bone's (local) X and Y according to a Vector3. The z component is ignored.</summary>
  114. public static void SetLocalPosition (this Bone bone, Vector3 position) {
  115. bone.X = position.x;
  116. bone.Y = position.y;
  117. }
  118. /// <summary>Gets the bone's local X and Y as a Vector2.</summary>
  119. public static Vector2 GetLocalPosition (this Bone bone) {
  120. return new Vector2(bone.x, bone.y);
  121. }
  122. /// <summary>Gets the position of the bone in Skeleton-space.</summary>
  123. public static Vector2 GetSkeletonSpacePosition (this Bone bone) {
  124. return new Vector2(bone.worldX, bone.worldY);
  125. }
  126. /// <summary>Gets a local offset from the bone and converts it into Skeleton-space.</summary>
  127. public static Vector2 GetSkeletonSpacePosition (this Bone bone, Vector2 boneLocal) {
  128. Vector2 o;
  129. bone.LocalToWorld(boneLocal.x, boneLocal.y, out o.x, out o.y);
  130. return o;
  131. }
  132. /// <summary>Gets the bone's Unity World position using its Spine GameObject Transform. UpdateWorldTransform needs to have been called for this to return the correct, updated value.</summary>
  133. public static Vector3 GetWorldPosition (this Bone bone, UnityEngine.Transform spineGameObjectTransform) {
  134. return spineGameObjectTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY));
  135. }
  136. public static Vector3 GetWorldPosition (this Bone bone, UnityEngine.Transform spineGameObjectTransform, float positionScale) {
  137. return spineGameObjectTransform.TransformPoint(new Vector3(bone.worldX * positionScale, bone.worldY * positionScale));
  138. }
  139. /// <summary>Gets a skeleton space UnityEngine.Quaternion representation of bone.WorldRotationX.</summary>
  140. public static Quaternion GetQuaternion (this Bone bone) {
  141. var halfRotation = Mathf.Atan2(bone.c, bone.a) * 0.5f;
  142. return new Quaternion(0, 0, Mathf.Sin(halfRotation), Mathf.Cos(halfRotation));
  143. }
  144. /// <summary>Gets a bone-local space UnityEngine.Quaternion representation of bone.rotation.</summary>
  145. public static Quaternion GetLocalQuaternion (this Bone bone) {
  146. var halfRotation = bone.rotation * Mathf.Deg2Rad * 0.5f;
  147. return new Quaternion(0, 0, Mathf.Sin(halfRotation), Mathf.Cos(halfRotation));
  148. }
  149. /// <summary>Returns the Skeleton's local scale as a UnityEngine.Vector2. If only individual components are needed, use Skeleton.ScaleX or Skeleton.ScaleY.</summary>
  150. public static Vector2 GetLocalScale (this Skeleton skeleton) {
  151. return new Vector2(skeleton.ScaleX, skeleton.ScaleY);
  152. }
  153. /// <summary>Calculates a 2x2 Transformation Matrix that can convert a skeleton-space position to a bone-local position.</summary>
  154. public static void GetWorldToLocalMatrix (this Bone bone, out float ia, out float ib, out float ic, out float id) {
  155. float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
  156. float invDet = 1 / (a * d - b * c);
  157. ia = invDet * d;
  158. ib = invDet * -b;
  159. ic = invDet * -c;
  160. id = invDet * a;
  161. }
  162. /// <summary>UnityEngine.Vector2 override of Bone.WorldToLocal. This converts a skeleton-space position into a bone local position.</summary>
  163. public static Vector2 WorldToLocal (this Bone bone, Vector2 worldPosition) {
  164. Vector2 o;
  165. bone.WorldToLocal(worldPosition.x, worldPosition.y, out o.x, out o.y);
  166. return o;
  167. }
  168. /// <summary>Sets the skeleton-space position of a bone.</summary>
  169. /// <returns>The local position in its parent bone space, or in skeleton space if it is the root bone.</returns>
  170. public static Vector2 SetPositionSkeletonSpace (this Bone bone, Vector2 skeletonSpacePosition) {
  171. if (bone.parent == null) { // root bone
  172. bone.SetLocalPosition(skeletonSpacePosition);
  173. return skeletonSpacePosition;
  174. } else {
  175. var parent = bone.parent;
  176. Vector2 parentLocal = parent.WorldToLocal(skeletonSpacePosition);
  177. bone.SetLocalPosition(parentLocal);
  178. return parentLocal;
  179. }
  180. }
  181. #endregion
  182. #region Attachments
  183. public static Material GetMaterial (this Attachment a) {
  184. object rendererObject = null;
  185. var renderableAttachment = a as IHasRendererObject;
  186. if (renderableAttachment != null)
  187. rendererObject = renderableAttachment.RendererObject;
  188. if (rendererObject == null)
  189. return null;
  190. #if SPINE_TK2D
  191. return (rendererObject.GetType() == typeof(Material)) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
  192. #else
  193. return (Material)((AtlasRegion)rendererObject).page.rendererObject;
  194. #endif
  195. }
  196. /// <summary>Fills a Vector2 buffer with local vertices.</summary>
  197. /// <param name="va">The VertexAttachment</param>
  198. /// <param name="slot">Slot where the attachment belongs.</param>
  199. /// <param name="buffer">Correctly-sized buffer. Use attachment's .WorldVerticesLength to get the correct size. If null, a new Vector2[] of the correct size will be allocated.</param>
  200. public static Vector2[] GetLocalVertices (this VertexAttachment va, Slot slot, Vector2[] buffer) {
  201. int floatsCount = va.worldVerticesLength;
  202. int bufferTargetSize = floatsCount >> 1;
  203. buffer = buffer ?? new Vector2[bufferTargetSize];
  204. if (buffer.Length < bufferTargetSize) throw new System.ArgumentException(string.Format("Vector2 buffer too small. {0} requires an array of size {1}. Use the attachment's .WorldVerticesLength to get the correct size.", va.Name, floatsCount), "buffer");
  205. if (va.bones == null) {
  206. var localVerts = va.vertices;
  207. for (int i = 0; i < bufferTargetSize; i++) {
  208. int j = i * 2;
  209. buffer[i] = new Vector2(localVerts[j], localVerts[j+1]);
  210. }
  211. } else {
  212. var floats = new float[floatsCount];
  213. va.ComputeWorldVertices(slot, floats);
  214. Bone sb = slot.bone;
  215. float ia, ib, ic, id, bwx = sb.worldX, bwy = sb.worldY;
  216. sb.GetWorldToLocalMatrix(out ia, out ib, out ic, out id);
  217. for (int i = 0; i < bufferTargetSize; i++) {
  218. int j = i * 2;
  219. float x = floats[j] - bwx, y = floats[j+1] - bwy;
  220. buffer[i] = new Vector2(x * ia + y * ib, x * ic + y * id);
  221. }
  222. }
  223. return buffer;
  224. }
  225. /// <summary>Calculates world vertices and fills a Vector2 buffer.</summary>
  226. /// <param name="a">The VertexAttachment</param>
  227. /// <param name="slot">Slot where the attachment belongs.</param>
  228. /// <param name="buffer">Correctly-sized buffer. Use attachment's .WorldVerticesLength to get the correct size. If null, a new Vector2[] of the correct size will be allocated.</param>
  229. public static Vector2[] GetWorldVertices (this VertexAttachment a, Slot slot, Vector2[] buffer) {
  230. int worldVertsLength = a.worldVerticesLength;
  231. int bufferTargetSize = worldVertsLength >> 1;
  232. buffer = buffer ?? new Vector2[bufferTargetSize];
  233. if (buffer.Length < bufferTargetSize) throw new System.ArgumentException(string.Format("Vector2 buffer too small. {0} requires an array of size {1}. Use the attachment's .WorldVerticesLength to get the correct size.", a.Name, worldVertsLength), "buffer");
  234. var floats = new float[worldVertsLength];
  235. a.ComputeWorldVertices(slot, floats);
  236. for (int i = 0, n = worldVertsLength >> 1; i < n; i++) {
  237. int j = i * 2;
  238. buffer[i] = new Vector2(floats[j], floats[j + 1]);
  239. }
  240. return buffer;
  241. }
  242. /// <summary>Gets the PointAttachment's Unity World position using its Spine GameObject Transform.</summary>
  243. public static Vector3 GetWorldPosition (this PointAttachment attachment, Slot slot, Transform spineGameObjectTransform) {
  244. Vector3 skeletonSpacePosition;
  245. skeletonSpacePosition.z = 0;
  246. attachment.ComputeWorldPosition(slot.bone, out skeletonSpacePosition.x, out skeletonSpacePosition.y);
  247. return spineGameObjectTransform.TransformPoint(skeletonSpacePosition);
  248. }
  249. /// <summary>Gets the PointAttachment's Unity World position using its Spine GameObject Transform.</summary>
  250. public static Vector3 GetWorldPosition (this PointAttachment attachment, Bone bone, Transform spineGameObjectTransform) {
  251. Vector3 skeletonSpacePosition;
  252. skeletonSpacePosition.z = 0;
  253. attachment.ComputeWorldPosition(bone, out skeletonSpacePosition.x, out skeletonSpacePosition.y);
  254. return spineGameObjectTransform.TransformPoint(skeletonSpacePosition);
  255. }
  256. #endregion
  257. }
  258. }
  259. namespace Spine {
  260. using System;
  261. using System.Collections.Generic;
  262. public struct BoneMatrix {
  263. public float a, b, c, d, x, y;
  264. /// <summary>Recursively calculates a worldspace bone matrix based on BoneData.</summary>
  265. public static BoneMatrix CalculateSetupWorld (BoneData boneData) {
  266. if (boneData == null)
  267. return default(BoneMatrix);
  268. // End condition: isRootBone
  269. if (boneData.parent == null)
  270. return GetInheritedInternal(boneData, default(BoneMatrix));
  271. BoneMatrix result = CalculateSetupWorld(boneData.parent);
  272. return GetInheritedInternal(boneData, result);
  273. }
  274. static BoneMatrix GetInheritedInternal (BoneData boneData, BoneMatrix parentMatrix) {
  275. var parent = boneData.parent;
  276. if (parent == null) return new BoneMatrix(boneData); // isRootBone
  277. float pa = parentMatrix.a, pb = parentMatrix.b, pc = parentMatrix.c, pd = parentMatrix.d;
  278. BoneMatrix result = default(BoneMatrix);
  279. result.x = pa * boneData.x + pb * boneData.y + parentMatrix.x;
  280. result.y = pc * boneData.x + pd * boneData.y + parentMatrix.y;
  281. switch (boneData.transformMode) {
  282. case TransformMode.Normal: {
  283. float rotationY = boneData.rotation + 90 + boneData.shearY;
  284. float la = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  285. float lb = MathUtils.CosDeg(rotationY) * boneData.scaleY;
  286. float lc = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  287. float ld = MathUtils.SinDeg(rotationY) * boneData.scaleY;
  288. result.a = pa * la + pb * lc;
  289. result.b = pa * lb + pb * ld;
  290. result.c = pc * la + pd * lc;
  291. result.d = pc * lb + pd * ld;
  292. break;
  293. }
  294. case TransformMode.OnlyTranslation: {
  295. float rotationY = boneData.rotation + 90 + boneData.shearY;
  296. result.a = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  297. result.b = MathUtils.CosDeg(rotationY) * boneData.scaleY;
  298. result.c = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  299. result.d = MathUtils.SinDeg(rotationY) * boneData.scaleY;
  300. break;
  301. }
  302. case TransformMode.NoRotationOrReflection: {
  303. float s = pa * pa + pc * pc, prx;
  304. if (s > 0.0001f) {
  305. s = Math.Abs(pa * pd - pb * pc) / s;
  306. pb = pc * s;
  307. pd = pa * s;
  308. prx = MathUtils.Atan2(pc, pa) * MathUtils.RadDeg;
  309. } else {
  310. pa = 0;
  311. pc = 0;
  312. prx = 90 - MathUtils.Atan2(pd, pb) * MathUtils.RadDeg;
  313. }
  314. float rx = boneData.rotation + boneData.shearX - prx;
  315. float ry = boneData.rotation + boneData.shearY - prx + 90;
  316. float la = MathUtils.CosDeg(rx) * boneData.scaleX;
  317. float lb = MathUtils.CosDeg(ry) * boneData.scaleY;
  318. float lc = MathUtils.SinDeg(rx) * boneData.scaleX;
  319. float ld = MathUtils.SinDeg(ry) * boneData.scaleY;
  320. result.a = pa * la - pb * lc;
  321. result.b = pa * lb - pb * ld;
  322. result.c = pc * la + pd * lc;
  323. result.d = pc * lb + pd * ld;
  324. break;
  325. }
  326. case TransformMode.NoScale:
  327. case TransformMode.NoScaleOrReflection: {
  328. float cos = MathUtils.CosDeg(boneData.rotation), sin = MathUtils.SinDeg(boneData.rotation);
  329. float za = pa * cos + pb * sin;
  330. float zc = pc * cos + pd * sin;
  331. float s = (float)Math.Sqrt(za * za + zc * zc);
  332. if (s > 0.00001f)
  333. s = 1 / s;
  334. za *= s;
  335. zc *= s;
  336. s = (float)Math.Sqrt(za * za + zc * zc);
  337. float r = MathUtils.PI / 2 + MathUtils.Atan2(zc, za);
  338. float zb = MathUtils.Cos(r) * s;
  339. float zd = MathUtils.Sin(r) * s;
  340. float la = MathUtils.CosDeg(boneData.shearX) * boneData.scaleX;
  341. float lb = MathUtils.CosDeg(90 + boneData.shearY) * boneData.scaleY;
  342. float lc = MathUtils.SinDeg(boneData.shearX) * boneData.scaleX;
  343. float ld = MathUtils.SinDeg(90 + boneData.shearY) * boneData.scaleY;
  344. if (boneData.transformMode != TransformMode.NoScaleOrReflection ? pa * pd - pb * pc < 0 : false) {
  345. zb = -zb;
  346. zd = -zd;
  347. }
  348. result.a = za * la + zb * lc;
  349. result.b = za * lb + zb * ld;
  350. result.c = zc * la + zd * lc;
  351. result.d = zc * lb + zd * ld;
  352. break;
  353. }
  354. }
  355. return result;
  356. }
  357. /// <summary>Constructor for a local bone matrix based on Setup Pose BoneData.</summary>
  358. public BoneMatrix (BoneData boneData) {
  359. float rotationY = boneData.rotation + 90 + boneData.shearY;
  360. float rotationX = boneData.rotation + boneData.shearX;
  361. a = MathUtils.CosDeg(rotationX) * boneData.scaleX;
  362. c = MathUtils.SinDeg(rotationX) * boneData.scaleX;
  363. b = MathUtils.CosDeg(rotationY) * boneData.scaleY;
  364. d = MathUtils.SinDeg(rotationY) * boneData.scaleY;
  365. x = boneData.x;
  366. y = boneData.y;
  367. }
  368. /// <summary>Constructor for a local bone matrix based on a bone instance's current pose.</summary>
  369. public BoneMatrix (Bone bone) {
  370. float rotationY = bone.rotation + 90 + bone.shearY;
  371. float rotationX = bone.rotation + bone.shearX;
  372. a = MathUtils.CosDeg(rotationX) * bone.scaleX;
  373. c = MathUtils.SinDeg(rotationX) * bone.scaleX;
  374. b = MathUtils.CosDeg(rotationY) * bone.scaleY;
  375. d = MathUtils.SinDeg(rotationY) * bone.scaleY;
  376. x = bone.x;
  377. y = bone.y;
  378. }
  379. public BoneMatrix TransformMatrix (BoneMatrix local) {
  380. return new BoneMatrix {
  381. a = this.a * local.a + this.b * local.c,
  382. b = this.a * local.b + this.b * local.d,
  383. c = this.c * local.a + this.d * local.c,
  384. d = this.c * local.b + this.d * local.d,
  385. x = this.a * local.x + this.b * local.y + this.x,
  386. y = this.c * local.x + this.d * local.y + this.y
  387. };
  388. }
  389. }
  390. public static class SpineSkeletonExtensions {
  391. public static bool IsWeighted (this VertexAttachment va) {
  392. return va.bones != null && va.bones.Length > 0;
  393. }
  394. public static bool IsRenderable (this Attachment a) {
  395. return a is IHasRendererObject;
  396. }
  397. #region Transform Modes
  398. public static bool InheritsRotation (this TransformMode mode) {
  399. const int RotationBit = 0;
  400. return ((int)mode & (1U << RotationBit)) == 0;
  401. }
  402. public static bool InheritsScale (this TransformMode mode) {
  403. const int ScaleBit = 1;
  404. return ((int)mode & (1U << ScaleBit)) == 0;
  405. }
  406. #endregion
  407. #region Posing
  408. internal static void SetPropertyToSetupPose (this Skeleton skeleton, int propertyID) {
  409. int tt = propertyID >> 24;
  410. var timelineType = (TimelineType)tt;
  411. int i = propertyID - (tt << 24);
  412. Bone bone;
  413. IkConstraint ikc;
  414. PathConstraint pc;
  415. switch (timelineType) {
  416. // Bone
  417. case TimelineType.Rotate:
  418. bone = skeleton.bones.Items[i];
  419. bone.rotation = bone.data.rotation;
  420. break;
  421. case TimelineType.Translate:
  422. bone = skeleton.bones.Items[i];
  423. bone.x = bone.data.x;
  424. bone.y = bone.data.y;
  425. break;
  426. case TimelineType.Scale:
  427. bone = skeleton.bones.Items[i];
  428. bone.scaleX = bone.data.scaleX;
  429. bone.scaleY = bone.data.scaleY;
  430. break;
  431. case TimelineType.Shear:
  432. bone = skeleton.bones.Items[i];
  433. bone.shearX = bone.data.shearX;
  434. bone.shearY = bone.data.shearY;
  435. break;
  436. // Slot
  437. case TimelineType.Attachment:
  438. skeleton.SetSlotAttachmentToSetupPose(i);
  439. break;
  440. case TimelineType.Color:
  441. skeleton.slots.Items[i].SetColorToSetupPose();
  442. break;
  443. case TimelineType.TwoColor:
  444. skeleton.slots.Items[i].SetColorToSetupPose();
  445. break;
  446. case TimelineType.Deform:
  447. skeleton.slots.Items[i].Deform.Clear();
  448. break;
  449. // Skeleton
  450. case TimelineType.DrawOrder:
  451. skeleton.SetDrawOrderToSetupPose();
  452. break;
  453. // IK Constraint
  454. case TimelineType.IkConstraint:
  455. ikc = skeleton.ikConstraints.Items[i];
  456. ikc.mix = ikc.data.mix;
  457. ikc.softness = ikc.data.softness;
  458. ikc.bendDirection = ikc.data.bendDirection;
  459. ikc.stretch = ikc.data.stretch;
  460. break;
  461. // TransformConstraint
  462. case TimelineType.TransformConstraint:
  463. var tc = skeleton.transformConstraints.Items[i];
  464. var tcData = tc.data;
  465. tc.rotateMix = tcData.rotateMix;
  466. tc.translateMix = tcData.translateMix;
  467. tc.scaleMix = tcData.scaleMix;
  468. tc.shearMix = tcData.shearMix;
  469. break;
  470. // Path Constraint
  471. case TimelineType.PathConstraintPosition:
  472. pc = skeleton.pathConstraints.Items[i];
  473. pc.position = pc.data.position;
  474. break;
  475. case TimelineType.PathConstraintSpacing:
  476. pc = skeleton.pathConstraints.Items[i];
  477. pc.spacing = pc.data.spacing;
  478. break;
  479. case TimelineType.PathConstraintMix:
  480. pc = skeleton.pathConstraints.Items[i];
  481. pc.rotateMix = pc.data.rotateMix;
  482. pc.translateMix = pc.data.translateMix;
  483. break;
  484. }
  485. }
  486. /// <summary>Resets the DrawOrder to the Setup Pose's draw order</summary>
  487. public static void SetDrawOrderToSetupPose (this Skeleton skeleton) {
  488. var slotsItems = skeleton.slots.Items;
  489. int n = skeleton.slots.Count;
  490. var drawOrder = skeleton.drawOrder;
  491. drawOrder.Clear(false);
  492. drawOrder.EnsureCapacity(n);
  493. drawOrder.Count = n;
  494. System.Array.Copy(slotsItems, drawOrder.Items, n);
  495. }
  496. /// <summary>Resets all the slots on the skeleton to their Setup Pose attachments but does not reset slot colors.</summary>
  497. public static void SetSlotAttachmentsToSetupPose (this Skeleton skeleton) {
  498. var slotsItems = skeleton.slots.Items;
  499. for (int i = 0; i < skeleton.slots.Count; i++) {
  500. Slot slot = slotsItems[i];
  501. string attachmentName = slot.data.attachmentName;
  502. slot.Attachment = string.IsNullOrEmpty(attachmentName) ? null : skeleton.GetAttachment(i, attachmentName);
  503. }
  504. }
  505. /// <summary>Resets the color of a slot to Setup Pose value.</summary>
  506. public static void SetColorToSetupPose (this Slot slot) {
  507. slot.r = slot.data.r;
  508. slot.g = slot.data.g;
  509. slot.b = slot.data.b;
  510. slot.a = slot.data.a;
  511. slot.r2 = slot.data.r2;
  512. slot.g2 = slot.data.g2;
  513. slot.b2 = slot.data.b2;
  514. }
  515. /// <summary>Sets a slot's attachment to setup pose. If you have the slotIndex, Skeleton.SetSlotAttachmentToSetupPose is faster.</summary>
  516. public static void SetAttachmentToSetupPose (this Slot slot) {
  517. var slotData = slot.data;
  518. slot.Attachment = slot.bone.skeleton.GetAttachment(slotData.name, slotData.attachmentName);
  519. }
  520. /// <summary>Resets the attachment of slot at a given slotIndex to setup pose. This is faster than Slot.SetAttachmentToSetupPose.</summary>
  521. public static void SetSlotAttachmentToSetupPose (this Skeleton skeleton, int slotIndex) {
  522. var slot = skeleton.slots.Items[slotIndex];
  523. string attachmentName = slot.data.attachmentName;
  524. if (string.IsNullOrEmpty(attachmentName)) {
  525. slot.Attachment = null;
  526. } else {
  527. var attachment = skeleton.GetAttachment(slotIndex, attachmentName);
  528. slot.Attachment = attachment;
  529. }
  530. }
  531. /// <summary>Resets Skeleton parts to Setup Pose according to a Spine.Animation's keyed items.</summary>
  532. public static void SetKeyedItemsToSetupPose (this Animation animation, Skeleton skeleton) {
  533. animation.Apply(skeleton, 0, 0, false, null, 0, MixBlend.Setup, MixDirection.Out);
  534. }
  535. public static void AllowImmediateQueue (this TrackEntry trackEntry) {
  536. if (trackEntry.nextTrackLast < 0) trackEntry.nextTrackLast = 0;
  537. }
  538. #endregion
  539. }
  540. }