From 200d0fecc8a116439e93bc6d9720f327d09a812c Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 12 Nov 2021 21:16:43 +0100 Subject: [PATCH] WIP: blend tree logic. --- CMakeLists.txt | 4 +- src/AnimNode.cc | 5 ++ src/AnimNode.h | 34 ++++++++++++ src/AnimSamplerNode.cc | 55 ++++++++++++++++++++ src/AnimSamplerNode.h | 41 +++++++++++++++ src/AnimationController.cc | 103 ++++++++++--------------------------- src/AnimationController.h | 14 +++-- 7 files changed, 173 insertions(+), 83 deletions(-) create mode 100644 src/AnimNode.cc create mode 100644 src/AnimNode.h create mode 100644 src/AnimSamplerNode.cc create mode 100644 src/AnimSamplerNode.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bd20372..28e1c42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,8 +46,10 @@ target_sources(AnimTestbed PRIVATE src/Camera.c src/SkinnedMesh.cc src/SkinnedMesh.h - src/AnimationController.cc + src/AnimNode.cc + src/AnimSamplerNode.cc src/BlendNode.cc + src/AnimationController.cc 3rdparty/glfw/deps/glad_gl.c 3rdparty/imgui/imgui.cpp 3rdparty/imgui/imgui_draw.cpp diff --git a/src/AnimNode.cc b/src/AnimNode.cc new file mode 100644 index 0000000..e37d5d6 --- /dev/null +++ b/src/AnimNode.cc @@ -0,0 +1,5 @@ +// +// Created by martin on 12.11.21. +// + +#include "AnimNode.h" diff --git a/src/AnimNode.h b/src/AnimNode.h new file mode 100644 index 0000000..14ac761 --- /dev/null +++ b/src/AnimNode.h @@ -0,0 +1,34 @@ +// +// Created by martin on 12.11.21. +// + +#ifndef ANIMTESTBED_ANIMNODE_H +#define ANIMTESTBED_ANIMNODE_H + +#include + +#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 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* local_matrices) = 0; + + virtual void DrawDebugUi() {}; +}; + +#endif //ANIMTESTBED_ANIMNODE_H diff --git a/src/AnimSamplerNode.cc b/src/AnimSamplerNode.cc new file mode 100644 index 0000000..409cdf2 --- /dev/null +++ b/src/AnimSamplerNode.cc @@ -0,0 +1,55 @@ +// +// Created by martin on 12.11.21. +// + +#include "AnimSamplerNode.h" +#include "SkinnedMesh.h" + +#include + +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* 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(); + } +} \ No newline at end of file diff --git a/src/AnimSamplerNode.h b/src/AnimSamplerNode.h new file mode 100644 index 0000000..7afe7d8 --- /dev/null +++ b/src/AnimSamplerNode.h @@ -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* local_matrices) override; + + virtual void DrawDebugUi(); +}; + +#endif //ANIMTESTBED_ANIMSAMPLERNODE_H diff --git a/src/AnimationController.cc b/src/AnimationController.cc index 1c946bd..ac9ba46 100644 --- a/src/AnimationController.cc +++ b/src/AnimationController.cc @@ -9,6 +9,7 @@ #include #include +#include "AnimSamplerNode.h" #include "SkinnedMesh.h" AnimationController::AnimationController(SkinnedMesh* skinned_mesh) @@ -29,6 +30,7 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh) 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); @@ -36,103 +38,50 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh) 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->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) { - m_current_time += dt; - if (!m_override_ratio_A) { - 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_output_node == nullptr) { + return; } - if (!m_override_ratio_B) { - 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); - } -} + m_output_node->Update(dt); } void AnimationController::Evaluate() { - // sample animation A - ozz::animation::SamplingJob sampling_job; - 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; + if (m_output_node == nullptr) { + return; } + m_output_node->Evaluate(&m_local_matrices); // convert joint matrices from local to model space ozz::animation::LocalToModelJob ltm_job; 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.Run(); }; void AnimationController::DrawDebugUi() { ImGui::Begin("AnimationController"); + if (m_output_node && ImGui::TreeNode("Output")) { - int anim_count = m_skinned_mesh->m_animation_names.size(); - const char* items[255] = {0}; - int item_current_A = 0; - 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; - } + m_output_node->DrawDebugUi(); + + ImGui::TreePop(); } - 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(); } \ No newline at end of file diff --git a/src/AnimationController.h b/src/AnimationController.h index 8e1be6a..e10f6a7 100644 --- a/src/AnimationController.h +++ b/src/AnimationController.h @@ -5,17 +5,18 @@ #ifndef ANIMTESTBED_ANIMATIONCONTROLLER_H #define ANIMTESTBED_ANIMATIONCONTROLLER_H +#include #include - -#include "ozz/animation/runtime/animation.h" -#include "ozz/base/containers/vector.h" -#include "ozz/base/maths/soa_transform.h" -#include "ozz/base/maths/vec_float.h" +#include +#include +#include struct SkinnedMesh; +struct AnimNode; struct AnimationController { explicit AnimationController(SkinnedMesh* skinned_mesh); + virtual ~AnimationController(); void SetBlendAnims( ozz::animation::Animation* blend_anim_A, @@ -47,6 +48,7 @@ struct AnimationController { bool m_override_ratio_B; float m_blend_weight; + ozz::vector m_local_matrices; ozz::vector m_local_matrices_A; ozz::vector m_local_matrices_B; ozz::vector m_local_matrices_blended; @@ -55,6 +57,8 @@ struct AnimationController { ozz::animation::SamplingCache m_sampling_cache_B; SkinnedMesh* m_skinned_mesh = nullptr; + + AnimNode* m_output_node; }; #endif //ANIMTESTBED_ANIMATIONCONTROLLER_H