123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- /******************************************************************************
- * Spine Runtimes License Agreement
- * Last updated January 1, 2020. Replaces all prior versions.
- *
- * Copyright (c) 2013-2020, Esoteric Software LLC
- *
- * Integration of the Spine Runtimes into software or otherwise creating
- * derivative works of the Spine Runtimes is permitted under the terms and
- * conditions of Section 2 of the Spine Editor License Agreement:
- * http://esotericsoftware.com/spine-editor-license
- *
- * Otherwise, it is permitted to integrate the Spine Runtimes into software
- * or otherwise create derivative works of the Spine Runtimes (collectively,
- * "Products"), provided that each user of the Products must obtain their own
- * Spine Editor license and redistribution of the Products in any form must
- * include this license and copyright notice.
- *
- * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
- * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *****************************************************************************/
- #ifdef SPINE_UE4
- #include "SpinePluginPrivatePCH.h"
- #endif
- #include <spine/DeformTimeline.h>
- #include <spine/Skeleton.h>
- #include <spine/Event.h>
- #include <spine/VertexAttachment.h>
- #include <spine/Animation.h>
- #include <spine/TimelineType.h>
- #include <spine/Slot.h>
- #include <spine/Bone.h>
- #include <spine/SlotData.h>
- using namespace spine;
- RTTI_IMPL(DeformTimeline, CurveTimeline)
- DeformTimeline::DeformTimeline(int frameCount) : CurveTimeline(frameCount), _slotIndex(0), _attachment(NULL) {
- _frames.ensureCapacity(frameCount);
- _frameVertices.ensureCapacity(frameCount);
- _frames.setSize(frameCount, 0);
- for (int i = 0; i < frameCount; ++i) {
- Vector<float> vec;
- _frameVertices.add(vec);
- }
- }
- void DeformTimeline::apply(Skeleton &skeleton, float lastTime, float time, Vector<Event *> *pEvents, float alpha,
- MixBlend blend, MixDirection direction
- ) {
- SP_UNUSED(lastTime);
- SP_UNUSED(pEvents);
- SP_UNUSED(direction);
- Slot *slotP = skeleton._slots[_slotIndex];
- Slot &slot = *slotP;
- if (!slot._bone.isActive()) return;
- Attachment *slotAttachment = slot.getAttachment();
- if (slotAttachment == NULL || !slotAttachment->getRTTI().instanceOf(VertexAttachment::rtti)) {
- return;
- }
- VertexAttachment *attachment = static_cast<VertexAttachment *>(slotAttachment);
- if (attachment->_deformAttachment != _attachment) {
- return;
- }
- Vector<float> &deformArray = slot._deform;
- if (deformArray.size() == 0) {
- blend = MixBlend_Setup;
- }
- Vector< Vector<float> > &frameVertices = _frameVertices;
- size_t vertexCount = frameVertices[0].size();
- Vector<float> &frames = _frames;
- if (time < _frames[0]) {
- switch (blend) {
- case MixBlend_Setup:
- deformArray.clear();
- return;
- case MixBlend_First: {
- if (alpha == 1) {
- deformArray.clear();
- return;
- }
- deformArray.setSize(vertexCount, 0);
- Vector<float> &deformInner = deformArray;
- if (attachment->getBones().size() == 0) {
- // Unweighted vertex positions.
- Vector<float> &setupVertices = attachment->getVertices();
- for (size_t i = 0; i < vertexCount; i++)
- deformInner[i] += (setupVertices[i] - deformInner[i]) * alpha;
- } else {
- // Weighted deform offsets.
- alpha = 1 - alpha;
- for (size_t i = 0; i < vertexCount; i++)
- deformInner[i] *= alpha;
- }
- }
- case MixBlend_Replace:
- case MixBlend_Add:
- return;
- }
- }
- deformArray.setSize(vertexCount, 0);
- Vector<float> &deform = deformArray;
- if (time >= frames[frames.size() - 1]) { // Time is after last frame.
- Vector<float> &lastVertices = frameVertices[frames.size() - 1];
- if (alpha == 1) {
- if (blend == MixBlend_Add) {
- VertexAttachment *vertexAttachment = static_cast<VertexAttachment*>(slotAttachment);
- if (vertexAttachment->getBones().size() == 0) {
- // Unweighted vertex positions, no alpha.
- Vector<float> &setupVertices = vertexAttachment->getVertices();
- for (size_t i = 0; i < vertexCount; i++)
- deform[i] += lastVertices[i] - setupVertices[i];
- } else {
- // Weighted deform offsets, no alpha.
- for (size_t i = 0; i < vertexCount; i++)
- deform[i] += lastVertices[i];
- }
- } else {
- // Vertex positions or deform offsets, no alpha.
- memcpy(deform.buffer(), lastVertices.buffer(), vertexCount * sizeof(float));
- }
- } else {
- switch (blend) {
- case MixBlend_Setup: {
- VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
- if (vertexAttachment->getBones().size() == 0) {
- // Unweighted vertex positions, with alpha.
- Vector<float> &setupVertices = vertexAttachment->getVertices();
- for (size_t i = 0; i < vertexCount; i++) {
- float setup = setupVertices[i];
- deform[i] = setup + (lastVertices[i] - setup) * alpha;
- }
- } else {
- // Weighted deform offsets, with alpha.
- for (size_t i = 0; i < vertexCount; i++)
- deform[i] = lastVertices[i] * alpha;
- }
- break;
- }
- case MixBlend_First:
- case MixBlend_Replace:
- // Vertex positions or deform offsets, with alpha.
- for (size_t i = 0; i < vertexCount; i++)
- deform[i] += (lastVertices[i] - deform[i]) * alpha;
- break;
- case MixBlend_Add:
- VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
- if (vertexAttachment->getBones().size() == 0) {
- // Unweighted vertex positions, no alpha.
- Vector<float> &setupVertices = vertexAttachment->getVertices();
- for (size_t i = 0; i < vertexCount; i++)
- deform[i] += (lastVertices[i] - setupVertices[i]) * alpha;
- } else {
- // Weighted deform offsets, alpha.
- for (size_t i = 0; i < vertexCount; i++)
- deform[i] += lastVertices[i] * alpha;
- }
- }
- }
- return;
- }
- // Interpolate between the previous frame and the current frame.
- int frame = Animation::binarySearch(frames, time);
- Vector<float> &prevVertices = frameVertices[frame - 1];
- Vector<float> &nextVertices = frameVertices[frame];
- float frameTime = frames[frame];
- float percent = getCurvePercent(frame - 1, 1 - (time - frameTime) / (frames[frame - 1] - frameTime));
- if (alpha == 1) {
- if (blend == MixBlend_Add) {
- VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
- if (vertexAttachment->getBones().size() == 0) {
- // Unweighted vertex positions, no alpha.
- Vector<float> &setupVertices = vertexAttachment->getVertices();
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i];
- deform[i] += prev + (nextVertices[i] - prev) * percent - setupVertices[i];
- }
- } else {
- // Weighted deform offsets, no alpha.
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i];
- deform[i] += prev + (nextVertices[i] - prev) * percent;
- }
- }
- } else {
- // Vertex positions or deform offsets, no alpha.
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i];
- deform[i] = prev + (nextVertices[i] - prev) * percent;
- }
- }
- } else {
- switch (blend) {
- case MixBlend_Setup: {
- VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
- if (vertexAttachment->getBones().size() == 0) {
- // Unweighted vertex positions, with alpha.
- Vector<float> &setupVertices = vertexAttachment->getVertices();
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i], setup = setupVertices[i];
- deform[i] = setup + (prev + (nextVertices[i] - prev) * percent - setup) * alpha;
- }
- } else {
- // Weighted deform offsets, with alpha.
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i];
- deform[i] = (prev + (nextVertices[i] - prev) * percent) * alpha;
- }
- }
- break;
- }
- case MixBlend_First:
- case MixBlend_Replace:
- // Vertex positions or deform offsets, with alpha.
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i];
- deform[i] += (prev + (nextVertices[i] - prev) * percent - deform[i]) * alpha;
- }
- break;
- case MixBlend_Add:
- VertexAttachment *vertexAttachment = static_cast<VertexAttachment *>(slotAttachment);
- if (vertexAttachment->getBones().size() == 0) {
- // Unweighted vertex positions, with alpha.
- Vector<float> &setupVertices = vertexAttachment->getVertices();
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i];
- deform[i] += (prev + (nextVertices[i] - prev) * percent - setupVertices[i]) * alpha;
- }
- } else {
- // Weighted deform offsets, with alpha.
- for (size_t i = 0; i < vertexCount; i++) {
- float prev = prevVertices[i];
- deform[i] += (prev + (nextVertices[i] - prev) * percent) * alpha;
- }
- }
- }
- }
- }
- int DeformTimeline::getPropertyId() {
- assert(_attachment != NULL);
- return ((int) TimelineType_Deform << 24) + _attachment->_id + _slotIndex;
- }
- void DeformTimeline::setFrame(int frameIndex, float time, Vector<float> &vertices) {
- _frames[frameIndex] = time;
- _frameVertices[frameIndex].clear();
- _frameVertices[frameIndex].addAll(vertices);
- }
- int DeformTimeline::getSlotIndex() {
- return _slotIndex;
- }
- void DeformTimeline::setSlotIndex(int inValue) {
- _slotIndex = inValue;
- }
- Vector<float> &DeformTimeline::getFrames() {
- return _frames;
- }
- Vector<Vector<float> > &DeformTimeline::getVertices() {
- return _frameVertices;
- }
- VertexAttachment *DeformTimeline::getAttachment() {
- return _attachment;
- }
- void DeformTimeline::setAttachment(VertexAttachment *inValue) {
- _attachment = inValue;
- }
|