1
0

CCSkeleton3D.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /****************************************************************************
  2. Copyright (c) 2014-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 "3d/CCSkeleton3D.h"
  21. NS_CC_BEGIN
  22. /**
  23. * Sets the inverse bind pose matrix.
  24. *
  25. * @param m C3DMatrix representing the inverse bind pose for this Bone.
  26. */
  27. void Bone3D::setInverseBindPose(const Mat4& m)
  28. {
  29. _invBindPose = m;
  30. }
  31. const Mat4& Bone3D::getInverseBindPose()
  32. {
  33. return _invBindPose;
  34. }
  35. void Bone3D::setOriPose(const Mat4& m)
  36. {
  37. _oriPose = m;
  38. }
  39. void Bone3D::resetPose()
  40. {
  41. _local =_oriPose;
  42. for (auto it : _children) {
  43. it->resetPose();
  44. }
  45. }
  46. void Bone3D::setWorldMatDirty(bool dirty)
  47. {
  48. _worldDirty = dirty;
  49. for (auto it : _children) {
  50. it->setWorldMatDirty(dirty);
  51. }
  52. }
  53. //update own world matrix and children's
  54. void Bone3D::updateWorldMat()
  55. {
  56. getWorldMat();
  57. for (auto itor : _children) {
  58. itor->updateWorldMat();
  59. }
  60. }
  61. const Mat4& Bone3D::getWorldMat()
  62. {
  63. if (_worldDirty)
  64. {
  65. updateLocalMat();
  66. if (_parent)
  67. {
  68. _world = _parent->getWorldMat() * _local;
  69. }
  70. else
  71. _world = _local;
  72. _worldDirty = false;
  73. }
  74. return _world;
  75. }
  76. void Bone3D::setAnimationValue(float* trans, float* rot, float* scale, void* tag, float weight)
  77. {
  78. for (auto& it : _blendStates) {
  79. if (it.tag == tag)
  80. {
  81. if (trans)
  82. it.localTranslate.set(trans);
  83. if (rot)
  84. it.localRot.set(rot);
  85. if (scale)
  86. it.localScale.set(scale);
  87. it.weight = weight;
  88. return;
  89. }
  90. }
  91. BoneBlendState state;
  92. if (trans)
  93. state.localTranslate.set(trans);
  94. if (rot)
  95. state.localRot.set(rot);
  96. if (scale)
  97. state.localScale.set(scale);
  98. state.weight = weight;
  99. state.tag = tag;
  100. _blendStates.push_back(state);
  101. }
  102. void Bone3D::clearBoneBlendState()
  103. {
  104. _blendStates.clear();
  105. for (auto it : _children) {
  106. it->clearBoneBlendState();
  107. }
  108. }
  109. /**
  110. * Creates C3DBone.
  111. */
  112. Bone3D* Bone3D::create(const std::string& id)
  113. {
  114. auto bone = new (std::nothrow) Bone3D(id);
  115. bone->autorelease();
  116. return bone;
  117. }
  118. void Bone3D::updateJointMatrix(Vec4* matrixPalette)
  119. {
  120. {
  121. static Mat4 t;
  122. Mat4::multiply(_world, getInverseBindPose(), &t);
  123. matrixPalette[0].set(t.m[0], t.m[4], t.m[8], t.m[12]);
  124. matrixPalette[1].set(t.m[1], t.m[5], t.m[9], t.m[13]);
  125. matrixPalette[2].set(t.m[2], t.m[6], t.m[10], t.m[14]);
  126. }
  127. }
  128. Bone3D* Bone3D::getParentBone()
  129. {
  130. return _parent;
  131. }
  132. ssize_t Bone3D::getChildBoneCount() const
  133. {
  134. return _children.size();
  135. }
  136. Bone3D* Bone3D::getChildBoneByIndex(int index) const
  137. {
  138. return _children.at(index);
  139. }
  140. void Bone3D::addChildBone(Bone3D* bone)
  141. {
  142. if (_children.find(bone) == _children.end())
  143. _children.pushBack(bone);
  144. }
  145. void Bone3D::removeChildBoneByIndex(int index)
  146. {
  147. _children.erase(index);
  148. }
  149. void Bone3D::removeChildBone(Bone3D* bone)
  150. {
  151. _children.eraseObject(bone);
  152. }
  153. void Bone3D::removeAllChildBone()
  154. {
  155. _children.clear();
  156. }
  157. Bone3D::Bone3D(const std::string& id)
  158. : _name(id)
  159. , _parent(nullptr)
  160. , _worldDirty(true)
  161. {
  162. }
  163. Bone3D::~Bone3D()
  164. {
  165. removeAllChildBone();
  166. }
  167. void Bone3D::updateLocalMat()
  168. {
  169. if (_blendStates.size())
  170. {
  171. Vec3 translate, scale;
  172. Quaternion quat(Quaternion::ZERO);
  173. float total = 0.f;
  174. for (auto it: _blendStates) {
  175. total += it.weight;
  176. }
  177. if (total)
  178. {
  179. if (_blendStates.size() == 1)
  180. {
  181. auto& state = _blendStates[0];
  182. translate = state.localTranslate;
  183. scale = state.localScale;
  184. quat = state.localRot;
  185. }
  186. else
  187. {
  188. float invTotal = 1.f / total;
  189. for (const auto& it : _blendStates)
  190. {
  191. float weight = (it.weight * invTotal);
  192. translate += it.localTranslate * weight;
  193. scale.x += it.localScale.x * weight;
  194. scale.y += it.localScale.y * weight;
  195. scale.z += it.localScale.z * weight;
  196. if (!quat.isZero())
  197. {
  198. Quaternion& q = _blendStates[0].localRot;
  199. if (q.x * quat.x + q.y * quat.y + q.z * quat.z + q.w * quat.w < 0)
  200. weight = -weight;
  201. }
  202. quat = Quaternion(it.localRot.x * weight + quat.x, it.localRot.y * weight + quat.y, it.localRot.z * weight + quat.z, it.localRot.w * weight + quat.w);
  203. }
  204. quat.normalize();
  205. }
  206. }
  207. Mat4::createTranslation(translate, &_local);
  208. _local.rotate(quat);
  209. _local.scale(scale);
  210. _blendStates.clear();
  211. }
  212. }
  213. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  214. Skeleton3D::Skeleton3D()
  215. {
  216. }
  217. Skeleton3D::~Skeleton3D()
  218. {
  219. removeAllBones();
  220. }
  221. Skeleton3D* Skeleton3D::create(const std::vector<NodeData*>& skeletondata)
  222. {
  223. auto skeleton = new (std::nothrow) Skeleton3D();
  224. for (const auto& it : skeletondata) {
  225. auto bone = skeleton->createBone3D(*it);
  226. bone->resetPose();
  227. skeleton->_rootBones.pushBack(bone);
  228. }
  229. skeleton->autorelease();
  230. return skeleton;
  231. }
  232. ssize_t Skeleton3D::getBoneCount() const
  233. {
  234. return _bones.size();
  235. }
  236. //get bone
  237. Bone3D* Skeleton3D::getBoneByIndex(unsigned int index) const
  238. {
  239. if (index < static_cast<unsigned int>(_bones.size()))
  240. return _bones.at(index);
  241. return nullptr;
  242. }
  243. Bone3D* Skeleton3D::getBoneByName(const std::string& id) const
  244. {
  245. //search from bones
  246. for (auto it : _bones) {
  247. if (it->getName() == id)
  248. return it;
  249. }
  250. return nullptr;
  251. }
  252. ssize_t Skeleton3D::getRootCount() const
  253. {
  254. return _rootBones.size();
  255. }
  256. Bone3D* Skeleton3D::getRootBone(int index) const
  257. {
  258. return _rootBones.at(index);
  259. }
  260. int Skeleton3D::getBoneIndex(Bone3D* bone) const
  261. {
  262. for (ssize_t i = 0, size = _bones.size(); i < size; ++i) {
  263. if (_bones.at(i) == bone)
  264. return static_cast<int>(i);
  265. }
  266. return -1;
  267. }
  268. //refresh bone world matrix
  269. void Skeleton3D::updateBoneMatrix()
  270. {
  271. for (const auto& it : _rootBones) {
  272. it->setWorldMatDirty(true);
  273. it->updateWorldMat();
  274. }
  275. }
  276. void Skeleton3D::removeAllBones()
  277. {
  278. _bones.clear();
  279. _rootBones.clear();
  280. }
  281. void Skeleton3D::addBone(Bone3D* bone)
  282. {
  283. _bones.pushBack(bone);
  284. }
  285. Bone3D* Skeleton3D::createBone3D(const NodeData& nodedata)
  286. {
  287. auto bone = Bone3D::create(nodedata.id);
  288. for (const auto& it : nodedata.children) {
  289. auto child = createBone3D(*it);
  290. bone->addChildBone(child);
  291. child->_parent = bone;
  292. }
  293. _bones.pushBack(bone);
  294. bone->_oriPose = nodedata.transform;
  295. return bone;
  296. }
  297. NS_CC_END