Separated asset data class

Parsing of animation clips, frames, channels
This commit is contained in:
Xiao Qi 2022-11-16 10:41:27 +08:00
parent 66baaf374d
commit 03c17ca5d4
7 changed files with 181 additions and 76 deletions

View File

@ -46,26 +46,26 @@ namespace SH_COMP
}
}
void MeshCompiler::ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept
{
if (scene.HasAnimations())
{
std::vector<AnimationAsset> anims(scene.mNumAnimations);
for (auto i{ 0 }; i < scene.mNumAnimations; ++i)
{
auto const& anim{ *scene.mAnimations[i] };
//void MeshCompiler::ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept
//{
// if (scene.HasAnimations())
// {
// std::vector<AnimationAsset> anims(scene.mNumAnimations);
// for (auto i{ 0 }; i < scene.mNumAnimations; ++i)
// {
// auto const& anim{ *scene.mAnimations[i] };
anims[i].name = anim.mName.C_Str();
// anims[i].name = anim.mName.C_Str();
anims[i].duration = anim.mDuration;
anims[i].ticksPerSecond = anim.mTicksPerSecond;
// anims[i].duration = anim.mDuration;
// anims[i].ticksPerSecond = anim.mTicksPerSecond;
std::copy_n(anim.mChannels, anim.mNumChannels, anims[i].nodeChannels.data());
std::copy_n(anim.mMeshChannels, anim.mNumMeshChannels, anims[i].meshChannels.data());
std::copy_n(anim.mMorphMeshChannels, anim.mNumMorphMeshChannels, anims[i].morphMeshChannels.data());
}
}
}
// std::copy_n(anim.mChannels, anim.mNumChannels, anims[i].nodeChannels.data());
// std::copy_n(anim.mMeshChannels, anim.mNumMeshChannels, anims[i].meshChannels.data());
// std::copy_n(anim.mMorphMeshChannels, anim.mNumMorphMeshChannels, anims[i].morphMeshChannels.data());
// }
// }
//}
void MeshCompiler::GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept
{
@ -226,7 +226,9 @@ namespace SH_COMP
return;
}
//ExtractAnimations(*scene, anims);
std::vector<AnimationAsset> anims;
ParseAnimations(*scene, anims);
ProcessNode(*scene->mRootNode, *scene, asset.meshes, asset.rig.root);
@ -285,6 +287,71 @@ namespace SH_COMP
}
}
void MeshCompiler::ParseAnimations(aiScene const& scene, std::vector<AnimationAsset>& anims) noexcept
{
// Size and read for number of animation clips
anims.resize(scene.mNumAnimations);
for (auto i {0}; i < scene.mNumAnimations; ++i)
{
auto const& animData = *scene.mAnimations[i];
auto& anim = anims[i];
anim.name = animData.mName.C_Str();
anim.duration = animData.mDuration;
anim.ticksPerSecond = animData.mTicksPerSecond;
// Size and read for number of animation frames
anim.nodeChannels.resize(animData.mNumChannels);
for (auto j {0}; j < animData.mNumChannels; ++j)
{
auto const& channelData = *animData.mChannels[j];
auto& node = anim.nodeChannels[j];
node.name = channelData.mNodeName.C_Str();
// Position Keys
node.positionKeys.resize(channelData.mNumPositionKeys);
for (auto k{0}; k < channelData.mNumPositionKeys; ++k)
{
auto const& posKeyData = channelData.mPositionKeys[k];
auto& posKey = node.positionKeys[k];
posKey.time = posKeyData.mTime;
posKey.value.x = posKeyData.mValue.x;
posKey.value.y = posKeyData.mValue.y;
posKey.value.z = posKeyData.mValue.z;
}
// Rotation Keys
node.rotationKeys.resize(channelData.mNumRotationKeys);
for (auto k{0}; k < channelData.mNumRotationKeys; ++k)
{
auto const& posKeyData = channelData.mRotationKeys[k];
auto& posKey = node.rotationKeys[k];
posKey.time = posKeyData.mTime;
posKey.value.x = posKeyData.mValue.x;
posKey.value.y = posKeyData.mValue.y;
posKey.value.z = posKeyData.mValue.z;
posKey.value.w = posKeyData.mValue.w;
}
// Scale Keys
node.scaleKeys.resize(channelData.mNumScalingKeys);
for (auto k{0}; k < channelData.mNumScalingKeys; ++k)
{
auto const& posKeyData = channelData.mScalingKeys[k];
auto& posKey = node.scaleKeys[k];
posKey.time = posKeyData.mTime;
posKey.value.x = posKeyData.mValue.x;
posKey.value.y = posKeyData.mValue.y;
posKey.value.z = posKeyData.mValue.z;
}
}
}
}
void MeshCompiler::LoadAndCompile(AssetPath path) noexcept
{
auto const asset = new ModelAsset();

View File

@ -17,7 +17,7 @@
#include <vector>
#include "Types/AnimationAsset.h"
#include "Types/MeshAsset.h"
#include "Types/ModelAsset.h"
#include "AssetMacros.h"
namespace SH_COMP
@ -31,7 +31,7 @@ namespace SH_COMP
static Assimp::Importer aiImporter;
static void ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigNode*& root) noexcept;
static void ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept;
//static void ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept;
static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept;
static void BuildHeaders(ModelAsset& asset) noexcept;
@ -43,6 +43,8 @@ namespace SH_COMP
static void BuildArmature(aiNode const& node, RigNode*& root) noexcept;
static void CopyNode(aiNode const& source, RigNode* parent) noexcept;
static void ParseAnimations(aiScene const& scene, std::vector<AnimationAsset>& anims) noexcept;
public:
static void LoadAndCompile(AssetPath path) noexcept;
};

