PathConstraint.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/PathConstraint.h>
  33. #include <spine/PathConstraintData.h>
  34. #include <spine/Skeleton.h>
  35. #include <spine/PathAttachment.h>
  36. #include <spine/Bone.h>
  37. #include <spine/Slot.h>
  38. #include <spine/SlotData.h>
  39. #include <spine/BoneData.h>
  40. using namespace spine;
  41. RTTI_IMPL(PathConstraint, Updatable)
  42. const float PathConstraint::EPSILON = 0.00001f;
  43. const int PathConstraint::NONE = -1;
  44. const int PathConstraint::BEFORE = -2;
  45. const int PathConstraint::AFTER = -3;
  46. PathConstraint::PathConstraint(PathConstraintData &data, Skeleton &skeleton) : Updatable(),
  47. _data(data),
  48. _target(skeleton.findSlot(
  49. data.getTarget()->getName())),
  50. _position(data.getPosition()),
  51. _spacing(data.getSpacing()),
  52. _rotateMix(data.getRotateMix()),
  53. _translateMix(data.getTranslateMix()),
  54. _active(false)
  55. {
  56. _bones.ensureCapacity(_data.getBones().size());
  57. for (size_t i = 0; i < _data.getBones().size(); i++) {
  58. BoneData *boneData = _data.getBones()[i];
  59. _bones.add(skeleton.findBone(boneData->getName()));
  60. }
  61. _segments.setSize(10, 0);
  62. }
  63. void PathConstraint::apply() {
  64. update();
  65. }
  66. void PathConstraint::update() {
  67. Attachment *baseAttachment = _target->getAttachment();
  68. if (baseAttachment == NULL || !baseAttachment->getRTTI().instanceOf(PathAttachment::rtti)) {
  69. return;
  70. }
  71. PathAttachment *attachment = static_cast<PathAttachment *>(baseAttachment);
  72. float rotateMix = _rotateMix;
  73. float translateMix = _translateMix;
  74. bool translate = translateMix > 0;
  75. bool rotate = rotateMix > 0;
  76. if (!translate && !rotate) {
  77. return;
  78. }
  79. PathConstraintData &data = _data;
  80. bool percentSpacing = data._spacingMode == SpacingMode_Percent;
  81. RotateMode rotateMode = data._rotateMode;
  82. bool tangents = rotateMode == RotateMode_Tangent, scale = rotateMode == RotateMode_ChainScale;
  83. size_t boneCount = _bones.size();
  84. size_t spacesCount = tangents ? boneCount : boneCount + 1;
  85. _spaces.setSize(spacesCount, 0);
  86. float spacing = _spacing;
  87. if (scale || !percentSpacing) {
  88. if (scale) _lengths.setSize(boneCount, 0);
  89. bool lengthSpacing = data._spacingMode == SpacingMode_Length;
  90. for (size_t i = 0, n = spacesCount - 1; i < n;) {
  91. Bone *boneP = _bones[i];
  92. Bone &bone = *boneP;
  93. float setupLength = bone._data.getLength();
  94. if (setupLength < PathConstraint::EPSILON) {
  95. if (scale) _lengths[i] = 0;
  96. _spaces[++i] = 0;
  97. } else if (percentSpacing) {
  98. if (scale) {
  99. float x = setupLength * bone._a, y = setupLength * bone._c;
  100. float length = MathUtil::sqrt(x * x + y * y);
  101. _lengths[i] = length;
  102. }
  103. _spaces[++i] = spacing;
  104. } else {
  105. float x = setupLength * bone._a;
  106. float y = setupLength * bone._c;
  107. float length = MathUtil::sqrt(x * x + y * y);
  108. if (scale) {
  109. _lengths[i] = length;
  110. }
  111. _spaces[++i] = (lengthSpacing ? setupLength + spacing : spacing) * length / setupLength;
  112. }
  113. }
  114. } else {
  115. for (size_t i = 1; i < spacesCount; ++i) {
  116. _spaces[i] = spacing;
  117. }
  118. }
  119. Vector<float>& positions = computeWorldPositions(*attachment, spacesCount, tangents,
  120. data.getPositionMode() == PositionMode_Percent, percentSpacing);
  121. float boneX = positions[0];
  122. float boneY = positions[1];
  123. float offsetRotation = data.getOffsetRotation();
  124. bool tip;
  125. if (offsetRotation == 0) {
  126. tip = rotateMode == RotateMode_Chain;
  127. } else {
  128. tip = false;
  129. Bone &p = _target->getBone();
  130. offsetRotation *= p.getA() * p.getD() - p.getB() * p.getC() > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad;
  131. }
  132. for (size_t i = 0, p = 3; i < boneCount; i++, p += 3) {
  133. Bone *boneP = _bones[i];
  134. Bone &bone = *boneP;
  135. bone._worldX += (boneX - bone._worldX) * translateMix;
  136. bone._worldY += (boneY - bone._worldY) * translateMix;
  137. float x = positions[p];
  138. float y = positions[p + 1];
  139. float dx = x - boneX;
  140. float dy = y - boneY;
  141. if (scale) {
  142. float length = _lengths[i];
  143. if (length >= PathConstraint::EPSILON) {
  144. float s = (MathUtil::sqrt(dx * dx + dy * dy) / length - 1) * rotateMix + 1;
  145. bone._a *= s;
  146. bone._c *= s;
  147. }
  148. }
  149. boneX = x;
  150. boneY = y;
  151. if (rotate) {
  152. float a = bone._a, b = bone._b, c = bone._c, d = bone._d, r, cos, sin;
  153. if (tangents)
  154. r = positions[p - 1];
  155. else if (_spaces[i + 1] < PathConstraint::EPSILON)
  156. r = positions[p + 2];
  157. else
  158. r = MathUtil::atan2(dy, dx);
  159. r -= MathUtil::atan2(c, a);
  160. if (tip) {
  161. cos = MathUtil::cos(r);
  162. sin = MathUtil::sin(r);
  163. float length = bone._data.getLength();
  164. boneX += (length * (cos * a - sin * c) - dx) * rotateMix;
  165. boneY += (length * (sin * a + cos * c) - dy) * rotateMix;
  166. } else
  167. r += offsetRotation;
  168. if (r > MathUtil::Pi)
  169. r -= MathUtil::Pi_2;
  170. else if (r < -MathUtil::Pi)
  171. r += MathUtil::Pi_2;
  172. r *= rotateMix;
  173. cos = MathUtil::cos(r);
  174. sin = MathUtil::sin(r);
  175. bone._a = cos * a - sin * c;
  176. bone._b = cos * b - sin * d;
  177. bone._c = sin * a + cos * c;
  178. bone._d = sin * b + cos * d;
  179. }
  180. bone._appliedValid = false;
  181. }
  182. }
  183. int PathConstraint::getOrder() {
  184. return _data.getOrder();
  185. }
  186. float PathConstraint::getPosition() {
  187. return _position;
  188. }
  189. void PathConstraint::setPosition(float inValue) {
  190. _position = inValue;
  191. }
  192. float PathConstraint::getSpacing() {
  193. return _spacing;
  194. }
  195. void PathConstraint::setSpacing(float inValue) {
  196. _spacing = inValue;
  197. }
  198. float PathConstraint::getRotateMix() {
  199. return _rotateMix;
  200. }
  201. void PathConstraint::setRotateMix(float inValue) {
  202. _rotateMix = inValue;
  203. }
  204. float PathConstraint::getTranslateMix() {
  205. return _translateMix;
  206. }
  207. void PathConstraint::setTranslateMix(float inValue) {
  208. _translateMix = inValue;
  209. }
  210. Vector<Bone *> &PathConstraint::getBones() {
  211. return _bones;
  212. }
  213. Slot *PathConstraint::getTarget() {
  214. return _target;
  215. }
  216. void PathConstraint::setTarget(Slot *inValue) {
  217. _target = inValue;
  218. }
  219. PathConstraintData &PathConstraint::getData() {
  220. return _data;
  221. }
  222. Vector<float>&
  223. PathConstraint::computeWorldPositions(PathAttachment &path, int spacesCount, bool tangents, bool percentPosition, bool percentSpacing) {
  224. Slot &target = *_target;
  225. float position = _position;
  226. _positions.setSize(spacesCount * 3 + 2, 0);
  227. Vector<float> &out = _positions;
  228. Vector<float> &world = _world;
  229. bool closed = path.isClosed();
  230. int verticesLength = path.getWorldVerticesLength();
  231. int curveCount = verticesLength / 6;
  232. int prevCurve = NONE;
  233. float pathLength;
  234. if (!path.isConstantSpeed()) {
  235. Vector<float> &lengths = path.getLengths();
  236. curveCount -= closed ? 1 : 2;
  237. pathLength = lengths[curveCount];
  238. if (percentPosition) position *= pathLength;
  239. if (percentSpacing) {
  240. for (int i = 1; i < spacesCount; ++i)
  241. _spaces[i] *= pathLength;
  242. }
  243. world.setSize(8, 0);
  244. for (int i = 0, o = 0, curve = 0; i < spacesCount; i++, o += 3) {
  245. float space = _spaces[i];
  246. position += space;
  247. float p = position;
  248. if (closed) {
  249. p = MathUtil::fmod(p, pathLength);
  250. if (p < 0) p += pathLength;
  251. curve = 0;
  252. } else if (p < 0) {
  253. if (prevCurve != BEFORE) {
  254. prevCurve = BEFORE;
  255. path.computeWorldVertices(target, 2, 4, world, 0);
  256. }
  257. addBeforePosition(p, world, 0, out, o);
  258. continue;
  259. } else if (p > pathLength) {
  260. if (prevCurve != AFTER) {
  261. prevCurve = AFTER;
  262. path.computeWorldVertices(target, verticesLength - 6, 4, world, 0);
  263. }
  264. addAfterPosition(p - pathLength, world, 0, out, o);
  265. continue;
  266. }
  267. // Determine curve containing position.
  268. for (;; curve++) {
  269. float length = lengths[curve];
  270. if (p > length) continue;
  271. if (curve == 0)
  272. p /= length;
  273. else {
  274. float prev = lengths[curve - 1];
  275. p = (p - prev) / (length - prev);
  276. }
  277. break;
  278. }
  279. if (curve != prevCurve) {
  280. prevCurve = curve;
  281. if (closed && curve == curveCount) {
  282. path.computeWorldVertices(target, verticesLength - 4, 4, world, 0);
  283. path.computeWorldVertices(target, 0, 4, world, 4);
  284. } else
  285. path.computeWorldVertices(target, curve * 6 + 2, 8, world, 0);
  286. }
  287. addCurvePosition(p, world[0], world[1], world[2], world[3], world[4], world[5], world[6], world[7],
  288. out, o, tangents || (i > 0 && space < EPSILON));
  289. }
  290. return out;
  291. }
  292. // World vertices.
  293. if (closed) {
  294. verticesLength += 2;
  295. world.setSize(verticesLength, 0);
  296. path.computeWorldVertices(target, 2, verticesLength - 4, world, 0);
  297. path.computeWorldVertices(target, 0, 2, world, verticesLength - 4);
  298. world[verticesLength - 2] = world[0];
  299. world[verticesLength - 1] = world[1];
  300. } else {
  301. curveCount--;
  302. verticesLength -= 4;
  303. world.setSize(verticesLength, 0);
  304. path.computeWorldVertices(target, 2, verticesLength, world, 0);
  305. }
  306. // Curve lengths.
  307. _curves.setSize(curveCount, 0);
  308. pathLength = 0;
  309. float x1 = world[0], y1 = world[1], cx1 = 0, cy1 = 0, cx2 = 0, cy2 = 0, x2 = 0, y2 = 0;
  310. float tmpx, tmpy, dddfx, dddfy, ddfx, ddfy, dfx, dfy;
  311. for (int i = 0, w = 2; i < curveCount; i++, w += 6) {
  312. cx1 = world[w];
  313. cy1 = world[w + 1];
  314. cx2 = world[w + 2];
  315. cy2 = world[w + 3];
  316. x2 = world[w + 4];
  317. y2 = world[w + 5];
  318. tmpx = (x1 - cx1 * 2 + cx2) * 0.1875f;
  319. tmpy = (y1 - cy1 * 2 + cy2) * 0.1875f;
  320. dddfx = ((cx1 - cx2) * 3 - x1 + x2) * 0.09375f;
  321. dddfy = ((cy1 - cy2) * 3 - y1 + y2) * 0.09375f;
  322. ddfx = tmpx * 2 + dddfx;
  323. ddfy = tmpy * 2 + dddfy;
  324. dfx = (cx1 - x1) * 0.75f + tmpx + dddfx * 0.16666667f;
  325. dfy = (cy1 - y1) * 0.75f + tmpy + dddfy * 0.16666667f;
  326. pathLength += MathUtil::sqrt(dfx * dfx + dfy * dfy);
  327. dfx += ddfx;
  328. dfy += ddfy;
  329. ddfx += dddfx;
  330. ddfy += dddfy;
  331. pathLength += MathUtil::sqrt(dfx * dfx + dfy * dfy);
  332. dfx += ddfx;
  333. dfy += ddfy;
  334. pathLength += MathUtil::sqrt(dfx * dfx + dfy * dfy);
  335. dfx += ddfx + dddfx;
  336. dfy += ddfy + dddfy;
  337. pathLength += MathUtil::sqrt(dfx * dfx + dfy * dfy);
  338. _curves[i] = pathLength;
  339. x1 = x2;
  340. y1 = y2;
  341. }
  342. if (percentPosition)
  343. position *= pathLength;
  344. else
  345. position *= pathLength / path.getLengths()[curveCount - 1];
  346. if (percentSpacing) {
  347. for (int i = 1; i < spacesCount; ++i)
  348. _spaces[i] *= pathLength;
  349. }
  350. float curveLength = 0;
  351. for (int i = 0, o = 0, curve = 0, segment = 0; i < spacesCount; i++, o += 3) {
  352. float space = _spaces[i];
  353. position += space;
  354. float p = position;
  355. if (closed) {
  356. p = MathUtil::fmod(p, pathLength);
  357. if (p < 0) p += pathLength;
  358. curve = 0;
  359. } else if (p < 0) {
  360. addBeforePosition(p, world, 0, out, o);
  361. continue;
  362. } else if (p > pathLength) {
  363. addAfterPosition(p - pathLength, world, verticesLength - 4, out, o);
  364. continue;
  365. }
  366. // Determine curve containing position.
  367. for (;; curve++) {
  368. float length = _curves[curve];
  369. if (p > length) continue;
  370. if (curve == 0)
  371. p /= length;
  372. else {
  373. float prev = _curves[curve - 1];
  374. p = (p - prev) / (length - prev);
  375. }
  376. break;
  377. }
  378. // Curve segment lengths.
  379. if (curve != prevCurve) {
  380. prevCurve = curve;
  381. int ii = curve * 6;
  382. x1 = world[ii];
  383. y1 = world[ii + 1];
  384. cx1 = world[ii + 2];
  385. cy1 = world[ii + 3];
  386. cx2 = world[ii + 4];
  387. cy2 = world[ii + 5];
  388. x2 = world[ii + 6];
  389. y2 = world[ii + 7];
  390. tmpx = (x1 - cx1 * 2 + cx2) * 0.03f;
  391. tmpy = (y1 - cy1 * 2 + cy2) * 0.03f;
  392. dddfx = ((cx1 - cx2) * 3 - x1 + x2) * 0.006f;
  393. dddfy = ((cy1 - cy2) * 3 - y1 + y2) * 0.006f;
  394. ddfx = tmpx * 2 + dddfx;
  395. ddfy = tmpy * 2 + dddfy;
  396. dfx = (cx1 - x1) * 0.3f + tmpx + dddfx * 0.16666667f;
  397. dfy = (cy1 - y1) * 0.3f + tmpy + dddfy * 0.16666667f;
  398. curveLength = MathUtil::sqrt(dfx * dfx + dfy * dfy);
  399. _segments[0] = curveLength;
  400. for (ii = 1; ii < 8; ii++) {
  401. dfx += ddfx;
  402. dfy += ddfy;
  403. ddfx += dddfx;
  404. ddfy += dddfy;
  405. curveLength += MathUtil::sqrt(dfx * dfx + dfy * dfy);
  406. _segments[ii] = curveLength;
  407. }
  408. dfx += ddfx;
  409. dfy += ddfy;
  410. curveLength += MathUtil::sqrt(dfx * dfx + dfy * dfy);
  411. _segments[8] = curveLength;
  412. dfx += ddfx + dddfx;
  413. dfy += ddfy + dddfy;
  414. curveLength += MathUtil::sqrt(dfx * dfx + dfy * dfy);
  415. _segments[9] = curveLength;
  416. segment = 0;
  417. }
  418. // Weight by segment length.
  419. p *= curveLength;
  420. for (;; segment++) {
  421. float length = _segments[segment];
  422. if (p > length) continue;
  423. if (segment == 0)
  424. p /= length;
  425. else {
  426. float prev = _segments[segment - 1];
  427. p = segment + (p - prev) / (length - prev);
  428. }
  429. break;
  430. }
  431. addCurvePosition(p * 0.1f, x1, y1, cx1, cy1, cx2, cy2, x2, y2, out, o,
  432. tangents || (i > 0 && space < EPSILON));
  433. }
  434. return out;
  435. }
  436. void PathConstraint::addBeforePosition(float p, Vector<float> &temp, int i, Vector<float> &output, int o) {
  437. float x1 = temp[i];
  438. float y1 = temp[i + 1];
  439. float dx = temp[i + 2] - x1;
  440. float dy = temp[i + 3] - y1;
  441. float r = MathUtil::atan2(dy, dx);
  442. output[o] = x1 + p * MathUtil::cos(r);
  443. output[o + 1] = y1 + p * MathUtil::sin(r);
  444. output[o + 2] = r;
  445. }
  446. void PathConstraint::addAfterPosition(float p, Vector<float> &temp, int i, Vector<float> &output, int o) {
  447. float x1 = temp[i + 2];
  448. float y1 = temp[i + 3];
  449. float dx = x1 - temp[i];
  450. float dy = y1 - temp[i + 1];
  451. float r = MathUtil::atan2(dy, dx);
  452. output[o] = x1 + p * MathUtil::cos(r);
  453. output[o + 1] = y1 + p * MathUtil::sin(r);
  454. output[o + 2] = r;
  455. }
  456. void PathConstraint::addCurvePosition(float p, float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2,
  457. float y2, Vector<float> &output, int o, bool tangents
  458. ) {
  459. if (p < EPSILON || MathUtil::isNan(p)) {
  460. output[o] = x1;
  461. output[o + 1] = y1;
  462. output[o + 2] = MathUtil::atan2(cy1 - y1, cx1 - x1);
  463. return;
  464. }
  465. float tt = p * p, ttt = tt * p, u = 1 - p, uu = u * u, uuu = uu * u;
  466. float ut = u * p, ut3 = ut * 3, uut3 = u * ut3, utt3 = ut3 * p;
  467. float x = x1 * uuu + cx1 * uut3 + cx2 * utt3 + x2 * ttt, y = y1 * uuu + cy1 * uut3 + cy2 * utt3 + y2 * ttt;
  468. output[o] = x;
  469. output[o + 1] = y;
  470. if (tangents) {
  471. if (p < 0.001)
  472. output[o + 2] = MathUtil::atan2(cy1 - y1, cx1 - x1);
  473. else
  474. output[o + 2] = MathUtil::atan2(y - (y1 * uu + cy1 * ut * 2 + cy2 * tt), x - (x1 * uu + cx1 * ut * 2 + cx2 * tt));
  475. }
  476. }
  477. bool PathConstraint::isActive() {
  478. return _active;
  479. }
  480. void PathConstraint::setActive(bool inValue) {
  481. _active = inValue;
  482. }