Animation WIP

This commit is contained in:
Xiao Qi 2023-02-27 00:56:41 +08:00
parent 52fc8132d9
commit 6615f0c2b8
4 changed files with 51 additions and 24 deletions

View File

@ -45,7 +45,10 @@ namespace SH_COMP
static BufferData buffer; static BufferData buffer;
static inline void LoadFromFile(AssetPath path, ModelRef asset) noexcept; static inline void LoadFromFile(AssetPath path, ModelRef asset) noexcept;
static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept;
static inline void ProcessModel(ModelData const& model, ModelRef asset) noexcept;
static inline void ProcessAnimations(ModelData const& model, ModelRef asset) noexcept;
static inline void BuildHeaders(ModelRef asset) noexcept; static inline void BuildHeaders(ModelRef asset) noexcept;
template<typename T> template<typename T>

View File

@ -36,7 +36,7 @@ namespace SH_COMP
BufferViewReference MeshCompiler::bufferViews{ nullptr }; BufferViewReference MeshCompiler::bufferViews{ nullptr };
BufferData MeshCompiler::buffer{ nullptr }; BufferData MeshCompiler::buffer{ nullptr };
void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept inline void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept
{ {
ModelData model; ModelData model;
tinygltf::TinyGLTF loader; tinygltf::TinyGLTF loader;
@ -57,9 +57,10 @@ namespace SH_COMP
} }
ProcessModel(model, asset); ProcessModel(model, asset);
ProcessAnimations(model, asset);
} }
void MeshCompiler::ProcessModel(ModelData const& data, ModelRef asset) noexcept inline void MeshCompiler::ProcessModel(ModelData const& data, ModelRef asset) noexcept
{ {
#if 0 #if 0
for (auto i {0}; i < 2; ++i) for (auto i {0}; i < 2; ++i)
@ -86,22 +87,18 @@ namespace SH_COMP
FetchData(primitive.attributes.at(ATT_POSITION.data()), meshIn.vertexPosition); FetchData(primitive.attributes.at(ATT_POSITION.data()), meshIn.vertexPosition);
FetchData(primitive.attributes.at(ATT_NORMAL.data()), meshIn.vertexNormal); FetchData(primitive.attributes.at(ATT_NORMAL.data()), meshIn.vertexNormal);
FetchData(primitive.attributes.at(ATT_TEXCOORD.data()), meshIn.texCoords); FetchData(primitive.attributes.at(ATT_TEXCOORD.data()), meshIn.texCoords);
FetchData(primitive.indices, meshIn.indices);
std::vector<unsigned short> indices_ushort;
FetchData(primitive.indices, indices_ushort);
meshIn.indices.resize(indices_ushort.size());
std::ranges::copy(indices_ushort, meshIn.indices.begin());
std::vector<SHVec4> intermediate; std::vector<SHVec4> intermediate;
FetchData(primitive.attributes.at(ATT_TANGENT.data()), intermediate); FetchData(primitive.attributes.at(ATT_TANGENT.data()), intermediate);
meshIn.vertexTangent.resize(intermediate.size()); meshIn.vertexTangent.resize(intermediate.size());
std::ranges::transform( std::ranges::transform(
intermediate, intermediate,
meshIn.vertexTangent.begin(), meshIn.vertexTangent.begin(),
[](auto const& inTan) [](auto const& inTan)
{ {
return SHVec3{ inTan.x, inTan.y, inTan.z }; return SHVec3{ inTan.x, inTan.y, inTan.z };
} }
); );
} }
catch (std::out_of_range e) catch (std::out_of_range e)
@ -120,9 +117,10 @@ namespace SH_COMP
auto const& view = (*bufferViews)[accessor.bufferView]; auto const& view = (*bufferViews)[accessor.bufferView];
auto const typeIdentifier{ static_cast<ACCESSOR_COMPONENT_TYPE>(accessor.componentType) }; auto const typeIdentifier{ static_cast<ACCESSOR_COMPONENT_TYPE>(accessor.componentType) };
auto const sizeIdentifier{ SizeOfType(typeIdentifier) }; auto const sizeIdentifier{ SizeOfType(typeIdentifier) };
auto const componentCount{ CountOfType(accessor.type) }; auto const componentCount{ CountOfType(static_cast<ACCESSOR_DATA_TYPE>(accessor.type))};
auto const totalStrideBytes{ sizeIdentifier * componentCount };
dst.resize(accessor.count); dst.resize(accessor.count);
if (sizeof(T) == sizeIdentifier * componentCount) if (sizeof(T) == totalStrideBytes)
{ {
std::memcpy( std::memcpy(
dst.data(), dst.data(),
@ -140,18 +138,27 @@ namespace SH_COMP
); );
auto srcPtr{ tempData.data() }; auto srcPtr{ tempData.data() };
auto dstPtr{ dst.data() }; T* dstPtr{ dst.data() };
size_t index{ 0 }; size_t index{ 0 };
for (auto i{0}; i < accessor.count; ++i, ++index) for (auto i{0}; i < accessor.count; ++i, ++index)
{ {
std::memcpy( auto srcCompPtr{ srcPtr };
dstPtr, auto dstCompPtr{ reinterpret_cast<IndexType*>(dstPtr)};
srcPtr, for (auto j{0}; j < componentCount; ++j)
sizeIdentifier {
); std::memcpy(
dstPtr,
srcPtr,
sizeIdentifier
);
srcPtr += sizeIdentifier; srcCompPtr += sizeIdentifier;
dstPtr += sizeof(T); ++dstCompPtr;
}
srcPtr += totalStrideBytes;
++dstPtr;
} }
} }
@ -182,4 +189,18 @@ namespace SH_COMP
delete asset; delete asset;
} }
inline void MeshCompiler::ProcessAnimations(ModelData const& model, ModelRef asset) noexcept
{
asset.anims.resize(model.animations.size());
for (auto i {0}; i < model.animations.size(); ++i)
{
auto& animData{ model.animations[i] };
auto& anim{ asset.anims[i] };
anim.name = animData.name;
}
}
} }

View File

@ -35,4 +35,7 @@ namespace SH_COMP
return true; return true;
} }
}; };
using IndexType = uint32_t;
} }

View File

@ -41,7 +41,7 @@ namespace SH_COMP
std::vector<SHVec3> vertexTangent; std::vector<SHVec3> vertexTangent;
std::vector<SHVec3> vertexNormal; std::vector<SHVec3> vertexNormal;
std::vector<SHVec2> texCoords; std::vector<SHVec2> texCoords;
std::vector<uint32_t> indices; std::vector<IndexType> indices;
std::vector<MeshBoneInfo> bonesInfo; std::vector<MeshBoneInfo> bonesInfo;
std::vector<MeshBone> bones; std::vector<MeshBone> bones;
}; };