base
This commit is contained in:
+16
-2
@@ -4,6 +4,7 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
@@ -98,8 +99,9 @@ private:
|
||||
|
||||
// ============================================================================
|
||||
// Animator
|
||||
// blendWeight ramps from 0?1 over blendDuration when PlayAnimation is called.
|
||||
// CharacterBody::GetBlendedBoneMatrices() lerps all animators by blendWeight.
|
||||
// blendWeight ramps from 0?1 over blendDuration when PlayAnimation is called,
|
||||
// and back down over the end of an animation that doesn't loop.
|
||||
// CharacterBody layers animators bone by bone: each one only moves the bones its animation has tracks for.
|
||||
// ============================================================================
|
||||
class Animator
|
||||
{
|
||||
@@ -172,11 +174,21 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
// A one-shot fades out over its last blendDuration instead of snapping back
|
||||
if (!loops && blendDuration > 0.0f) {
|
||||
const float remaining = (m_CurrentAnimation->GetDuration() - m_CurrentTime)
|
||||
/ glm::max(m_CurrentAnimation->GetTicksPerSecond() * playbackSpeed, 1e-4f);
|
||||
blendWeight = glm::min(blendWeight, glm::clamp(remaining / blendDuration, 0.0f, 1.0f));
|
||||
}
|
||||
|
||||
CalculateBoneTransform(&m_CurrentAnimation->GetRootNode(), glm::mat4(1.0f));
|
||||
}
|
||||
|
||||
const std::vector<glm::mat4>& GetFinalBoneMatrices() const { return m_FinalBoneMatrices; }
|
||||
|
||||
// This frame's transform of each bone relative to its parent, only for bones the animation has a track for
|
||||
const std::unordered_map<std::string, glm::mat4>& GetLocalTransforms() const { return m_LocalTransforms; }
|
||||
|
||||
public:
|
||||
std::vector<glm::mat4> m_FinalBoneMatrices;
|
||||
Animation* m_CurrentAnimation = nullptr;
|
||||
@@ -186,6 +198,7 @@ public:
|
||||
|
||||
private:
|
||||
std::map<std::string, BoneInfo> m_BoneInfoMap;
|
||||
std::unordered_map<std::string, glm::mat4> m_LocalTransforms;
|
||||
float _blendTimer = 0.0f;
|
||||
|
||||
void RebuildBoneInfoMap()
|
||||
@@ -205,6 +218,7 @@ private:
|
||||
if (bone) {
|
||||
bone->Update(m_CurrentTime);
|
||||
nodeTransform = bone->GetLocalTransform();
|
||||
m_LocalTransforms[nodeName] = nodeTransform; // the Animation is shared, so keep this animator's own copy
|
||||
}
|
||||
|
||||
glm::mat4 globalTransform = parentTransform * nodeTransform;
|
||||
|
||||
Reference in New Issue
Block a user