PathConstraint.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /// <para>
  33. /// Stores the current pose for a path constraint. A path constraint adjusts the rotation, translation, and scale of the
  34. /// constrained bones so they follow a {@link PathAttachment}.</para>
  35. /// <para>
  36. /// See <a href="http://esotericsoftware.com/spine-path-constraints">Path constraints</a> in the Spine User Guide.</para>
  37. /// </summary>
  38. public class PathConstraint : IUpdatable {
  39. const int NONE = -1, BEFORE = -2, AFTER = -3;
  40. const float Epsilon = 0.00001f;
  41. internal PathConstraintData data;
  42. internal ExposedList<Bone> bones;
  43. internal Slot target;
  44. internal float position, spacing, rotateMix, translateMix;
  45. internal bool active;
  46. internal ExposedList<float> spaces = new ExposedList<float>(), positions = new ExposedList<float>();
  47. internal ExposedList<float> world = new ExposedList<float>(), curves = new ExposedList<float>(), lengths = new ExposedList<float>();
  48. internal float[] segments = new float[10];
  49. public PathConstraint (PathConstraintData data, Skeleton skeleton) {
  50. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  51. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  52. this.data = data;
  53. bones = new ExposedList<Bone>(data.Bones.Count);
  54. foreach (BoneData boneData in data.bones)
  55. bones.Add(skeleton.FindBone(boneData.name));
  56. target = skeleton.FindSlot(data.target.name);
  57. position = data.position;
  58. spacing = data.spacing;
  59. rotateMix = data.rotateMix;
  60. translateMix = data.translateMix;
  61. }
  62. /// <summary>Copy constructor.</summary>
  63. public PathConstraint (PathConstraint constraint, Skeleton skeleton) {
  64. if (constraint == null) throw new ArgumentNullException("constraint cannot be null.");
  65. if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null.");
  66. data = constraint.data;
  67. bones = new ExposedList<Bone>(constraint.Bones.Count);
  68. foreach (Bone bone in constraint.Bones)
  69. bones.Add(skeleton.Bones.Items[bone.data.index]);
  70. target = skeleton.slots.Items[constraint.target.data.index];
  71. position = constraint.position;
  72. spacing = constraint.spacing;
  73. rotateMix = constraint.rotateMix;
  74. translateMix = constraint.translateMix;
  75. }
  76. /// <summary>Applies the constraint to the constrained bones.</summary>
  77. public void Apply () {
  78. Update();
  79. }
  80. public void Update () {
  81. PathAttachment attachment = target.Attachment as PathAttachment;
  82. if (attachment == null) return;
  83. float rotateMix = this.rotateMix, translateMix = this.translateMix;
  84. bool translate = translateMix > 0, rotate = rotateMix > 0;
  85. if (!translate && !rotate) return;
  86. PathConstraintData data = this.data;
  87. bool percentSpacing = data.spacingMode == SpacingMode.Percent;
  88. RotateMode rotateMode = data.rotateMode;
  89. bool tangents = rotateMode == RotateMode.Tangent, scale = rotateMode == RotateMode.ChainScale;
  90. int boneCount = this.bones.Count, spacesCount = tangents ? boneCount : boneCount + 1;
  91. Bone[] bonesItems = this.bones.Items;
  92. ExposedList<float> spaces = this.spaces.Resize(spacesCount), lengths = null;
  93. float spacing = this.spacing;
  94. if (scale || !percentSpacing) {
  95. if (scale) lengths = this.lengths.Resize(boneCount);
  96. bool lengthSpacing = data.spacingMode == SpacingMode.Length;
  97. for (int i = 0, n = spacesCount - 1; i < n;) {
  98. Bone bone = bonesItems[i];
  99. float setupLength = bone.data.length;
  100. if (setupLength < PathConstraint.Epsilon) {
  101. if (scale) lengths.Items[i] = 0;
  102. spaces.Items[++i] = 0;
  103. } else if (percentSpacing) {
  104. if (scale) {
  105. float x = setupLength * bone.a, y = setupLength * bone.c;
  106. float length = (float)Math.Sqrt(x * x + y * y);
  107. lengths.Items[i] = length;
  108. }
  109. spaces.Items[++i] = spacing;
  110. } else {
  111. float x = setupLength * bone.a, y = setupLength * bone.c;
  112. float length = (float)Math.Sqrt(x * x + y * y);
  113. if (scale) lengths.Items[i] = length;
  114. spaces.Items[++i] = (lengthSpacing ? setupLength + spacing : spacing) * length / setupLength;
  115. }
  116. }
  117. } else {
  118. for (int i = 1; i < spacesCount; i++)
  119. spaces.Items[i] = spacing;
  120. }
  121. float[] positions = ComputeWorldPositions(attachment, spacesCount, tangents,
  122. data.positionMode == PositionMode.Percent, percentSpacing);
  123. float boneX = positions[0], boneY = positions[1], offsetRotation = data.offsetRotation;
  124. bool tip;
  125. if (offsetRotation == 0) {
  126. tip = rotateMode == RotateMode.Chain;
  127. } else {
  128. tip = false;
  129. Bone p = target.bone;
  130. offsetRotation *= p.a * p.d - p.b * p.c > 0 ? MathUtils.DegRad : -MathUtils.DegRad;
  131. }
  132. for (int i = 0, p = 3; i < boneCount; i++, p += 3) {
  133. Bone bone = bonesItems[i];
  134. bone.worldX += (boneX - bone.worldX) * translateMix;
  135. bone.worldY += (boneY - bone.worldY) * translateMix;
  136. float x = positions[p], y = positions[p + 1], dx = x - boneX, dy = y - boneY;
  137. if (scale) {
  138. float length = lengths.Items[i];
  139. if (length >= PathConstraint.Epsilon) {
  140. float s = ((float)Math.Sqrt(dx * dx + dy * dy) / length - 1) * rotateMix + 1;
  141. bone.a *= s;
  142. bone.c *= s;
  143. }
  144. }
  145. boneX = x;
  146. boneY = y;
  147. if (rotate) {
  148. float a = bone.a, b = bone.b, c = bone.c, d = bone.d, r, cos, sin;
  149. if (tangents)
  150. r = positions[p - 1];
  151. else if (spaces.Items[i + 1] < PathConstraint.Epsilon)
  152. r = positions[p + 2];
  153. else
  154. r = MathUtils.Atan2(dy, dx);
  155. r -= MathUtils.Atan2(c, a);
  156. if (tip) {
  157. cos = MathUtils.Cos(r);
  158. sin = MathUtils.Sin(r);
  159. float length = bone.data.length;
  160. boneX += (length * (cos * a - sin * c) - dx) * rotateMix;
  161. boneY += (length * (sin * a + cos * c) - dy) * rotateMix;
  162. } else
  163. r += offsetRotation;
  164. if (r > MathUtils.PI)
  165. r -= MathUtils.PI2;
  166. else if (r < -MathUtils.PI) //
  167. r += MathUtils.PI2;
  168. r *= rotateMix;
  169. cos = MathUtils.Cos(r);
  170. sin = MathUtils.Sin(r);
  171. bone.a = cos * a - sin * c;
  172. bone.b = cos * b - sin * d;
  173. bone.c = sin * a + cos * c;
  174. bone.d = sin * b + cos * d;
  175. }
  176. bone.appliedValid = false;
  177. }
  178. }
  179. float[] ComputeWorldPositions (PathAttachment path, int spacesCount, bool tangents, bool percentPosition,
  180. bool percentSpacing) {
  181. Slot target = this.target;
  182. float position = this.position;
  183. float[] spacesItems = this.spaces.Items, output = this.positions.Resize(spacesCount * 3 + 2).Items, world;
  184. bool closed = path.Closed;
  185. int verticesLength = path.WorldVerticesLength, curveCount = verticesLength / 6, prevCurve = NONE;
  186. float pathLength = 0;
  187. if (!path.ConstantSpeed) {
  188. float[] lengths = path.Lengths;
  189. curveCount -= closed ? 1 : 2;
  190. pathLength = lengths[curveCount];
  191. if (percentPosition) position *= pathLength;
  192. if (percentSpacing) {
  193. for (int i = 1; i < spacesCount; i++)
  194. spacesItems[i] *= pathLength;
  195. }
  196. world = this.world.Resize(8).Items;
  197. for (int i = 0, o = 0, curve = 0; i < spacesCount; i++, o += 3) {
  198. float space = spacesItems[i];
  199. position += space;
  200. float p = position;
  201. if (closed) {
  202. p %= pathLength;
  203. if (p < 0) p += pathLength;
  204. curve = 0;
  205. } else if (p < 0) {
  206. if (prevCurve != BEFORE) {
  207. prevCurve = BEFORE;
  208. path.ComputeWorldVertices(target, 2, 4, world, 0, 2);
  209. }
  210. AddBeforePosition(p, world, 0, output, o);
  211. continue;
  212. } else if (p > pathLength) {
  213. if (prevCurve != AFTER) {
  214. prevCurve = AFTER;
  215. path.ComputeWorldVertices(target, verticesLength - 6, 4, world, 0, 2);
  216. }
  217. AddAfterPosition(p - pathLength, world, 0, output, o);
  218. continue;
  219. }
  220. // Determine curve containing position.
  221. for (;; curve++) {
  222. float length = lengths[curve];
  223. if (p > length) continue;
  224. if (curve == 0)
  225. p /= length;
  226. else {
  227. float prev = lengths[curve - 1];
  228. p = (p - prev) / (length - prev);
  229. }
  230. break;
  231. }
  232. if (curve != prevCurve) {
  233. prevCurve = curve;
  234. if (closed && curve == curveCount) {
  235. path.ComputeWorldVertices(target, verticesLength - 4, 4, world, 0, 2);
  236. path.ComputeWorldVertices(target, 0, 4, world, 4, 2);
  237. } else
  238. path.ComputeWorldVertices(target, curve * 6 + 2, 8, world, 0, 2);
  239. }
  240. AddCurvePosition(p, world[0], world[1], world[2], world[3], world[4], world[5], world[6], world[7], output, o,
  241. tangents || (i > 0 && space < PathConstraint.Epsilon));
  242. }
  243. return output;
  244. }
  245. // World vertices.
  246. if (closed) {
  247. verticesLength += 2;
  248. world = this.world.Resize(verticesLength).Items;
  249. path.ComputeWorldVertices(target, 2, verticesLength - 4, world, 0, 2);
  250. path.ComputeWorldVertices(target, 0, 2, world, verticesLength - 4, 2);
  251. world[verticesLength - 2] = world[0];
  252. world[verticesLength - 1] = world[1];
  253. } else {
  254. curveCount--;
  255. verticesLength -= 4;
  256. world = this.world.Resize(verticesLength).Items;
  257. path.ComputeWorldVertices(target, 2, verticesLength, world, 0, 2);
  258. }
  259. // Curve lengths.
  260. float[] curves = this.curves.Resize(curveCount).Items;
  261. pathLength = 0;
  262. float x1 = world[0], y1 = world[1], cx1 = 0, cy1 = 0, cx2 = 0, cy2 = 0, x2 = 0, y2 = 0;
  263. float tmpx, tmpy, dddfx, dddfy, ddfx, ddfy, dfx, dfy;
  264. for (int i = 0, w = 2; i < curveCount; i++, w += 6) {
  265. cx1 = world[w];
  266. cy1 = world[w + 1];
  267. cx2 = world[w + 2];
  268. cy2 = world[w + 3];
  269. x2 = world[w + 4];
  270. y2 = world[w + 5];
  271. tmpx = (x1 - cx1 * 2 + cx2) * 0.1875f;
  272. tmpy = (y1 - cy1 * 2 + cy2) * 0.1875f;
  273. dddfx = ((cx1 - cx2) * 3 - x1 + x2) * 0.09375f;
  274. dddfy = ((cy1 - cy2) * 3 - y1 + y2) * 0.09375f;
  275. ddfx = tmpx * 2 + dddfx;
  276. ddfy = tmpy * 2 + dddfy;
  277. dfx = (cx1 - x1) * 0.75f + tmpx + dddfx * 0.16666667f;
  278. dfy = (cy1 - y1) * 0.75f + tmpy + dddfy * 0.16666667f;
  279. pathLength += (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  280. dfx += ddfx;
  281. dfy += ddfy;
  282. ddfx += dddfx;
  283. ddfy += dddfy;
  284. pathLength += (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  285. dfx += ddfx;
  286. dfy += ddfy;
  287. pathLength += (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  288. dfx += ddfx + dddfx;
  289. dfy += ddfy + dddfy;
  290. pathLength += (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  291. curves[i] = pathLength;
  292. x1 = x2;
  293. y1 = y2;
  294. }
  295. if (percentPosition)
  296. position *= pathLength;
  297. else
  298. position *= pathLength / path.lengths[curveCount - 1];
  299. if (percentSpacing) {
  300. for (int i = 1; i < spacesCount; i++)
  301. spacesItems[i] *= pathLength;
  302. }
  303. float[] segments = this.segments;
  304. float curveLength = 0;
  305. for (int i = 0, o = 0, curve = 0, segment = 0; i < spacesCount; i++, o += 3) {
  306. float space = spacesItems[i];
  307. position += space;
  308. float p = position;
  309. if (closed) {
  310. p %= pathLength;
  311. if (p < 0) p += pathLength;
  312. curve = 0;
  313. } else if (p < 0) {
  314. AddBeforePosition(p, world, 0, output, o);
  315. continue;
  316. } else if (p > pathLength) {
  317. AddAfterPosition(p - pathLength, world, verticesLength - 4, output, o);
  318. continue;
  319. }
  320. // Determine curve containing position.
  321. for (;; curve++) {
  322. float length = curves[curve];
  323. if (p > length) continue;
  324. if (curve == 0)
  325. p /= length;
  326. else {
  327. float prev = curves[curve - 1];
  328. p = (p - prev) / (length - prev);
  329. }
  330. break;
  331. }
  332. // Curve segment lengths.
  333. if (curve != prevCurve) {
  334. prevCurve = curve;
  335. int ii = curve * 6;
  336. x1 = world[ii];
  337. y1 = world[ii + 1];
  338. cx1 = world[ii + 2];
  339. cy1 = world[ii + 3];
  340. cx2 = world[ii + 4];
  341. cy2 = world[ii + 5];
  342. x2 = world[ii + 6];
  343. y2 = world[ii + 7];
  344. tmpx = (x1 - cx1 * 2 + cx2) * 0.03f;
  345. tmpy = (y1 - cy1 * 2 + cy2) * 0.03f;
  346. dddfx = ((cx1 - cx2) * 3 - x1 + x2) * 0.006f;
  347. dddfy = ((cy1 - cy2) * 3 - y1 + y2) * 0.006f;
  348. ddfx = tmpx * 2 + dddfx;
  349. ddfy = tmpy * 2 + dddfy;
  350. dfx = (cx1 - x1) * 0.3f + tmpx + dddfx * 0.16666667f;
  351. dfy = (cy1 - y1) * 0.3f + tmpy + dddfy * 0.16666667f;
  352. curveLength = (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  353. segments[0] = curveLength;
  354. for (ii = 1; ii < 8; ii++) {
  355. dfx += ddfx;
  356. dfy += ddfy;
  357. ddfx += dddfx;
  358. ddfy += dddfy;
  359. curveLength += (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  360. segments[ii] = curveLength;
  361. }
  362. dfx += ddfx;
  363. dfy += ddfy;
  364. curveLength += (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  365. segments[8] = curveLength;
  366. dfx += ddfx + dddfx;
  367. dfy += ddfy + dddfy;
  368. curveLength += (float)Math.Sqrt(dfx * dfx + dfy * dfy);
  369. segments[9] = curveLength;
  370. segment = 0;
  371. }
  372. // Weight by segment length.
  373. p *= curveLength;
  374. for (;; segment++) {
  375. float length = segments[segment];
  376. if (p > length) continue;
  377. if (segment == 0)
  378. p /= length;
  379. else {
  380. float prev = segments[segment - 1];
  381. p = segment + (p - prev) / (length - prev);
  382. }
  383. break;
  384. }
  385. AddCurvePosition(p * 0.1f, x1, y1, cx1, cy1, cx2, cy2, x2, y2, output, o, tangents || (i > 0 && space < PathConstraint.Epsilon));
  386. }
  387. return output;
  388. }
  389. static void AddBeforePosition (float p, float[] temp, int i, float[] output, int o) {
  390. float x1 = temp[i], y1 = temp[i + 1], dx = temp[i + 2] - x1, dy = temp[i + 3] - y1, r = MathUtils.Atan2(dy, dx);
  391. output[o] = x1 + p * MathUtils.Cos(r);
  392. output[o + 1] = y1 + p * MathUtils.Sin(r);
  393. output[o + 2] = r;
  394. }
  395. static void AddAfterPosition (float p, float[] temp, int i, float[] output, int o) {
  396. float x1 = temp[i + 2], y1 = temp[i + 3], dx = x1 - temp[i], dy = y1 - temp[i + 1], r = MathUtils.Atan2(dy, dx);
  397. output[o] = x1 + p * MathUtils.Cos(r);
  398. output[o + 1] = y1 + p * MathUtils.Sin(r);
  399. output[o + 2] = r;
  400. }
  401. static void AddCurvePosition (float p, float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2,
  402. float[] output, int o, bool tangents) {
  403. if (p < PathConstraint.Epsilon || float.IsNaN(p)) {
  404. output[o] = x1;
  405. output[o + 1] = y1;
  406. output[o + 2] = (float)Math.Atan2(cy1 - y1, cx1 - x1);
  407. return;
  408. }
  409. float tt = p * p, ttt = tt * p, u = 1 - p, uu = u * u, uuu = uu * u;
  410. float ut = u * p, ut3 = ut * 3, uut3 = u * ut3, utt3 = ut3 * p;
  411. float x = x1 * uuu + cx1 * uut3 + cx2 * utt3 + x2 * ttt, y = y1 * uuu + cy1 * uut3 + cy2 * utt3 + y2 * ttt;
  412. output[o] = x;
  413. output[o + 1] = y;
  414. if (tangents) {
  415. if (p < 0.001f)
  416. output[o + 2] = (float)Math.Atan2(cy1 - y1, cx1 - x1);
  417. else
  418. output[o + 2] = (float)Math.Atan2(y - (y1 * uu + cy1 * ut * 2 + cy2 * tt), x - (x1 * uu + cx1 * ut * 2 + cx2 * tt));
  419. }
  420. }
  421. /// <summary>The position along the path.</summary>
  422. public float Position { get { return position; } set { position = value; } }
  423. /// <summary>The spacing between bones.</summary>
  424. public float Spacing { get { return spacing; } set { spacing = value; } }
  425. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained rotations.</summary>
  426. public float RotateMix { get { return rotateMix; } set { rotateMix = value; } }
  427. /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained translations.</summary>
  428. public float TranslateMix { get { return translateMix; } set { translateMix = value; } }
  429. /// <summary>The bones that will be modified by this path constraint.</summary>
  430. public ExposedList<Bone> Bones { get { return bones; } }
  431. /// <summary>The slot whose path attachment will be used to constrained the bones.</summary>
  432. public Slot Target { get { return target; } set { target = value; } }
  433. public bool Active { get { return active; } }
  434. /// <summary>The path constraint's setup pose data.</summary>
  435. public PathConstraintData Data { get { return data; } }
  436. }
  437. }