WIP: blend tree logic.

AnimGraphEditor
Martin Felis 2021-11-12 21:16:43 +01:00
parent 37d2845b09
commit 200d0fecc8
7 changed files with 173 additions and 83 deletions

View File

@ -46,8 +46,10 @@ target_sources(AnimTestbed PRIVATE
src/Camera.c src/Camera.c
src/SkinnedMesh.cc src/SkinnedMesh.cc
src/SkinnedMesh.h src/SkinnedMesh.h
src/AnimationController.cc src/AnimNode.cc
src/AnimSamplerNode.cc
src/BlendNode.cc src/BlendNode.cc
src/AnimationController.cc
3rdparty/glfw/deps/glad_gl.c 3rdparty/glfw/deps/glad_gl.c
3rdparty/imgui/imgui.cpp 3rdparty/imgui/imgui.cpp
3rdparty/imgui/imgui_draw.cpp 3rdparty/imgui/imgui_draw.cpp

5
src/AnimNode.cc Normal file
View File

@ -0,0 +1,5 @@
//
// Created by martin on 12.11.21.
//
#include "AnimNode.h"

34
src/AnimNode.h Normal file
View File

@ -0,0 +1,34 @@
//
// Created by martin on 12.11.21.
//
#ifndef ANIMTESTBED_ANIMNODE_H
#define ANIMTESTBED_ANIMNODE_H
#include <string>
#include "AnimationController.h"
struct AnimNode {
AnimNode(AnimationController* animation_controller) : m_animation_controller(animation_controller) {}
virtual ~AnimNode() {};
std::string m_name;
float m_current_time;
AnimationController* m_animation_controller;
ozz::vector<ozz::math::SoaTransform> m_local_matrices;
virtual void Reset() {
m_current_time = 0.f;
}
virtual void Update(float dt) {
m_current_time += dt;
}
virtual void Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) = 0;
virtual void DrawDebugUi() {};
};
#endif //ANIMTESTBED_ANIMNODE_H

55
src/AnimSamplerNode.cc Normal file
View File

@ -0,0 +1,55 @@
//
// Created by martin on 12.11.21.
//
#include "AnimSamplerNode.h"
#include "SkinnedMesh.h"
#include <imgui.h>
void AnimSamplerNode::SetAnimation(ozz::animation::Animation* animation) {
m_animation = animation;
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();
m_local_matrices.resize(num_soa_joints);
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;
sampling_job.cache = &m_sampling_cache;
sampling_job.ratio = m_anim_ratio;
sampling_job.output = make_span(*local_matrices);
if (!sampling_job.Run()) {
ozz::log::Err() << "Error sampling animation." << std::endl;
}
}
void AnimSamplerNode::DrawDebugUi() {
std::string node_name = "AnimSamplerNode: " + m_name;
if (ImGui::TreeNode(node_name.c_str())) {
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
int anim_count = skinned_mesh->m_animation_names.size();
const char* items[255] = {0};
int item_current = 0;
for (int i = 0; i < anim_count; i++) {
items[i] = skinned_mesh->m_animation_names[i].c_str();
if (skinned_mesh->m_animations[i] == m_animation) {
item_current = i;
}
}
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);
ImGui::TreePop();
}
}

41
src/AnimSamplerNode.h Normal file
View File

@ -0,0 +1,41 @@
//
// Created by martin on 12.11.21.
//
#ifndef ANIMTESTBED_ANIMSAMPLERNODE_H
#define ANIMTESTBED_ANIMSAMPLERNODE_H
#include "AnimNode.h"
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;
ozz::animation::SamplingCache m_sampling_cache;
void SetAnimation(ozz::animation::Animation* animation);
virtual void Update(float dt) override {
m_current_time += dt * m_time_scale;
if (!m_override_ratio) {
const float duration = m_animation->duration();
m_anim_ratio = fmodf((float)m_current_time / duration, 1.0f);
}
}
virtual void Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
virtual void DrawDebugUi();
};
#endif //ANIMTESTBED_ANIMSAMPLERNODE_H

View File

