Bone.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 System;
  30. namespace Spine {
  31. /// <summary>
  32. /// Stores a bone's current pose.
  33. /// <para>
  34. /// A bone has a local transform which is used to compute its world transform. A bone also has an applied transform, which is a
  35. /// local transform that can be applied to compute the world transform. The local transform and applied transform may differ if a
  36. /// constraint or application code modifies the world transform after it was computed from the local transform.
  37. /// </para>
  38. /// </summary>
  39. public class Bone : IUpdatable {
  40. static public bool yDown;
  41. internal BoneData data;
  42. internal Skeleton skeleton;
  43. internal Bone parent;
  44. internal ExposedList<Bone> children = new ExposedList<Bone>();
  45. internal float x, y, rotation, scaleX, scaleY, shearX, shearY;
  46. internal float ax, ay, arotation, ascaleX, ascaleY, ashearX, ashearY;
  47. internal bool appliedValid;
  48. internal float a, b, worldX;
  49. internal float c, d, worldY;
  50. internal bool sorted, active;
  51. public BoneData Data { get { return data; } }
  52. public Skeleton Skeleton { get { return skeleton; } }
  53. public Bone Parent { get { return parent; } }
  54. public ExposedList<Bone> Children { get { return children; } }
  55. /// <summary>Returns false when the bone has not been computed because <see cref="BoneData.SkinRequired"/> is true and the
  56. /// <see cref="Skeleton.Skin">active skin</see> does not <see cref="Skin.Bones">contain</see> this bone.</summary>
  57. public bool Active { get { return active; } }
  58. /// <summary>The local X translation.</summary>
  59. public float X { get { return x; } set { x = value; } }
  60. /// <summary>The local Y translation.</summary>
  61. public float Y { get { return y; } set { y = value; } }
  62. /// <summary>The local rotation.</summary>
  63. public float Rotation { get { return rotation; } set { rotation = value; } }
  64. /// <summary>The local scaleX.</summary>
  65. public float ScaleX { get { return scaleX; } set { scaleX = value; } }
  66. /// <summary>The local scaleY.</summary>
  67. public float ScaleY { get { return scaleY; } set { scaleY = value; } }
  68. /// <summary>The local shearX.</summary>
  69. public float ShearX { get { return shearX; } set { shearX = value; } }
  70. /// <summary>The local shearY.</summary>
  71. public float ShearY { get { return shearY; } set { shearY = value; } }
  72. /// <summary>The rotation, as calculated by any constraints.</summary>
  73. public float AppliedRotation { get { return arotation; } set { arotation = value; } }
  74. /// <summary>The applied local x translation.</summary>
  75. public float AX { get { return ax; } set { ax = value; } }
  76. /// <summary>The applied local y translation.</summary>
  77. public float AY { get { return ay; } set { ay = value; } }
  78. /// <summary>The applied local scaleX.</summary>
  79. public float AScaleX { get { return ascaleX; } set { ascaleX = value; } }
  80. /// <summary>The applied local scaleY.</summary>
  81. public float AScaleY { get { return ascaleY; } set { ascaleY = value; } }
  82. /// <summary>The applied local shearX.</summary>
  83. public float AShearX { get { return ashearX; } set { ashearX = value; } }
  84. /// <summary>The applied local shearY.</summary>
  85. public float AShearY { get { return ashearY; } set { ashearY = value; } }
  86. public float A { get { return a; } }
  87. public float B { get { return b; } }
  88. public float C { get { return c; } }
  89. public float D { get { return d; } }
  90. public float WorldX { get { return worldX; } }
  91. public float WorldY { get { return worldY; } }
  92. public float WorldRotationX { get { return MathUtils.Atan2(c, a) * MathUtils.RadDeg; } }
  93. public float WorldRotationY { get { return MathUtils.Atan2(d, b) * MathUtils.RadDeg; } }
  94. /// <summary>Returns the magnitide (always positive) of the world scale X.</summary>
  95. public float WorldScaleX { get { return (float)Math.Sqrt(a * a + c * c); } }
  96. /// <summary>Returns the magnitide (always positive) of the world scale Y.</summary>
  97. public float WorldScaleY { get { return (float)Math.Sqrt(b * b + d * d); } }
  98. /// <param name="parent">May be null.</param>
  99. public Bone (BoneData data, Skeleton skeleton, Bone parent) {
  100. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  101. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  102. this.data = data;
  103. this.skeleton = skeleton;
  104. this.parent = parent;
  105. SetToSetupPose();
  106. }
  107. /// <summary>Same as <see cref="UpdateWorldTransform"/>. This method exists for Bone to implement <see cref="Spine.IUpdatable"/>.</summary>
  108. public void Update () {
  109. UpdateWorldTransform(x, y, rotation, scaleX, scaleY, shearX, shearY);
  110. }
  111. /// <summary>Computes the world transform using the parent bone and this bone's local transform.</summary>
  112. public void UpdateWorldTransform () {
  113. UpdateWorldTransform(x, y, rotation, scaleX, scaleY, shearX, shearY);
  114. }
  115. /// <summary>Computes the world transform using the parent bone and the specified local transform.</summary>
  116. public void UpdateWorldTransform (float x, float y, float rotation, float scaleX, float scaleY, float shearX, float shearY) {
  117. ax = x;
  118. ay = y;
  119. arotation = rotation;
  120. ascaleX = scaleX;
  121. ascaleY = scaleY;
  122. ashearX = shearX;
  123. ashearY = shearY;
  124. appliedValid = true;
  125. Skeleton skeleton = this.skeleton;
  126. Bone parent = this.parent;
  127. if (parent == null) { // Root bone.
  128. float rotationY = rotation + 90 + shearY, sx = skeleton.ScaleX, sy = skeleton.ScaleY;
  129. a = MathUtils.CosDeg(rotation + shearX) * scaleX * sx;
  130. b = MathUtils.CosDeg(rotationY) * scaleY * sx;
  131. c = MathUtils.SinDeg(rotation + shearX) * scaleX * sy;
  132. d = MathUtils.SinDeg(rotationY) * scaleY * sy;
  133. worldX = x * sx + skeleton.x;
  134. worldY = y * sy + skeleton.y;
  135. return;
  136. }
  137. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  138. worldX = pa * x + pb * y + parent.worldX;
  139. worldY = pc * x + pd * y + parent.worldY;
  140. switch (data.transformMode) {
  141. case TransformMode.Normal: {
  142. float rotationY = rotation + 90 + shearY;
  143. float la = MathUtils.CosDeg(rotation + shearX) * scaleX;
  144. float lb = MathUtils.CosDeg(rotationY) * scaleY;
  145. float lc = MathUtils.SinDeg(rotation + shearX) * scaleX;
  146. float ld = MathUtils.SinDeg(rotationY) * scaleY;
  147. a = pa * la + pb * lc;
  148. b = pa * lb + pb * ld;
  149. c = pc * la + pd * lc;
  150. d = pc * lb + pd * ld;
  151. return;
  152. }
  153. case TransformMode.OnlyTranslation: {
  154. float rotationY = rotation + 90 + shearY;
  155. a = MathUtils.CosDeg(rotation + shearX) * scaleX;
  156. b = MathUtils.CosDeg(rotationY) * scaleY;
  157. c = MathUtils.SinDeg(rotation + shearX) * scaleX;
  158. d = MathUtils.SinDeg(rotationY) * scaleY;
  159. break;
  160. }
  161. case TransformMode.NoRotationOrReflection: {
  162. float s = pa * pa + pc * pc, prx;
  163. if (s > 0.0001f) {
  164. s = Math.Abs(pa * pd - pb * pc) / s;
  165. pa /= skeleton.ScaleX;
  166. pc /= skeleton.ScaleY;
  167. pb = pc * s;
  168. pd = pa * s;
  169. prx = MathUtils.Atan2(pc, pa) * MathUtils.RadDeg;
  170. } else {
  171. pa = 0;
  172. pc = 0;
  173. prx = 90 - MathUtils.Atan2(pd, pb) * MathUtils.RadDeg;
  174. }
  175. float rx = rotation + shearX - prx;
  176. float ry = rotation + shearY - prx + 90;
  177. float la = MathUtils.CosDeg(rx) * scaleX;
  178. float lb = MathUtils.CosDeg(ry) * scaleY;
  179. float lc = MathUtils.SinDeg(rx) * scaleX;
  180. float ld = MathUtils.SinDeg(ry) * scaleY;
  181. a = pa * la - pb * lc;
  182. b = pa * lb - pb * ld;
  183. c = pc * la + pd * lc;
  184. d = pc * lb + pd * ld;
  185. break;
  186. }
  187. case TransformMode.NoScale:
  188. case TransformMode.NoScaleOrReflection: {
  189. float cos = MathUtils.CosDeg(rotation), sin = MathUtils.SinDeg(rotation);
  190. float za = (pa * cos + pb * sin) / skeleton.ScaleX;
  191. float zc = (pc * cos + pd * sin) / skeleton.ScaleY;
  192. float s = (float)Math.Sqrt(za * za + zc * zc);
  193. if (s > 0.00001f) s = 1 / s;
  194. za *= s;
  195. zc *= s;
  196. s = (float)Math.Sqrt(za * za + zc * zc);
  197. if (data.transformMode == TransformMode.NoScale
  198. && (pa * pd - pb * pc < 0) != (skeleton.ScaleX < 0 != skeleton.ScaleY < 0)) s = -s;
  199. float r = MathUtils.PI / 2 + MathUtils.Atan2(zc, za);
  200. float zb = MathUtils.Cos(r) * s;
  201. float zd = MathUtils.Sin(r) * s;
  202. float la = MathUtils.CosDeg(shearX) * scaleX;
  203. float lb = MathUtils.CosDeg(90 + shearY) * scaleY;
  204. float lc = MathUtils.SinDeg(shearX) * scaleX;
  205. float ld = MathUtils.SinDeg(90 + shearY) * scaleY;
  206. a = za * la + zb * lc;
  207. b = za * lb + zb * ld;
  208. c = zc * la + zd * lc;
  209. d = zc * lb + zd * ld;
  210. break;
  211. }
  212. }
  213. a *= skeleton.ScaleX;
  214. b *= skeleton.ScaleX;
  215. c *= skeleton.ScaleY;
  216. d *= skeleton.ScaleY;
  217. }
  218. public void SetToSetupPose () {
  219. BoneData data = this.data;
  220. x = data.x;
  221. y = data.y;
  222. rotation = data.rotation;
  223. scaleX = data.scaleX;
  224. scaleY = data.scaleY;
  225. shearX = data.shearX;
  226. shearY = data.shearY;
  227. }
  228. /// <summary>
  229. /// Computes the individual applied transform values from the world transform. This can be useful to perform processing using
  230. /// the applied transform after the world transform has been modified directly (eg, by a constraint)..
  231. ///
  232. /// Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation.
  233. /// </summary>
  234. internal void UpdateAppliedTransform () {
  235. appliedValid = true;
  236. Bone parent = this.parent;
  237. if (parent == null) {
  238. ax = worldX;
  239. ay = worldY;
  240. arotation = MathUtils.Atan2(c, a) * MathUtils.RadDeg;
  241. ascaleX = (float)Math.Sqrt(a * a + c * c);
  242. ascaleY = (float)Math.Sqrt(b * b + d * d);
  243. ashearX = 0;
  244. ashearY = MathUtils.Atan2(a * b + c * d, a * d - b * c) * MathUtils.RadDeg;
  245. return;
  246. }
  247. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  248. float pid = 1 / (pa * pd - pb * pc);
  249. float dx = worldX - parent.worldX, dy = worldY - parent.worldY;
  250. ax = (dx * pd * pid - dy * pb * pid);
  251. ay = (dy * pa * pid - dx * pc * pid);
  252. float ia = pid * pd;
  253. float id = pid * pa;
  254. float ib = pid * pb;
  255. float ic = pid * pc;
  256. float ra = ia * a - ib * c;
  257. float rb = ia * b - ib * d;
  258. float rc = id * c - ic * a;
  259. float rd = id * d - ic * b;
  260. ashearX = 0;
  261. ascaleX = (float)Math.Sqrt(ra * ra + rc * rc);
  262. if (ascaleX > 0.0001f) {
  263. float det = ra * rd - rb * rc;
  264. ascaleY = det / ascaleX;
  265. ashearY = MathUtils.Atan2(ra * rb + rc * rd, det) * MathUtils.RadDeg;
  266. arotation = MathUtils.Atan2(rc, ra) * MathUtils.RadDeg;
  267. } else {
  268. ascaleX = 0;
  269. ascaleY = (float)Math.Sqrt(rb * rb + rd * rd);
  270. ashearY = 0;
  271. arotation = 90 - MathUtils.Atan2(rd, rb) * MathUtils.RadDeg;
  272. }
  273. }
  274. public void WorldToLocal (float worldX, float worldY, out float localX, out float localY) {
  275. float a = this.a, b = this.b, c = this.c, d = this.d;
  276. float invDet = 1 / (a * d - b * c);
  277. float x = worldX - this.worldX, y = worldY - this.worldY;
  278. localX = (x * d * invDet - y * b * invDet);
  279. localY = (y * a * invDet - x * c * invDet);
  280. }
  281. public void LocalToWorld (float localX, float localY, out float worldX, out float worldY) {
  282. worldX = localX * a + localY * b + this.worldX;
  283. worldY = localX * c + localY * d + this.worldY;
  284. }
  285. public float WorldToLocalRotationX {
  286. get {
  287. Bone parent = this.parent;
  288. if (parent == null) return arotation;
  289. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d, a = this.a, c = this.c;
  290. return MathUtils.Atan2(pa * c - pc * a, pd * a - pb * c) * MathUtils.RadDeg;
  291. }
  292. }
  293. public float WorldToLocalRotationY {
  294. get {
  295. Bone parent = this.parent;
  296. if (parent == null) return arotation;
  297. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d, b = this.b, d = this.d;
  298. return MathUtils.Atan2(pa * d - pc * b, pd * b - pb * d) * MathUtils.RadDeg;
  299. }
  300. }
  301. public float WorldToLocalRotation (float worldRotation) {
  302. float sin = MathUtils.SinDeg(worldRotation), cos = MathUtils.CosDeg(worldRotation);
  303. return MathUtils.Atan2(a * sin - c * cos, d * cos - b * sin) * MathUtils.RadDeg + rotation - shearX;
  304. }
  305. public float LocalToWorldRotation (float localRotation) {
  306. localRotation -= rotation - shearX;
  307. float sin = MathUtils.SinDeg(localRotation), cos = MathUtils.CosDeg(localRotation);
  308. return MathUtils.Atan2(cos * c + sin * d, cos * a + sin * b) * MathUtils.RadDeg;
  309. }
  310. /// <summary>
  311. /// Rotates the world transform the specified amount and sets isAppliedValid to false.
  312. /// </summary>
  313. /// <param name="degrees">Degrees.</param>
  314. public void RotateWorld (float degrees) {
  315. float a = this.a, b = this.b, c = this.c, d = this.d;
  316. float cos = MathUtils.CosDeg(degrees), sin = MathUtils.SinDeg(degrees);
  317. this.a = cos * a - sin * c;
  318. this.b = cos * b - sin * d;
  319. this.c = sin * a + cos * c;
  320. this.d = sin * b + cos * d;
  321. appliedValid = false;
  322. }
  323. override public string ToString () {
  324. return data.name;
  325. }
  326. }
  327. }