Compare commits

...

4 Commits

Author SHA1 Message Date
Martin Felis
554125dde2 Added LockTranslationNode. 2021-11-16 22:57:58 +01:00
Martin Felis
d006b43d04 Added additional sync track tests. 2021-11-16 22:57:45 +01:00
Martin Felis
5e6e81b60b Implemented node like debug gui for anim nodes. 2021-11-16 21:17:52 +01:00
Martin Felis
1cfba1c388 Implemented simple tests for sync track calculations. 2021-11-16 20:14:22 +01:00
9 changed files with 386 additions and 118 deletions

View File

@ -48,6 +48,7 @@ add_library(AnimTestbedCode OBJECT
src/AnimNodes/AnimSamplerNode.cc
src/AnimNodes/SpeedScaleNode.cc
src/AnimNodes/BlendNode.cc
src/AnimNodes/LockTranslationNode.cc
src/AnimationController.cc
3rdparty/imgui/imgui.cpp
3rdparty/imgui/imgui_draw.cpp

View File

@ -18,9 +18,8 @@ void AnimSamplerNode::SetAnimation(ozz::animation::Animation* animation) {
m_sampling_cache.Resize(num_joints);
}
void AnimSamplerNode::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) {
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;
@ -31,11 +30,7 @@ void AnimSamplerNode::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matri
}
}
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};
@ -53,7 +48,4 @@ void AnimSamplerNode::DrawDebugUi() {
ImGui::Checkbox("Override", &m_override_ratio);
ImGui::SameLine();
ImGui::SliderFloat("Ratio", &m_anim_ratio, 0.f, 1.f);
ImGui::TreePop();
}
}

View File

@ -49,14 +49,6 @@ void BlendNode::Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) {
}
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::Checkbox("Sync Inputs", &m_sync_inputs);
ImGui::TreePop();
}
}

View File

@ -0,0 +1,59 @@
//
// Created by martin on 16.11.21.
//
#include "LockTranslationNode.h"
#include <imgui.h>
#include "ozz/base/maths/soa_transform.h"
void LockTranslationNode::Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) {
m_input->Evaluate(local_matrices);
ozz::math::SoaFloat3 translation = (*local_matrices)[m_locked_bone_index].translation;
float x[4];
float y[4];
float z[4];
_mm_store_ps(x, translation.x);
_mm_store_ps(y, translation.y);
_mm_store_ps(z, translation.z);
if (m_lock_x) {
x[0] = 0.f;
}
if (m_lock_y) {
y[0] = 0.f;
}
if (m_lock_z) {
z[0] = 0.f;
}
translation.x = _mm_load_ps(x);
translation.y = _mm_load_ps(y);
translation.z = _mm_load_ps(z);
//translation = ozz::math::SoaFloat3::zero();
// ozz::math::SetX(translation, 0.f);
// ozz::math::SetZ(translation, 0.f);
(*local_matrices)[m_locked_bone_index].translation = translation;
}
void LockTranslationNode::DrawDebugUi() {
const ozz::animation::Skeleton& skeleton = m_animation_controller->m_skinned_mesh->m_skeleton;
ozz::span<const char* const> joint_names = skeleton.joint_names();
const char* items[255] = {0};
int item_current = 0;
for (int i = 0; i < joint_names.size(); i++) {
items[i] = joint_names[i];
}
ImGui::Combo("Bone", &m_locked_bone_index, items, joint_names.size());
ImGui::Checkbox("Lock X", &m_lock_x);
ImGui::Checkbox("Lock Y", &m_lock_y);
ImGui::Checkbox("Lock Z", &m_lock_z);
}

View File

