SkeletonRenderer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include <spine/SkeletonRenderer.h>
  31. #include <spine/extension.h>
  32. #include <spine/SkeletonBatch.h>
  33. #include <spine/AttachmentVertices.h>
  34. #include <spine/Cocos2dAttachmentLoader.h>
  35. #include <algorithm>
  36. USING_NS_CC;
  37. using std::min;
  38. using std::max;
  39. namespace spine {
  40. SkeletonRenderer* SkeletonRenderer::createWithData (spSkeletonData* skeletonData, bool ownsSkeletonData) {
  41. SkeletonRenderer* node = new SkeletonRenderer(skeletonData, ownsSkeletonData);
  42. node->autorelease();
  43. return node;
  44. }
  45. SkeletonRenderer* SkeletonRenderer::createWithFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale) {
  46. SkeletonRenderer* node = new SkeletonRenderer(skeletonDataFile, atlas, scale);
  47. node->autorelease();
  48. return node;
  49. }
  50. SkeletonRenderer* SkeletonRenderer::createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {
  51. SkeletonRenderer* node = new SkeletonRenderer(skeletonDataFile, atlasFile, scale);
  52. node->autorelease();
  53. return node;
  54. }
  55. void SkeletonRenderer::initialize () {
  56. _worldVertices = new float[1000]; // Max number of vertices per mesh.
  57. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  58. setOpacityModifyRGB(true);
  59. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
  60. }
  61. void SkeletonRenderer::setSkeletonData (spSkeletonData *skeletonData, bool ownsSkeletonData) {
  62. _skeleton = spSkeleton_create(skeletonData);
  63. _ownsSkeletonData = ownsSkeletonData;
  64. }
  65. SkeletonRenderer::SkeletonRenderer ()
  66. : _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
  67. }
  68. SkeletonRenderer::SkeletonRenderer (spSkeletonData *skeletonData, bool ownsSkeletonData)
  69. : _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
  70. initWithData(skeletonData, ownsSkeletonData);
  71. }
  72. SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, spAtlas* atlas, float scale)
  73. : _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
  74. initWithJsonFile(skeletonDataFile, atlas, scale);
  75. }
  76. SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, const std::string& atlasFile, float scale)
  77. : _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
  78. initWithJsonFile(skeletonDataFile, atlasFile, scale);
  79. }
  80. SkeletonRenderer::~SkeletonRenderer () {
  81. if (_ownsSkeletonData) spSkeletonData_dispose(_skeleton->data);
  82. spSkeleton_dispose(_skeleton);
  83. if (_atlas) spAtlas_dispose(_atlas);
  84. if (_attachmentLoader) spAttachmentLoader_dispose(_attachmentLoader);
  85. delete [] _worldVertices;
  86. }
  87. void SkeletonRenderer::initWithData (spSkeletonData* skeletonData, bool ownsSkeletonData) {
  88. setSkeletonData(skeletonData, ownsSkeletonData);
  89. initialize();
  90. }
  91. void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale) {
  92. _atlas = atlas;
  93. _attachmentLoader = SUPER(Cocos2dAttachmentLoader_create(_atlas));
  94. spSkeletonJson* json = spSkeletonJson_createWithLoader(_attachmentLoader);
  95. json->scale = scale;
  96. spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile.c_str());
  97. CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data.");
  98. spSkeletonJson_dispose(json);
  99. setSkeletonData(skeletonData, true);
  100. initialize();
  101. }
  102. void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {
  103. _atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
  104. CCASSERT(_atlas, "Error reading atlas file.");
  105. _attachmentLoader = SUPER(Cocos2dAttachmentLoader_create(_atlas));
  106. spSkeletonJson* json = spSkeletonJson_createWithLoader(_attachmentLoader);
  107. json->scale = scale;
  108. spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile.c_str());
  109. CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data file.");
  110. spSkeletonJson_dispose(json);
  111. setSkeletonData(skeletonData, true);
  112. initialize();
  113. }
  114. void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale) {
  115. _atlas = atlas;
  116. _attachmentLoader = SUPER(Cocos2dAttachmentLoader_create(_atlas));
  117. spSkeletonBinary* binary = spSkeletonBinary_createWithLoader(_attachmentLoader);
  118. binary->scale = scale;
  119. spSkeletonData* skeletonData = spSkeletonBinary_readSkeletonDataFile(binary, skeletonDataFile.c_str());
  120. CCASSERT(skeletonData, binary->error ? binary->error : "Error reading skeleton data file.");
  121. spSkeletonBinary_dispose(binary);
  122. setSkeletonData(skeletonData, true);
  123. initialize();
  124. }
  125. void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {
  126. _atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
  127. CCASSERT(_atlas, "Error reading atlas file.");
  128. _attachmentLoader = SUPER(Cocos2dAttachmentLoader_create(_atlas));
  129. spSkeletonBinary* binary = spSkeletonBinary_createWithLoader(_attachmentLoader);
  130. binary->scale = scale;
  131. spSkeletonData* skeletonData = spSkeletonBinary_readSkeletonDataFile(binary, skeletonDataFile.c_str());
  132. CCASSERT(skeletonData, binary->error ? binary->error : "Error reading skeleton data file.");
  133. spSkeletonBinary_dispose(binary);
  134. setSkeletonData(skeletonData, true);
  135. initialize();
  136. }
  137. void SkeletonRenderer::update (float deltaTime) {
  138. spSkeleton_update(_skeleton, deltaTime * _timeScale);
  139. }
  140. void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) {
  141. SkeletonBatch* batch = SkeletonBatch::getInstance();
  142. Color3B nodeColor = getColor();
  143. _skeleton->r = nodeColor.r / (float)255;
  144. _skeleton->g = nodeColor.g / (float)255;
  145. _skeleton->b = nodeColor.b / (float)255;
  146. _skeleton->a = getDisplayedOpacity() / (float)255;
  147. Color4F color;
  148. AttachmentVertices* attachmentVertices = nullptr;
  149. for (int i = 0, n = _skeleton->slotsCount; i < n; ++i) {
  150. spSlot* slot = _skeleton->drawOrder[i];
  151. if (!slot->attachment) continue;
  152. switch (slot->attachment->type) {
  153. case SP_ATTACHMENT_REGION: {
  154. spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
  155. spRegionAttachment_computeWorldVertices(attachment, slot->bone, _worldVertices);
  156. attachmentVertices = getAttachmentVertices(attachment);
  157. color.r = attachment->r;
  158. color.g = attachment->g;
  159. color.b = attachment->b;
  160. color.a = attachment->a;
  161. break;
  162. }
  163. case SP_ATTACHMENT_MESH: {
  164. spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment;
  165. spMeshAttachment_computeWorldVertices(attachment, slot, _worldVertices);
  166. attachmentVertices = getAttachmentVertices(attachment);
  167. color.r = attachment->r;
  168. color.g = attachment->g;
  169. color.b = attachment->b;
  170. color.a = attachment->a;
  171. break;
  172. }
  173. default:
  174. continue;
  175. }
  176. color.a *= _skeleton->a * slot->a * 255;
  177. float multiplier = _premultipliedAlpha ? color.a : 255;
  178. color.r *= _skeleton->r * slot->r * multiplier;
  179. color.g *= _skeleton->g * slot->g * multiplier;
  180. color.b *= _skeleton->b * slot->b * multiplier;
  181. for (int v = 0, w = 0, vn = attachmentVertices->_triangles->vertCount; v < vn; ++v, w += 2) {
  182. V3F_C4B_T2F* vertex = attachmentVertices->_triangles->verts + v;
  183. vertex->vertices.x = _worldVertices[w];
  184. vertex->vertices.y = _worldVertices[w + 1];
  185. vertex->colors.r = (GLubyte)color.r;
  186. vertex->colors.g = (GLubyte)color.g;
  187. vertex->colors.b = (GLubyte)color.b;
  188. vertex->colors.a = (GLubyte)color.a;
  189. }
  190. BlendFunc blendFunc;
  191. switch (slot->data->blendMode) {
  192. case SP_BLEND_MODE_ADDITIVE:
  193. blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
  194. blendFunc.dst = GL_ONE;
  195. break;
  196. case SP_BLEND_MODE_MULTIPLY:
  197. blendFunc.src = GL_DST_COLOR;
  198. blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
  199. break;
  200. case SP_BLEND_MODE_SCREEN:
  201. blendFunc.src = GL_ONE;
  202. blendFunc.dst = GL_ONE_MINUS_SRC_COLOR;
  203. break;
  204. default:
  205. blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
  206. blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
  207. }
  208. batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture->getName(), _glProgramState, blendFunc,
  209. *attachmentVertices->_triangles, transform, transformFlags);
  210. }
  211. if (_debugSlots || _debugBones) {
  212. drawDebug(renderer, transform, transformFlags);
  213. }
  214. }
  215. void SkeletonRenderer::drawDebug (Renderer* renderer, const Mat4 &transform, uint32_t transformFlags) {
  216. Director* director = Director::getInstance();
  217. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  218. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
  219. DrawNode* drawNode = DrawNode::create();
  220. if (_debugSlots) {
  221. // Slots.
  222. // DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
  223. glLineWidth(1);
  224. Vec2 points[4];
  225. V3F_C4B_T2F_Quad quad;
  226. for (int i = 0, n = _skeleton->slotsCount; i < n; i++) {
  227. spSlot* slot = _skeleton->drawOrder[i];
  228. if (!slot->attachment || slot->attachment->type != SP_ATTACHMENT_REGION) continue;
  229. spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
  230. spRegionAttachment_computeWorldVertices(attachment, slot->bone, _worldVertices);
  231. points[0] = Vec2(_worldVertices[0], _worldVertices[1]);
  232. points[1] = Vec2(_worldVertices[2], _worldVertices[3]);
  233. points[2] = Vec2(_worldVertices[4], _worldVertices[5]);
  234. points[3] = Vec2(_worldVertices[6], _worldVertices[7]);
  235. drawNode->drawPoly(points, 4, true, Color4F::BLUE);
  236. }
  237. }
  238. if (_debugBones) {
  239. // Bone lengths.
  240. glLineWidth(2);
  241. for (int i = 0, n = _skeleton->bonesCount; i < n; i++) {
  242. spBone *bone = _skeleton->bones[i];
  243. float x = bone->data->length * bone->a + bone->worldX;
  244. float y = bone->data->length * bone->c + bone->worldY;
  245. drawNode->drawLine(Vec2(bone->worldX, bone->worldY), Vec2(x, y), Color4F::RED);
  246. }
  247. // Bone origins.
  248. auto color = Color4F::BLUE; // Root bone is blue.
  249. for (int i = 0, n = _skeleton->bonesCount; i < n; i++) {
  250. spBone *bone = _skeleton->bones[i];
  251. drawNode->drawPoint(Vec2(bone->worldX, bone->worldY), 4, color);
  252. if (i == 0) color = Color4F::GREEN;
  253. }
  254. }
  255. drawNode->draw(renderer, transform, transformFlags);
  256. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  257. }
  258. AttachmentVertices* SkeletonRenderer::getAttachmentVertices (spRegionAttachment* attachment) const {
  259. return (AttachmentVertices*)attachment->rendererObject;
  260. }
  261. AttachmentVertices* SkeletonRenderer::getAttachmentVertices (spMeshAttachment* attachment) const {
  262. return (AttachmentVertices*)attachment->rendererObject;
  263. }
  264. Rect SkeletonRenderer::getBoundingBox () const {
  265. float minX = FLT_MAX, minY = FLT_MAX, maxX = -FLT_MAX, maxY = -FLT_MAX;
  266. float scaleX = getScaleX(), scaleY = getScaleY();
  267. for (int i = 0; i < _skeleton->slotsCount; ++i) {
  268. spSlot* slot = _skeleton->slots[i];
  269. if (!slot->attachment) continue;
  270. int verticesCount;
  271. if (slot->attachment->type == SP_ATTACHMENT_REGION) {
  272. spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
  273. spRegionAttachment_computeWorldVertices(attachment, slot->bone, _worldVertices);
  274. verticesCount = 8;
  275. } else if (slot->attachment->type == SP_ATTACHMENT_MESH) {
  276. spMeshAttachment* mesh = (spMeshAttachment*)slot->attachment;
  277. spMeshAttachment_computeWorldVertices(mesh, slot, _worldVertices);
  278. verticesCount = mesh->super.worldVerticesLength;
  279. } else
  280. continue;
  281. for (int ii = 0; ii < verticesCount; ii += 2) {
  282. float x = _worldVertices[ii] * scaleX, y = _worldVertices[ii + 1] * scaleY;
  283. minX = min(minX, x);
  284. minY = min(minY, y);
  285. maxX = max(maxX, x);
  286. maxY = max(maxY, y);
  287. }
  288. }
  289. Vec2 position = getPosition();
  290. if (minX == FLT_MAX) minX = minY = maxX = maxY = 0;
  291. return Rect(position.x + minX, position.y + minY, maxX - minX, maxY - minY);
  292. }
  293. // --- Convenience methods for Skeleton_* functions.
  294. void SkeletonRenderer::updateWorldTransform () {
  295. spSkeleton_updateWorldTransform(_skeleton);
  296. }
  297. void SkeletonRenderer::setToSetupPose () {
  298. spSkeleton_setToSetupPose(_skeleton);
  299. }
  300. void SkeletonRenderer::setBonesToSetupPose () {
  301. spSkeleton_setBonesToSetupPose(_skeleton);
  302. }
  303. void SkeletonRenderer::setSlotsToSetupPose () {
  304. spSkeleton_setSlotsToSetupPose(_skeleton);
  305. }
  306. spBone* SkeletonRenderer::findBone (const std::string& boneName) const {
  307. return spSkeleton_findBone(_skeleton, boneName.c_str());
  308. }
  309. spSlot* SkeletonRenderer::findSlot (const std::string& slotName) const {
  310. return spSkeleton_findSlot(_skeleton, slotName.c_str());
  311. }
  312. bool SkeletonRenderer::setSkin (const std::string& skinName) {
  313. return spSkeleton_setSkinByName(_skeleton, skinName.empty() ? 0 : skinName.c_str()) ? true : false;
  314. }
  315. bool SkeletonRenderer::setSkin (const char* skinName) {
  316. return spSkeleton_setSkinByName(_skeleton, skinName) ? true : false;
  317. }
  318. spAttachment* SkeletonRenderer::getAttachment (const std::string& slotName, const std::string& attachmentName) const {
  319. return spSkeleton_getAttachmentForSlotName(_skeleton, slotName.c_str(), attachmentName.c_str());
  320. }
  321. bool SkeletonRenderer::setAttachment (const std::string& slotName, const std::string& attachmentName) {
  322. return spSkeleton_setAttachment(_skeleton, slotName.c_str(), attachmentName.empty() ? 0 : attachmentName.c_str()) ? true : false;
  323. }
  324. bool SkeletonRenderer::setAttachment (const std::string& slotName, const char* attachmentName) {
  325. return spSkeleton_setAttachment(_skeleton, slotName.c_str(), attachmentName) ? true : false;
  326. }
  327. spSkeleton* SkeletonRenderer::getSkeleton () {
  328. return _skeleton;
  329. }
  330. void SkeletonRenderer::setTimeScale (float scale) {
  331. _timeScale = scale;
  332. }
  333. float SkeletonRenderer::getTimeScale () const {
  334. return _timeScale;
  335. }
  336. void SkeletonRenderer::setDebugSlotsEnabled (bool enabled) {
  337. _debugSlots = enabled;
  338. }
  339. bool SkeletonRenderer::getDebugSlotsEnabled () const {
  340. return _debugSlots;
  341. }
  342. void SkeletonRenderer::setDebugBonesEnabled (bool enabled) {
  343. _debugBones = enabled;
  344. }
  345. bool SkeletonRenderer::getDebugBonesEnabled () const {
  346. return _debugBones;
  347. }
  348. void SkeletonRenderer::onEnter () {
  349. #if CC_ENABLE_SCRIPT_BINDING
  350. if (_scriptType == kScriptTypeJavascript && ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return;
  351. #endif
  352. Node::onEnter();
  353. scheduleUpdate();
  354. }
  355. void SkeletonRenderer::onExit () {
  356. #if CC_ENABLE_SCRIPT_BINDING
  357. if (_scriptType == kScriptTypeJavascript && ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit)) return;
  358. #endif
  359. Node::onExit();
  360. unscheduleUpdate();
  361. }
  362. // --- CCBlendProtocol
  363. const BlendFunc& SkeletonRenderer::getBlendFunc () const {
  364. return _blendFunc;
  365. }
  366. void SkeletonRenderer::setBlendFunc (const BlendFunc &blendFunc) {
  367. _blendFunc = blendFunc;
  368. }
  369. void SkeletonRenderer::setOpacityModifyRGB (bool value) {
  370. _premultipliedAlpha = value;
  371. }
  372. bool SkeletonRenderer::isOpacityModifyRGB () const {
  373. return _premultipliedAlpha;
  374. }
  375. }