From 33b5b84fd4e41c5d0b8ccf28c750748ea98fd204 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Mon, 27 Feb 2023 17:04:16 +0800 Subject: [PATCH] add anim clip --- .../src/Animation/SHAnimationClip.cpp | 65 +++++++++++++++++++ SHADE_Engine/src/Animation/SHAnimationClip.h | 64 ++++++++++++++++++ .../src/Animation/SHAnimationController.h | 4 +- .../src/Animation/SHAnimatorComponent.cpp | 17 +++-- .../src/Animation/SHAnimatorComponent.h | 12 ++-- SHADE_Engine/src/Animation/SHRawAnimation.h | 3 +- 6 files changed, 149 insertions(+), 16 deletions(-) create mode 100644 SHADE_Engine/src/Animation/SHAnimationClip.cpp create mode 100644 SHADE_Engine/src/Animation/SHAnimationClip.h diff --git a/SHADE_Engine/src/Animation/SHAnimationClip.cpp b/SHADE_Engine/src/Animation/SHAnimationClip.cpp new file mode 100644 index 00000000..86d0af18 --- /dev/null +++ b/SHADE_Engine/src/Animation/SHAnimationClip.cpp @@ -0,0 +1,65 @@ +/************************************************************************************//*! +\file SHRawAnimation.cpp +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 20, 2022 +\brief Contains the function definitions of the SHRawAnimation class. + +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. +*//*************************************************************************************/ +// Pre-compiled Header +#include "SHpch.h" +// Primary Header +#include "SHRawAnimation.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------------*/ + SHRawAnimation::SHRawAnimation(const SHAnimAsset& asset) + : ticksPerSecond { static_cast(asset.ticksPerSecond) } + , totalTime { static_cast(asset.duration) / static_cast(asset.ticksPerSecond) } + { + // Populate keyframes + for (const auto& channel : asset.nodeChannels) + { + // Create a channel + Channel newChannel; + newChannel.Name = std::string(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}); + } + + newChannel.MaxFrames = std::max({ newChannel.PositionKeyFrames.size(), newChannel.RotationKeyFrames.size(), newChannel.ScaleKeyFrames.size() }); + + // Insert the channel + channels.emplace_back(std::move(newChannel)); + } + } + + /*-----------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-----------------------------------------------------------------------------------*/ + + /*-----------------------------------------------------------------------------------*/ + /* Helper Functions */ + /*-----------------------------------------------------------------------------------*/ + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Animation/SHAnimationClip.h b/SHADE_Engine/src/Animation/SHAnimationClip.h new file mode 100644 index 00000000..a6ec923f --- /dev/null +++ b/SHADE_Engine/src/Animation/SHAnimationClip.h @@ -0,0 +1,64 @@ +/************************************************************************************//*! +\file SHAnimationClip.h +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Dec 12, 2022 +\brief Contains the definition of the SHAnimationClip struct and related types. + +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 + +// Project Includes +#include "SH_API.h" +#include "Math/SHMatrix.h" +#include "Assets/Asset Types/Models/SHAnimationAsset.h" +#include "Resource/SHHandle.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + class SHRawAnimation; + + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + /// + /// Represents a snippet of 3D animation that is stored in a SHRawAnimation object. + /// + class SH_API SHAnimationClip + { + public: + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*---------------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + inline Handle GetRawAnimation() const noexcept { return rawAnim; } + inline float GetTotalTime() const noexcept { } + + private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + Handle rawAnim; + int startFrameIndex; // First Frame + int endFrameIndex; // Last Frame (inclusive) + float totalTime; // Time to take from first to last frame + + /*---------------------------------------------------------------------------------*/ + /* Helper Functions */ + /*---------------------------------------------------------------------------------*/ + + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Animation/SHAnimationController.h b/SHADE_Engine/src/Animation/SHAnimationController.h index b06065ed..c6d353e6 100644 --- a/SHADE_Engine/src/Animation/SHAnimationController.h +++ b/SHADE_Engine/src/Animation/SHAnimationController.h @@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited. // Project Includes #include "SH_API.h" #include "Resource/SHHandle.h" -#include "SHRawAnimation.h" +#include "SHAnimationClip.h" namespace SHADE { @@ -100,7 +100,7 @@ namespace SHADE /// struct Node { - Handle Clip; + Handle Clip; std::vector Transitions; }; diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp index 63cec09c..2f886c75 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp @@ -19,7 +19,7 @@ of DigiPen Institute of Technology is prohibited. // Project Includes #include "SHRig.h" #include "Math/SHMatrix.h" -#include "SHRawAnimation.h" +#include "SHAnimationClip.h" #include "Graphics/SHVkUtil.h" #include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" #include "ECS_Base/Managers/SHSystemManager.h" @@ -37,14 +37,14 @@ namespace SHADE playOnce = false; } - void SHAnimatorComponent::Play(Handle clip) + void SHAnimatorComponent::Play(Handle clip) { currClip = clip; currPlaybackTime = 0.0f; Play(); } - void SHAnimatorComponent::PlayOneShot(Handle clip) + void SHAnimatorComponent::PlayOneShot(Handle clip) { Play(clip); playOnce = true; @@ -86,7 +86,7 @@ namespace SHADE } } - void SHAnimatorComponent::SetClip(Handle newClip) + void SHAnimatorComponent::SetClip(Handle newClip) { // No change if (currClip == newClip) @@ -124,9 +124,14 @@ namespace SHADE if (!currClip || !isPlaying || !rig || !rig->GetRootNode()) return; + // Get animation data + auto animData = currClip->GetRawAnimation(); + if (!animData) + return; + // Update time on the playback currPlaybackTime += dt; - if (currPlaybackTime > currClip->GetTotalTime()) + if (currPlaybackTime > animData->GetTotalTime()) { if (playOnce) { @@ -135,7 +140,7 @@ namespace SHADE } else { - currPlaybackTime = currPlaybackTime - currClip->GetTotalTime(); + currPlaybackTime = currPlaybackTime - animData->GetTotalTime(); } } diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.h b/SHADE_Engine/src/Animation/SHAnimatorComponent.h index feec443d..b5ea02b0 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.h +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.h @@ -31,7 +31,7 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ class SHRig; struct SHRigNode; - class SHRawAnimation; + class SHAnimationClip; class SHVkBuffer; /*-----------------------------------------------------------------------------------*/ @@ -55,12 +55,12 @@ namespace SHADE /// Plays the specified animation clip from the start. /// /// Animation clip to play. - void Play(Handle clip); + void Play(Handle clip); /// /// Plays the specified animation clip from the start one time only. /// /// Animation clip to play. - void PlayOneShot(Handle clip); + void PlayOneShot(Handle clip); /// /// Plays the currently loaded animation clip from the start. /// @@ -88,7 +88,7 @@ namespace SHADE /// If the clip is the same as the current clip, nothing happens. /// /// Clip to use. - void SetClip(Handle newClip); + void SetClip(Handle newClip); /*---------------------------------------------------------------------------------*/ /* Getter Functions */ @@ -108,7 +108,7 @@ namespace SHADE /// Retrieve the currently set animation clip. /// /// Handle to the currently set animation clip. - Handle GetCurrentClip() const noexcept { return currClip; } + Handle GetCurrentClip() const noexcept { return currClip; } /// /// Checks if an animation is currently playing. /// @@ -131,7 +131,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ // Resources Handle rig; - Handle currClip; + Handle currClip; // Playback Tracking float currPlaybackTime = 0.0f; bool isPlaying = true; diff --git a/SHADE_Engine/src/Animation/SHRawAnimation.h b/SHADE_Engine/src/Animation/SHRawAnimation.h index cb1b0ebe..ee5d3769 100644 --- a/SHADE_Engine/src/Animation/SHRawAnimation.h +++ b/SHADE_Engine/src/Animation/SHRawAnimation.h @@ -32,8 +32,7 @@ namespace SHADE }; /// - /// Represents a animation clip of a 3D animation that is made for a specific model - /// rig. + /// Represents the raw 3D animation data for a rigged 3D model. /// class SH_API SHRawAnimation {