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
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<SHRenderable>(rendId);
auto mesh = renderable->GetMesh();
// Mark start
boneMatrixIndices.emplace_back(static_cast<uint32_t>(boneMatrixData.size()));
// Add matrices
const int BONE_COUNT = static_cast<int>(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<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);
}
@ -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<uint32_t>(boneMatrixData.size()));
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)
{
boneMatrixIndices.emplace_back(static_cast<uint32_t>(boneMatrixData.size()));
const auto& BONE_MATRICES = animator->GetBoneMatrices();
boneMatrixData.insert(boneMatrixData.end(), BONE_MATRICES.cbegin(), BONE_MATRICES.cend());
}
else
// Add matrices
const auto& MATRICES = animator->GetBoneMatrices();
if (MATRICES.size() <= BONE_COUNT)
{
// Take the first matrix which is always identity
boneMatrixIndices.emplace_back(static_cast<uint32_t>(0));
boneMatrixData.insert(boneMatrixData.end(), MATRICES.cbegin(), MATRICES.cend());
extraMatricesToAdd = std::max({0, BONE_COUNT - static_cast<int>(MATRICES.size())});
}
}
// If we need to patch up with more matrices, add it
if (extraMatricesToAdd > 0)
{
boneMatrixData.insert(boneMatrixData.end(), extraMatricesToAdd, SHMatrix::Identity);
}
}
}