Implemented simple tree logic, added speed scale node.

AnimGraphEditor
Martin Felis 2021-11-12 21:33:53 +01:00
parent 200d0fecc8
commit ba4869149b
7 changed files with 84 additions and 43 deletions

View File

@ -48,6 +48,7 @@ target_sources(AnimTestbed PRIVATE
src/SkinnedMesh.h
src/AnimNode.cc
src/AnimSamplerNode.cc
src/SpeedScaleNode.cc
src/BlendNode.cc
src/AnimationController.cc
3rdparty/glfw/deps/glad_gl.c

View File

@ -17,6 +17,8 @@ void AnimSamplerNode::SetAnimation(ozz::animation::Animation* animation) {
m_sampling_cache.Resize(num_joints);
}
void AnimSamplerNode::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) {
ozz::animation::SamplingJob sampling_job;
sampling_job.animation = m_animation;
@ -28,6 +30,8 @@ void AnimSamplerNode::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matri
}
}
void AnimSamplerNode::DrawDebugUi() {
std::string node_name = "AnimSamplerNode: " + m_name;
if (ImGui::TreeNode(node_name.c_str())) {
@ -45,7 +49,6 @@ void AnimSamplerNode::DrawDebugUi() {
if (ImGui::Combo("Animation", &item_current, items, anim_count)) {
m_animation = skinned_mesh->m_animations[item_current];
}
ImGui::SliderFloat("Time Scale", &m_time_scale, 0.01f, 5.f);
ImGui::Checkbox("Override", &m_override_ratio);
ImGui::SameLine();
ImGui::SliderFloat("Ratio", &m_anim_ratio, 0.f, 1.f);

View File

@ -10,14 +10,12 @@
struct AnimSamplerNode : public AnimNode {
AnimSamplerNode(AnimationController* animation_controller)
: AnimNode(animation_controller),
m_time_scale(1.f),
m_anim_ratio(0.f),
m_override_ratio(false){};
virtual ~AnimSamplerNode() {}
ozz::animation::Animation* m_animation;
float m_time_scale;
float m_anim_ratio;
bool m_override_ratio;
@ -26,7 +24,7 @@ struct AnimSamplerNode : public AnimNode {
void SetAnimation(ozz::animation::Animation* animation);
virtual void Update(float dt) override {
m_current_time += dt * m_time_scale;
m_current_time += dt;
if (!m_override_ratio) {
const float duration = m_animation->duration();
m_anim_ratio = fmodf((float)m_current_time / duration, 1.0f);

View File

@ -11,54 +11,57 @@
#include "AnimSamplerNode.h"
#include "SkinnedMesh.h"
#include "SpeedScaleNode.h"
AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
: m_current_time(0.f),
m_skinned_mesh(skinned_mesh),
m_blend_anim_A(nullptr),
m_blend_anim_B(nullptr),
m_blend_weight(0.f),
m_time_scale_A(1.f),
m_time_scale_B(1.f),
m_override_ratio_A(false),
m_override_ratio_B(false) {
: m_current_time(0.f), m_skinned_mesh(skinned_mesh) {
const int num_soa_joints = skinned_mesh->m_skeleton.num_soa_joints();
const int num_joints = skinned_mesh->m_skeleton.num_joints();
skinned_mesh->m_local_matrices.resize(num_soa_joints);
skinned_mesh->m_model_matrices.resize(num_joints);
m_sampling_cache_A.Resize(num_joints);
m_sampling_cache_B.Resize(num_joints);
m_local_matrices.resize(num_soa_joints);
m_local_matrices_A.resize(num_soa_joints);
m_local_matrices_B.resize(num_soa_joints);
m_local_matrices_blended.resize(num_soa_joints);
assert(skinned_mesh->m_animations.size() > 1);
SetBlendAnims(skinned_mesh->m_animations[1], skinned_mesh->m_animations[0]);
ResetAnims();
AnimSamplerNode* sampler_node = new AnimSamplerNode(this);
sampler_node->m_name = "AnimSampler0";
sampler_node->SetAnimation(skinned_mesh->m_animations[0]);
m_output_node = sampler_node;
m_output_node->m_name = "AnimSampler0";
m_anim_nodes.push_back(sampler_node);
SpeedScaleNode* speed_node = new SpeedScaleNode(this);
speed_node->m_name = "SpeedNode0";
speed_node->m_child_node = sampler_node;
m_anim_nodes.push_back(speed_node);
m_output_node = speed_node;
m_output_node->Reset();
}
AnimationController::~AnimationController() {
if (m_output_node) {
delete m_output_node;
while (m_anim_nodes.size() > 0) {
delete m_anim_nodes[m_anim_nodes.size() - 1];
m_anim_nodes.pop_back();
}
m_output_node = nullptr;
}
}
void AnimationController::Update(float dt) {
if (m_output_node == nullptr) {
return;
}
m_output_node->Update(dt); }
m_output_node->Update(dt);
}
void AnimationController::Evaluate() {
if (m_output_node == nullptr) {
@ -74,10 +77,11 @@ void AnimationController::Evaluate() {
ltm_job.Run();
};
void AnimationController::DrawDebugUi() {
ImGui::Begin("AnimationController");
if (m_output_node && ImGui::TreeNode("Output")) {
m_output_node->DrawDebugUi();
ImGui::TreePop();

View File

@ -21,12 +21,8 @@ struct AnimationController {
void SetBlendAnims(
ozz::animation::Animation* blend_anim_A,
ozz::animation::Animation* blend_anim_B) {
m_blend_anim_A = blend_anim_A;
m_blend_anim_B = blend_anim_B;
}
void ResetAnims() {
m_anim_time_A = 0.f;
m_anim_time_B = 0.f;
}
void Update(float dt);
void Evaluate();
@ -35,19 +31,6 @@ struct AnimationController {
float m_current_time;
ozz::animation::Animation* m_blend_anim_A;
ozz::animation::Animation* m_blend_anim_B;
float m_time_scale_A;
float m_time_scale_B;
float m_anim_time_A;
float m_anim_time_B;
float m_anim_ratio_A;
float m_anim_ratio_B;
bool m_override_ratio_A;
bool m_override_ratio_B;
float m_blend_weight;
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;
@ -59,6 +42,8 @@ struct AnimationController {
SkinnedMesh* m_skinned_mesh = nullptr;
AnimNode* m_output_node;
std::vector<AnimNode*> m_anim_nodes;
};
#endif //ANIMTESTBED_ANIMATIONCONTROLLER_H

18
src/SpeedScaleNode.cc Normal file
View File

@ -0,0 +1,18 @@
//
// Created by martin on 12.11.21.
//
#include "SpeedScaleNode.h"
#include <imgui.h>
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);
m_child_node->DrawDebugUi();
ImGui::TreePop();
}
}

32
src/SpeedScaleNode.h Normal file
View File

@ -0,0 +1,32 @@
//
// Created by martin on 12.11.21.
//
#ifndef ANIMTESTBED_SPEEDSCALENODE_H
#define ANIMTESTBED_SPEEDSCALENODE_H
#include "AnimNode.h"
struct SpeedScaleNode : public AnimNode {
SpeedScaleNode(AnimationController* animation_controller): AnimNode (animation_controller), m_time_scale(1.f) {}
float m_time_scale;
AnimNode* m_child_node;
virtual void Reset() {
m_current_time = 0.f;
}
virtual void Update(float dt) {
m_current_time += dt * m_time_scale;
m_child_node->Update(dt * m_time_scale);
}
virtual void Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) override {
m_child_node->Evaluate(local_matrices);
};
virtual void DrawDebugUi() override;
};
#endif //ANIMTESTBED_SPEEDSCALENODE_H