MeshAttachment.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/MeshAttachment.h>
  33. #include <spine/HasRendererObject.h>
  34. using namespace spine;
  35. RTTI_IMPL(MeshAttachment, VertexAttachment)
  36. MeshAttachment::MeshAttachment(const String &name) : VertexAttachment(name), HasRendererObject(),
  37. _regionOffsetX(0),
  38. _regionOffsetY(0),
  39. _regionWidth(0),
  40. _regionHeight(0),
  41. _regionOriginalWidth(0),
  42. _regionOriginalHeight(0),
  43. _parentMesh(NULL),
  44. _path(),
  45. _regionU(0),
  46. _regionV(0),
  47. _regionU2(0),
  48. _regionV2(0),
  49. _width(0),
  50. _height(0),
  51. _color(1, 1, 1, 1),
  52. _hullLength(0),
  53. _regionRotate(false),
  54. _regionDegrees(0)
  55. {}
  56. MeshAttachment::~MeshAttachment() {}
  57. void MeshAttachment::updateUVs() {
  58. if (_uvs.size() != _regionUVs.size()) {
  59. _uvs.setSize(_regionUVs.size(), 0);
  60. }
  61. int i = 0, n = _regionUVs.size();
  62. float u = _regionU, v = _regionV;
  63. float width = 0, height = 0;
  64. switch (_regionDegrees) {
  65. case 90: {
  66. float textureWidth = _regionHeight / (_regionU2 - _regionU);
  67. float textureHeight = _regionWidth / (_regionV2 - _regionV);
  68. u -= (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureWidth;
  69. v -= (_regionOriginalWidth - _regionOffsetX - _regionWidth) / textureHeight;
  70. width = _regionOriginalHeight / textureWidth;
  71. height = _regionOriginalWidth / textureHeight;
  72. for (i = 0; i < n; i += 2) {
  73. _uvs[i] = u + _regionUVs[i + 1] * width;
  74. _uvs[i + 1] = v + (1 - _regionUVs[i]) * height;
  75. }
  76. return;
  77. }
  78. case 180: {
  79. float textureWidth = _regionWidth / (_regionU2 - _regionU);
  80. float textureHeight = _regionHeight / (_regionV2 - _regionV);
  81. u -= (_regionOriginalWidth - _regionOffsetX - _regionWidth) / textureWidth;
  82. v -= _regionOffsetY / textureHeight;
  83. width = _regionOriginalWidth / textureWidth;
  84. height = _regionOriginalHeight / textureHeight;
  85. for (i = 0; i < n; i += 2) {
  86. _uvs[i] = u + (1 - _regionUVs[i]) * width;
  87. _uvs[i + 1] = v + (1 - _regionUVs[i + 1]) * height;
  88. }
  89. return;
  90. }
  91. case 270: {
  92. float textureHeight = _regionHeight / (_regionV2 - _regionV);
  93. float textureWidth = _regionWidth / (_regionU2 - _regionU);
  94. u -= _regionOffsetY / textureWidth;
  95. v -= _regionOffsetX / textureHeight;
  96. width = _regionOriginalHeight / textureWidth;
  97. height = _regionOriginalWidth / textureHeight;
  98. for (i = 0; i < n; i += 2) {
  99. _uvs[i] = u + (1 - _regionUVs[i + 1]) * width;
  100. _uvs[i + 1] = v + _regionUVs[i] * height;
  101. }
  102. return;
  103. }
  104. default: {
  105. float textureWidth = _regionWidth / (_regionU2 - _regionU);
  106. float textureHeight = _regionHeight / (_regionV2 - _regionV);
  107. u -= _regionOffsetX / textureWidth;
  108. v -= (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureHeight;
  109. width = _regionOriginalWidth / textureWidth;
  110. height = _regionOriginalHeight / textureHeight;
  111. for (i = 0; i < n; i += 2) {
  112. _uvs[i] = u + _regionUVs[i] * width;
  113. _uvs[i + 1] = v + _regionUVs[i + 1] * height;
  114. }
  115. }
  116. }
  117. }
  118. int MeshAttachment::getHullLength() {
  119. return _hullLength;
  120. }
  121. void MeshAttachment::setHullLength(int inValue) {
  122. _hullLength = inValue;
  123. }
  124. Vector<float> &MeshAttachment::getRegionUVs() {
  125. return _regionUVs;
  126. }
  127. Vector<float> &MeshAttachment::getUVs() {
  128. return _uvs;
  129. }
  130. Vector<unsigned short> &MeshAttachment::getTriangles() {
  131. return _triangles;
  132. }
  133. const String &MeshAttachment::getPath() {
  134. return _path;
  135. }
  136. void MeshAttachment::setPath(const String &inValue) {
  137. _path = inValue;
  138. }
  139. float MeshAttachment::getRegionU() {
  140. return _regionU;
  141. }
  142. void MeshAttachment::setRegionU(float inValue) {
  143. _regionU = inValue;
  144. }
  145. float MeshAttachment::getRegionV() {
  146. return _regionV;
  147. }
  148. void MeshAttachment::setRegionV(float inValue) {
  149. _regionV = inValue;
  150. }
  151. float MeshAttachment::getRegionU2() {
  152. return _regionU2;
  153. }
  154. void MeshAttachment::setRegionU2(float inValue) {
  155. _regionU2 = inValue;
  156. }
  157. float MeshAttachment::getRegionV2() {
  158. return _regionV2;
  159. }
  160. void MeshAttachment::setRegionV2(float inValue) {
  161. _regionV2 = inValue;
  162. }
  163. bool MeshAttachment::getRegionRotate() {
  164. return _regionRotate;
  165. }
  166. void MeshAttachment::setRegionRotate(bool inValue) {
  167. _regionRotate = inValue;
  168. }
  169. int MeshAttachment::getRegionDegrees() {
  170. return _regionDegrees;
  171. }
  172. void MeshAttachment::setRegionDegrees(int inValue) {
  173. _regionDegrees = inValue;
  174. }
  175. float MeshAttachment::getRegionOffsetX() {
  176. return _regionOffsetX;
  177. }
  178. void MeshAttachment::setRegionOffsetX(float inValue) {
  179. _regionOffsetX = inValue;
  180. }
  181. float MeshAttachment::getRegionOffsetY() {
  182. return _regionOffsetY;
  183. }
  184. void MeshAttachment::setRegionOffsetY(float inValue) {
  185. _regionOffsetY = inValue;
  186. }
  187. float MeshAttachment::getRegionWidth() {
  188. return _regionWidth;
  189. }
  190. void MeshAttachment::setRegionWidth(float inValue) {
  191. _regionWidth = inValue;
  192. }
  193. float MeshAttachment::getRegionHeight() {
  194. return _regionHeight;
  195. }
  196. void MeshAttachment::setRegionHeight(float inValue) {
  197. _regionHeight = inValue;
  198. }
  199. float MeshAttachment::getRegionOriginalWidth() {
  200. return _regionOriginalWidth;
  201. }
  202. void MeshAttachment::setRegionOriginalWidth(float inValue) {
  203. _regionOriginalWidth = inValue;
  204. }
  205. float MeshAttachment::getRegionOriginalHeight() {
  206. return _regionOriginalHeight;
  207. }
  208. void MeshAttachment::setRegionOriginalHeight(float inValue) {
  209. _regionOriginalHeight = inValue;
  210. }
  211. MeshAttachment *MeshAttachment::getParentMesh() {
  212. return _parentMesh;
  213. }
  214. void MeshAttachment::setParentMesh(MeshAttachment *inValue) {
  215. _parentMesh = inValue;
  216. if (inValue != NULL) {
  217. _bones.clearAndAddAll(inValue->_bones);
  218. _vertices.clearAndAddAll(inValue->_vertices);
  219. _worldVerticesLength = inValue->_worldVerticesLength;
  220. _regionUVs.clearAndAddAll(inValue->_regionUVs);
  221. _triangles.clearAndAddAll(inValue->_triangles);
  222. _hullLength = inValue->_hullLength;
  223. _edges.clearAndAddAll(inValue->_edges);
  224. _width = inValue->_width;
  225. _height = inValue->_height;
  226. }
  227. }
  228. Vector<unsigned short> &MeshAttachment::getEdges() {
  229. return _edges;
  230. }
  231. float MeshAttachment::getWidth() {
  232. return _width;
  233. }
  234. void MeshAttachment::setWidth(float inValue) {
  235. _width = inValue;
  236. }
  237. float MeshAttachment::getHeight() {
  238. return _height;
  239. }
  240. void MeshAttachment::setHeight(float inValue) {
  241. _height = inValue;
  242. }
  243. spine::Color &MeshAttachment::getColor() {
  244. return _color;
  245. }
  246. Attachment* MeshAttachment::copy() {
  247. if (_parentMesh) return newLinkedMesh();
  248. MeshAttachment* copy = new (__FILE__, __LINE__) MeshAttachment(getName());
  249. copy->setRendererObject(getRendererObject());
  250. copy->_regionU = _regionU;
  251. copy->_regionV = _regionV;
  252. copy->_regionU2 = _regionU2;
  253. copy->_regionV2 = _regionV2;
  254. copy->_regionRotate = _regionRotate;
  255. copy->_regionDegrees = _regionDegrees;
  256. copy->_regionOffsetX = _regionOffsetX;
  257. copy->_regionOffsetY = _regionOffsetY;
  258. copy->_regionWidth = _regionWidth;
  259. copy->_regionHeight = _regionHeight;
  260. copy->_regionOriginalWidth = _regionOriginalWidth;
  261. copy->_regionOriginalHeight = _regionOriginalHeight;
  262. copy->_path = _path;
  263. copy->_color.set(_color);
  264. copyTo(copy);
  265. copy->_regionUVs.clearAndAddAll(_regionUVs);
  266. copy->_uvs.clearAndAddAll(_uvs);
  267. copy->_triangles.clearAndAddAll(_triangles);
  268. copy->_hullLength = _hullLength;
  269. // Nonessential.
  270. copy->_edges.clearAndAddAll(copy->_edges);
  271. copy->_width = _width;
  272. copy->_height = _height;
  273. return copy;
  274. }
  275. MeshAttachment* MeshAttachment::newLinkedMesh() {
  276. MeshAttachment* copy = new (__FILE__, __LINE__) MeshAttachment(getName());
  277. copy->setRendererObject(getRendererObject());
  278. copy->_regionU = _regionU;
  279. copy->_regionV = _regionV;
  280. copy->_regionU2 = _regionU2;
  281. copy->_regionV2 = _regionV2;
  282. copy->_regionRotate = _regionRotate;
  283. copy->_regionDegrees = _regionDegrees;
  284. copy->_regionOffsetX = _regionOffsetX;
  285. copy->_regionOffsetY = _regionOffsetY;
  286. copy->_regionWidth = _regionWidth;
  287. copy->_regionHeight = _regionHeight;
  288. copy->_regionOriginalWidth = _regionOriginalWidth;
  289. copy->_regionOriginalHeight = _regionOriginalHeight;
  290. copy->_path = _path;
  291. copy->_color.set(_color);
  292. copy->_deformAttachment = this->_deformAttachment;
  293. copy->setParentMesh(_parentMesh ? _parentMesh : this);
  294. copy->updateUVs();
  295. return copy;
  296. }