DeformTimeline.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/DeformTimeline.h>
  33. #include <spine/Skeleton.h>
  34. #include <spine/Event.h>
  35. #include <spine/VertexAttachment.h>
  36. #include <spine/Animation.h>
  37. #include <spine/TimelineType.h>
  38. #include <spine/Slot.h>
  39. #include <spine/Bone.h>
  40. #include <spine/SlotData.h>
  41. using namespace spine;
  42. RTTI_IMPL(DeformTimeline, CurveTimeline)
  43. DeformTimeline::DeformTimeline(int frameCount) : CurveTimeline(frameCount), _slotIndex(0), _attachment(NULL) {
  44. _frames.ensureCapacity(frameCount);
  45. _frameVertices.ensureCapacity(frameCount);
  46. _frames.setSize(frameCount, 0);
  47. for (int i = 0; i < frameCount; ++i) {
  48. Vector<float> vec;
  49. _frameVertices.add(vec);
  50. }
  51. }
  52. void DeformTimeline::apply(Skeleton &skeleton, float lastTime, float time, Vector<Event *> *pEvents, float alpha,
  53. MixBlend blend, MixDirection direction
  54. ) {
  55. SP_UNUSED(lastTime);
  56. SP_UNUSED(pEvents);
  57. SP_UNUSED(direction);
  58. Slot *slotP = skeleton._slots[_slotIndex];
  59. Slot &slot = *slotP;
  60. if (!slot._bone.isActive()) return;
  61. Attachment *slotAttachment = slot.getAttachment();
  62. if (slotAttachment == NULL || !slotAttachment->getRTTI().instanceOf(VertexAttachment::rtti)) {
  63. return;
  64. }
  65. VertexAttachment *attachment = static_cast<VertexAttachment *>(slotAttachment);
  66. if (attachment->_deformAttachment != _attachment) {
  67. return;
  68. }
  69. Vector<float> &deformArray = slot._deform;
  70. if (deformArray.size() == 0) {
  71. blend = MixBlend_Setup;
  72. }
  73. Vector< Vector<float> > &frameVertices = _frameVertices;
  74. size_t vertexCount = frameVertices[0].size();
  75. Vector<float> &frames = _frames;
  76. if (time < _frames[0]) {
  77. switch (blend) {
  78. case MixBlend_Setup:
  79. deformArray.clear();
  80. return;
  81. case MixBlend_First: {
  82. if (alpha == 1) {
  83. deformArray.clear();
  84. return;
  85. }
  86. deformArray.setSize(vertexCount, 0);
  87. Vector<float> &deformInner = deformArray;
  88. if (attachment->getBones().size() == 0) {
  89. // Unweighted vertex positions.
  90. Vector<float> &setupVertices = attachment->getVertices();
  91. for (size_t i = 0; i < vertexCount; i++)
  92. deformInner[i] += (setupVertices[i] - deformInner[i]) * alpha;
  93. } else {
  94. // Weighted deform offsets.
  95. alpha = 1 - alpha;
  96. for (size_t i = 0; i < vertexCount; i++)
  97. deformInner[i] *= alpha;
  98. }
  99. }
  100. case MixBlend_Replace:
  101. case MixBlend_Add:
  102. return;
  103. }
  104. }
  105. deformArray.setSize(vertexCount, 0);
  106. Vector<float> &deform = deformArray;
  107. if (time >= frames[frames.size() - 1]) { // Time is after last frame.
  108. Vector<float> &lastVertices = frameVertices[frames.size() - 1];
  109. if (alpha == 1) {
  110. if (blend == MixBlend_Add) {
  111. VertexAttachment *vertexAttachment = static_cast<VertexAttachment*>(slotAttachment);
  112. if (vertexAttachment->getBones().size() == 0) {
  113. // Unweighted vertex positions, no alpha.
  114. Vector<float> &setupVertices = vertexAttachment->getVertices();
  115. for (size_t i = 0; i < vertexCount; i++)
  116. deform[i] += lastVertices[i] - setupVertices[i];
  117. } else {
  118. // Weighted deform offsets, no alpha.
  119. for (size_t i = 0; i < vertexCount; i++)
  120. deform[i] += lastVertices[i];
  121. }
  122. } else {
  123. // Vertex positions or deform offsets, no alpha.
  124. memcpy(deform.buffer(), lastVertices.buffer(), vertexCount * sizeof(float));
  125. }
  126. } else {
  127. switch (blend) {
  128. case MixBlend_Setup: {
  129. VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
  130. if (vertexAttachment->getBones().size() == 0) {
  131. // Unweighted vertex positions, with alpha.
  132. Vector<float> &setupVertices = vertexAttachment->getVertices();
  133. for (size_t i = 0; i < vertexCount; i++) {
  134. float setup = setupVertices[i];
  135. deform[i] = setup + (lastVertices[i] - setup) * alpha;
  136. }
  137. } else {
  138. // Weighted deform offsets, with alpha.
  139. for (size_t i = 0; i < vertexCount; i++)
  140. deform[i] = lastVertices[i] * alpha;
  141. }
  142. break;
  143. }
  144. case MixBlend_First:
  145. case MixBlend_Replace:
  146. // Vertex positions or deform offsets, with alpha.
  147. for (size_t i = 0; i < vertexCount; i++)
  148. deform[i] += (lastVertices[i] - deform[i]) * alpha;
  149. break;
  150. case MixBlend_Add:
  151. VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
  152. if (vertexAttachment->getBones().size() == 0) {
  153. // Unweighted vertex positions, no alpha.
  154. Vector<float> &setupVertices = vertexAttachment->getVertices();
  155. for (size_t i = 0; i < vertexCount; i++)
  156. deform[i] += (lastVertices[i] - setupVertices[i]) * alpha;
  157. } else {
  158. // Weighted deform offsets, alpha.
  159. for (size_t i = 0; i < vertexCount; i++)
  160. deform[i] += lastVertices[i] * alpha;
  161. }
  162. }
  163. }
  164. return;
  165. }
  166. // Interpolate between the previous frame and the current frame.
  167. int frame = Animation::binarySearch(frames, time);
  168. Vector<float> &prevVertices = frameVertices[frame - 1];
  169. Vector<float> &nextVertices = frameVertices[frame];
  170. float frameTime = frames[frame];
  171. float percent = getCurvePercent(frame - 1, 1 - (time - frameTime) / (frames[frame - 1] - frameTime));
  172. if (alpha == 1) {
  173. if (blend == MixBlend_Add) {
  174. VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
  175. if (vertexAttachment->getBones().size() == 0) {
  176. // Unweighted vertex positions, no alpha.
  177. Vector<float> &setupVertices = vertexAttachment->getVertices();
  178. for (size_t i = 0; i < vertexCount; i++) {
  179. float prev = prevVertices[i];
  180. deform[i] += prev + (nextVertices[i] - prev) * percent - setupVertices[i];
  181. }
  182. } else {
  183. // Weighted deform offsets, no alpha.
  184. for (size_t i = 0; i < vertexCount; i++) {
  185. float prev = prevVertices[i];
  186. deform[i] += prev + (nextVertices[i] - prev) * percent;
  187. }
  188. }
  189. } else {
  190. // Vertex positions or deform offsets, no alpha.
  191. for (size_t i = 0; i < vertexCount; i++) {
  192. float prev = prevVertices[i];
  193. deform[i] = prev + (nextVertices[i] - prev) * percent;
  194. }
  195. }
  196. } else {
  197. switch (blend) {
  198. case MixBlend_Setup: {
  199. VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
  200. if (vertexAttachment->getBones().size() == 0) {
  201. // Unweighted vertex positions, with alpha.
  202. Vector<float> &setupVertices = vertexAttachment->getVertices();
  203. for (size_t i = 0; i < vertexCount; i++) {
  204. float prev = prevVertices[i], setup = setupVertices[i];
  205. deform[i] = setup + (prev + (nextVertices[i] - prev) * percent - setup) * alpha;
  206. }
  207. } else {
  208. // Weighted deform offsets, with alpha.
  209. for (size_t i = 0; i < vertexCount; i++) {
  210. float prev = prevVertices[i];
  211. deform[i] = (prev + (nextVertices[i] - prev) * percent) * alpha;
  212. }
  213. }
  214. break;
  215. }
  216. case MixBlend_First:
  217. case MixBlend_Replace:
  218. // Vertex positions or deform offsets, with alpha.
  219. for (size_t i = 0; i < vertexCount; i++) {
  220. float prev = prevVertices[i];
  221. deform[i] += (prev + (nextVertices[i] - prev) * percent - deform[i]) * alpha;
  222. }
  223. break;
  224. case MixBlend_Add:
  225. VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
  226. if (vertexAttachment->getBones().size() == 0) {
  227. // Unweighted vertex positions, with alpha.
  228. Vector<float> &setupVertices = vertexAttachment->getVertices();
  229. for (size_t i = 0; i < vertexCount; i++) {
  230. float prev = prevVertices[i];
  231. deform[i] += (prev + (nextVertices[i] - prev) * percent - setupVertices[i]) * alpha;
  232. }
  233. } else {
  234. // Weighted deform offsets, with alpha.
  235. for (size_t i = 0; i < vertexCount; i++) {
  236. float prev = prevVertices[i];
  237. deform[i] += (prev + (nextVertices[i] - prev) * percent) * alpha;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. int DeformTimeline::getPropertyId() {
  244. assert(_attachment != NULL);
  245. return ((int) TimelineType_Deform << 24) + _attachment->_id + _slotIndex;
  246. }
  247. void DeformTimeline::setFrame(int frameIndex, float time, Vector<float> &vertices) {
  248. _frames[frameIndex] = time;
  249. _frameVertices[frameIndex].clear();
  250. _frameVertices[frameIndex].addAll(vertices);
  251. }
  252. int DeformTimeline::getSlotIndex() {
  253. return _slotIndex;
  254. }
  255. void DeformTimeline::setSlotIndex(int inValue) {
  256. _slotIndex = inValue;
  257. }
  258. Vector<float> &DeformTimeline::getFrames() {
  259. return _frames;
  260. }
  261. Vector<Vector<float> > &DeformTimeline::getVertices() {
  262. return _frameVertices;
  263. }
  264. VertexAttachment *DeformTimeline::getAttachment() {
  265. return _attachment;
  266. }
  267. void DeformTimeline::setAttachment(VertexAttachment *inValue) {
  268. _attachment = inValue;
  269. }