Synced animations looks good.

AnimGraphEditor
Martin Felis 2021-11-13 00:08:32 +01:00
parent c659caebb9
commit 30f1025947
8 changed files with 185 additions and 42 deletions

View File

@ -9,28 +9,42 @@
#include "AnimationController.h"
struct AnimNode {
AnimNode(AnimationController* animation_controller) : m_animation_controller(animation_controller) {}
virtual ~AnimNode() {};
enum class AnimNodeType {
Blend,
SpeedScale,
AnimSampler
};
struct AnimNode {
AnimNode(AnimationController* animation_controller)
: m_animation_controller(animation_controller),
m_current_time(0.f),
m_is_time_synced(false),
m_anim_duration(0.f) {}
virtual ~AnimNode(){};
AnimNodeType m_anim_node_type;
std::string m_name;
float m_current_time;
bool m_is_time_synced;
float m_anim_duration;
AnimationController* m_animation_controller;
ozz::vector<ozz::math::SoaTransform> m_local_matrices;
virtual void Reset() {
m_current_time = 0.f;
}
virtual void Reset() { m_current_time = 0.f; }
virtual void Update(float dt) {
m_current_time += dt;
}
virtual void UpdateIsSynced(bool is_synced) = 0;
virtual void Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) = 0;
virtual void EvalAnimDuration() = 0;
virtual void CollectNodeOrdering (std::vector<AnimNode*>& anim_nodes) = 0;
virtual void UpdateTime(float dt) { m_current_time += dt; }
virtual void DrawDebugUi() {};
virtual void Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) = 0;
virtual void CollectNodeOrdering(std::vector<AnimNode*>& anim_nodes) = 0;
virtual void DrawDebugUi(){};
};
#endif //ANIMTESTBED_ANIMNODE_H

View File

