From 8c3703ce04cf14b72e6a44ed113be5e4b1d5ad1e Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 29 Jan 2023 20:52:04 +0800 Subject: [PATCH] Fixed models using wrong animation bone matrices --- .../Graphics/MiddleEnd/Batching/SHBatch.cpp | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 297a12a6..c35223aa 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -410,6 +410,7 @@ namespace SHADE // Reset Animation Matrix Data boneMatrixData.clear(); + boneMatrixIndices.clear(); // Add the first identity matrix into the bone matrix data boneMatrixData.emplace_back(SHMatrix::Identity); // This kills the GPU @@ -423,6 +424,9 @@ namespace SHADE auto renderable = SHComponentManager::GetComponent(rendId); auto mesh = renderable->GetMesh(); + // Mark start + boneMatrixIndices.emplace_back(static_cast(boneMatrixData.size())); + // Add matrices const int BONE_COUNT = static_cast(mesh->BoneCount); int extraMatricesToAdd = BONE_COUNT; @@ -442,11 +446,19 @@ namespace SHADE { boneMatrixData.insert(boneMatrixData.end(), extraMatricesToAdd, SHMatrix::Identity); } - - // TODO: Recompute boneindexdata if a new animator was added } // Update GPU Buffers + if (!boneMatrixIndices.empty()) + { + const uint32_t BMI_DATA_BYTES = static_cast(boneMatrixIndices.size() * sizeof(uint32_t)); + SHVkUtil::EnsureBufferAndCopyHostVisibleData + ( + device, boneMatrixFirstIndexBuffer[frameIndex], boneMatrixIndices.data(), BMI_DATA_BYTES, + vk::BufferUsageFlagBits::eVertexBuffer, + "Batch Instance Bone Matrix First Index Buffer" + ); + } rebuildBoneMatrixDescSetBuffer(frameIndex); } @@ -586,24 +598,34 @@ namespace SHADE { SHLOG_WARNING("[SHBatch] Entity with a missing SHRenderable found!"); } - //propsCurrPtr += singleMatPropAlignedSize; propsCurrPtr += singleMatPropSize; } // Bone Data if (isAnimated) { + // Mark start + boneMatrixIndices.emplace_back(static_cast(boneMatrixData.size())); + auto animator = SHComponentManager::GetComponent_s(rendId); + auto mesh = renderable->GetMesh(); + const int BONE_COUNT = static_cast(mesh->BoneCount); + int extraMatricesToAdd = BONE_COUNT; if (animator) { - boneMatrixIndices.emplace_back(static_cast(boneMatrixData.size())); - const auto& BONE_MATRICES = animator->GetBoneMatrices(); - boneMatrixData.insert(boneMatrixData.end(), BONE_MATRICES.cbegin(), BONE_MATRICES.cend()); + // Add matrices + const auto& MATRICES = animator->GetBoneMatrices(); + if (MATRICES.size() <= BONE_COUNT) + { + boneMatrixData.insert(boneMatrixData.end(), MATRICES.cbegin(), MATRICES.cend()); + extraMatricesToAdd = std::max({0, BONE_COUNT - static_cast(MATRICES.size())}); + } } - else + + // If we need to patch up with more matrices, add it + if (extraMatricesToAdd > 0) { - // Take the first matrix which is always identity - boneMatrixIndices.emplace_back(static_cast(0)); + boneMatrixData.insert(boneMatrixData.end(), extraMatricesToAdd, SHMatrix::Identity); } } }