@ -0,0 +1,52 @@
//
// Created by martin on 16.11.21.
//
#ifndef ANIMTESTBED_LOCKBONES_H
#define ANIMTESTBED_LOCKBONES_H
#include "../AnimNode.h"
struct LockTranslationNode : public AnimNode {
LockTranslationNode(AnimationController* animation_controller)
: AnimNode(animation_controller),
m_input(nullptr),
m_locked_bone_index(0),
m_lock_x(false),
m_lock_y(false),
m_lock_z(false) {}
virtual ~LockTranslationNode() {}
AnimNode* m_input;
int m_locked_bone_index;
bool m_lock_x;
bool m_lock_y;
bool m_lock_z;
virtual void Reset() { m_current_time = 0.f; }
virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced;
m_input->UpdateIsSynced(m_is_time_synced);
}
virtual void EvalAnimDuration() override {
m_anim_duration = m_input->m_anim_duration;
}
virtual void UpdateTime(float dt) { m_input->UpdateTime(dt); }
virtual void Evaluate(
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
virtual void GetInputNodes(
std::vector<AnimNode*>& input_nodes) const override {
input_nodes.push_back(m_input);
};
virtual void DrawDebugUi() override;
};
#endif //ANIMTESTBED_LOCKBONES_H

View File