@ -10,26 +10,47 @@
struct AnimSamplerNode : public AnimNode {
AnimSamplerNode(AnimationController* animation_controller)
: AnimNode(animation_controller),
m_anim_ratio(0.f),
m_override_ratio(false){};
m_override_ratio(false),
m_anim_ratio(0.f) {
assert(m_current_time < 100.0f);
m_anim_node_type = AnimNodeType::AnimSampler;
};
virtual ~AnimSamplerNode() {}
ozz::animation::Animation* m_animation;
float m_anim_ratio;
bool m_override_ratio;
float m_anim_ratio;
ozz::animation::SamplingCache m_sampling_cache;
void SetAnimation(ozz::animation::Animation* animation);
virtual void Update(float dt) override {
virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced;
}
virtual void EvalAnimDuration() override {
m_anim_duration = m_animation->duration();
}
virtual void UpdateTime(float dt) override {
m_current_time += dt;
if (!m_override_ratio) {
if (m_is_time_synced) {
m_anim_ratio = fmodf((float)m_current_time, 1.0f);
m_current_time = m_anim_ratio;
} else {
const float duration = m_animation->duration();
m_anim_ratio = fmodf((float)m_current_time / duration, 1.0f);
}
}
if (m_anim_ratio < 0.f) {
m_anim_ratio += 1.0f;
}
}
virtual void Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;

View File

@ -13,7 +13,9 @@ BlendNode::BlendNode(AnimationController* animation_controller)
: AnimNode(animation_controller),
m_input_A(nullptr),
m_input_B(nullptr),
m_weight(0.f) {
m_weight(0.f),
m_sync_inputs(false) {
m_anim_node_type = AnimNodeType::Blend;
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
const int num_soa_joints = skinned_mesh->m_skeleton.num_soa_joints();
const int num_joints = skinned_mesh->m_skeleton.num_joints();
@ -54,6 +56,7 @@ void BlendNode::DrawDebugUi() {
ImGui::Text("Input B:");
m_input_B->DrawDebugUi();
ImGui::SliderFloat("Weight", &m_weight, 0.f, 1.f);
ImGui::Checkbox("Sync Inputs", &m_sync_inputs);
ImGui::TreePop();
}
}

View File

@ -9,11 +9,13 @@
struct BlendNode : public AnimNode {
BlendNode(AnimationController* animation_controller);
virtual ~BlendNode() {}
AnimNode* m_input_A;
AnimNode* m_input_B;
float m_weight;
bool m_sync_inputs;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_A;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_B;
@ -22,9 +24,37 @@ struct BlendNode : public AnimNode {
m_current_time = 0.f;
}
virtual void Update(float dt) {
m_input_A->Update(dt);
m_input_B->Update(dt);
virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced;
if (m_sync_inputs) {
m_is_time_synced = true;
}
m_input_B->UpdateIsSynced(m_is_time_synced);
m_input_A->UpdateIsSynced(m_is_time_synced);
}
virtual void EvalAnimDuration() override {
if (m_is_time_synced) {
m_anim_duration = (1.0f - m_weight) * m_input_A->m_anim_duration + m_weight * m_input_B->m_anim_duration;
} else {
assert (false);
}
}
virtual void UpdateTime(float dt) {
if (m_is_time_synced) {
float current_time_absolute = m_current_time * m_anim_duration + dt;
m_current_time = fmodf(current_time_absolute / m_anim_duration, 1.0);
m_input_A->UpdateTime(m_current_time - m_input_A->m_current_time);
m_input_B->UpdateTime(m_current_time - m_input_B->m_current_time);
} else {
m_current_time += dt;
m_input_A->UpdateTime(dt);
m_input_B->UpdateTime(dt);
}
}
virtual void Evaluate(

View File

@ -9,7 +9,16 @@
void SpeedScaleNode::DrawDebugUi() {
std::string node_name = "SpeedScaleNode: " + m_name;
if (ImGui::TreeNode(node_name.c_str())) {
ImGui::SliderFloat("Time Scale", &m_time_scale, -5.f, 5.f);
bool is_negative = m_time_scale < 0.f;
if (ImGui::Checkbox("Reverse Time", &is_negative)) {
m_time_scale = m_time_scale * -1.f;
}
// ensure m_time_scale is positive
m_time_scale = m_time_scale * (is_negative ? -1.f : 1.f);
ImGui::SliderFloat("Time Scale", &m_time_scale, 0.01f, 5.f);
// and back to the original negative or positive sign
m_time_scale = m_time_scale * (is_negative ? -1.f : 1.f);
m_input_node->DrawDebugUi();

View File

@ -8,25 +8,43 @@
#include "../AnimNode.h"
struct SpeedScaleNode : public AnimNode {
SpeedScaleNode(AnimationController* animation_controller): AnimNode (animation_controller), m_time_scale(1.f) {}
SpeedScaleNode(AnimationController* animation_controller)
: AnimNode(animation_controller), m_time_scale(1.f) {
m_anim_node_type = AnimNodeType::SpeedScale;
}
float m_time_scale;
AnimNode* m_input_node;
virtual void Reset() {
m_current_time = 0.f;
virtual void Reset() { m_current_time = 0.f; }
virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced;
m_input_node->UpdateIsSynced(is_synced);
}
virtual void Update(float dt) {
virtual void EvalAnimDuration() override {
assert(fabs(m_time_scale) >= 0.01f);
m_anim_duration = fabsf(m_input_node->m_anim_duration / m_time_scale);
}
virtual void UpdateTime(float dt) {
if (!m_is_time_synced) {
m_current_time += dt * m_time_scale;
m_input_node->Update(dt * m_time_scale);
m_input_node->UpdateTime(dt * m_time_scale);
} else {
m_current_time += dt;
m_input_node->UpdateTime(dt);
}
}
virtual void Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) override {
virtual void Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) override {
m_input_node->Evaluate(local_matrices);
};
virtual void CollectNodeOrdering (std::vector<AnimNode*>& anim_nodes) override {
virtual void CollectNodeOrdering(
std::vector<AnimNode*>& anim_nodes) override {
anim_nodes.push_back(this);
m_input_node->CollectNodeOrdering(anim_nodes);
}

View File

@ -33,7 +33,7 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
m_anim_nodes.push_back(sampler_node0);
AnimSamplerNode* sampler_node1 = new AnimSamplerNode(this);
sampler_node1->m_name = "AnimSampler0";
sampler_node1->m_name = "AnimSampler1";
sampler_node1->SetAnimation(skinned_mesh->m_animations[2]);
m_anim_nodes.push_back(sampler_node1);
@ -46,13 +46,18 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
blend_node->m_name = "Blend0";
blend_node->m_input_A = speed_node;
blend_node->m_input_B = sampler_node1;
blend_node->m_sync_inputs = true;
m_anim_nodes.push_back(blend_node);
m_output_node = blend_node;
SpeedScaleNode* speed_node1 = new SpeedScaleNode(this);
speed_node1->m_name = "SpeedNode1";
speed_node1->m_input_node = blend_node;
m_anim_nodes.push_back(speed_node1);
m_output_node = speed_node1;
m_output_node->CollectNodeOrdering(m_ordered_nodes);
std::cout << "Have " << m_ordered_nodes.size() << " nodes in blend tree." << std::endl;
m_output_node->Reset();
}
@ -67,14 +72,27 @@ AnimationController::~AnimationController() {
m_output_node = nullptr;
}
void AnimationController::ResetAnims() {
for (int i = 0; i < m_ordered_nodes.size(); i++) {
m_ordered_nodes[i]->Reset();
}
}
void AnimationController::Update(float dt) {
if (m_output_node == nullptr) {
return;
}
m_output_node->Update(dt);
m_output_node->UpdateIsSynced(false);
for (int i = m_ordered_nodes.size() - 1; i >= 0; i--) {
AnimNode* node = m_ordered_nodes[i];
if (node->m_is_time_synced) {
node->EvalAnimDuration();
}
}
m_output_node->UpdateTime(dt);
}
@ -96,7 +114,13 @@ void AnimationController::Evaluate() {
void AnimationController::DrawDebugUi() {
ImGui::SetNextWindowSize(ImVec2(500, 300), ImGuiCond_FirstUseEver);
ImGui::Begin("AnimationController");
if (ImGui::Button("Reset")) {
ResetAnims();
}
if (m_output_node && ImGui::TreeNode("Output")) {
m_output_node->DrawDebugUi();
@ -104,10 +128,37 @@ void AnimationController::DrawDebugUi() {
}
if (m_output_node && ImGui::TreeNode("Nodes")) {
ImGui::Columns(4, "NodeOverview"); // 4-ways, with border
ImGui::Separator();
ImGui::Text("Name"); ImGui::NextColumn();
ImGui::Text("Synced"); ImGui::NextColumn();
ImGui::Text("Duration"); ImGui::NextColumn();
ImGui::Text("Time"); ImGui::NextColumn();
ImGui::Separator();
static int selected = -1;
for (int i = 0; i < m_ordered_nodes.size(); i++) {
ImGui::Text(m_ordered_nodes[i]->m_name.c_str());
AnimNode* node = m_ordered_nodes[i];
if (ImGui::Selectable(node->m_name.c_str(), selected == i, ImGuiSelectableFlags_SpanAllColumns))
selected = i;
bool hovered = ImGui::IsItemHovered();
ImGui::NextColumn();
ImGui::Text(node->m_is_time_synced ? "X" : "-"); ImGui::NextColumn();
ImGui::PushID((void*)&node->m_anim_duration);
ImGui::Text("%2.3f", node->m_anim_duration); ImGui::NextColumn();
ImGui::PopID();
ImGui::PushID((void*)&node->m_current_time);
ImGui::Text("%2.3f", node->m_current_time); ImGui::NextColumn();
ImGui::PopID();
}
ImGui::Columns(1);
ImGui::Separator();
ImGui::TreePop();
}

View File

@ -22,8 +22,8 @@ struct AnimationController {
ozz::animation::Animation* blend_anim_A,
ozz::animation::Animation* blend_anim_B) {
}
void ResetAnims() {
}
void ResetAnims();
void Update(float dt);
void Evaluate();
@ -32,12 +32,9 @@ struct AnimationController {
float m_current_time;
ozz::vector<ozz::math::SoaTransform> m_local_matrices;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_A;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_B;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_blended;
ozz::animation::SamplingCache m_sampling_cache_A;
ozz::animation::SamplingCache m_sampling_cache_B;
SkinnedMesh* m_skinned_mesh = nullptr;