VertexAttachment.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/VertexAttachment.h>
  33. #include <spine/Slot.h>
  34. #include <spine/Bone.h>
  35. #include <spine/Skeleton.h>
  36. using namespace spine;
  37. RTTI_IMPL(VertexAttachment, Attachment)
  38. VertexAttachment::VertexAttachment(const String &name) : Attachment(name), _worldVerticesLength(0), _deformAttachment(this), _id(getNextID()) {
  39. }
  40. VertexAttachment::~VertexAttachment() {
  41. }
  42. void VertexAttachment::computeWorldVertices(Slot &slot, Vector<float> &worldVertices) {
  43. computeWorldVertices(slot, 0, _worldVerticesLength, worldVertices, 0);
  44. }
  45. void VertexAttachment::computeWorldVertices(Slot &slot, float *worldVertices) {
  46. computeWorldVertices(slot, 0, _worldVerticesLength, worldVertices, 0);
  47. }
  48. void VertexAttachment::computeWorldVertices(Slot &slot, size_t start, size_t count, Vector<float> &worldVertices, size_t offset, size_t stride) {
  49. computeWorldVertices(slot, start, count, worldVertices.buffer(), offset, stride);
  50. }
  51. void VertexAttachment::computeWorldVertices(Slot &slot, size_t start, size_t count, float *worldVertices, size_t offset, size_t stride) {
  52. count = offset + (count >> 1) * stride;
  53. Skeleton &skeleton = slot._bone._skeleton;
  54. Vector<float> *deformArray = &slot.getDeform();
  55. Vector<float> *vertices = &_vertices;
  56. Vector<size_t> &bones = _bones;
  57. if (bones.size() == 0) {
  58. if (deformArray->size() > 0) vertices = deformArray;
  59. Bone &bone = slot._bone;
  60. float x = bone._worldX;
  61. float y = bone._worldY;
  62. float a = bone._a, b = bone._b, c = bone._c, d = bone._d;
  63. for (size_t vv = start, w = offset; w < count; vv += 2, w += stride) {
  64. float vx = (*vertices)[vv];
  65. float vy = (*vertices)[vv + 1];
  66. worldVertices[w] = vx * a + vy * b + x;
  67. worldVertices[w + 1] = vx * c + vy * d + y;
  68. }
  69. return;
  70. }
  71. int v = 0, skip = 0;
  72. for (size_t i = 0; i < start; i += 2) {
  73. int n = bones[v];
  74. v += n + 1;
  75. skip += n;
  76. }
  77. Vector<Bone *> &skeletonBones = skeleton.getBones();
  78. if (deformArray->size() == 0) {
  79. for (size_t w = offset, b = skip * 3; w < count; w += stride) {
  80. float wx = 0, wy = 0;
  81. int n = bones[v++];
  82. n += v;
  83. for (; v < n; v++, b += 3) {
  84. Bone *boneP = skeletonBones[bones[v]];
  85. Bone &bone = *boneP;
  86. float vx = (*vertices)[b];
  87. float vy = (*vertices)[b + 1];
  88. float weight = (*vertices)[b + 2];
  89. wx += (vx * bone._a + vy * bone._b + bone._worldX) * weight;
  90. wy += (vx * bone._c + vy * bone._d + bone._worldY) * weight;
  91. }
  92. worldVertices[w] = wx;
  93. worldVertices[w + 1] = wy;
  94. }
  95. } else {
  96. for (size_t w = offset, b = skip * 3, f = skip << 1; w < count; w += stride) {
  97. float wx = 0, wy = 0;
  98. int n = bones[v++];
  99. n += v;
  100. for (; v < n; v++, b += 3, f += 2) {
  101. Bone *boneP = skeletonBones[bones[v]];
  102. Bone &bone = *boneP;
  103. float vx = (*vertices)[b] + (*deformArray)[f];
  104. float vy = (*vertices)[b + 1] + (*deformArray)[f + 1];
  105. float weight = (*vertices)[b + 2];
  106. wx += (vx * bone._a + vy * bone._b + bone._worldX) * weight;
  107. wy += (vx * bone._c + vy * bone._d + bone._worldY) * weight;
  108. }
  109. worldVertices[w] = wx;
  110. worldVertices[w + 1] = wy;
  111. }
  112. }
  113. }
  114. int VertexAttachment::getId() {
  115. return _id;
  116. }
  117. Vector<size_t> &VertexAttachment::getBones() {
  118. return _bones;
  119. }
  120. Vector<float> &VertexAttachment::getVertices() {
  121. return _vertices;
  122. }
  123. size_t VertexAttachment::getWorldVerticesLength() {
  124. return _worldVerticesLength;
  125. }
  126. void VertexAttachment::setWorldVerticesLength(size_t inValue) {
  127. _worldVerticesLength = inValue;
  128. }
  129. VertexAttachment* VertexAttachment::getDeformAttachment() {
  130. return _deformAttachment;
  131. }
  132. void VertexAttachment::setDeformAttachment(VertexAttachment* attachment) {
  133. _deformAttachment = attachment;
  134. }
  135. int VertexAttachment::getNextID() {
  136. static int nextID = 0;
  137. return (nextID++ & 65535) << 11;
  138. }
  139. void VertexAttachment::copyTo(VertexAttachment* other) {
  140. other->_bones.clearAndAddAll(this->_bones);
  141. other->_vertices.clearAndAddAll(this->_vertices);
  142. other->_worldVerticesLength = this->_worldVerticesLength;
  143. other->_deformAttachment = this->_deformAttachment;
  144. }