diff --git a/src/AssetMacros.h b/src/AssetMacros.h index 1880d3c..62324d4 100644 --- a/src/AssetMacros.h +++ b/src/AssetMacros.h @@ -95,6 +95,11 @@ enum class PRIMITIVE_MODE : int 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 #ifdef _PUBLISH constexpr std::string_view ASSET_ROOT{ "Assets" }; diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index bc12609..7afe25e 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -53,6 +53,9 @@ namespace SH_COMP template static void FetchData(int accessorID, std::vector& dst); + + template + static void FetchChannelKeyFrame(int targetNode, int inputAcc, int outputAcc, int nodeTarget, std::vector& dst); public: static void LoadAndCompile(AssetPath path) noexcept; }; diff --git a/src/Libraries/MeshCompiler.hpp b/src/Libraries/MeshCompiler.hpp index 5d05033..3a99c6d 100644 --- a/src/Libraries/MeshCompiler.hpp +++ b/src/Libraries/MeshCompiler.hpp @@ -162,6 +162,30 @@ namespace SH_COMP } } + template + void MeshCompiler::FetchChannelKeyFrame(int targetNode, int inputAcc, int outputAcc, int nodeTarget, std::vector& dst) + { + // ONLY ALLOW THIS FUNCTION TO BE USED ON KEY DATA STRUCT + static_assert(std::derived_from == true); + + std::vector inputVec; + std::vector 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 { // Mesh Headers @@ -195,12 +219,34 @@ namespace SH_COMP asset.anims.resize(model.animations.size()); 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] }; 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"; } } } diff --git a/src/Libraries/MeshWriter.cpp b/src/Libraries/MeshWriter.cpp index c74e00a..094bdbe 100644 --- a/src/Libraries/MeshWriter.cpp +++ b/src/Libraries/MeshWriter.cpp @@ -111,7 +111,7 @@ namespace SH_COMP 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( - reinterpret_cast(&node.pre), - sizeof(AnimationBehaviour) - ); - - file.write( - reinterpret_cast(&node.post), - sizeof(AnimationBehaviour) + reinterpret_cast(&node.interpolation), + sizeof(AnimationInterpolation) ); uint32_t const keySize = node.positionKeys.size(); diff --git a/src/Types/AnimationAsset.h b/src/Types/AnimationAsset.h index 98cee45..255d555 100644 --- a/src/Types/AnimationAsset.h +++ b/src/Types/AnimationAsset.h @@ -16,32 +16,27 @@ namespace SH_COMP { - enum class AnimationBehaviour : uint8_t + enum class AnimationInterpolation : uint8_t { - DEFAULT = 0x0, - CONSTANT = 0x1, - LINEAR = 0x2, - REPEAT = 0x3 + DEFAULT = 0x1, + LINEAR = 0x1, + STEP = 0x2, + CUBICSPLINE = 0x3 + }; + + // Base + struct KeyBase + { + float time; + SHVec3 value; }; // Smallest data containers - struct PositionKey - { - float time; - SHVec3 value; - }; + struct PositionKey :KeyBase {}; - struct RotationKey - { - float time; - SHVec4 value; - }; + struct RotationKey : KeyBase {}; - struct ScaleKey - { - float time; - SHVec3 value; - }; + struct ScaleKey :KeyBase {}; // Headers for read/write struct AnimNodeInfo @@ -63,13 +58,11 @@ namespace SH_COMP struct AnimNode { std::string name; - AnimationBehaviour pre; - AnimationBehaviour post; + AnimationInterpolation interpolation; std::vector positionKeys; std::vector rotationKeys; std::vector scaleKeys; - }; struct AnimData @@ -79,7 +72,7 @@ namespace SH_COMP double duration; double ticksPerSecond; - std::vector nodeChannels; + std::vector nodes; //std::vector meshChannels; //std::vector morphMeshChannels; };