From 7bf0c2605217da318b8ce0707958559c761529f3 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Mon, 9 Jan 2023 17:51:54 +0800 Subject: [PATCH] Added support for loading SHRig and SHAnimationClip into SHResourceManager and modified SHAnimationClip to support proper keyframe data --- .../src/Animation/SHAnimationClip.cpp | 29 ++++++++++++++++++- SHADE_Engine/src/Animation/SHAnimationClip.h | 20 ++++++++----- SHADE_Engine/src/Resource/SHResourceManager.h | 6 +++- .../src/Resource/SHResourceManager.hpp | 10 ++++++- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/SHADE_Engine/src/Animation/SHAnimationClip.cpp b/SHADE_Engine/src/Animation/SHAnimationClip.cpp index 4ef91a39..a6841e0a 100644 --- a/SHADE_Engine/src/Animation/SHAnimationClip.cpp +++ b/SHADE_Engine/src/Animation/SHAnimationClip.cpp @@ -20,11 +20,38 @@ namespace SHADE /* Constructors */ /*-----------------------------------------------------------------------------------*/ SHAnimationClip::SHAnimationClip(const SHAnimAsset& asset) + : ticksPerSecond { asset.ticksPerSecond } + , totalTime { asset.duration } { + // Populate keyframes + for (const auto& channel : asset.nodeChannels) + { + // Create a channel + Channel newChannel; + newChannel.Name = channel.name; + newChannel.PositionKeyFrames.reserve(channel.positionKeys.size()); + newChannel.RotationKeyFrames.reserve(channel.rotationKeys.size()); + newChannel.ScaleKeyFrames.reserve(channel.scaleKeys.size()); + // Populate Keyframes + for (const auto& posKey : channel.positionKeys) + { + newChannel.PositionKeyFrames.emplace_back(SHAnimationKeyFrame{ static_cast(posKey.time), posKey.value}); + } + for (const auto& rotKey : channel.rotationKeys) + { + newChannel.RotationKeyFrames.emplace_back(SHAnimationKeyFrame{ static_cast(rotKey.time), rotKey.value}); + } + for (const auto& scaleKey : channel.scaleKeys) + { + newChannel.ScaleKeyFrames.emplace_back(SHAnimationKeyFrame{ static_cast(scaleKey.time), scaleKey.value}); + } + + // Insert the channel + channels.emplace_back(std::move(newChannel)); + } } - /*-----------------------------------------------------------------------------------*/ /* Usage Functions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Animation/SHAnimationClip.h b/SHADE_Engine/src/Animation/SHAnimationClip.h index d8415942..9e34256b 100644 --- a/SHADE_Engine/src/Animation/SHAnimationClip.h +++ b/SHADE_Engine/src/Animation/SHAnimationClip.h @@ -23,15 +23,13 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ /// - /// Defines a single key frame in an animation. + /// Defines a single key frame in an animation for a specific type of data. /// + template struct SHAnimationKeyFrame { - float TimeStamp; - SHVec3 Position; - SHQuaternion Orientation; - SHVec3 Scale; - SHMatrix TransformationMatrix; + int FrameIndex; + T Data; }; /// @@ -50,18 +48,25 @@ namespace SHADE struct Channel { std::string Name; - std::vector KeyFrames; + std::vector> PositionKeyFrames; + std::vector> RotationKeyFrames; + std::vector> ScaleKeyFrames; }; /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ + /// + /// Constructs an SHAnimation Clip from a specified SHAnimAsset. + /// + /// Animation asset to load. explicit SHAnimationClip(const SHAnimAsset& asset); /*---------------------------------------------------------------------------------*/ /* Getter Functions */ /*---------------------------------------------------------------------------------*/ const std::vector& GetChannels() const noexcept { return channels; } + int GetTicksPerSecond() const noexcept { return ticksPerSecond; } float GetTotalTime() const noexcept { return totalTime; } private: @@ -69,6 +74,7 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ std::vector channels; + int ticksPerSecond; float totalTime; /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Resource/SHResourceManager.h b/SHADE_Engine/src/Resource/SHResourceManager.h index efdd15be..086b8570 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.h +++ b/SHADE_Engine/src/Resource/SHResourceManager.h @@ -27,6 +27,8 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/Materials/SHMaterialSpec.h" #include "Assets/Asset Types/SHMaterialAsset.h" #include "Graphics/MiddleEnd/TextRendering/SHFont.h" +#include "Animation/SHAnimationClip.h" +#include "Animation/SHRig.h" namespace SHADE { @@ -43,12 +45,14 @@ namespace SHADE /// template struct SHResourceLoader { using AssetType = void; }; - template<> struct SHResourceLoader { using AssetType = SHMeshAsset; }; + template<> struct SHResourceLoader { using AssetType = SHMeshAsset; }; template<> struct SHResourceLoader { using AssetType = SHTextureAsset; }; template<> struct SHResourceLoader { using AssetType = SHShaderAsset; }; template<> struct SHResourceLoader { using AssetType = SHMaterialAsset; }; template<> struct SHResourceLoader { using AssetType = SHMaterialSpec; }; template<> struct SHResourceLoader { using AssetType = SHFontAsset; }; + template<> struct SHResourceLoader { using AssetType = SHAnimAsset; }; + template<> struct SHResourceLoader { using AssetType = SHRigAsset; }; /// /// Static class responsible for loading and caching runtime resources from their diff --git a/SHADE_Engine/src/Resource/SHResourceManager.hpp b/SHADE_Engine/src/Resource/SHResourceManager.hpp index 51ee356a..44c38c50 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.hpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.hpp @@ -40,7 +40,9 @@ namespace SHADE !std::is_same_v && !std::is_same_v && !std::is_same_v && - !std::is_same_v + !std::is_same_v && + !std::is_same_v && + !std::is_same_v ) { static_assert(true, "Unsupported Resource Type specified for SHResourceManager."); @@ -345,5 +347,11 @@ namespace SHADE return gfxSystem->AddFont(assetData); } + // Animation Clip and Rig + else + { + loadedAssetData.emplace_back(assetId); + return resourceHub.Create(assetData); + } } }