TransformConstraint.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. namespace Spine {
  31. using Physics = Skeleton.Physics;
  32. /// <summary>
  33. /// <para>
  34. /// Stores the current pose for a transform constraint. A transform constraint adjusts the world transform of the constrained
  35. /// bones to match that of the target bone.</para>
  36. /// <para>
  37. /// See <a href="http://esotericsoftware.com/spine-transform-constraints">Transform constraints</a> in the Spine User Guide.</para>
  38. /// </summary>
  39. public class TransformConstraint : IUpdatable {
  40. internal readonly TransformConstraintData data;
  41. internal readonly ExposedList<Bone> bones;
  42. internal Bone target;
  43. internal float mixRotate, mixX, mixY, mixScaleX, mixScaleY, mixShearY;
  44. internal bool active;
  45. public TransformConstraint (TransformConstraintData data, Skeleton skeleton) {
  46. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  47. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  48. this.data = data;
  49. bones = new ExposedList<Bone>();
  50. foreach (BoneData boneData in data.bones)
  51. bones.Add(skeleton.bones.Items[boneData.index]);
  52. target = skeleton.bones.Items[data.target.index];
  53. mixRotate = data.mixRotate;
  54. mixX = data.mixX;
  55. mixY = data.mixY;
  56. mixScaleX = data.mixScaleX;
  57. mixScaleY = data.mixScaleY;
  58. mixShearY = data.mixShearY;
  59. }
  60. /// <summary>Copy constructor.</summary>
  61. public TransformConstraint (TransformConstraint constraint, Skeleton skeleton)
  62. : this(constraint.data, skeleton) {
  63. mixRotate = constraint.mixRotate;
  64. mixX = constraint.mixX;
  65. mixY = constraint.mixY;
  66. mixScaleX = constraint.mixScaleX;
  67. mixScaleY = constraint.mixScaleY;
  68. mixShearY = constraint.mixShearY;
  69. }
  70. public void SetToSetupPose () {
  71. TransformConstraintData data = this.data;
  72. mixRotate = data.mixRotate;
  73. mixX = data.mixX;
  74. mixY = data.mixY;
  75. mixScaleX = data.mixScaleX;
  76. mixScaleY = data.mixScaleY;
  77. mixShearY = data.mixShearY;
  78. }
  79. public void Update (Physics physics) {
  80. if (mixRotate == 0 && mixX == 0 && mixY == 0 && mixScaleX == 0 && mixScaleY == 0 && mixShearY == 0) return;
  81. if (data.local) {
  82. if (data.relative)
  83. ApplyRelativeLocal();
  84. else
  85. ApplyAbsoluteLocal();
  86. } else {
  87. if (data.relative)
  88. ApplyRelativeWorld();
  89. else
  90. ApplyAbsoluteWorld();
  91. }
  92. }
  93. void ApplyAbsoluteWorld () {
  94. float mixRotate = this.mixRotate, mixX = this.mixX, mixY = this.mixY, mixScaleX = this.mixScaleX,
  95. mixScaleY = this.mixScaleY, mixShearY = this.mixShearY;
  96. bool translate = mixX != 0 || mixY != 0;
  97. Bone target = this.target;
  98. float ta = target.a, tb = target.b, tc = target.c, td = target.d;
  99. float degRadReflect = ta * td - tb * tc > 0 ? MathUtils.DegRad : -MathUtils.DegRad;
  100. float offsetRotation = data.offsetRotation * degRadReflect, offsetShearY = data.offsetShearY * degRadReflect;
  101. Bone[] bones = this.bones.Items;
  102. for (int i = 0, n = this.bones.Count; i < n; i++) {
  103. Bone bone = bones[i];
  104. if (mixRotate != 0) {
  105. float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
  106. float r = MathUtils.Atan2(tc, ta) - MathUtils.Atan2(c, a) + offsetRotation;
  107. if (r > MathUtils.PI)
  108. r -= MathUtils.PI2;
  109. else if (r < -MathUtils.PI) //
  110. r += MathUtils.PI2;
  111. r *= mixRotate;
  112. float cos = MathUtils.Cos(r), sin = MathUtils.Sin(r);
  113. bone.a = cos * a - sin * c;
  114. bone.b = cos * b - sin * d;
  115. bone.c = sin * a + cos * c;
  116. bone.d = sin * b + cos * d;
  117. }
  118. if (translate) {
  119. float tx, ty; //Vector2 temp = this.temp;
  120. target.LocalToWorld(data.offsetX, data.offsetY, out tx, out ty); //target.localToWorld(temp.set(data.offsetX, data.offsetY));
  121. bone.worldX += (tx - bone.worldX) * mixX;
  122. bone.worldY += (ty - bone.worldY) * mixY;
  123. }
  124. if (mixScaleX != 0) {
  125. float s = (float)Math.Sqrt(bone.a * bone.a + bone.c * bone.c);
  126. if (s != 0) s = (s + ((float)Math.Sqrt(ta * ta + tc * tc) - s + data.offsetScaleX) * mixScaleX) / s;
  127. bone.a *= s;
  128. bone.c *= s;
  129. }
  130. if (mixScaleY != 0) {
  131. float s = (float)Math.Sqrt(bone.b * bone.b + bone.d * bone.d);
  132. if (s != 0) s = (s + ((float)Math.Sqrt(tb * tb + td * td) - s + data.offsetScaleY) * mixScaleY) / s;
  133. bone.b *= s;
  134. bone.d *= s;
  135. }
  136. if (mixShearY > 0) {
  137. float b = bone.b, d = bone.d;
  138. float by = MathUtils.Atan2(d, b);
  139. float r = MathUtils.Atan2(td, tb) - MathUtils.Atan2(tc, ta) - (by - MathUtils.Atan2(bone.c, bone.a));
  140. if (r > MathUtils.PI)
  141. r -= MathUtils.PI2;
  142. else if (r < -MathUtils.PI) //
  143. r += MathUtils.PI2;
  144. r = by + (r + offsetShearY) * mixShearY;
  145. float s = (float)Math.Sqrt(b * b + d * d);
  146. bone.b = MathUtils.Cos(r) * s;
  147. bone.d = MathUtils.Sin(r) * s;
  148. }
  149. bone.UpdateAppliedTransform();
  150. }
  151. }
  152. void ApplyRelativeWorld () {
  153. float mixRotate = this.mixRotate, mixX = this.mixX, mixY = this.mixY, mixScaleX = this.mixScaleX,
  154. mixScaleY = this.mixScaleY, mixShearY = this.mixShearY;
  155. bool translate = mixX != 0 || mixY != 0;
  156. Bone target = this.target;
  157. float ta = target.a, tb = target.b, tc = target.c, td = target.d;
  158. float degRadReflect = ta * td - tb * tc > 0 ? MathUtils.DegRad : -MathUtils.DegRad;
  159. float offsetRotation = data.offsetRotation * degRadReflect, offsetShearY = data.offsetShearY * degRadReflect;
  160. Bone[] bones = this.bones.Items;
  161. for (int i = 0, n = this.bones.Count; i < n; i++) {
  162. Bone bone = bones[i];
  163. if (mixRotate != 0) {
  164. float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
  165. float r = MathUtils.Atan2(tc, ta) + offsetRotation;
  166. if (r > MathUtils.PI)
  167. r -= MathUtils.PI2;
  168. else if (r < -MathUtils.PI) //
  169. r += MathUtils.PI2;
  170. r *= mixRotate;
  171. float cos = MathUtils.Cos(r), sin = MathUtils.Sin(r);
  172. bone.a = cos * a - sin * c;
  173. bone.b = cos * b - sin * d;
  174. bone.c = sin * a + cos * c;
  175. bone.d = sin * b + cos * d;
  176. }
  177. if (translate) {
  178. float tx, ty; //Vector2 temp = this.temp;
  179. target.LocalToWorld(data.offsetX, data.offsetY, out tx, out ty); //target.localToWorld(temp.set(data.offsetX, data.offsetY));
  180. bone.worldX += tx * mixX;
  181. bone.worldY += ty * mixY;
  182. }
  183. if (mixScaleX != 0) {
  184. float s = ((float)Math.Sqrt(ta * ta + tc * tc) - 1 + data.offsetScaleX) * mixScaleX + 1;
  185. bone.a *= s;
  186. bone.c *= s;
  187. }
  188. if (mixScaleY != 0) {
  189. float s = ((float)Math.Sqrt(tb * tb + td * td) - 1 + data.offsetScaleY) * mixScaleY + 1;
  190. bone.b *= s;
  191. bone.d *= s;
  192. }
  193. if (mixShearY > 0) {
  194. float r = MathUtils.Atan2(td, tb) - MathUtils.Atan2(tc, ta);
  195. if (r > MathUtils.PI)
  196. r -= MathUtils.PI2;
  197. else if (r < -MathUtils.PI) //
  198. r += MathUtils.PI2;
  199. float b = bone.b, d = bone.d;
  200. r = MathUtils.Atan2(d, b) + (r - MathUtils.PI / 2 + offsetShearY) * mixShearY;
  201. float s = (float)Math.Sqrt(b * b + d * d);
  202. bone.b = MathUtils.Cos(r) * s;
  203. bone.d = MathUtils.Sin(r) * s;
  204. }
  205. bone.UpdateAppliedTransform();
  206. }
  207. }
  208. void ApplyAbsoluteLocal () {
  209. float mixRotate = this.mixRotate, mixX = this.mixX, mixY = this.mixY, mixScaleX = this.mixScaleX,
  210. mixScaleY = this.mixScaleY, mixShearY = this.mixShearY;
  211. Bone target = this.target;
  212. Bone[] bones = this.bones.Items;
  213. for (int i = 0, n = this.bones.Count; i < n; i++) {
  214. Bone bone = bones[i];
  215. float rotation = bone.arotation;
  216. if (mixRotate != 0) rotation += (target.arotation - rotation + data.offsetRotation) * mixRotate;
  217. float x = bone.ax, y = bone.ay;
  218. x += (target.ax - x + data.offsetX) * mixX;
  219. y += (target.ay - y + data.offsetY) * mixY;
  220. float scaleX = bone.ascaleX, scaleY = bone.ascaleY;
  221. if (mixScaleX != 0 && scaleX != 0)
  222. scaleX = (scaleX + (target.ascaleX - scaleX + data.offsetScaleX) * mixScaleX) / scaleX;
  223. if (mixScaleY != 0 && scaleY != 0)
  224. scaleY = (scaleY + (target.ascaleY - scaleY + data.offsetScaleY) * mixScaleY) / scaleY;
  225. float shearY = bone.ashearY;
  226. if (mixShearY != 0) shearY += (target.ashearY - shearY + data.offsetShearY) * mixShearY;
  227. bone.UpdateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY);
  228. }
  229. }
  230. void ApplyRelativeLocal () {
  231. float mixRotate = this.mixRotate, mixX = this.mixX, mixY = this.mixY, mixScaleX = this.mixScaleX,
  232. mixScaleY = this.mixScaleY, mixShearY = this.mixShearY;
  233. Bone target = this.target;
  234. Bone[] bones = this.bones.Items;
  235. for (int i = 0, n = this.bones.Count; i < n; i++) {
  236. Bone bone = bones[i];
  237. float rotation = bone.arotation + (target.arotation + data.offsetRotation) * mixRotate;
  238. float x = bone.ax + (target.ax + data.offsetX) * mixX;
  239. float y = bone.ay + (target.ay + data.offsetY) * mixY;
  240. float scaleX = bone.ascaleX * (((target.ascaleX - 1 + data.offsetScaleX) * mixScaleX) + 1);
  241. float scaleY = bone.ascaleY * (((target.ascaleY - 1 + data.offsetScaleY) * mixScaleY) + 1);
  242. float shearY = bone.ashearY + (target.ashearY + data.offsetShearY) * mixShearY;
  243. bone.UpdateWorldTransform(x, y, rotation, scaleX, scaleY, bone.ashearX, shearY);
  244. }
  245. }
  246. /// <summary>The bones that will be modified by this transform constraint.</summary>
  247. public ExposedList<Bone> Bones { get { return bones; } }
  248. /// <summary>The target bone whose world transform will be copied to the constrained bones.</summary>
  249. public Bone Target { get { return target; } set { target = value; } }
  250. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.</summary>
  251. public float MixRotate { get { return mixRotate; } set { mixRotate = value; } }
  252. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained translation X.</summary>
  253. public float MixX { get { return mixX; } set { mixX = value; } }
  254. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained translation Y.</summary>
  255. public float MixY { get { return mixY; } set { mixY = value; } }
  256. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained scale X.</summary>
  257. public float MixScaleX { get { return mixScaleX; } set { mixScaleX = value; } }
  258. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained scale Y.</summary>
  259. public float MixScaleY { get { return mixScaleY; } set { mixScaleY = value; } }
  260. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained shear Y.</summary>
  261. public float MixShearY { get { return mixShearY; } set { mixShearY = value; } }
  262. public bool Active { get { return active; } }
  263. /// <summary>The transform constraint's setup pose data.</summary>
  264. public TransformConstraintData Data { get { return data; } }
  265. override public string ToString () {
  266. return data.name;
  267. }
  268. }
  269. }