From f9ae0c10ce98397c10f417081fe7084f608f096b Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 3 Mar 2023 17:11:33 +0800 Subject: [PATCH] Modified SHAnimator and SHRig to use new model formats --- .../src/Animation/SHAnimationClip.cpp | 5 +++- SHADE_Engine/src/Animation/SHAnimationClip.h | 1 - .../src/Animation/SHAnimatorComponent.cpp | 24 +++++++------------ .../src/Animation/SHAnimatorComponent.h | 2 -- SHADE_Engine/src/Animation/SHRig.cpp | 2 +- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/SHADE_Engine/src/Animation/SHAnimationClip.cpp b/SHADE_Engine/src/Animation/SHAnimationClip.cpp index 939275d3..e1a67be8 100644 --- a/SHADE_Engine/src/Animation/SHAnimationClip.cpp +++ b/SHADE_Engine/src/Animation/SHAnimationClip.cpp @@ -24,11 +24,11 @@ namespace SHADE , totalTime { static_cast(asset.duration) / static_cast(asset.ticksPerSecond) } { // Populate keyframes + int channelIdx = 0; 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()); @@ -51,6 +51,9 @@ namespace SHADE // Insert the channel channels.emplace_back(std::move(newChannel)); + + // Go to next channel + ++channelIdx; } } diff --git a/SHADE_Engine/src/Animation/SHAnimationClip.h b/SHADE_Engine/src/Animation/SHAnimationClip.h index 8a10ce3a..bf70c980 100644 --- a/SHADE_Engine/src/Animation/SHAnimationClip.h +++ b/SHADE_Engine/src/Animation/SHAnimationClip.h @@ -46,7 +46,6 @@ namespace SHADE /// struct Channel { - std::string Name; std::vector> PositionKeyFrames; std::vector> RotationKeyFrames; std::vector> ScaleKeyFrames; diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp index 71db24db..1d17244c 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp @@ -89,16 +89,7 @@ namespace SHADE currClip = newClip; secsPerTick = 1.0f / currClip->GetTicksPerSecond(); - // Build channel map - channelMap.clear(); - if (currClip) - { - for (const auto& channel : currClip->GetChannels()) - { - channelMap.emplace(channel.Name, &channel); - } - } - + // Set to initial pose if (rig && rig->GetRootNode() && currClip) { updatePoseWithClip(0.0f); @@ -141,16 +132,17 @@ namespace SHADE void SHAnimatorComponent::updatePoseWithClip(int closestFrameIndex, float poseTime, Handle node, const SHMatrix& parentMatrix) { // Check if there is a channel for this node - const std::string& BONE_NAME = rig->GetName(node); + const int BONE_IDX = rig->GetNodeIndex(node); SHMatrix transformMatrix = node->TransformMatrix; - if (channelMap.contains(BONE_NAME)) + const auto& CHANNELS = currClip->GetChannels(); + if (CHANNELS.size() < BONE_IDX) { - const auto CHANNEL = channelMap[BONE_NAME]; + const auto& CHANNEL = CHANNELS[BONE_IDX]; transformMatrix = SHMatrix::Transform ( - getInterpolatedValue(CHANNEL->PositionKeyFrames, closestFrameIndex, poseTime), - getInterpolatedValue(CHANNEL->RotationKeyFrames, closestFrameIndex, poseTime), - getInterpolatedValue(CHANNEL->ScaleKeyFrames, closestFrameIndex, poseTime) + getInterpolatedValue(CHANNEL.PositionKeyFrames, closestFrameIndex, poseTime), + getInterpolatedValue(CHANNEL.RotationKeyFrames, closestFrameIndex, poseTime), + getInterpolatedValue(CHANNEL.ScaleKeyFrames, closestFrameIndex, poseTime) ); } diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.h b/SHADE_Engine/src/Animation/SHAnimatorComponent.h index b47106f8..e9b6c501 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.h +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.h @@ -134,8 +134,6 @@ namespace SHADE float secsPerTick = 0.0f; // Buffer std::vector boneMatrices; - // Caches - std::unordered_map channelMap; /*---------------------------------------------------------------------------------*/ /* Helper Functions */ diff --git a/SHADE_Engine/src/Animation/SHRig.cpp b/SHADE_Engine/src/Animation/SHRig.cpp index d96d6153..e088622e 100644 --- a/SHADE_Engine/src/Animation/SHRig.cpp +++ b/SHADE_Engine/src/Animation/SHRig.cpp @@ -120,7 +120,7 @@ namespace SHADE // Fill the node with data const auto& NODE_DATA = asset.nodeDataCollection.at(sourceNode->idRef); - newNode->OffsetMatrix = SHMatrix::Transpose(NODE_DATA.offset); + newNode->OffsetMatrix = SHMatrix::Transpose(NODE_DATA.inverseBindMatrix); newNode->TransformMatrix = SHMatrix::Transpose(NODE_DATA.transform); // Populate maps