@ -7,8 +7,6 @@
#include <imgui.h>
void SpeedScaleNode::DrawDebugUi() {
std::string node_name = "SpeedScaleNode: " + m_name;
if (ImGui::TreeNode(node_name.c_str())) {
bool is_negative = m_time_scale < 0.f;
if (ImGui::Checkbox("Reverse Time", &is_negative)) {
m_time_scale = m_time_scale * -1.f;
@ -19,9 +17,4 @@ void SpeedScaleNode::DrawDebugUi() {
ImGui::SliderFloat("Time Scale", &m_time_scale, 0.01f, 5.f);
// and back to the original negative or positive sign
m_time_scale = m_time_scale * (is_negative ? -1.f : 1.f);
m_input_node->DrawDebugUi();
ImGui::TreePop();
}
}

View File

@ -5,7 +5,6 @@
#include "AnimationController.h"
#include <imgui.h>
#include <ozz/animation/runtime/blending_job.h>
#include <ozz/animation/runtime/local_to_model_job.h>
#include <ozz/animation/runtime/sampling_job.h>
@ -13,6 +12,7 @@
#include "AnimNodes/AnimSamplerNode.h"
#include "AnimNodes/BlendNode.h"
#include "AnimNodes/LockTranslationNode.h"
#include "AnimNodes/SpeedScaleNode.h"
#include "SkinnedMesh.h"
@ -54,15 +54,18 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh)
speed_node1->m_input_node = blend_node;
m_anim_nodes.push_back(speed_node1);
m_output_node = speed_node1;
LockTranslationNode* lock_node = new LockTranslationNode(this);
lock_node->m_name = "LockNode0";
lock_node->m_locked_bone_index = 0;
lock_node->m_input = speed_node1;
m_output_node = lock_node;
UpdateOrderedNodes();
m_output_node->Reset();
}
AnimationController::~AnimationController() {
while (m_anim_nodes.size() > 0) {
delete m_anim_nodes[m_anim_nodes.size() - 1];
@ -97,9 +100,7 @@ void AnimationController::UpdateOrderedNodes() {
}
}
void AnimationController::UpdateBlendLogic() {
}
void AnimationController::UpdateBlendLogic() {}
void AnimationController::UpdateTime(float dt) {
if (m_output_node == nullptr) {
@ -121,8 +122,6 @@ void AnimationController::UpdateTime(float dt) {
m_output_node->UpdateTime(dt);
}
void AnimationController::Evaluate() {
if (m_output_node == nullptr) {
return;
@ -137,8 +136,6 @@ void AnimationController::Evaluate() {
ltm_job.Run();
};
void AnimationController::DrawDebugUi() {
ImGui::SetNextWindowSize(ImVec2(500, 300), ImGuiCond_FirstUseEver);
ImGui::Begin("AnimationController");
@ -147,46 +144,59 @@ void AnimationController::DrawDebugUi() {
ResetAnims();
}
if (m_output_node && ImGui::TreeNode("Output")) {
m_output_node->DrawDebugUi();
ImVec2 node_size(200, 100);
for (int i = 0; i < m_ordered_nodes.size(); i++) {
AnimNode* node = m_ordered_nodes[i];
ImGui::TreePop();
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();
}
if (m_output_node && ImGui::TreeNode("Nodes")) {
ImGui::Columns(4, "NodeOverview"); // 4-ways, with border
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();
ImGui::Text("Name");
ImGui::NextColumn();
ImGui::Text("Synced");
ImGui::NextColumn();
ImGui::Text("Duration");
ImGui::NextColumn();
ImGui::Text("Time");
ImGui::NextColumn();
ImGui::Separator();
static int selected = -1;
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))
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();
ImGui::Text(node->m_is_time_synced ? "X" : "-");
ImGui::NextColumn();
ImGui::PushID((void*)&node->m_anim_duration);
ImGui::Text("%2.3f", node->m_anim_duration); ImGui::NextColumn();
ImGui::Text("%2.3f", node->m_anim_duration);
ImGui::NextColumn();
ImGui::PopID();
ImGui::PushID((void*)&node->m_current_time);
ImGui::Text("%2.3f", node->m_current_time); ImGui::NextColumn();
ImGui::Text("%2.3f", node->m_current_time);
ImGui::NextColumn();
ImGui::PopID();
}
ImGui::Columns(1);
ImGui::Separator();
ImGui::TreePop();
}
ImGui::End();
}

View File

@ -25,35 +25,90 @@ constexpr int cSyncTrackMaxIntervals = 8;
struct SyncTrack {
float m_duration;
int m_num_intervals;
float m_sync_markers[cSyncTrackMaxIntervals];
float m_interval_start[cSyncTrackMaxIntervals];
float m_interval_end[cSyncTrackMaxIntervals];
float m_interval_durations[cSyncTrackMaxIntervals];
float ConvertAbsTimeToSyncTime (float abs_time) {
void CalcIntervals() {
int i;
if (m_num_intervals == 0) {
m_num_intervals = 1;
m_interval_start[0] = 0.f;
m_interval_end[0] = 1.f;
} else {
for (i = 0; i < m_num_intervals; i++) {
int end_index = i < m_num_intervals - 1 ? i + 1 : 0;
m_interval_start[i] = m_sync_markers[i];
m_interval_end[i] = m_sync_markers[end_index];
if (m_interval_end[i] > m_interval_start[i]) {
m_interval_durations[i] = m_interval_end[i] - m_interval_start[i];
} else {
m_interval_durations[i] =
m_interval_end[i] + (1. - m_interval_start[i]);
}
}
}
}
float CalcSyncFromAbsTime(float abs_time) {
float sync_time = fmodf(abs_time, m_duration) / m_duration;
int interval_index = 0;
while (sync_time < m_interval_durations[interval_index]) {
while (sync_time >= m_interval_durations[interval_index]) {
sync_time -= m_interval_durations[interval_index];
interval_index++;
}
return float(interval_index)
+ sync_time / m_interval_durations[interval_index];
}
static SyncTrack Blend(float weight, const SyncTrack& track_A, const SyncTrack& track_B) {
SyncTrack result;
float CalcRatioFromSyncTime(float sync_time) {
float interval_ratio = fmodf(sync_time, 1.0f);
int interval = int(sync_time - interval_ratio);
return fmodf(
m_interval_start[interval]
+ m_interval_durations[interval] * interval_ratio,
1.0f);
}
static SyncTrack
Blend(float weight, const SyncTrack& track_A, const SyncTrack& track_B) {
assert(track_A.m_num_intervals == track_B.m_num_intervals);
SyncTrack result;
result.m_num_intervals = track_A.m_num_intervals;
result.m_duration = (1.0f - weight) * track_A.m_duration + weight * track_B.m_duration;
result.m_duration =
(1.0f - weight) * track_A.m_duration + weight * track_B.m_duration;
for (int i = 0; i < result.m_num_intervals; i++) {
result.m_interval_durations[i] = (1.0f - weight) * track_A.m_interval_durations[i] + weight * track_B.m_interval_durations[i];
result.m_interval_durations[i] =
(1.0f - weight) * track_A.m_interval_durations[i]
+ weight * track_B.m_interval_durations[i];
result.m_interval_start[i] =
(1.0f - weight) * track_A.m_interval_start[i]
+ weight * track_B.m_interval_start[i];
result.m_interval_end[i] =
(1.0f - weight) * track_A.m_interval_end[i]
+ weight * track_B.m_interval_end[i];
result.m_sync_markers[i] =
(1.0f - weight) * track_A.m_sync_markers[i]
+ weight * track_B.m_sync_markers[i];
}
return result;
}
};
struct SkinnedMesh {
virtual ~SkinnedMesh();
bool LoadSkeleton(const char* filename);
@ -61,8 +116,12 @@ struct SkinnedMesh {
//bool LoadMesh (const char* filename);
void SetCurrentAnimation(int index);
const ozz::animation::Animation* GetCurrentAnimation() {return m_current_animation; };
float GetCurrentAnimationDuration() {return m_current_animation->duration(); };
const ozz::animation::Animation* GetCurrentAnimation() {
return m_current_animation;
};
float GetCurrentAnimationDuration() {
return m_current_animation->duration();
};
void EvalAnimation(float in_time);
void DrawSkeleton();

View File

@ -2,22 +2,69 @@
// Created by martin on 16.11.21.
//
#include "SkinnedMesh.h"
#include "catch.hpp"
#include "SkinnedMesh.h"
TEST_CASE("SyncTrackBlendSimple", "[SyncTrackBlend]") {
TEST_CASE("Basic", "[SyncTrack]") {
SyncTrack track_A;
track_A.m_num_intervals = 2;
track_A.m_duration = 1.0;
track_A.m_interval_durations[0] = 0.8;
track_A.m_interval_durations[1] = 0.2;
track_A.m_duration = 2.0;
track_A.m_interval_start[0] = 0.f;
track_A.m_interval_end[0] = 0.7f;
track_A.m_interval_durations[0] = 0.7;
track_A.m_interval_start[1] = 0.7f;
track_A.m_interval_end[1] = 1.0f;
track_A.m_interval_durations[1] = 0.3;
SyncTrack track_B;
track_B.m_num_intervals = 2;
track_B.m_duration = 2.0;
track_B.m_interval_durations[0] = 0.1;
track_B.m_interval_durations[1] = 0.9;
track_B.m_duration = 1.5;
track_B.m_interval_start[0] = 0.0f;
track_B.m_interval_end[0] = 0.6f;
track_B.m_interval_durations[0] = 0.6;
track_B.m_interval_start[1] = 0.6f;
track_B.m_interval_end[1] = 1.0f;
track_B.m_interval_durations[1] = 0.4;
WHEN("Calculating sync time of track_B at 0.5 duration") {
float sync_time_at_0_75 =
track_B.CalcSyncFromAbsTime(0.5 * track_B.m_duration);
REQUIRE(sync_time_at_0_75 == Catch::Detail::Approx(0.83333));
}
WHEN("Calculating sync time of track_B at 0.6 duration") {
float sync_time_at_0_6 =
track_B.CalcSyncFromAbsTime(0.6 * track_B.m_duration);
REQUIRE(sync_time_at_0_6 == Catch::Detail::Approx(1.0));
}
WHEN("Calculating sync time of track_B at 0.7 duration") {
float sync_time_at_0_7 =
track_B.CalcSyncFromAbsTime(0.7 * track_B.m_duration);
REQUIRE(sync_time_at_0_7 == Catch::Detail::Approx(1.25));
}
WHEN("Calculating sync time of track_B at 0.0 duration") {
float sync_time_at_1_0 =
track_B.CalcSyncFromAbsTime(0.0 * track_B.m_duration);
REQUIRE(sync_time_at_1_0 == Catch::Detail::Approx(0.0));
}
WHEN("Calculating sync time of track_B at 1.0 duration") {
float sync_time_at_1_0 =
track_B.CalcSyncFromAbsTime(0.9999 * track_B.m_duration);
REQUIRE(sync_time_at_1_0 == Catch::Detail::Approx(2.0).epsilon(0.001f));
}
WHEN("Calculating ratio from sync time on track_A at 0.83333") {
float ratio = track_A.CalcRatioFromSyncTime(0.83333333);
REQUIRE(ratio == Catch::Detail::Approx(0.5833333));
}
WHEN("Calculating ratio from sync time on track_A at 0.83333") {
float ratio = track_A.CalcRatioFromSyncTime(1.25);
REQUIRE(ratio == Catch::Detail::Approx(0.775));
}
WHEN("Blending two synctracks with weight 0.") {
SyncTrack blended = SyncTrack::Blend(0.f, track_A, track_B);
@ -28,6 +75,15 @@ TEST_CASE("SyncTrackBlendSimple", "[SyncTrackBlend]") {
blended.m_interval_durations[0] == track_A.m_interval_durations[0]);
REQUIRE(
blended.m_interval_durations[1] == track_A.m_interval_durations[1]);
REQUIRE(blended.m_sync_markers[0] == track_A.m_sync_markers[0]);
REQUIRE(blended.m_sync_markers[1] == track_A.m_sync_markers[1]);
REQUIRE(blended.m_interval_start[0] == track_A.m_interval_start[0]);
REQUIRE(blended.m_interval_start[1] == track_A.m_interval_start[1]);
REQUIRE(blended.m_interval_end[0] == track_A.m_interval_end[0]);
REQUIRE(blended.m_interval_end[1] == track_A.m_interval_end[1]);
}
}
@ -40,6 +96,60 @@ TEST_CASE("SyncTrackBlendSimple", "[SyncTrackBlend]") {
blended.m_interval_durations[0] == track_B.m_interval_durations[0]);
REQUIRE(
blended.m_interval_durations[1] == track_B.m_interval_durations[1]);
REQUIRE(blended.m_sync_markers[0] == track_B.m_sync_markers[0]);
REQUIRE(blended.m_sync_markers[1] == track_B.m_sync_markers[1]);
REQUIRE(blended.m_interval_start[0] == track_B.m_interval_start[0]);
REQUIRE(blended.m_interval_start[1] == track_B.m_interval_start[1]);
REQUIRE(blended.m_interval_end[0] == track_B.m_interval_end[0]);
REQUIRE(blended.m_interval_end[1] == track_B.m_interval_end[1]);
}
}
}
TEST_CASE("Sync Marker Interval Calculation", "[SyncTrack]") {
SyncTrack track_A;
track_A.m_num_intervals = 2;
track_A.m_duration = 2.0;
track_A.m_sync_markers[0] = 0.9;
track_A.m_sync_markers[1] = 0.2;
WHEN("Calculating intervals") {
track_A.CalcIntervals();
CHECK(track_A.m_interval_start[0] == 0.9f);
CHECK(track_A.m_interval_end[0] == 0.2f);
CHECK(track_A.m_interval_durations[0] == 0.3f);
CHECK(track_A.m_interval_start[1] == 0.2f);
CHECK(track_A.m_interval_end[1] == 0.9f);
CHECK(track_A.m_interval_durations[1] == 0.7f);
WHEN("Querying ratio at sync time at 1.001") {
float ratio = track_A.CalcRatioFromSyncTime(1.0001f);
CHECK(ratio == Catch::Detail::Approx(0.2).epsilon(0.001));
}
WHEN("Querying ratio at sync time at 1.001") {
float ratio = track_A.CalcRatioFromSyncTime(0.0001f);
CHECK(ratio == Catch::Detail::Approx(0.9).epsilon(0.001));
}
WHEN("Querying ratio at sync time at 1.9999") {
float ratio = track_A.CalcRatioFromSyncTime(0.9999f);
CHECK(ratio == Catch::Detail::Approx(0.2).epsilon(0.001));
}
}
WHEN ("Blending with another sync track") {
SyncTrack track_B;
track_B.m_num_intervals = 2;
track_B.m_duration = 1.0;
track_B.m_sync_markers[0] = 0.9;
track_B.m_sync_markers[1] = 0.2;
}
}