源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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