@ -9,6 +9,7 @@
#include <ozz/animation/runtime/local_to_model_job.h> #include <ozz/animation/runtime/local_to_model_job.h>
#include <ozz/animation/runtime/sampling_job.h> #include <ozz/animation/runtime/sampling_job.h>
#include "AnimSamplerNode.h"
#include "SkinnedMesh.h" #include "SkinnedMesh.h"
AnimationController::AnimationController(SkinnedMesh* skinned_mesh) AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
@ -29,6 +30,7 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
m_sampling_cache_A.Resize(num_joints); m_sampling_cache_A.Resize(num_joints);
m_sampling_cache_B.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_A.resize(num_soa_joints);
m_local_matrices_B.resize(num_soa_joints); m_local_matrices_B.resize(num_soa_joints);
m_local_matrices_blended.resize(num_soa_joints); m_local_matrices_blended.resize(num_soa_joints);
@ -36,103 +38,50 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
assert(skinned_mesh->m_animations.size() > 1); assert(skinned_mesh->m_animations.size() > 1);
SetBlendAnims(skinned_mesh->m_animations[1], skinned_mesh->m_animations[0]); SetBlendAnims(skinned_mesh->m_animations[1], skinned_mesh->m_animations[0]);
ResetAnims(); ResetAnims();
AnimSamplerNode* sampler_node = new AnimSamplerNode(this);
sampler_node->SetAnimation(skinned_mesh->m_animations[0]);
m_output_node = sampler_node;
m_output_node->m_name = "AnimSampler0";
m_output_node->Reset();
}
AnimationController::~AnimationController() {
if (m_output_node) {
delete m_output_node;
m_output_node = nullptr;
}
} }
void AnimationController::Update(float dt) { void AnimationController::Update(float dt) {
m_current_time += dt; if (m_output_node == nullptr) {
if (!m_override_ratio_A) { return;
m_anim_time_A += dt * m_time_scale_A;
const float duration_A = m_blend_anim_A->duration();
m_anim_ratio_A = fmodf((float)m_anim_time_A / duration_A, 1.0f);
} }
if (!m_override_ratio_B) { m_output_node->Update(dt); }
m_anim_time_B += dt * m_time_scale_B;
const float duration_B = m_blend_anim_B->duration();
m_anim_ratio_B = fmodf((float)m_anim_time_B / duration_B, 1.0f);
}
}
void AnimationController::Evaluate() { void AnimationController::Evaluate() {
// sample animation A if (m_output_node == nullptr) {
ozz::animation::SamplingJob sampling_job; return;
sampling_job.animation = m_blend_anim_A;
sampling_job.cache = &m_sampling_cache_A;
sampling_job.ratio = m_anim_ratio_A;
sampling_job.output = make_span(m_local_matrices_A);
if (!sampling_job.Run()) {
ozz::log::Err() << "Error sampling animation." << std::endl;
}
// sample animation B
sampling_job.animation = m_blend_anim_B;
sampling_job.cache = &m_sampling_cache_B;
sampling_job.ratio = m_anim_ratio_B;
sampling_job.output = make_span(m_local_matrices_B);
if (!sampling_job.Run()) {
ozz::log::Err() << "Error sampling animation." << std::endl;
}
// perform blend
ozz::animation::BlendingJob::Layer layers[2];
layers[0].transform = make_span(m_local_matrices_A);
layers[0].weight = (1.0f - m_blend_weight);
layers[1].transform = make_span(m_local_matrices_B);
layers[1].weight = (m_blend_weight);
ozz::animation::BlendingJob blend_job;
blend_job.threshold = ozz::animation::BlendingJob().threshold;
blend_job.layers = layers;
blend_job.bind_pose = m_skinned_mesh->m_skeleton.joint_bind_poses();
blend_job.output = make_span(m_local_matrices_blended);
if (!blend_job.Run()) {
ozz::log::Err() << "Error blending animations." << std::endl;
} }
m_output_node->Evaluate(&m_local_matrices);
// convert joint matrices from local to model space // convert joint matrices from local to model space
ozz::animation::LocalToModelJob ltm_job; ozz::animation::LocalToModelJob ltm_job;
ltm_job.skeleton = &m_skinned_mesh->m_skeleton; ltm_job.skeleton = &m_skinned_mesh->m_skeleton;
ltm_job.input = make_span(m_local_matrices_blended); ltm_job.input = make_span(m_local_matrices);
ltm_job.output = make_span(m_skinned_mesh->m_model_matrices); ltm_job.output = make_span(m_skinned_mesh->m_model_matrices);
ltm_job.Run(); ltm_job.Run();
}; };
void AnimationController::DrawDebugUi() { void AnimationController::DrawDebugUi() {
ImGui::Begin("AnimationController"); ImGui::Begin("AnimationController");
if (m_output_node && ImGui::TreeNode("Output")) {
int anim_count = m_skinned_mesh->m_animation_names.size(); m_output_node->DrawDebugUi();
const char* items[255] = {0};
int item_current_A = 0; ImGui::TreePop();
int item_current_B = 0;
for (int i = 0; i < anim_count; i++) {
items[i] = m_skinned_mesh->m_animation_names[i].c_str();
if (m_skinned_mesh->m_animations[i] == m_blend_anim_A) {
item_current_A = i;
}
if (m_skinned_mesh->m_animations[i] == m_blend_anim_B) {
item_current_B = i;
}
} }
bool anim_changed = false;
anim_changed =
ImGui::Combo("Animation A", &item_current_A, items, anim_count);
ImGui::SliderFloat("Time Scale", &m_time_scale_A, 0.01f, 5.f);
ImGui::Checkbox("Override", &m_override_ratio_A);
ImGui::SameLine();
ImGui::SliderFloat("Ratio", &m_anim_ratio_A, 0.f, 1.f);
anim_changed = ImGui::Combo("Animation B", &item_current_B, items, anim_count)
|| anim_changed;
ImGui::SliderFloat("Time Scale##", &m_time_scale_B, 0.01f, 5.f);
ImGui::Checkbox("Override##", &m_override_ratio_B);
ImGui::SameLine();
ImGui::SliderFloat("Ratio##", &m_anim_ratio_B, 0.f, 1.f);
SetBlendAnims(
m_skinned_mesh->m_animations[item_current_A],
m_skinned_mesh->m_animations[item_current_B]);
ImGui::SliderFloat("Weight", &m_blend_weight, 0.f, 1.f);
ImGui::End(); ImGui::End();
} }

