TransformConstraint.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/TransformConstraint.h>
  33. #include <spine/TransformConstraintData.h>
  34. #include <spine/Skeleton.h>
  35. #include <spine/Bone.h>
  36. #include <spine/BoneData.h>
  37. using namespace spine;
  38. RTTI_IMPL(TransformConstraint, Updatable)
  39. TransformConstraint::TransformConstraint(TransformConstraintData &data, Skeleton &skeleton) : Updatable(),
  40. _data(data),
  41. _target(skeleton.findBone(
  42. data.getTarget()->getName())),
  43. _rotateMix(
  44. data.getRotateMix()),
  45. _translateMix(
  46. data.getTranslateMix()),
  47. _scaleMix(
  48. data.getScaleMix()),
  49. _shearMix(
  50. data.getShearMix()),
  51. _active(false)
  52. {
  53. _bones.ensureCapacity(_data.getBones().size());
  54. for (size_t i = 0; i < _data.getBones().size(); ++i) {
  55. BoneData *boneData = _data.getBones()[i];
  56. _bones.add(skeleton.findBone(boneData->getName()));
  57. }
  58. }
  59. void TransformConstraint::apply() {
  60. update();
  61. }
  62. void TransformConstraint::update() {
  63. if (_data.isLocal()) {
  64. if (_data.isRelative())
  65. applyRelativeLocal();
  66. else
  67. applyAbsoluteLocal();
  68. } else {
  69. if (_data.isRelative())
  70. applyRelativeWorld();
  71. else
  72. applyAbsoluteWorld();
  73. }
  74. }
  75. int TransformConstraint::getOrder() {
  76. return _data.getOrder();
  77. }
  78. TransformConstraintData &TransformConstraint::getData() {
  79. return _data;
  80. }
  81. Vector<Bone *> &TransformConstraint::getBones() {
  82. return _bones;
  83. }
  84. Bone *TransformConstraint::getTarget() {
  85. return _target;
  86. }
  87. void TransformConstraint::setTarget(Bone *inValue) {
  88. _target = inValue;
  89. }
  90. float TransformConstraint::getRotateMix() {
  91. return _rotateMix;
  92. }
  93. void TransformConstraint::setRotateMix(float inValue) {
  94. _rotateMix = inValue;
  95. }
  96. float TransformConstraint::getTranslateMix() {
  97. return _translateMix;
  98. }
  99. void TransformConstraint::setTranslateMix(float inValue) {
  100. _translateMix = inValue;
  101. }
  102. float TransformConstraint::getScaleMix() {
  103. return _scaleMix;
  104. }
  105. void TransformConstraint::setScaleMix(float inValue) {
  106. _scaleMix = inValue;
  107. }
  108. float TransformConstraint::getShearMix() {
  109. return _shearMix;
  110. }
  111. void TransformConstraint::setShearMix(float inValue) {
  112. _shearMix = inValue;
  113. }
  114. void TransformConstraint::applyAbsoluteWorld() {
  115. float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix;
  116. Bone &target = *_target;
  117. float ta = target._a, tb = target._b, tc = target._c, td = target._d;
  118. float degRadReflect = ta * td - tb * tc > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad;
  119. float offsetRotation = _data._offsetRotation * degRadReflect, offsetShearY = _data._offsetShearY * degRadReflect;
  120. for (size_t i = 0; i < _bones.size(); ++i) {
  121. Bone *item = _bones[i];
  122. Bone &bone = *item;
  123. bool modified = false;
  124. if (rotateMix != 0) {
  125. float a = bone._a, b = bone._b, c = bone._c, d = bone._d;
  126. float r = MathUtil::atan2(tc, ta) - MathUtil::atan2(c, a) + offsetRotation;
  127. if (r > MathUtil::Pi)
  128. r -= MathUtil::Pi_2;
  129. else if (r < -MathUtil::Pi)
  130. r += MathUtil::Pi_2;
  131. r *= rotateMix;
  132. float cos = MathUtil::cos(r), sin = MathUtil::sin(r);
  133. bone._a = cos * a - sin * c;
  134. bone._b = cos * b - sin * d;
  135. bone._c = sin * a + cos * c;
  136. bone._d = sin * b + cos * d;
  137. modified = true;
  138. }
  139. if (translateMix != 0) {
  140. float tx, ty;
  141. target.localToWorld(_data._offsetX, _data._offsetY, tx, ty);
  142. bone._worldX += (tx - bone._worldX) * translateMix;
  143. bone._worldY += (ty - bone._worldY) * translateMix;
  144. modified = true;
  145. }
  146. if (scaleMix > 0) {
  147. float s = MathUtil::sqrt(bone._a * bone._a + bone._c * bone._c);
  148. if (s > 0.00001f) s = (s + (MathUtil::sqrt(ta * ta + tc * tc) - s + _data._offsetScaleX) * scaleMix) / s;
  149. bone._a *= s;
  150. bone._c *= s;
  151. s = MathUtil::sqrt(bone._b * bone._b + bone._d * bone._d);
  152. if (s > 0.00001f) s = (s + (MathUtil::sqrt(tb * tb + td * td) - s + _data._offsetScaleY) * scaleMix) / s;
  153. bone._b *= s;
  154. bone._d *= s;
  155. modified = true;
  156. }
  157. if (shearMix > 0) {
  158. float b = bone._b, d = bone._d;
  159. float by = MathUtil::atan2(d, b);
  160. float r = MathUtil::atan2(td, tb) - MathUtil::atan2(tc, ta) - (by - MathUtil::atan2(bone._c, bone._a));
  161. if (r > MathUtil::Pi)
  162. r -= MathUtil::Pi_2;
  163. else if (r < -MathUtil::Pi)
  164. r += MathUtil::Pi_2;
  165. r = by + (r + offsetShearY) * shearMix;
  166. float s = MathUtil::sqrt(b * b + d * d);
  167. bone._b = MathUtil::cos(r) * s;
  168. bone._d = MathUtil::sin(r) * s;
  169. modified = true;
  170. }
  171. if (modified) bone._appliedValid = false;
  172. }
  173. }
  174. void TransformConstraint::applyRelativeWorld() {
  175. float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix;
  176. Bone &target = *_target;
  177. float ta = target._a, tb = target._b, tc = target._c, td = target._d;
  178. float degRadReflect = ta * td - tb * tc > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad;
  179. float offsetRotation = _data._offsetRotation * degRadReflect, offsetShearY = _data._offsetShearY * degRadReflect;
  180. for (size_t i = 0; i < _bones.size(); ++i) {
  181. Bone *item = _bones[i];
  182. Bone &bone = *item;
  183. bool modified = false;
  184. if (rotateMix != 0) {
  185. float a = bone._a, b = bone._b, c = bone._c, d = bone._d;
  186. float r = MathUtil::atan2(tc, ta) + offsetRotation;
  187. if (r > MathUtil::Pi)
  188. r -= MathUtil::Pi_2;
  189. else if (r < -MathUtil::Pi)
  190. r += MathUtil::Pi_2;
  191. r *= rotateMix;
  192. float cos = MathUtil::cos(r), sin = MathUtil::sin(r);
  193. bone._a = cos * a - sin * c;
  194. bone._b = cos * b - sin * d;
  195. bone._c = sin * a + cos * c;
  196. bone._d = sin * b + cos * d;
  197. modified = true;
  198. }
  199. if (translateMix != 0) {
  200. float tx, ty;
  201. target.localToWorld(_data._offsetX, _data._offsetY, tx, ty);
  202. bone._worldX += tx * translateMix;
  203. bone._worldY += ty * translateMix;
  204. modified = true;
  205. }
  206. if (scaleMix > 0) {
  207. float s = (MathUtil::sqrt(ta * ta + tc * tc) - 1 + _data._offsetScaleX) * scaleMix + 1;
  208. bone._a *= s;
  209. bone._c *= s;
  210. s = (MathUtil::sqrt(tb * tb + td * td) - 1 + _data._offsetScaleY) * scaleMix + 1;
  211. bone._b *= s;
  212. bone._d *= s;
  213. modified = true;
  214. }
  215. if (shearMix > 0) {
  216. float r = MathUtil::atan2(td, tb) - MathUtil::atan2(tc, ta);
  217. if (r > MathUtil::Pi)
  218. r -= MathUtil::Pi_2;
  219. else if (r < -MathUtil::Pi)
  220. r += MathUtil::Pi_2;
  221. float b = bone._b, d = bone._d;
  222. r = MathUtil::atan2(d, b) + (r - MathUtil::Pi / 2 + offsetShearY) * shearMix;
  223. float s = MathUtil::sqrt(b * b + d * d);
  224. bone._b = MathUtil::cos(r) * s;
  225. bone._d = MathUtil::sin(r) * s;
  226. modified = true;
  227. }
  228. if (modified) bone._appliedValid = false;
  229. }
  230. }
  231. void TransformConstraint::applyAbsoluteLocal() {
  232. float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix;
  233. Bone &target = *_target;
  234. if (!target._appliedValid) target.updateAppliedTransform();
  235. for (size_t i = 0; i < _bones.size(); ++i) {
  236. Bone *item = _bones[i];
  237. Bone &bone = *item;
  238. if (!bone._appliedValid) bone.updateAppliedTransform();
  239. float rotation = bone._arotation;
  240. if (rotateMix != 0) {
  241. float r = target._arotation - rotation + _data._offsetRotation;
  242. r -= (16384 - (int) (16384.499999999996 - r / 360)) * 360;
  243. rotation += r * rotateMix;
  244. }
  245. float x = bone._ax, y = bone._ay;
  246. if (translateMix != 0) {
  247. x += (target._ax - x + _data._offsetX) * translateMix;
  248. y += (target._ay - y + _data._offsetY) * translateMix;
  249. }
  250. float scaleX = bone._ascaleX, scaleY = bone._ascaleY;
  251. if (scaleMix != 0) {
  252. if (scaleX > 0.00001f) scaleX = (scaleX + (target._ascaleX - scaleX + _data._offsetScaleX) * scaleMix) / scaleX;
  253. if (scaleY > 0.00001f) scaleY = (scaleY + (target._ascaleY - scaleY + _data._offsetScaleY) * scaleMix) / scaleY;
  254. }
  255. float shearY = bone._ashearY;
  256. if (shearMix != 0) {
  257. float r = target._ashearY - shearY + _data._offsetShearY;
  258. r -= (16384 - (int) (16384.499999999996 - r / 360)) * 360;
  259. bone._shearY += r * shearMix;
  260. }
  261. bone.updateWorldTransform(x, y, rotation, scaleX, scaleY, bone._ashearX, shearY);
  262. }
  263. }
  264. void TransformConstraint::applyRelativeLocal() {
  265. float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix;
  266. Bone &target = *_target;
  267. if (!target._appliedValid) target.updateAppliedTransform();
  268. for (size_t i = 0; i < _bones.size(); ++i) {
  269. Bone *item = _bones[i];
  270. Bone &bone = *item;
  271. if (!bone._appliedValid) bone.updateAppliedTransform();
  272. float rotation = bone._arotation;
  273. if (rotateMix != 0) rotation += (target._arotation + _data._offsetRotation) * rotateMix;
  274. float x = bone._ax, y = bone._ay;
  275. if (translateMix != 0) {
  276. x += (target._ax + _data._offsetX) * translateMix;
  277. y += (target._ay + _data._offsetY) * translateMix;
  278. }
  279. float scaleX = bone._ascaleX, scaleY = bone._ascaleY;
  280. if (scaleMix != 0) {
  281. if (scaleX > 0.00001f) scaleX *= ((target._ascaleX - 1 + _data._offsetScaleX) * scaleMix) + 1;
  282. if (scaleY > 0.00001f) scaleY *= ((target._ascaleY - 1 + _data._offsetScaleY) * scaleMix) + 1;
  283. }
  284. float shearY = bone._ashearY;
  285. if (shearMix != 0) shearY += (target._ashearY + _data._offsetShearY) * shearMix;
  286. bone.updateWorldTransform(x, y, rotation, scaleX, scaleY, bone._ashearX, shearY);
  287. }
  288. }
  289. bool TransformConstraint::isActive() {
  290. return _active;
  291. }
  292. void TransformConstraint::setActive(bool inValue) {
  293. _active = inValue;
  294. }