25
src/PseudoMath.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
namespace SH_COMP
{
struct SHVec2
{
float x, y;
};
struct SHVec3
{
float x, y, z;
};
struct SHVec4
{
float x, y, z, w;
};
struct SHMat4
{
float data[16];
};
}

View File

@ -10,18 +10,32 @@
*****************************************************************************/
#pragma once
#include "PseudoMath.h"
#include <vector>
#include <assimp/anim.h>
namespace SH_COMP
{
struct AnimationKey
{
float time;
SHVec4 value;
};
struct AnimationNode
{
std::string name;
std::vector<AnimationKey> positionKeys;
std::vector<AnimationKey> rotationKeys;
std::vector<AnimationKey> scaleKeys;
};
struct AnimationAsset
{
std::string name;
std::vector<aiNodeAnim*> nodeChannels;
std::vector<aiMeshAnim*> meshChannels;
std::vector<aiMeshMorphAnim*> morphMeshChannels;
std::vector<AnimationNode> nodeChannels;
//std::vector<aiMeshAnim*> meshChannels;
//std::vector<aiMeshMorphAnim*> morphMeshChannels;
double duration;
double ticksPerSecond;

View File

@ -1,38 +1,12 @@
/*************************************************************************//**
* \file SHMeshAsset.h
* \author Loh Xiao Qi
* \date 30 September 2022
* \brief Struct to contain ready data for loading into GPU. Also used for
* compilation into binary files
*
*
* Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
* disclosure of this file or its contents without the prior written consent
* of DigiPen Institute of Technology is prohibited.
*****************************************************************************/
#pragma once
#include <vector>
#include <string>
#include "PseudoMath.h"
namespace SH_COMP
{
struct SHVec2
{
float x, y;
};
struct SHVec3
{
float x, y, z;
};
struct SHMat4
{
float data[16];
};
struct MeshDataHeader
{
uint32_t vertexCount;
@ -71,29 +45,4 @@ namespace SH_COMP
std::vector<MeshBoneInfo> bonesInfo;
std::vector<MeshBone> bones;
};
struct RigNode
{
std::string name;
uint32_t id;
SHMat4 transform;
std::vector<RigNode*> children;
};
struct RigData
{
RigNode* root;
};
struct ModelAssetHeader
{
size_t meshCount;
};
struct ModelAsset
{
ModelAssetHeader header;
std::vector<MeshDataHeader> headers;
std::vector<MeshData> meshes;
};
}

47
src/Types/ModelAsset.h Normal file
View File

@ -0,0 +1,47 @@
/*************************************************************************//**
* \file SHMeshAsset.h
* \author Loh Xiao Qi
* \date 30 September 2022
* \brief Struct to contain ready data for loading into GPU. Also used for
* compilation into binary files
*
*
* Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
* disclosure of this file or its contents without the prior written consent
* of DigiPen Institute of Technology is prohibited.
*****************************************************************************/
#pragma once
#include <vector>
#include <string>
#include "MeshAsset.h"
namespace SH_COMP
{
struct RigNode
{
std::string name;
uint32_t id;
SHMat4 transform;
std::vector<RigNode*> children;
};
struct RigData
{
RigNode* root;
};
struct ModelAssetHeader
{
size_t meshCount;
};
struct ModelAsset
{
ModelAssetHeader header;
RigData rig;
std::vector<MeshDataHeader> headers;
std::vector<MeshData> meshes;
};
}

1
src/Types/RigAsset.h Normal file
View File

@ -0,0 +1 @@
#pragma once