CCSkeletonNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/ActionTimeline/CCSkeletonNode.h"
  21. #include "base/CCDirector.h"
  22. #include "math/TransformUtils.h"
  23. #include "renderer/CCRenderer.h"
  24. #include "renderer/ccGLStateCache.h"
  25. #include "renderer/CCGLProgramState.h"
  26. #include <stack>
  27. using namespace cocos2d::GL;
  28. NS_TIMELINE_BEGIN
  29. SkeletonNode* SkeletonNode::create()
  30. {
  31. SkeletonNode* skeletonNode = new (std::nothrow) SkeletonNode();
  32. if (skeletonNode && skeletonNode->init())
  33. {
  34. skeletonNode->autorelease();
  35. return skeletonNode;
  36. }
  37. CC_SAFE_DELETE(skeletonNode);
  38. return nullptr;
  39. }
  40. bool SkeletonNode::init()
  41. {
  42. _rackLength = _rackWidth = 20;
  43. updateVertices();
  44. setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
  45. _rootSkeleton = this;
  46. return true;
  47. }
  48. cocos2d::Rect SkeletonNode::getBoundingBox() const
  49. {
  50. float minx, miny, maxx, maxy = 0;
  51. minx = miny = maxx = maxy;
  52. cocos2d::Rect boundingBox = getVisibleSkinsRect();
  53. bool first = true;
  54. if (!boundingBox.equals(cocos2d::Rect::ZERO))
  55. {
  56. minx = boundingBox.getMinX();
  57. miny = boundingBox.getMinY();
  58. maxx = boundingBox.getMaxX();
  59. maxy = boundingBox.getMaxY();
  60. first = false;
  61. }
  62. auto allbones = getAllSubBones();
  63. for (const auto& bone : allbones)
  64. {
  65. cocos2d::Rect r = RectApplyAffineTransform(bone->getVisibleSkinsRect(),
  66. bone->getNodeToParentAffineTransform(bone->getRootSkeletonNode()));
  67. if (r.equals(cocos2d::Rect::ZERO))
  68. continue;
  69. if (first)
  70. {
  71. minx = r.getMinX();
  72. miny = r.getMinY();
  73. maxx = r.getMaxX();
  74. maxy = r.getMaxY();
  75. first = false;
  76. }
  77. else
  78. {
  79. minx = MIN(r.getMinX(), minx);
  80. miny = MIN(r.getMinY(), miny);
  81. maxx = MAX(r.getMaxX(), maxx);
  82. maxy = MAX(r.getMaxY(), maxy);
  83. }
  84. }
  85. boundingBox.setRect(minx, miny, maxx - minx, maxy - miny);
  86. return RectApplyAffineTransform(boundingBox, this->getNodeToParentAffineTransform());
  87. }
  88. SkeletonNode::SkeletonNode()
  89. : BoneNode()
  90. , _subBonesDirty(true)
  91. , _subBonesOrderDirty(true)
  92. , _batchedVeticesCount(0)
  93. {
  94. }
  95. SkeletonNode::~SkeletonNode()
  96. {
  97. for (auto &bonepair : _subBonesMap)
  98. {
  99. setRootSkeleton(bonepair.second, nullptr);
  100. }
  101. }
  102. void SkeletonNode::updateVertices()
  103. {
  104. if (_rackLength != _squareVertices[6].x - _anchorPointInPoints.x || _rackWidth != _squareVertices[3].y - _anchorPointInPoints.y)
  105. {
  106. const float radiusl = _rackLength * .5f;
  107. const float radiusw = _rackWidth * .5f;
  108. const float radiusl_2 = radiusl * .25f;
  109. const float radiusw_2 = radiusw * .25f;
  110. _squareVertices[5].y = _squareVertices[2].y = _squareVertices[1].y = _squareVertices[6].y
  111. = _squareVertices[0].x = _squareVertices[4].x = _squareVertices[7].x = _squareVertices[3].x = .0f;
  112. _squareVertices[5].x = -radiusl; _squareVertices[0].y = -radiusw;
  113. _squareVertices[6].x = radiusl; _squareVertices[3].y = radiusw;
  114. _squareVertices[1].x = radiusl_2; _squareVertices[7].y = radiusw_2;
  115. _squareVertices[2].x = -radiusl_2; _squareVertices[4].y = -radiusw_2;
  116. for (int i = 0; i < 8; i++)
  117. {
  118. _squareVertices[i] += _anchorPointInPoints;
  119. }
  120. _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
  121. }
  122. }
  123. void SkeletonNode::updateColor()
  124. {
  125. for (unsigned int i = 0; i < 8; i++)
  126. {
  127. _squareColors[i] = _rackColor;
  128. }
  129. _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
  130. }
  131. void SkeletonNode::visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags)
  132. {
  133. // quick return if not visible. children won't be drawn.
  134. if (!_visible)
  135. {
  136. return;
  137. }
  138. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  139. // IMPORTANT:
  140. // To ease the migration to v3.0, we still support the Mat4 stack,
  141. // but it is deprecated and your code should not rely on it
  142. _director->pushMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  143. _director->loadMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  144. int i = 0;
  145. if (!_children.empty())
  146. {
  147. sortAllChildren();
  148. // draw children zOrder < 0
  149. for (; i < _children.size(); i++)
  150. {
  151. auto node = _children.at(i);
  152. if (node && node->getLocalZOrder() < 0)
  153. node->visit(renderer, _modelViewTransform, flags);
  154. else
  155. break;
  156. }
  157. for (auto it = _children.cbegin() + i; it != _children.cend(); ++it)
  158. (*it)->visit(renderer, _modelViewTransform, flags);
  159. }
  160. checkSubBonesDirty();
  161. for (const auto& bone : _subOrderedAllBones)
  162. {
  163. visitSkins(renderer, bone);
  164. }
  165. if (_isRackShow)
  166. {
  167. this->draw(renderer, _modelViewTransform, flags);
  168. // batch draw all sub bones
  169. _batchBoneCommand.init(_globalZOrder, _modelViewTransform, parentFlags);
  170. _batchBoneCommand.func = CC_CALLBACK_0(SkeletonNode::batchDrawAllSubBones, this, _modelViewTransform);
  171. renderer->addCommand(&_batchBoneCommand);
  172. }
  173. _director->popMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  174. // FIX ME: Why need to set _orderOfArrival to 0??
  175. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  176. // reset for next frame
  177. // _orderOfArrival = 0;
  178. }
  179. void SkeletonNode::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
  180. {
  181. _customCommand.init(_globalZOrder, transform, flags);
  182. _customCommand.func = CC_CALLBACK_0(SkeletonNode::onDraw, this, transform, flags);
  183. renderer->addCommand(&_customCommand);
  184. for (int i = 0; i < 8; ++i)
  185. {
  186. cocos2d::Vec4 pos;
  187. pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
  188. pos.w = 1;
  189. _modelViewTransform.transformVector(&pos);
  190. _noMVPVertices[i] = cocos2d::Vec3(pos.x, pos.y, pos.z) / pos.w;
  191. }
  192. }
  193. void SkeletonNode::batchDrawAllSubBones(const cocos2d::Mat4 &transform)
  194. {
  195. checkSubBonesDirty();
  196. _batchedVeticesCount = 0;
  197. for (const auto& bone : _subOrderedAllBones)
  198. {
  199. batchBoneDrawToSkeleton(bone);
  200. }
  201. cocos2d::Vec3* vetices = _batchedBoneVetices.data();
  202. cocos2d::Color4F* veticesColor = _batchedBoneColors.data();
  203. getGLProgram()->use();
  204. getGLProgram()->setUniformsForBuiltins(transform);
  205. cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR);
  206. glBindBuffer(GL_ARRAY_BUFFER, 0);
  207. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, vetices);
  208. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, veticesColor);
  209. cocos2d::GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  210. #ifdef CC_STUDIO_ENABLED_VIEW
  211. glLineWidth(1);
  212. glEnable(GL_LINE_SMOOTH);
  213. glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  214. for (int i = 0; i < _batchedVeticesCount; i += 4 )
  215. {
  216. glDrawArrays(GL_TRIANGLE_FAN, i, 4);
  217. glDrawArrays(GL_LINE_LOOP, i, 4);
  218. }
  219. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _batchedVeticesCount);
  220. #else
  221. for (int i = 0; i < _batchedVeticesCount; i += 4)
  222. {
  223. glDrawArrays(GL_TRIANGLE_FAN, i, 4);
  224. }
  225. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _batchedVeticesCount);
  226. #endif //CC_STUDIO_ENABLED_VIEW
  227. }
  228. void SkeletonNode::onDraw(const cocos2d::Mat4 &transform, uint32_t /*flags*/)
  229. {
  230. getGLProgram()->use();
  231. getGLProgram()->setUniformsForBuiltins(transform);
  232. cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR);
  233. glBindBuffer(GL_ARRAY_BUFFER, 0);
  234. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
  235. glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
  236. cocos2d::GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  237. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  238. glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
  239. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
  240. }
  241. void SkeletonNode::changeSkins(const std::map<std::string, std::string>& boneSkinNameMap)
  242. {
  243. for (auto &boneskin : boneSkinNameMap)
  244. {
  245. auto bone = getBoneNode(boneskin.first);
  246. if (nullptr != bone)
  247. bone->displaySkin(boneskin.second, true);
  248. }
  249. }
  250. void SkeletonNode::changeSkins(const std::string& skinGroupName)
  251. {
  252. auto suit = _skinGroupMap.find(skinGroupName);
  253. if (suit != _skinGroupMap.end())
  254. {
  255. changeSkins(suit->second);
  256. }
  257. }
  258. BoneNode* SkeletonNode::getBoneNode(const std::string& boneName)
  259. {
  260. auto iter = _subBonesMap.find(boneName);
  261. if (iter != _subBonesMap.end())
  262. {
  263. return iter->second;
  264. }
  265. return nullptr;
  266. }
  267. const cocos2d::Map<std::string, BoneNode*>& SkeletonNode::getAllSubBonesMap() const
  268. {
  269. return _subBonesMap;
  270. }
  271. void SkeletonNode::addSkinGroup(std::string groupName, std::map<std::string, std::string> boneSkinNameMap)
  272. {
  273. _skinGroupMap.emplace(groupName, boneSkinNameMap);
  274. }
  275. void SkeletonNode::checkSubBonesDirty()
  276. {
  277. if (_subBonesDirty)
  278. {
  279. updateOrderedAllbones();
  280. _subBonesDirty = false;
  281. }
  282. if (_subBonesOrderDirty)
  283. {
  284. sortOrderedAllBones();
  285. _subBonesOrderDirty = false;
  286. }
  287. }
  288. void SkeletonNode::updateOrderedAllbones()
  289. {
  290. _subOrderedAllBones.clear();
  291. // update sub bones, get All Visible SubBones
  292. // get all sub bones as visit with visible
  293. std::stack<BoneNode*> boneStack;
  294. for (const auto& bone : _childBones)
  295. {
  296. if (bone->isVisible())
  297. boneStack.push(bone);
  298. }
  299. while (boneStack.size() > 0)
  300. {
  301. auto top = boneStack.top();
  302. _subOrderedAllBones.pushBack(top);
  303. boneStack.pop();
  304. auto topChildren = top->getChildBones();
  305. for (const auto& childbone : topChildren)
  306. {
  307. if (childbone->isVisible())
  308. boneStack.push(childbone);
  309. }
  310. }
  311. }
  312. void SkeletonNode::sortOrderedAllBones()
  313. {
  314. sortNodes(this->_subOrderedAllBones);
  315. }
  316. NS_TIMELINE_END