Copy Bone offsets into rig bone node data

This commit is contained in:
Xiao Qi 2023-01-22 21:47:19 +08:00
parent 5670f06046
commit 393ae91333
5 changed files with 51 additions and 3 deletions

View File

@ -179,6 +179,34 @@ namespace SH_COMP
} }
} }
void MeshCompiler::BoneOffsetCopy(ModelRef asset) noexcept
{
auto const& boneVec {asset.meshes[0].bones};
std::stack<RigNodeData*> nodeStack;
nodeStack.push(asset.rig.root);
while(!nodeStack.empty())
{
auto& node = *nodeStack.top();
nodeStack.pop();
for (auto const& bone : boneVec)
{
if (node.name == bone.name)
{
node.offset = bone.offset;
break;
}
}
for (auto const& child : node.children)
{
nodeStack.push(child);
}
}
}
void MeshCompiler::ParseAnimations(aiScene const& scene, std::vector<AnimData>& anims) noexcept void MeshCompiler::ParseAnimations(aiScene const& scene, std::vector<AnimData>& anims) noexcept
{ {
// Size and read for number of animation clips // Size and read for number of animation clips
@ -317,6 +345,7 @@ namespace SH_COMP
LoadFromFile(path, *asset); LoadFromFile(path, *asset);
BuildHeaders(*asset); BuildHeaders(*asset);
BoneOffsetCopy(*asset);
MeshWriter::CompileMeshBinary(path, *asset); MeshWriter::CompileMeshBinary(path, *asset);
delete asset; delete asset;

View File

@ -39,6 +39,8 @@ namespace SH_COMP
static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept; static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept;
static void BuildHeaders(ModelRef asset) noexcept; static void BuildHeaders(ModelRef asset) noexcept;
static void BoneOffsetCopy(ModelRef asset) noexcept;
static void BuildArmature(AiNodeConstPtr node, RigData& rig) noexcept; static void BuildArmature(AiNodeConstPtr node, RigData& rig) noexcept;
static void ParseAnimations(aiScene const& scene, std::vector<AnimData>& anims) noexcept; static void ParseAnimations(aiScene const& scene, std::vector<AnimData>& anims) noexcept;

View File

@ -198,7 +198,7 @@ namespace SH_COMP
auto currWriteNode = currPair.first; auto currWriteNode = currPair.first;
auto currDataNode = currPair.second; auto currDataNode = currPair.second;
dataToWrite.emplace_back(currDataNode->name, currDataNode->transform); dataToWrite.emplace_back(currDataNode->name, currDataNode->transform, currDataNode->offset);
uint32_t idCounter = dataToWrite.size() + currDataNode->children.size() - 1; uint32_t idCounter = dataToWrite.size() + currDataNode->children.size() - 1;
for (auto i{0}; i < currDataNode->children.size(); ++i) for (auto i{0}; i < currDataNode->children.size(); ++i)
@ -221,6 +221,10 @@ namespace SH_COMP
reinterpret_cast<char const*>(&data.transform), reinterpret_cast<char const*>(&data.transform),
sizeof(SHMat4) sizeof(SHMat4)
); );
file.write(
reinterpret_cast<char const*>(&data.offset),
sizeof(SHMat4)
);
} }
} }
@ -248,8 +252,6 @@ namespace SH_COMP
sizeof(uint32_t) sizeof(uint32_t)
); );
std::cout << "Write Node: " << node->id << ", " << node->children.size() << std::endl;;
for (auto child : node->children) for (auto child : node->children)
{ {
nodeQueue.push(child); nodeQueue.push(child);

View File

@ -21,5 +21,18 @@ namespace SH_COMP
struct SHMat4 struct SHMat4
{ {
float data[16]; float data[16];
bool operator==(SHMat4 const& rhs) const
{
for (auto i {0}; i < 16; ++i)
{
if (data[i] != rhs.data[i])
{
return false;
}
}
return true;
}
}; };
} }

View File

@ -21,6 +21,7 @@ namespace SH_COMP
std::string name; std::string name;
SHMat4 transform; SHMat4 transform;
SHMat4 offset;
std::vector<RigNodeData*> children; std::vector<RigNodeData*> children;
}; };
@ -28,6 +29,7 @@ namespace SH_COMP
{ {
std::string name; std::string name;
SHMat4 transform; SHMat4 transform;
SHMat4 offset;
}; };
struct RigWriteNode struct RigWriteNode