源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

384 行
14 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. namespace Spine {
  32. /// <summary>
  33. /// Stores a bone's current pose.
  34. /// <para>
  35. /// A bone has a local transform which is used to compute its world transform. A bone also has an applied transform, which is a
  36. /// local transform that can be applied to compute the world transform. The local transform and applied transform may differ if a
  37. /// constraint or application code modifies the world transform after it was computed from the local transform.
  38. /// </para>
  39. /// </summary>
  40. public class Bone : IUpdatable {
  41. static public bool yDown;
  42. internal BoneData data;
  43. internal Skeleton skeleton;
  44. internal Bone parent;
  45. internal ExposedList<Bone> children = new ExposedList<Bone>();
  46. internal float x, y, rotation, scaleX, scaleY, shearX, shearY;
  47. internal float ax, ay, arotation, ascaleX, ascaleY, ashearX, ashearY;
  48. internal bool appliedValid;
  49. internal float a, b, worldX;
  50. internal float c, d, worldY;
  51. // internal float worldSignX, worldSignY;
  52. // public float WorldSignX { get { return worldSignX; } }
  53. // public float WorldSignY { get { return worldSignY; } }
  54. internal bool sorted;
  55. public BoneData Data { get { return data; } }
  56. public Skeleton Skeleton { get { return skeleton; } }
  57. public Bone Parent { get { return parent; } }
  58. public ExposedList<Bone> Children { get { return children; } }
  59. /// <summary>The local X translation.</summary>
  60. public float X { get { return x; } set { x = value; } }
  61. /// <summary>The local Y translation.</summary>
  62. public float Y { get { return y; } set { y = value; } }
  63. /// <summary>The local rotation.</summary>
  64. public float Rotation { get { return rotation; } set { rotation = value; } }
  65. /// <summary>The local scaleX.</summary>
  66. public float ScaleX { get { return scaleX; } set { scaleX = value; } }
  67. /// <summary>The local scaleY.</summary>
  68. public float ScaleY { get { return scaleY; } set { scaleY = value; } }
  69. /// <summary>The local shearX.</summary>
  70. public float ShearX { get { return shearX; } set { shearX = value; } }
  71. /// <summary>The local shearY.</summary>
  72. public float ShearY { get { return shearY; } set { shearY = value; } }
  73. /// <summary>The rotation, as calculated by any constraints.</summary>
  74. public float AppliedRotation { get { return arotation; } set { arotation = value; } }
  75. /// <summary>The applied local x translation.</summary>
  76. public float AX { get { return ax; } set { ax = value; } }
  77. /// <summary>The applied local y translation.</summary>
  78. public float AY { get { return ay; } set { ay = value; } }
  79. /// <summary>The applied local scaleX.</summary>
  80. public float AScaleX { get { return ascaleX; } set { ascaleX = value; } }
  81. /// <summary>The applied local scaleY.</summary>
  82. public float AScaleY { get { return ascaleY; } set { ascaleY = value; } }
  83. /// <summary>The applied local shearX.</summary>
  84. public float AShearX { get { return ashearX; } set { ashearX = value; } }
  85. /// <summary>The applied local shearY.</summary>
  86. public float AShearY { get { return ashearY; } set { ashearY = value; } }
  87. public float A { get { return a; } }
  88. public float B { get { return b; } }
  89. public float C { get { return c; } }
  90. public float D { get { return d; } }
  91. public float WorldX { get { return worldX; } }
  92. public float WorldY { get { return worldY; } }
  93. public float WorldRotationX { get { return MathUtils.Atan2(c, a) * MathUtils.RadDeg; } }
  94. public float WorldRotationY { get { return MathUtils.Atan2(d, b) * MathUtils.RadDeg; } }
  95. /// <summary>Returns the magnitide (always positive) of the world scale X.</summary>
  96. public float WorldScaleX { get { return (float)Math.Sqrt(a * a + c * c); } }
  97. /// <summary>Returns the magnitide (always positive) of the world scale Y.</summary>
  98. public float WorldScaleY { get { return (float)Math.Sqrt(b * b + d * d); } }
  99. /// <param name="parent">May be null.</param>
  100. public Bone (BoneData data, Skeleton skeleton, Bone parent) {
  101. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  102. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  103. this.data = data;
  104. this.skeleton = skeleton;
  105. this.parent = parent;
  106. SetToSetupPose();
  107. }
  108. /// <summary>Same as <see cref="UpdateWorldTransform"/>. This method exists for Bone to implement <see cref="Spine.IUpdatable"/>.</summary>
  109. public void Update () {
  110. UpdateWorldTransform(x, y, rotation, scaleX, scaleY, shearX, shearY);
  111. }
  112. /// <summary>Computes the world transform using the parent bone and this bone's local transform.</summary>
  113. public void UpdateWorldTransform () {
  114. UpdateWorldTransform(x, y, rotation, scaleX, scaleY, shearX, shearY);
  115. }
  116. /// <summary>Computes the world transform using the parent bone and the specified local transform.</summary>
  117. public void UpdateWorldTransform (float x, float y, float rotation, float scaleX, float scaleY, float shearX, float shearY) {
  118. ax = x;
  119. ay = y;
  120. arotation = rotation;
  121. ascaleX = scaleX;
  122. ascaleY = scaleY;
  123. ashearX = shearX;
  124. ashearY = shearY;
  125. appliedValid = true;
  126. Skeleton skeleton = this.skeleton;
  127. Bone parent = this.parent;
  128. if (parent == null) { // Root bone.
  129. float rotationY = rotation + 90 + shearY;
  130. float la = MathUtils.CosDeg(rotation + shearX) * scaleX;
  131. float lb = MathUtils.CosDeg(rotationY) * scaleY;
  132. float lc = MathUtils.SinDeg(rotation + shearX) * scaleX;
  133. float ld = MathUtils.SinDeg(rotationY) * scaleY;
  134. if (skeleton.flipX) {
  135. x = -x;
  136. la = -la;
  137. lb = -lb;
  138. }
  139. if (skeleton.flipY != yDown) {
  140. y = -y;
  141. lc = -lc;
  142. ld = -ld;
  143. }
  144. a = la;
  145. b = lb;
  146. c = lc;
  147. d = ld;
  148. worldX = x + skeleton.x;
  149. worldY = y + skeleton.y;
  150. return;
  151. }
  152. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  153. worldX = pa * x + pb * y + parent.worldX;
  154. worldY = pc * x + pd * y + parent.worldY;
  155. switch (data.transformMode) {
  156. case TransformMode.Normal: {
  157. float rotationY = rotation + 90 + shearY;
  158. float la = MathUtils.CosDeg(rotation + shearX) * scaleX;
  159. float lb = MathUtils.CosDeg(rotationY) * scaleY;
  160. float lc = MathUtils.SinDeg(rotation + shearX) * scaleX;
  161. float ld = MathUtils.SinDeg(rotationY) * scaleY;
  162. a = pa * la + pb * lc;
  163. b = pa * lb + pb * ld;
  164. c = pc * la + pd * lc;
  165. d = pc * lb + pd * ld;
  166. return;
  167. }
  168. case TransformMode.OnlyTranslation: {
  169. float rotationY = rotation + 90 + shearY;
  170. a = MathUtils.CosDeg(rotation + shearX) * scaleX;
  171. b = MathUtils.CosDeg(rotationY) * scaleY;
  172. c = MathUtils.SinDeg(rotation + shearX) * scaleX;
  173. d = MathUtils.SinDeg(rotationY) * scaleY;
  174. break;
  175. }
  176. case TransformMode.NoRotationOrReflection: {
  177. float s = pa * pa + pc * pc, prx;
  178. if (s > 0.0001f) {
  179. s = Math.Abs(pa * pd - pb * pc) / s;
  180. pb = pc * s;
  181. pd = pa * s;
  182. prx = MathUtils.Atan2(pc, pa) * MathUtils.RadDeg;
  183. } else {
  184. pa = 0;
  185. pc = 0;
  186. prx = 90 - MathUtils.Atan2(pd, pb) * MathUtils.RadDeg;
  187. }
  188. float rx = rotation + shearX - prx;
  189. float ry = rotation + shearY - prx + 90;
  190. float la = MathUtils.CosDeg(rx) * scaleX;
  191. float lb = MathUtils.CosDeg(ry) * scaleY;
  192. float lc = MathUtils.SinDeg(rx) * scaleX;
  193. float ld = MathUtils.SinDeg(ry) * scaleY;
  194. a = pa * la - pb * lc;
  195. b = pa * lb - pb * ld;
  196. c = pc * la + pd * lc;
  197. d = pc * lb + pd * ld;
  198. break;
  199. }
  200. case TransformMode.NoScale:
  201. case TransformMode.NoScaleOrReflection: {
  202. float cos = MathUtils.CosDeg(rotation), sin = MathUtils.SinDeg(rotation);
  203. float za = pa * cos + pb * sin;
  204. float zc = pc * cos + pd * sin;
  205. float s = (float)Math.Sqrt(za * za + zc * zc);
  206. if (s > 0.00001f) s = 1 / s;
  207. za *= s;
  208. zc *= s;
  209. s = (float)Math.Sqrt(za * za + zc * zc);
  210. float r = MathUtils.PI / 2 + MathUtils.Atan2(zc, za);
  211. float zb = MathUtils.Cos(r) * s;
  212. float zd = MathUtils.Sin(r) * s;
  213. float la = MathUtils.CosDeg(shearX) * scaleX;
  214. float lb = MathUtils.CosDeg(90 + shearY) * scaleY;
  215. float lc = MathUtils.SinDeg(shearX) * scaleX;
  216. float ld = MathUtils.SinDeg(90 + shearY) * scaleY;
  217. if (data.transformMode != TransformMode.NoScaleOrReflection? pa * pd - pb* pc< 0 : skeleton.flipX != skeleton.flipY) {
  218. zb = -zb;
  219. zd = -zd;
  220. }
  221. a = za * la + zb * lc;
  222. b = za * lb + zb * ld;
  223. c = zc * la + zd * lc;
  224. d = zc * lb + zd * ld;
  225. return;
  226. }
  227. }
  228. if (skeleton.flipX) {
  229. a = -a;
  230. b = -b;
  231. }
  232. if (skeleton.flipY != Bone.yDown) {
  233. c = -c;
  234. d = -d;
  235. }
  236. }
  237. public void SetToSetupPose () {
  238. BoneData data = this.data;
  239. x = data.x;
  240. y = data.y;
  241. rotation = data.rotation;
  242. scaleX = data.scaleX;
  243. scaleY = data.scaleY;
  244. shearX = data.shearX;
  245. shearY = data.shearY;
  246. }
  247. /// <summary>
  248. /// Computes the individual applied transform values from the world transform. This can be useful to perform processing using
  249. /// the applied transform after the world transform has been modified directly (eg, by a constraint)..
  250. ///
  251. /// Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation.
  252. /// </summary>
  253. internal void UpdateAppliedTransform () {
  254. appliedValid = true;
  255. Bone parent = this.parent;
  256. if (parent == null) {
  257. ax = worldX;
  258. ay = worldY;
  259. arotation = MathUtils.Atan2(c, a) * MathUtils.RadDeg;
  260. ascaleX = (float)Math.Sqrt(a * a + c * c);
  261. ascaleY = (float)Math.Sqrt(b * b + d * d);
  262. ashearX = 0;
  263. ashearY = MathUtils.Atan2(a * b + c * d, a * d - b * c) * MathUtils.RadDeg;
  264. return;
  265. }
  266. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  267. float pid = 1 / (pa * pd - pb * pc);
  268. float dx = worldX - parent.worldX, dy = worldY - parent.worldY;
  269. ax = (dx * pd * pid - dy * pb * pid);
  270. ay = (dy * pa * pid - dx * pc * pid);
  271. float ia = pid * pd;
  272. float id = pid * pa;
  273. float ib = pid * pb;
  274. float ic = pid * pc;
  275. float ra = ia * a - ib * c;
  276. float rb = ia * b - ib * d;
  277. float rc = id * c - ic * a;
  278. float rd = id * d - ic * b;
  279. ashearX = 0;
  280. ascaleX = (float)Math.Sqrt(ra * ra + rc * rc);
  281. if (ascaleX > 0.0001f) {
  282. float det = ra * rd - rb * rc;
  283. ascaleY = det / ascaleX;
  284. ashearY = MathUtils.Atan2(ra * rb + rc * rd, det) * MathUtils.RadDeg;
  285. arotation = MathUtils.Atan2(rc, ra) * MathUtils.RadDeg;
  286. } else {
  287. ascaleX = 0;
  288. ascaleY = (float)Math.Sqrt(rb * rb + rd * rd);
  289. ashearY = 0;
  290. arotation = 90 - MathUtils.Atan2(rd, rb) * MathUtils.RadDeg;
  291. }
  292. }
  293. public void WorldToLocal (float worldX, float worldY, out float localX, out float localY) {
  294. float a = this.a, b = this.b, c = this.c, d = this.d;
  295. float invDet = 1 / (a * d - b * c);
  296. float x = worldX - this.worldX, y = worldY - this.worldY;
  297. localX = (x * d * invDet - y * b * invDet);
  298. localY = (y * a * invDet - x * c * invDet);
  299. }
  300. public void LocalToWorld (float localX, float localY, out float worldX, out float worldY) {
  301. worldX = localX * a + localY * b + this.worldX;
  302. worldY = localX * c + localY * d + this.worldY;
  303. }
  304. public float WorldToLocalRotationX {
  305. get {
  306. Bone parent = this.parent;
  307. if (parent == null) return arotation;
  308. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d, a = this.a, c = this.c;
  309. return MathUtils.Atan2(pa * c - pc * a, pd * a - pb * c) * MathUtils.RadDeg;
  310. }
  311. }
  312. public float WorldToLocalRotationY {
  313. get {
  314. Bone parent = this.parent;
  315. if (parent == null) return arotation;
  316. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d, b = this.b, d = this.d;
  317. return MathUtils.Atan2(pa * d - pc * b, pd * b - pb * d) * MathUtils.RadDeg;
  318. }
  319. }
  320. public float WorldToLocalRotation (float worldRotation) {
  321. float sin = MathUtils.SinDeg(worldRotation), cos = MathUtils.CosDeg(worldRotation);
  322. return MathUtils.Atan2(a * sin - c * cos, d * cos - b * sin) * MathUtils.RadDeg;
  323. }
  324. public float LocalToWorldRotation (float localRotation) {
  325. float sin = MathUtils.SinDeg(localRotation), cos = MathUtils.CosDeg(localRotation);
  326. return MathUtils.Atan2(cos * c + sin * d, cos * a + sin * b) * MathUtils.RadDeg;
  327. }
  328. /// <summary>
  329. /// Rotates the world transform the specified amount and sets isAppliedValid to false.
  330. /// </summary>
  331. /// <param name="degrees">Degrees.</param>
  332. public void RotateWorld (float degrees) {
  333. float a = this.a, b = this.b, c = this.c, d = this.d;
  334. float cos = MathUtils.CosDeg(degrees), sin = MathUtils.SinDeg(degrees);
  335. this.a = cos * a - sin * c;
  336. this.b = cos * b - sin * d;
  337. this.c = sin * a + cos * c;
  338. this.d = sin * b + cos * d;
  339. appliedValid = false;
  340. }
  341. override public string ToString () {
  342. return data.name;
  343. }
  344. }
  345. }