View File

@ -5,17 +5,18 @@
#ifndef ANIMTESTBED_ANIMATIONCONTROLLER_H #ifndef ANIMTESTBED_ANIMATIONCONTROLLER_H
#define ANIMTESTBED_ANIMATIONCONTROLLER_H #define ANIMTESTBED_ANIMATIONCONTROLLER_H
#include <ozz/animation/runtime/animation.h>
#include <ozz/animation/runtime/sampling_job.h> #include <ozz/animation/runtime/sampling_job.h>
#include <ozz/base/containers/vector.h>
#include "ozz/animation/runtime/animation.h" #include <ozz/base/maths/soa_transform.h>
#include "ozz/base/containers/vector.h" #include <ozz/base/maths/vec_float.h>
#include "ozz/base/maths/soa_transform.h"
#include "ozz/base/maths/vec_float.h"
struct SkinnedMesh; struct SkinnedMesh;
struct AnimNode;
struct AnimationController { struct AnimationController {
explicit AnimationController(SkinnedMesh* skinned_mesh); explicit AnimationController(SkinnedMesh* skinned_mesh);
virtual ~AnimationController();
void SetBlendAnims( void SetBlendAnims(
ozz::animation::Animation* blend_anim_A, ozz::animation::Animation* blend_anim_A,
@ -47,6 +48,7 @@ struct AnimationController {
bool m_override_ratio_B; bool m_override_ratio_B;
float m_blend_weight; 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_A;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_B; ozz::vector<ozz::math::SoaTransform> m_local_matrices_B;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_blended; ozz::vector<ozz::math::SoaTransform> m_local_matrices_blended;
@ -55,6 +57,8 @@ struct AnimationController {
ozz::animation::SamplingCache m_sampling_cache_B; ozz::animation::SamplingCache m_sampling_cache_B;
SkinnedMesh* m_skinned_mesh = nullptr; SkinnedMesh* m_skinned_mesh = nullptr;
AnimNode* m_output_node;
}; };
#endif //ANIMTESTBED_ANIMATIONCONTROLLER_H #endif //ANIMTESTBED_ANIMATIONCONTROLLER_H