From 2d1987e14bf481b44a6f3e005d3b6f25e5ac162c Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 29 Jan 2023 19:27:58 +0800 Subject: [PATCH] Added handling for rendering objects using an animated shader but without an animator component or an attached rig --- .../Assets/Asset Types/Models/SHMeshAsset.h | 3 +-- .../Libraries/Loaders/SHModelLoader.cpp | 2 +- .../Graphics/MiddleEnd/Batching/SHBatch.cpp | 21 ++++++++++++++++--- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 4 ++-- .../MiddleEnd/Interface/SHGraphicsSystem.h | 2 +- .../MiddleEnd/Interface/SHMeshLibrary.cpp | 5 ++++- .../MiddleEnd/Interface/SHMeshLibrary.h | 5 ++++- .../MiddleEnd/Meshes/SHPrimitiveGenerator.cpp | 3 ++- .../src/Resource/SHResourceManager.hpp | 3 ++- 9 files changed, 35 insertions(+), 13 deletions(-) diff --git a/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h b/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h index d8afc217..18272d02 100644 --- a/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h @@ -58,7 +58,6 @@ namespace SHADE std::vector Indices; std::vector VertexBoneIndices; std::vector VertexBoneWeights; - - uint32_t boneCount; + uint32_t BoneCount; }; } diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp index ce6d0f07..52a3a925 100644 --- a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp @@ -221,7 +221,7 @@ namespace SHADE data.VertexNormals.resize(header.vertexCount); data.VertexTexCoords.resize(header.vertexCount); data.Indices.resize(header.indexCount); - data.boneCount = header.boneCount; + data.BoneCount = header.boneCount; file.read(data.name.data(), header.charCount); file.read(reinterpret_cast(data.VertexPositions.data()), vertexVec3Byte); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 8332f170..b30933be 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -418,16 +418,31 @@ namespace SHADE for (auto& subBatch : subBatches) for (auto rendId : subBatch.Renderables) { + // Get resources auto animator = SHComponentManager::GetComponent_s(rendId); + auto renderable = SHComponentManager::GetComponent(rendId); + auto mesh = renderable->GetMesh(); + + // Add matrices + const int BONE_COUNT = static_cast(mesh->BoneCount); + int extraMatricesToAdd = BONE_COUNT; if (animator) { + // Add matrices const auto& MATRICES = animator->GetBoneMatrices(); - boneMatrixData.insert(boneMatrixData.end(), MATRICES.cbegin(), MATRICES.cend()); + 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) { - // TODO: Populate with empty matrices + boneMatrixData.insert(boneMatrixData.end(), extraMatricesToAdd, SHMatrix::Identity); } + // We don't have to account for missing animators or reset indices as the renderable list are not updated at this point } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index a2be7405..7075024e 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -797,9 +797,9 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Mesh Registration Functions */ /*---------------------------------------------------------------------------------*/ - SHADE::Handle SHGraphicsSystem::AddMesh(uint32_t vertexCount, const SHMesh::VertexPosition* const positions, const SHMesh::VertexTexCoord* const texCoords, const SHMesh::VertexTangent* const tangents, const SHMesh::VertexNormal* const normals, uint32_t indexCount, const SHMesh::Index* const indices, const SHMesh::VertexBoneIndices* const boneIndices, const SHMesh::VertexWeights* const boneWeights) + SHADE::Handle SHGraphicsSystem::AddMesh(uint32_t vertexCount, const SHMesh::VertexPosition* const positions, const SHMesh::VertexTexCoord* const texCoords, const SHMesh::VertexTangent* const tangents, const SHMesh::VertexNormal* const normals, uint32_t indexCount, const SHMesh::Index* const indices, const SHMesh::VertexBoneIndices* const boneIndices, const SHMesh::VertexWeights* const boneWeights, uint32_t boneCount) { - return meshLibrary.AddMesh(vertexCount, positions, texCoords, tangents, normals, boneIndices, boneWeights, indexCount, indices); + return meshLibrary.AddMesh(vertexCount, positions, texCoords, tangents, normals, boneIndices, boneWeights, indexCount, indices, boneCount); } void SHGraphicsSystem::RemoveMesh(Handle mesh) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index c2151b21..2c548172 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -229,7 +229,7 @@ namespace SHADE */ /*******************************************************************************/ - Handle AddMesh(uint32_t vertexCount, const SHMesh::VertexPosition* const positions, const SHMesh::VertexTexCoord* const texCoords, const SHMesh::VertexTangent* const tangents, const SHMesh::VertexNormal* const normals, uint32_t indexCount, const SHMesh::Index* const indices, const SHMesh::VertexBoneIndices* const boneIndices = nullptr, const SHMesh::VertexWeights* const boneWeights = nullptr); + Handle AddMesh(uint32_t vertexCount, const SHMesh::VertexPosition* const positions, const SHMesh::VertexTexCoord* const texCoords, const SHMesh::VertexTangent* const tangents, const SHMesh::VertexNormal* const normals, uint32_t indexCount, const SHMesh::Index* const indices, const SHMesh::VertexBoneIndices* const boneIndices = nullptr, const SHMesh::VertexWeights* const boneWeights = nullptr, uint32_t boneCount = 0); /*******************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.cpp index 658cc5fe..2aedad61 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.cpp @@ -28,7 +28,8 @@ namespace SHADE const SHMesh::VertexNormal* const normals, const SHMesh::VertexBoneIndices* const boneIndices, const SHMesh::VertexWeights* const boneWeights, - uint32_t indexCount, const SHMesh::Index* const indices) + uint32_t indexCount, const SHMesh::Index* const indices, + uint32_t boneCount) { isDirty = true; @@ -44,6 +45,7 @@ namespace SHADE boneWeights, indexCount, indices, + boneCount, handle }); return handle; @@ -140,6 +142,7 @@ namespace SHADE .VertexCount = static_cast(addJob.VertexCount), .FirstIndex = static_cast(indexStorage.size()), .IndexCount = static_cast(addJob.IndexCount), + .BoneCount = addJob.BoneCount }; // Copy into storage diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h index 39b669b8..c953c9e2 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h @@ -61,6 +61,7 @@ namespace SHADE uint32_t VertexCount; uint32_t FirstIndex; uint32_t IndexCount; + uint32_t BoneCount; }; /***********************************************************************************/ /*! @@ -115,7 +116,8 @@ namespace SHADE const SHMesh::VertexNormal* const normals, const SHMesh::VertexBoneIndices* const boneIndices, const SHMesh::VertexWeights* const boneWeights, - uint32_t indexCount, const SHMesh::Index* const indices); + uint32_t indexCount, const SHMesh::Index* const indices, + uint32_t boneCount); /*******************************************************************************/ /*! @@ -173,6 +175,7 @@ namespace SHADE const SHMesh::VertexWeights* VertexBoneWeights = nullptr; uint32_t IndexCount = 0; const SHMesh::Index* Indices = nullptr; + uint32_t BoneCount = 0; Handle Handle; }; /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index a9d83560..7e5d8e28 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp @@ -407,7 +407,8 @@ namespace SHADE nullptr, nullptr, static_cast(meshData.Indices.size()), - meshData.Indices.data() + meshData.Indices.data(), + 0 ); } diff --git a/SHADE_Engine/src/Resource/SHResourceManager.hpp b/SHADE_Engine/src/Resource/SHResourceManager.hpp index baf915e0..8b0af04f 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.hpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.hpp @@ -218,7 +218,8 @@ namespace SHADE assetData.Indices.size(), assetData.Indices.data(), assetData.VertexBoneIndices.empty() ? nullptr : assetData.VertexBoneIndices.data(), - assetData.VertexBoneWeights.empty() ? nullptr : assetData.VertexBoneWeights.data() + assetData.VertexBoneWeights.empty() ? nullptr : assetData.VertexBoneWeights.data(), + assetData.BoneCount ); } // Textures