Added code to evaluate node ordering for spanning tree of blend tree.

This commit is contained in:
Martin Felis
2021-11-12 22:12:25 +01:00
parent 1f858d68c0
commit c659caebb9
10 changed files with 44 additions and 12 deletions
+59
View File
@@ -0,0 +1,59 @@
//
// Created by martin on 12.11.21.
//
#include "AnimSamplerNode.h"
#include <imgui.h>
#include "../SkinnedMesh.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::Checkbox("Override", &m_override_ratio);
ImGui::SameLine();
ImGui::SliderFloat("Ratio", &m_anim_ratio, 0.f, 1.f);
ImGui::TreePop();
}
}
+44
View File
@@ -0,0 +1,44 @@
//
// 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_anim_ratio(0.f),
m_override_ratio(false){};
virtual ~AnimSamplerNode() {}
ozz::animation::Animation* m_animation;
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;
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 CollectNodeOrdering(
std::vector<AnimNode*>& anim_nodes) override {
anim_nodes.push_back(this);
};
virtual void DrawDebugUi();
};
#endif //ANIMTESTBED_ANIMSAMPLERNODE_H
+59
View File
@@ -0,0 +1,59 @@
//
// Created by martin on 12.11.21.
//
#include "BlendNode.h"
#include <imgui.h>
#include <ozz/animation/runtime/blending_job.h>
#include "../SkinnedMesh.h"
BlendNode::BlendNode(AnimationController* animation_controller)
: AnimNode(animation_controller),
m_input_A(nullptr),
m_input_B(nullptr),
m_weight(0.f) {
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_A.resize(num_soa_joints);
m_local_matrices_B.resize(num_soa_joints);
}
void BlendNode::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) {
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
m_input_A->Evaluate(&m_local_matrices_A);
m_input_B->Evaluate(&m_local_matrices_B);
// perform blend
ozz::animation::BlendingJob::Layer layers[2];
layers[0].transform = make_span(m_local_matrices_A);
layers[0].weight = (1.0f - m_weight);
layers[1].transform = make_span(m_local_matrices_B);
layers[1].weight = (m_weight);
ozz::animation::BlendingJob blend_job;
blend_job.threshold = ozz::animation::BlendingJob().threshold;
blend_job.layers = layers;
blend_job.bind_pose = skinned_mesh->m_skeleton.joint_bind_poses();
blend_job.output = make_span(*local_matrices);
if (!blend_job.Run()) {
ozz::log::Err() << "Error blending animations." << std::endl;
}
}
void BlendNode::DrawDebugUi() {
std::string node_name = "BlendNode: " + m_name;
if (ImGui::TreeNode(node_name.c_str())) {
ImGui::Text("Input A:");
m_input_A->DrawDebugUi();
ImGui::Text("Input B:");
m_input_B->DrawDebugUi();
ImGui::SliderFloat("Weight", &m_weight, 0.f, 1.f);
ImGui::TreePop();
}
}
+42
View File
@@ -0,0 +1,42 @@
//
// Created by martin on 12.11.21.
//
#ifndef ANIMTESTBED_BLENDNODE_H
#define ANIMTESTBED_BLENDNODE_H
#include "../AnimNode.h"
struct BlendNode : public AnimNode {
BlendNode(AnimationController* animation_controller);
virtual ~BlendNode() {}
AnimNode* m_input_A;
AnimNode* m_input_B;
float m_weight;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_A;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_B;
virtual void Reset() {
m_current_time = 0.f;
}
virtual void Update(float dt) {
m_input_A->Update(dt);
m_input_B->Update(dt);
}
virtual void Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
virtual void CollectNodeOrdering (std::vector<AnimNode*>& anim_nodes) override {
anim_nodes.push_back(this);
m_input_A->CollectNodeOrdering(anim_nodes);
m_input_B->CollectNodeOrdering(anim_nodes);
}
virtual void DrawDebugUi();
};
#endif //ANIMTESTBED_BLENDNODE_H
+18
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_input_node->DrawDebugUi();
ImGui::TreePop();
}
}
+37
View File
@@ -0,0 +1,37 @@
//
// 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_input_node;
virtual void Reset() {
m_current_time = 0.f;
}
virtual void Update(float dt) {
m_current_time += dt * m_time_scale;
m_input_node->Update(dt * m_time_scale);
}
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 {
anim_nodes.push_back(this);
m_input_node->CollectNodeOrdering(anim_nodes);
}
virtual void DrawDebugUi() override;
};
#endif //ANIMTESTBED_SPEEDSCALENODE_H