Animation node Channels copy complete

This commit is contained in:
Xiao Qi 2023-02-27 03:41:38 +08:00
parent 6615f0c2b8
commit 69d3cc7619
5 changed files with 75 additions and 33 deletions

View File

@ -95,6 +95,11 @@ enum class PRIMITIVE_MODE : int
TRIANLE_FAN = 6 TRIANLE_FAN = 6
}; };
constexpr std::string_view TRANSLATION_PATH{ "translation" };
constexpr std::string_view SCALE_PATH{ "scale" };
constexpr std::string_view ROTATION_PATH{ "rotation" };
constexpr std::string_view WEIGHTS_PATH{ "weights" };
//Directory //Directory
#ifdef _PUBLISH #ifdef _PUBLISH
constexpr std::string_view ASSET_ROOT{ "Assets" }; constexpr std::string_view ASSET_ROOT{ "Assets" };

View File

@ -53,6 +53,9 @@ namespace SH_COMP
template<typename T> template<typename T>
static void FetchData(int accessorID, std::vector<T>& dst); static void FetchData(int accessorID, std::vector<T>& dst);
template<typename T>
static void FetchChannelKeyFrame(int targetNode, int inputAcc, int outputAcc, int nodeTarget, std::vector<T>& dst);
public: public:
static void LoadAndCompile(AssetPath path) noexcept; static void LoadAndCompile(AssetPath path) noexcept;
}; };

View File

@ -162,6 +162,30 @@ namespace SH_COMP
} }
} }
template <typename T>
void MeshCompiler::FetchChannelKeyFrame(int targetNode, int inputAcc, int outputAcc, int nodeTarget, std::vector<T>& dst)
{
// ONLY ALLOW THIS FUNCTION TO BE USED ON KEY DATA STRUCT
static_assert(std::derived_from<T, KeyBase> == true);
std::vector<float> inputVec;
std::vector<SHVec3> outputVec;
FetchData(inputAcc, inputVec);
FetchData(outputAcc, outputVec);
dst.resize(inputVec.size());
std::ranges::transform(
inputVec,
outputVec,
dst.begin(),
[](float const& time, SHVec3 const& value)->T
{
return { time, value };
}
);
}
inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept
{ {
// Mesh Headers // Mesh Headers
@ -195,12 +219,34 @@ namespace SH_COMP
asset.anims.resize(model.animations.size()); asset.anims.resize(model.animations.size());
for (auto i {0}; i < model.animations.size(); ++i) for (auto i {0}; i < model.animations.size(); ++i)
{ {
auto& animData{ model.animations[i] }; auto const& animData{ model.animations[i] };
auto& anim{ asset.anims[i] }; auto& anim{ asset.anims[i] };
anim.name = animData.name; anim.name = animData.name;
for (auto const& channel : animData.channels)
{
auto const& sampler{ animData.samplers[channel.sampler] };
// Resize nodes vector to latest largest index called
if (anim.nodes.size() <= channel.target_node)
anim.nodes.resize(channel.target_node + 1);
if (channel.target_path == TRANSLATION_PATH.data())
FetchChannelKeyFrame(channel.target_node, sampler.input, sampler.output, channel.target_node, anim.nodes[channel.target_node].positionKeys);
else if (channel.target_path == SCALE_PATH.data())
FetchChannelKeyFrame(channel.target_node, sampler.input, sampler.output, channel.target_node, anim.nodes[channel.target_node].scaleKeys);
else if (channel.target_path == ROTATION_PATH.data())
FetchChannelKeyFrame(channel.target_node, sampler.input, sampler.output, channel.target_node, anim.nodes[channel.target_node].rotationKeys);
anim.nodes[channel.target_node].interpolation =
sampler.interpolation == "LINEAR" ? AnimationInterpolation::LINEAR :
sampler.interpolation == "STEP" ? AnimationInterpolation::STEP :
sampler.interpolation == "CUBICSPLINE" ? AnimationInterpolation::CUBICSPLINE :
AnimationInterpolation::DEFAULT;
}
std::cout << "all anim channels copied\n";
} }
} }
} }

View File

@ -111,7 +111,7 @@ namespace SH_COMP
for (auto i{0}; i < header.animNodeCount; ++i) for (auto i{0}; i < header.animNodeCount; ++i)
{ {
WriteAnimNode(file, header.nodeHeaders[i], data.nodeChannels[i]); WriteAnimNode(file, header.nodeHeaders[i], data.nodes[i]);
} }
} }
} }
@ -124,13 +124,8 @@ namespace SH_COMP
); );
file.write( file.write(
reinterpret_cast<char const*>(&node.pre), reinterpret_cast<char const*>(&node.interpolation),
sizeof(AnimationBehaviour) sizeof(AnimationInterpolation)
);
file.write(
reinterpret_cast<char const*>(&node.post),
sizeof(AnimationBehaviour)
); );
uint32_t const keySize = node.positionKeys.size(); uint32_t const keySize = node.positionKeys.size();

View File

@ -16,32 +16,27 @@
namespace SH_COMP namespace SH_COMP
{ {
enum class AnimationBehaviour : uint8_t enum class AnimationInterpolation : uint8_t
{ {
DEFAULT = 0x0, DEFAULT = 0x1,
CONSTANT = 0x1, LINEAR = 0x1,
LINEAR = 0x2, STEP = 0x2,
REPEAT = 0x3 CUBICSPLINE = 0x3
};
// Base
struct KeyBase
{
float time;
SHVec3 value;
}; };
// Smallest data containers // Smallest data containers
struct PositionKey struct PositionKey :KeyBase {};
{
float time;
SHVec3 value;
};
struct RotationKey struct RotationKey : KeyBase {};
{
float time;
SHVec4 value;
};
struct ScaleKey struct ScaleKey :KeyBase {};
{
float time;
SHVec3 value;
};
// Headers for read/write // Headers for read/write
struct AnimNodeInfo struct AnimNodeInfo
@ -63,13 +58,11 @@ namespace SH_COMP
struct AnimNode struct AnimNode
{ {
std::string name; std::string name;
AnimationBehaviour pre; AnimationInterpolation interpolation;
AnimationBehaviour post;
std::vector<PositionKey> positionKeys; std::vector<PositionKey> positionKeys;
std::vector<RotationKey> rotationKeys; std::vector<RotationKey> rotationKeys;
std::vector<ScaleKey> scaleKeys; std::vector<ScaleKey> scaleKeys;
}; };
struct AnimData struct AnimData
@ -79,7 +72,7 @@ namespace SH_COMP
double duration; double duration;
double ticksPerSecond; double ticksPerSecond;
std::vector<AnimNode> nodeChannels; std::vector<AnimNode> nodes;
//std::vector<aiMeshAnim*> meshChannels; //std::vector<aiMeshAnim*> meshChannels;
//std::vector<aiMeshMorphAnim*> morphMeshChannels; //std::vector<aiMeshMorphAnim*> morphMeshChannels;
}; };