Implemented BlendSpace1D.

AnimGraphEditor
Martin Felis 2021-11-19 22:52:08 +01:00
parent 382730960f
commit 89108f4e1f
4 changed files with 191 additions and 7 deletions

View File

@ -49,6 +49,7 @@ add_library(AnimTestbedCode OBJECT
src/AnimNode.cc src/AnimNode.cc
src/AnimNodes/AnimSamplerNode.cc src/AnimNodes/AnimSamplerNode.cc
src/AnimNodes/SpeedScaleNode.cc src/AnimNodes/SpeedScaleNode.cc
src/AnimNodes/BlendSpace1D.cc
src/AnimNodes/BlendNode.cc src/AnimNodes/BlendNode.cc
src/AnimNodes/LockTranslationNode.cc src/AnimNodes/LockTranslationNode.cc
src/AnimationController.cc src/AnimationController.cc

View File

@ -0,0 +1,63 @@
//
// Created by martin on 19.11.21.
//
#include "BlendSpace1D.h"
#include <imgui.h>
#include <ozz/animation/runtime/blending_job.h>
#include "../SkinnedMesh.h"
BlendSpace1D::BlendSpace1D(AnimationController* animation_controller)
: AnimNode(animation_controller),
m_input_0(nullptr),
m_weight_0(0.f),
m_input_1(nullptr),
m_weight_1(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();
m_local_matrices_0.resize(num_soa_joints);
m_local_matrices_1.resize(num_soa_joints);
}
void BlendSpace1D::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) {
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
m_input_0->Evaluate(&m_local_matrices_0);
m_input_1->Evaluate(&m_local_matrices_1);
// perform blend
ozz::animation::BlendingJob::Layer layers[2];
layers[0].transform = make_span(m_local_matrices_0);
layers[0].weight = (1.0f - m_normalized_weight);
layers[1].transform = make_span(m_local_matrices_1);
layers[1].weight = (m_normalized_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 BlendSpace1D::DrawDebugUi() {
float min_weight = m_input_weights[0];
float max_weight = m_input_weights.back();
if (ImGui::SliderFloat("Weight", &m_weight, min_weight, max_weight)) {
m_sync_track = SyncTrack::Blend(m_weight, m_input_0->m_sync_track, m_input_1->m_sync_track);
}
ImGui::Checkbox("Sync Inputs", &m_sync_inputs);
ImGui::Text("SyncTrack");
m_sync_track.DrawDebugUi();
}

View File

@ -0,0 +1,99 @@
//
// Created by martin on 19.11.21.
//
#ifndef ANIMTESTBED_BLENDSPACE1D_H
#define ANIMTESTBED_BLENDSPACE1D_H
#include "../AnimNode.h"
struct BlendSpace1D : public AnimNode {
BlendSpace1D(AnimationController* animation_controller);
virtual ~BlendSpace1D() {}
int m_num_inputs;
std::vector<float> m_input_weights;
std::vector<AnimNode*> m_inputs;
float m_weight;
bool m_sync_inputs;
AnimNode* m_input_0;
AnimNode* m_input_1;
float m_normalized_weight;
float m_weight_0;
float m_weight_1;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_0;
ozz::vector<ozz::math::SoaTransform> m_local_matrices_1;
virtual void Reset() {
m_current_time = 0.f;
}
virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced;
if (m_sync_inputs) {
m_is_time_synced = true;
}
assert (m_input_weights.size() > 0);
assert (m_weight >= m_input_weights[0] && m_weight <= m_input_weights[m_input_weights.size() - 1]);
int prev_idx = 0;
for (int next_idx = 1; next_idx < m_input_weights.size(); next_idx++) {
if (m_input_weights[prev_idx] <= m_weight && m_input_weights[next_idx] >= m_weight) {
m_input_0 = m_inputs[prev_idx];
m_weight_0 = m_input_weights[prev_idx];
m_input_1 = m_inputs[next_idx];
m_weight_1 = m_input_weights[next_idx];
break;
}
prev_idx = next_idx;
}
m_input_0->UpdateIsSynced(m_is_time_synced);
m_input_1->UpdateIsSynced(m_is_time_synced);
m_normalized_weight = (m_weight - m_weight_0) / (m_weight_1 - m_weight_0);
}
virtual void UpdateSyncTrack() override {
if (m_is_time_synced) {
m_sync_track = SyncTrack::Blend(m_normalized_weight, m_input_0->m_sync_track, m_input_1->m_sync_track);
} else {
assert (false);
}
}
virtual void UpdateTime(float dt) {
if (m_is_time_synced) {
m_current_time = fmodf(m_current_time + dt, m_sync_track.m_duration);
float current_sync_time = m_sync_track.CalcSyncFromAbsTime(m_current_time);
m_input_0->UpdateTime(current_sync_time - m_input_0->m_current_time);
m_input_1->UpdateTime(current_sync_time - m_input_1->m_current_time);
} else {
m_current_time += dt;
m_input_0->UpdateTime(dt);
m_input_1->UpdateTime(dt);
}
}
virtual void Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
virtual void GetInputNodes(std::vector<AnimNode*>& input_nodes) const override {
for (int i = 0; i < m_inputs.size(); i++) {
input_nodes.push_back(m_inputs[i]);
}
};
virtual void DrawDebugUi();
};
#endif //ANIMTESTBED_BLENDSPACE1D_H

View File

@ -12,6 +12,7 @@
#include "AnimNodes/AnimSamplerNode.h" #include "AnimNodes/AnimSamplerNode.h"
#include "AnimNodes/BlendNode.h" #include "AnimNodes/BlendNode.h"
#include "AnimNodes/BlendSpace1D.h"
#include "AnimNodes/LockTranslationNode.h" #include "AnimNodes/LockTranslationNode.h"
#include "AnimNodes/SpeedScaleNode.h" #include "AnimNodes/SpeedScaleNode.h"
#include "SkinnedMesh.h" #include "SkinnedMesh.h"
@ -35,21 +36,37 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
sampler_node1->SetAnimation(skinned_mesh->m_animations[2], skinned_mesh->m_animation_sync_track[2]); sampler_node1->SetAnimation(skinned_mesh->m_animations[2], skinned_mesh->m_animation_sync_track[2]);
m_anim_nodes.push_back(sampler_node1); m_anim_nodes.push_back(sampler_node1);
// SpeedScaleNode* speed_node = new SpeedScaleNode(this); AnimSamplerNode* sampler_node2 = new AnimSamplerNode(this);
// speed_node->m_name = "SpeedNode0"; sampler_node2->m_name = "AnimSampler2";
// speed_node->m_input_node = sampler_node0; sampler_node2->SetAnimation(skinned_mesh->m_animations[3], skinned_mesh->m_animation_sync_track[3]);
// m_anim_nodes.push_back(speed_node); m_anim_nodes.push_back(sampler_node2);
BlendSpace1D* blend_space = new BlendSpace1D(this);
blend_space->m_name = "BlendSpace0";
blend_space->m_weight = 0.f;
blend_space->m_inputs.push_back(sampler_node0);
blend_space->m_input_weights.push_back(-1.f);
blend_space->m_inputs.push_back(sampler_node1);
blend_space->m_input_weights.push_back(0.f);
blend_space->m_inputs.push_back(sampler_node2);
blend_space->m_input_weights.push_back(1.f);
m_anim_nodes.push_back(blend_space);
SpeedScaleNode* speed_node = new SpeedScaleNode(this);
speed_node->m_name = "SpeedNode0";
speed_node->m_input_node = sampler_node0;
m_anim_nodes.push_back(speed_node);
BlendNode* blend_node = new BlendNode(this); BlendNode* blend_node = new BlendNode(this);
blend_node->m_name = "Blend0"; blend_node->m_name = "Blend0";
blend_node->m_input_A = sampler_node0; blend_node->m_input_A = speed_node;
blend_node->m_input_B = sampler_node1; blend_node->m_input_B = sampler_node1;
blend_node->m_sync_inputs = true; blend_node->m_sync_inputs = true;
m_anim_nodes.push_back(blend_node); m_anim_nodes.push_back(blend_node);
SpeedScaleNode* speed_node1 = new SpeedScaleNode(this); SpeedScaleNode* speed_node1 = new SpeedScaleNode(this);
speed_node1->m_name = "SpeedNode1"; speed_node1->m_name = "SpeedNode1";
speed_node1->m_input_node = blend_node; speed_node1->m_input_node = blend_space;
m_anim_nodes.push_back(speed_node1); m_anim_nodes.push_back(speed_node1);
LockTranslationNode* lock_node = new LockTranslationNode(this); LockTranslationNode* lock_node = new LockTranslationNode(this);
@ -100,13 +117,17 @@ void AnimationController::UpdateOrderedNodes() {
} }
void AnimationController::UpdateTime(float dt) { void AnimationController::UpdateTime(float dt) {
if (m_paused || m_output_node == nullptr) { if (m_output_node == nullptr) {
return; return;
} }
// Mark all nodes that evaluate time using sync tracks. // Mark all nodes that evaluate time using sync tracks.
m_output_node->UpdateIsSynced(false); m_output_node->UpdateIsSynced(false);
if (m_paused) {
return;
}
// For all synced nodes calculate their current sync track durations // For all synced nodes calculate their current sync track durations
for (int i = m_ordered_nodes.size() - 1; i >= 0; i--) { for (int i = m_ordered_nodes.size() - 1; i >= 0; i--) {
AnimNode* node = m_ordered_nodes[i]; AnimNode* node = m_ordered_nodes[i];