diff --git a/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h b/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h index c772f717..dee88cd0 100644 --- a/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/Models/SHMeshAsset.h @@ -18,6 +18,13 @@ namespace SHADE { + constexpr int BONE_INDEX_ALIGHTMENT = 4; + + struct IndexUInt4 + { + std::array indices; + }; + struct SHMeshDataHeader { uint32_t vertexCount; @@ -48,12 +55,12 @@ namespace SHADE struct SH_API SHMeshAsset : SHAssetData { std::string name; - std::vector VertexPositions; - std::vector VertexTangents; - std::vector VertexNormals; - std::vector VertexTexCoords; - std::vector Indices; - std::vector VertexBoneIndices; - std::vector VertexBoneWeights; + std::vector VertexPositions; + std::vector VertexTangents; + std::vector VertexNormals; + std::vector VertexTexCoords; + std::vector Indices; + std::vector VertexBoneIndices; + std::vector VertexBoneWeights; }; } diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp index b90205bc..649e6ffc 100644 --- a/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHModelLoader.cpp @@ -222,33 +222,50 @@ namespace SHADE file.read(reinterpret_cast(data.VertexNormals.data()), vertexVec3Byte); file.read(reinterpret_cast(data.VertexTexCoords.data()), vertexVec2Byte); file.read(reinterpret_cast(data.Indices.data()), sizeof(uint32_t) * header.indexCount); - - std::vector boneInfos(header.boneCount); - std::vector bones(header.boneCount); - file.read(reinterpret_cast(boneInfos.data()), sizeof(MeshBoneInfo) * header.boneCount); - - for (auto i{ 0 }; i < header.boneCount; ++i) + if (header.boneCount) { - auto& bone = bones[i]; - auto const& info = boneInfos[i]; + std::vector boneInfos(header.boneCount); + std::vector bones(header.boneCount); - bone.name.resize(info.charCount); - file.read(bone.name.data(), info.charCount); - file.read(reinterpret_cast(&bone.offset), sizeof(SHMatrix)); + file.read(reinterpret_cast(boneInfos.data()), sizeof(MeshBoneInfo) * header.boneCount); - uint32_t weightCount; - file.read(reinterpret_cast(&weightCount), sizeof(uint32_t)); - bone.weights.resize(weightCount); - file.read(reinterpret_cast(bone.weights.data()), sizeof(BoneWeight) * weightCount); - } + for (auto i{ 0 }; i < header.boneCount; ++i) + { + auto& bone = bones[i]; + auto const& info = boneInfos[i]; - data.VertexBoneIndices.resize(header.vertexCount); - data.VertexBoneWeights.resize(header.vertexCount); + bone.name.resize(info.charCount); + file.read(bone.name.data(), info.charCount); + file.read(reinterpret_cast(&bone.offset), sizeof(SHMatrix)); - for (auto const& bone : bones) - { - + uint32_t weightCount; + file.read(reinterpret_cast(&weightCount), sizeof(uint32_t)); + bone.weights.resize(weightCount); + file.read(reinterpret_cast(bone.weights.data()), sizeof(BoneWeight) * weightCount); + } + + data.VertexBoneIndices.resize(header.vertexCount); + data.VertexBoneWeights.resize(header.vertexCount); + + for (uint32_t boneIndex{0}; boneIndex < bones.size(); ++boneIndex) + { + auto const& bone = bones[boneIndex]; + for (auto const& weight : bone.weights) + { + auto& boneIndices = data.VertexBoneIndices[weight.index]; + auto& boneWeight = data.VertexBoneWeights[weight.index]; + + for (auto j{0}; j < BONE_INDEX_ALIGHTMENT; ++j) + { + if (boneWeight[j] == 0.f) + { + boneIndices.indices[j] = boneIndex; + boneWeight[j] = weight.weight; + } + } + } + } } meshes[i] = &data;