From 393ae9133329592b6221d1e1d5438ccf61e6b3d0 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Sun, 22 Jan 2023 21:47:19 +0800 Subject: [PATCH] Copy Bone offsets into rig bone node data --- src/Libraries/MeshCompiler.cpp | 29 +++++++++++++++++++++++++++++ src/Libraries/MeshCompiler.h | 2 ++ src/Libraries/MeshWriter.cpp | 8 +++++--- src/PseudoMath.h | 13 +++++++++++++ src/Types/RigAsset.h | 2 ++ 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/Libraries/MeshCompiler.cpp b/src/Libraries/MeshCompiler.cpp index cd64052..c518bbe 100644 --- a/src/Libraries/MeshCompiler.cpp +++ b/src/Libraries/MeshCompiler.cpp @@ -179,6 +179,34 @@ namespace SH_COMP } } + void MeshCompiler::BoneOffsetCopy(ModelRef asset) noexcept + { + auto const& boneVec {asset.meshes[0].bones}; + + std::stack 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& anims) noexcept { // Size and read for number of animation clips @@ -317,6 +345,7 @@ namespace SH_COMP LoadFromFile(path, *asset); BuildHeaders(*asset); + BoneOffsetCopy(*asset); MeshWriter::CompileMeshBinary(path, *asset); delete asset; diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index 60920d7..16dc36f 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -39,6 +39,8 @@ namespace SH_COMP static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept; static void BuildHeaders(ModelRef asset) noexcept; + static void BoneOffsetCopy(ModelRef asset) noexcept; + static void BuildArmature(AiNodeConstPtr node, RigData& rig) noexcept; static void ParseAnimations(aiScene const& scene, std::vector& anims) noexcept; diff --git a/src/Libraries/MeshWriter.cpp b/src/Libraries/MeshWriter.cpp index 187830c..c74e00a 100644 --- a/src/Libraries/MeshWriter.cpp +++ b/src/Libraries/MeshWriter.cpp @@ -198,7 +198,7 @@ namespace SH_COMP auto currWriteNode = currPair.first; 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; for (auto i{0}; i < currDataNode->children.size(); ++i) @@ -221,6 +221,10 @@ namespace SH_COMP reinterpret_cast(&data.transform), sizeof(SHMat4) ); + file.write( + reinterpret_cast(&data.offset), + sizeof(SHMat4) + ); } } @@ -248,8 +252,6 @@ namespace SH_COMP sizeof(uint32_t) ); - std::cout << "Write Node: " << node->id << ", " << node->children.size() << std::endl;; - for (auto child : node->children) { nodeQueue.push(child); diff --git a/src/PseudoMath.h b/src/PseudoMath.h index 5f88a59..2a23aba 100644 --- a/src/PseudoMath.h +++ b/src/PseudoMath.h @@ -21,5 +21,18 @@ namespace SH_COMP struct SHMat4 { 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; + } }; } \ No newline at end of file diff --git a/src/Types/RigAsset.h b/src/Types/RigAsset.h index d7e42e0..9f7a321 100644 --- a/src/Types/RigAsset.h +++ b/src/Types/RigAsset.h @@ -21,6 +21,7 @@ namespace SH_COMP std::string name; SHMat4 transform; + SHMat4 offset; std::vector children; }; @@ -28,6 +29,7 @@ namespace SH_COMP { std::string name; SHMat4 transform; + SHMat4 offset; }; struct RigWriteNode