Fixed models using wrong animation bone matrices

This commit is contained in:
Kah Wei 2023-01-29 20:52:04 +08:00
parent 4a00312f57
commit 8c3703ce04
1 changed files with 31 additions and 9 deletions

View File

@ -410,6 +410,7 @@ namespace SHADE
// Reset Animation Matrix Data // Reset Animation Matrix Data
boneMatrixData.clear(); boneMatrixData.clear();
boneMatrixIndices.clear();
// Add the first identity matrix into the bone matrix data // Add the first identity matrix into the bone matrix data
boneMatrixData.emplace_back(SHMatrix::Identity); // This kills the GPU boneMatrixData.emplace_back(SHMatrix::Identity); // This kills the GPU
@ -423,6 +424,9 @@ namespace SHADE
auto renderable = SHComponentManager::GetComponent<SHRenderable>(rendId); auto renderable = SHComponentManager::GetComponent<SHRenderable>(rendId);
auto mesh = renderable->GetMesh(); auto mesh = renderable->GetMesh();
// Mark start
boneMatrixIndices.emplace_back(static_cast<uint32_t>(boneMatrixData.size()));
// Add matrices // Add matrices
const int BONE_COUNT = static_cast<int>(mesh->BoneCount); const int BONE_COUNT = static_cast<int>(mesh->BoneCount);
int extraMatricesToAdd = BONE_COUNT; int extraMatricesToAdd = BONE_COUNT;
@ -442,11 +446,19 @@ namespace SHADE
{ {
boneMatrixData.insert(boneMatrixData.end(), extraMatricesToAdd, SHMatrix::Identity); boneMatrixData.insert(boneMatrixData.end(), extraMatricesToAdd, SHMatrix::Identity);
} }
// TODO: Recompute boneindexdata if a new animator was added
} }
// Update GPU Buffers // Update GPU Buffers
if (!boneMatrixIndices.empty())
{
const uint32_t BMI_DATA_BYTES = static_cast<uint32_t>(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); rebuildBoneMatrixDescSetBuffer(frameIndex);
} }
@ -586,24 +598,34 @@ namespace SHADE
{ {
SHLOG_WARNING("[SHBatch] Entity with a missing SHRenderable found!"); SHLOG_WARNING("[SHBatch] Entity with a missing SHRenderable found!");
} }
//propsCurrPtr += singleMatPropAlignedSize;
propsCurrPtr += singleMatPropSize; propsCurrPtr += singleMatPropSize;
} }
// Bone Data // Bone Data
if (isAnimated) if (isAnimated)
{ {
// Mark start
boneMatrixIndices.emplace_back(static_cast<uint32_t>(boneMatrixData.size()));
auto animator = SHComponentManager::GetComponent_s<SHAnimatorComponent>(rendId); auto animator = SHComponentManager::GetComponent_s<SHAnimatorComponent>(rendId);
auto mesh = renderable->GetMesh();
const int BONE_COUNT = static_cast<int>(mesh->BoneCount);
int extraMatricesToAdd = BONE_COUNT;
if (animator) if (animator)
{ {
boneMatrixIndices.emplace_back(static_cast<uint32_t>(boneMatrixData.size())); // Add matrices
const auto& BONE_MATRICES = animator->GetBoneMatrices(); const auto& MATRICES = animator->GetBoneMatrices();
boneMatrixData.insert(boneMatrixData.end(), BONE_MATRICES.cbegin(), BONE_MATRICES.cend()); if (MATRICES.size() <= BONE_COUNT)
{
boneMatrixData.insert(boneMatrixData.end(), MATRICES.cbegin(), MATRICES.cend());
extraMatricesToAdd = std::max({0, BONE_COUNT - static_cast<int>(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 boneMatrixData.insert(boneMatrixData.end(), extraMatricesToAdd, SHMatrix::Identity);
boneMatrixIndices.emplace_back(static_cast<uint32_t>(0));
} }
} }
} }