2021-11-12 20:10:56 +01:00
|
|
|
//
|
|
|
|
// Created by martin on 12.11.21.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AnimationController.h"
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <ozz/animation/runtime/local_to_model_job.h>
|
|
|
|
#include <ozz/animation/runtime/sampling_job.h>
|
|
|
|
|
2021-11-13 12:35:23 +01:00
|
|
|
#include <queue>
|
|
|
|
|
2021-11-12 22:12:25 +01:00
|
|
|
#include "AnimNodes/AnimSamplerNode.h"
|
|
|
|
#include "AnimNodes/BlendNode.h"
|
2021-11-16 22:57:58 +01:00
|
|
|
#include "AnimNodes/LockTranslationNode.h"
|
2021-11-12 22:12:25 +01:00
|
|
|
#include "AnimNodes/SpeedScaleNode.h"
|
2021-11-12 20:10:56 +01:00
|
|
|
#include "SkinnedMesh.h"
|
|
|
|
|
|
|
|
AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
|
2021-11-19 12:40:14 +01:00
|
|
|
: m_current_time(0.f), m_paused(false), m_skinned_mesh(skinned_mesh) {
|
2021-11-12 20:10:56 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
ResetAnims();
|
|
|
|
|
2021-11-12 21:54:21 +01:00
|
|
|
AnimSamplerNode* sampler_node0 = new AnimSamplerNode(this);
|
|
|
|
sampler_node0->m_name = "AnimSampler0";
|
2021-11-19 12:40:14 +01:00
|
|
|
sampler_node0->SetAnimation(skinned_mesh->m_animations[1], skinned_mesh->m_animation_sync_track[1]);
|
2021-11-12 21:54:21 +01:00
|
|
|
m_anim_nodes.push_back(sampler_node0);
|
|
|
|
|
|
|
|
AnimSamplerNode* sampler_node1 = new AnimSamplerNode(this);
|
2021-11-13 00:08:32 +01:00
|
|
|
sampler_node1->m_name = "AnimSampler1";
|
2021-11-19 12:40:14 +01:00
|
|
|
sampler_node1->SetAnimation(skinned_mesh->m_animations[2], skinned_mesh->m_animation_sync_track[1]);
|
2021-11-12 21:54:21 +01:00
|
|
|
m_anim_nodes.push_back(sampler_node1);
|
2021-11-12 21:33:53 +01:00
|
|
|
|
2021-11-19 12:40:14 +01:00
|
|
|
// 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);
|
2021-11-12 21:33:53 +01:00
|
|
|
|
2021-11-12 21:54:21 +01:00
|
|
|
BlendNode* blend_node = new BlendNode(this);
|
|
|
|
blend_node->m_name = "Blend0";
|
2021-11-19 12:40:14 +01:00
|
|
|
blend_node->m_input_A = sampler_node0;
|
2021-11-12 21:54:21 +01:00
|
|
|
blend_node->m_input_B = sampler_node1;
|
2021-11-19 12:40:14 +01:00
|
|
|
blend_node->m_sync_inputs = true;
|
2021-11-12 21:54:21 +01:00
|
|
|
m_anim_nodes.push_back(blend_node);
|
|
|
|
|
2021-11-13 00:08:32 +01:00
|
|
|
SpeedScaleNode* speed_node1 = new SpeedScaleNode(this);
|
|
|
|
speed_node1->m_name = "SpeedNode1";
|
|
|
|
speed_node1->m_input_node = blend_node;
|
|
|
|
m_anim_nodes.push_back(speed_node1);
|
|
|
|
|
2021-11-19 12:40:14 +01:00
|
|
|
// LockTranslationNode* lock_node = new LockTranslationNode(this);
|
|
|
|
// lock_node->m_name = "LockNode0";
|
|
|
|
// lock_node->m_locked_bone_index = 0;
|
|
|
|
// lock_node->m_input = speed_node1;
|
2021-11-16 22:57:58 +01:00
|
|
|
|
2021-11-19 12:40:14 +01:00
|
|
|
m_output_node = m_anim_nodes.back();
|
2021-11-12 22:12:25 +01:00
|
|
|
|
2021-11-13 12:35:23 +01:00
|
|
|
UpdateOrderedNodes();
|
2021-11-12 22:12:25 +01:00
|
|
|
|
2021-11-12 21:16:43 +01:00
|
|
|
m_output_node->Reset();
|
2021-11-12 20:10:56 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 21:16:43 +01:00
|
|
|
AnimationController::~AnimationController() {
|
2021-11-12 21:33:53 +01:00
|
|
|
while (m_anim_nodes.size() > 0) {
|
|
|
|
delete m_anim_nodes[m_anim_nodes.size() - 1];
|
|
|
|
m_anim_nodes.pop_back();
|
2021-11-12 20:10:56 +01:00
|
|
|
}
|
2021-11-12 21:33:53 +01:00
|
|
|
|
|
|
|
m_output_node = nullptr;
|
2021-11-12 21:16:43 +01:00
|
|
|
}
|
2021-11-12 20:10:56 +01:00
|
|
|
|
2021-11-13 00:08:32 +01:00
|
|
|
void AnimationController::ResetAnims() {
|
|
|
|
for (int i = 0; i < m_ordered_nodes.size(); i++) {
|
|
|
|
m_ordered_nodes[i]->Reset();
|
|
|
|
}
|
|
|
|
}
|
2021-11-12 21:33:53 +01:00
|
|
|
|
2021-11-13 12:35:23 +01:00
|
|
|
void AnimationController::UpdateOrderedNodes() {
|
|
|
|
std::vector<AnimNode*> node_stack;
|
|
|
|
node_stack.push_back(m_output_node);
|
|
|
|
|
|
|
|
m_ordered_nodes.clear();
|
|
|
|
|
|
|
|
while (node_stack.size() > 0) {
|
|
|
|
AnimNode* node = node_stack.back();
|
|
|
|
m_ordered_nodes.push_back(node);
|
|
|
|
node_stack.pop_back();
|
|
|
|
|
|
|
|
std::vector<AnimNode*> node_inputs;
|
|
|
|
node->GetInputNodes(node_inputs);
|
|
|
|
for (int i = node_inputs.size() - 1; i >= 0; i--) {
|
|
|
|
node_stack.push_back(node_inputs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 18:15:56 +01:00
|
|
|
void AnimationController::UpdateTime(float dt) {
|
2021-11-19 12:40:14 +01:00
|
|
|
if (m_paused || m_output_node == nullptr) {
|
2021-11-12 21:16:43 +01:00
|
|
|
return;
|
2021-11-12 20:10:56 +01:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:35:23 +01:00
|
|
|
// Mark all nodes that evaluate time using sync tracks.
|
2021-11-13 00:08:32 +01:00
|
|
|
m_output_node->UpdateIsSynced(false);
|
|
|
|
|
2021-11-13 12:35:23 +01:00
|
|
|
// For all synced nodes calculate their current sync track durations
|
2021-11-13 00:08:32 +01:00
|
|
|
for (int i = m_ordered_nodes.size() - 1; i >= 0; i--) {
|
|
|
|
AnimNode* node = m_ordered_nodes[i];
|
|
|
|
if (node->m_is_time_synced) {
|
2021-11-19 12:40:14 +01:00
|
|
|
node->UpdateSyncTrack();
|
2021-11-13 00:08:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:35:23 +01:00
|
|
|
// Update the time of all nodes.
|
2021-11-13 00:08:32 +01:00
|
|
|
m_output_node->UpdateTime(dt);
|
2021-11-12 21:33:53 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 21:16:43 +01:00
|
|
|
void AnimationController::Evaluate() {
|
|
|
|
if (m_output_node == nullptr) {
|
|
|
|
return;
|
2021-11-12 20:10:56 +01:00
|
|
|
}
|
2021-11-19 12:40:14 +01:00
|
|
|
|
|
|
|
m_output_node->Evaluate(&m_skinned_mesh->m_local_matrices);
|
2021-11-12 20:10:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void AnimationController::DrawDebugUi() {
|
2021-11-13 00:08:32 +01:00
|
|
|
ImGui::SetNextWindowSize(ImVec2(500, 300), ImGuiCond_FirstUseEver);
|
2021-11-12 20:10:56 +01:00
|
|
|
ImGui::Begin("AnimationController");
|
2021-11-13 00:08:32 +01:00
|
|
|
|
|
|
|
if (ImGui::Button("Reset")) {
|
|
|
|
ResetAnims();
|
|
|
|
}
|
2021-11-19 12:40:14 +01:00
|
|
|
ImGui::SameLine();
|
|
|
|
if (m_paused) {
|
|
|
|
if (ImGui::Button("Play")) {
|
|
|
|
m_paused = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ImGui::Button("Pause")) {
|
|
|
|
m_paused = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("Step")) {
|
|
|
|
bool was_paused = m_paused;
|
|
|
|
m_paused = false;
|
|
|
|
UpdateTime(0.1);
|
|
|
|
Evaluate();
|
|
|
|
m_paused = was_paused;
|
|
|
|
}
|
2021-11-13 00:08:32 +01:00
|
|
|
|
2021-11-16 21:17:52 +01:00
|
|
|
ImVec2 node_size(200, 100);
|
|
|
|
for (int i = 0; i < m_ordered_nodes.size(); i++) {
|
|
|
|
AnimNode* node = m_ordered_nodes[i];
|
2021-11-12 20:10:56 +01:00
|
|
|
|
2021-11-16 21:17:52 +01:00
|
|
|
ImGui::SetNextWindowSize(node_size, ImGuiCond_FirstUseEver);
|
|
|
|
ImGui::SetNextWindowPos(
|
|
|
|
ImVec2((m_ordered_nodes.size() - 1 - i) * node_size.x - i * 10, 300),
|
|
|
|
ImGuiCond_FirstUseEver);
|
|
|
|
ImGui::Begin(node->m_name.c_str());
|
|
|
|
node->DrawDebugUi();
|
|
|
|
ImGui::End();
|
2021-11-12 20:10:56 +01:00
|
|
|
}
|
|
|
|
|
2021-11-19 12:40:14 +01:00
|
|
|
ImGui::Text ("Node States");
|
|
|
|
|
2021-11-16 21:17:52 +01:00
|
|
|
ImGui::Columns(4, "Node States"); // 4-ways, with border
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::Text("Name");
|
|
|
|
ImGui::NextColumn();
|
|
|
|
ImGui::Text("Synced");
|
|
|
|
ImGui::NextColumn();
|
|
|
|
ImGui::Text("Duration");
|
|
|
|
ImGui::NextColumn();
|
|
|
|
ImGui::Text("Time");
|
|
|
|
ImGui::NextColumn();
|
2021-11-12 22:12:25 +01:00
|
|
|
|
2021-11-16 21:17:52 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
static int selected = -1;
|
2021-11-13 00:08:32 +01:00
|
|
|
|
2021-11-16 21:17:52 +01:00
|
|
|
for (int i = 0; i < m_ordered_nodes.size(); i++) {
|
|
|
|
AnimNode* node = m_ordered_nodes[i];
|
|
|
|
if (ImGui::Selectable(
|
|
|
|
node->m_name.c_str(),
|
|
|
|
selected == i,
|
|
|
|
ImGuiSelectableFlags_SpanAllColumns))
|
|
|
|
selected = i;
|
|
|
|
bool hovered = ImGui::IsItemHovered();
|
|
|
|
ImGui::NextColumn();
|
|
|
|
|
|
|
|
ImGui::Text(node->m_is_time_synced ? "X" : "-");
|
|
|
|
ImGui::NextColumn();
|
|
|
|
|
2021-11-19 12:40:14 +01:00
|
|
|
ImGui::PushID((void*)&node->m_sync_track.m_duration);
|
|
|
|
ImGui::Text("%2.3f", node->m_sync_track.m_duration);
|
2021-11-16 21:17:52 +01:00
|
|
|
ImGui::NextColumn();
|
|
|
|
ImGui::PopID();
|
|
|
|
|
|
|
|
ImGui::PushID((void*)&node->m_current_time);
|
|
|
|
ImGui::Text("%2.3f", node->m_current_time);
|
|
|
|
ImGui::NextColumn();
|
|
|
|
ImGui::PopID();
|
2021-11-12 22:12:25 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:17:52 +01:00
|
|
|
ImGui::Columns(1);
|
|
|
|
ImGui::Separator();
|
|
|
|
|
2021-11-12 20:10:56 +01:00
|
|
|
ImGui::End();
|
|
|
|
}
|