2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "3d/MeshSkin.h"
|
|
|
|
#include "3d/Bundle3D.h"
|
|
|
|
#include "3d/Skeleton3D.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static int PALETTE_ROWS = 3;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
MeshSkin::MeshSkin() : _rootBone(nullptr), _skeleton(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
MeshSkin::~MeshSkin()
|
|
|
|
{
|
|
|
|
removeAllBones();
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_skeleton);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
MeshSkin* MeshSkin::create(Skeleton3D* skeleton,
|
|
|
|
const std::vector<std::string>& boneNames,
|
|
|
|
const std::vector<Mat4>& invBindPose)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto skin = new MeshSkin();
|
2019-11-23 20:27:39 +08:00
|
|
|
skin->_skeleton = skeleton;
|
|
|
|
skeleton->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(boneNames.size() == invBindPose.size(), "bone names' num should equals to invBindPose's num");
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : boneNames)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto bone = skeleton->getBoneByName(it);
|
|
|
|
if (bone)
|
|
|
|
{
|
|
|
|
skin->addSkinBone(bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skin->_invBindPoses = invBindPose;
|
|
|
|
skin->autorelease();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return skin;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t MeshSkin::getBoneCount() const
|
|
|
|
{
|
|
|
|
return _skinBones.size();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// get bone
|
2019-11-23 20:27:39 +08:00
|
|
|
Bone3D* MeshSkin::getBoneByIndex(unsigned int index) const
|
|
|
|
{
|
|
|
|
if (static_cast<int>(index) < _skinBones.size())
|
|
|
|
return _skinBones.at(index);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
Bone3D* MeshSkin::getBoneByName(std::string_view id) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// search from skin bones
|
|
|
|
for (const auto& it : _skinBones)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (it->getName() == id)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeshSkin::getBoneIndex(Bone3D* bone) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0, size = _skinBones.size(); i < size; ++i)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_skinBones.at(i) == bone)
|
|
|
|
return static_cast<int>(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// compute matrix palette used by gpu skin
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec4* MeshSkin::getMatrixPalette()
|
|
|
|
{
|
|
|
|
_matrixPalette.resize(_skinBones.size() * PALETTE_ROWS);
|
|
|
|
int i = 0, paletteIndex = 0;
|
|
|
|
static Mat4 t;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _skinBones)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Mat4::multiply(it->getWorldMat(), _invBindPoses[i++], &t);
|
|
|
|
_matrixPalette[paletteIndex++].set(t.m[0], t.m[4], t.m[8], t.m[12]);
|
|
|
|
_matrixPalette[paletteIndex++].set(t.m[1], t.m[5], t.m[9], t.m[13]);
|
|
|
|
_matrixPalette[paletteIndex++].set(t.m[2], t.m[6], t.m[10], t.m[14]);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return _matrixPalette.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t MeshSkin::getMatrixPaletteSize() const
|
|
|
|
{
|
|
|
|
return _skinBones.size() * PALETTE_ROWS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t MeshSkin::getMatrixPaletteSizeInBytes() const
|
|
|
|
{
|
|
|
|
return _skinBones.size() * PALETTE_ROWS * sizeof(_matrixPalette[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshSkin::removeAllBones()
|
|
|
|
{
|
|
|
|
_skinBones.clear();
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_rootBone);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshSkin::addSkinBone(Bone3D* bone)
|
|
|
|
{
|
|
|
|
_skinBones.pushBack(bone);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bone3D* MeshSkin::getRootBone() const
|
|
|
|
{
|
|
|
|
Bone3D* root = nullptr;
|
|
|
|
if (_skinBones.size())
|
|
|
|
{
|
|
|
|
root = _skinBones.at(0);
|
2021-12-25 10:04:45 +08:00
|
|
|
while (root->getParentBone())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
root = root->getParentBone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Mat4& MeshSkin::getInvBindPose(const Bone3D* bone)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0, size = _skinBones.size(); i < size; ++i)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_skinBones.at(i) == bone)
|
|
|
|
{
|
|
|
|
return _invBindPoses.at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Mat4::IDENTITY;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|