CCMeshSkin.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "3d/CCMeshSkin.h"
  22. #include "3d/CCBundle3D.h"
  23. #include "3d/CCSkeleton3D.h"
  24. NS_CC_BEGIN
  25. static int PALETTE_ROWS = 3;
  26. MeshSkin::MeshSkin()
  27. : _rootBone(nullptr)
  28. , _skeleton(nullptr)
  29. , _matrixPalette(nullptr)
  30. {
  31. }
  32. MeshSkin::~MeshSkin()
  33. {
  34. removeAllBones();
  35. CC_SAFE_RELEASE(_skeleton);
  36. }
  37. MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::vector<std::string>& boneNames, const std::vector<Mat4>& invBindPose)
  38. {
  39. auto skin = new (std::nothrow) MeshSkin();
  40. skin->_skeleton = skeleton;
  41. skeleton->retain();
  42. CCASSERT(boneNames.size() == invBindPose.size(), "bone names' num should equals to invBindPose's num");
  43. for (const auto& it : boneNames) {
  44. auto bone = skeleton->getBoneByName(it);
  45. if (bone)
  46. {
  47. skin->addSkinBone(bone);
  48. }
  49. }
  50. skin->_invBindPoses = invBindPose;
  51. skin->autorelease();
  52. return skin;
  53. }
  54. ssize_t MeshSkin::getBoneCount() const
  55. {
  56. return _skinBones.size();
  57. }
  58. //get bone
  59. Bone3D* MeshSkin::getBoneByIndex(unsigned int index) const
  60. {
  61. if (static_cast<int>(index) < _skinBones.size())
  62. return _skinBones.at(index);
  63. return nullptr;
  64. }
  65. Bone3D* MeshSkin::getBoneByName(const std::string& id) const
  66. {
  67. //search from skin bones
  68. for (const auto& it : _skinBones) {
  69. if (it->getName() == id)
  70. return it;
  71. }
  72. return nullptr;
  73. }
  74. int MeshSkin::getBoneIndex(Bone3D* bone) const
  75. {
  76. for (ssize_t i = 0, size = _skinBones.size(); i < size; ++i) {
  77. if (_skinBones.at(i) == bone)
  78. return static_cast<int>(i);
  79. }
  80. return -1;
  81. }
  82. //compute matrix palette used by gpu skin
  83. Vec4* MeshSkin::getMatrixPalette()
  84. {
  85. if (_matrixPalette == nullptr)
  86. {
  87. _matrixPalette = new (std::nothrow) Vec4[_skinBones.size() * PALETTE_ROWS];
  88. }
  89. int i = 0, paletteIndex = 0;
  90. static Mat4 t;
  91. for (auto it : _skinBones )
  92. {
  93. Mat4::multiply(it->getWorldMat(), _invBindPoses[i++], &t);
  94. _matrixPalette[paletteIndex++].set(t.m[0], t.m[4], t.m[8], t.m[12]);
  95. _matrixPalette[paletteIndex++].set(t.m[1], t.m[5], t.m[9], t.m[13]);
  96. _matrixPalette[paletteIndex++].set(t.m[2], t.m[6], t.m[10], t.m[14]);
  97. }
  98. return _matrixPalette;
  99. }
  100. ssize_t MeshSkin::getMatrixPaletteSize() const
  101. {
  102. return _skinBones.size() * PALETTE_ROWS;
  103. }
  104. void MeshSkin::removeAllBones()
  105. {
  106. _skinBones.clear();
  107. CC_SAFE_DELETE_ARRAY(_matrixPalette);
  108. CC_SAFE_RELEASE(_rootBone);
  109. }
  110. void MeshSkin::addSkinBone(Bone3D* bone)
  111. {
  112. _skinBones.pushBack(bone);
  113. }
  114. Bone3D* MeshSkin::getRootBone() const
  115. {
  116. Bone3D* root = nullptr;
  117. if (_skinBones.size())
  118. {
  119. root = _skinBones.at(0);
  120. while (root->getParentBone()) {
  121. root = root->getParentBone();
  122. }
  123. }
  124. return root;
  125. }
  126. const Mat4& MeshSkin::getInvBindPose(const Bone3D* bone)
  127. {
  128. for (ssize_t i = 0, size = _skinBones.size(); i < size; ++i) {
  129. if (_skinBones.at(i) == bone)
  130. {
  131. return _invBindPoses.at(i);
  132. }
  133. }
  134. return Mat4::IDENTITY;
  135. }
  136. NS_CC_END