From 382730960f7cdf46e73a024bdb11c79f9d44b197 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 19 Nov 2021 22:05:59 +0100 Subject: [PATCH] Transition walking-zombie look good. --- src/AnimNodes/AnimSamplerNode.cc | 3 +-- src/AnimNodes/AnimSamplerNode.h | 5 ++--- src/AnimNodes/BlendNode.cc | 4 +++- src/AnimNodes/BlendNode.h | 17 ++++------------- src/AnimationController.cc | 13 +++++++------ src/SyncTrack.h | 8 ++++++++ 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/AnimNodes/AnimSamplerNode.cc b/src/AnimNodes/AnimSamplerNode.cc index 7fee075..27458e5 100644 --- a/src/AnimNodes/AnimSamplerNode.cc +++ b/src/AnimNodes/AnimSamplerNode.cc @@ -23,10 +23,9 @@ void AnimSamplerNode::SetAnimation(ozz::animation::Animation* animation, const S void AnimSamplerNode::Evaluate( ozz::vector* local_matrices) { ozz::animation::SamplingJob sampling_job; - float ratio = m_sync_track.CalcRatioFromSyncTime(m_anim_ratio); sampling_job.animation = m_animation; sampling_job.cache = &m_sampling_cache; - sampling_job.ratio = ratio; + 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; diff --git a/src/AnimNodes/AnimSamplerNode.h b/src/AnimNodes/AnimSamplerNode.h index 615218e..6053873 100644 --- a/src/AnimNodes/AnimSamplerNode.h +++ b/src/AnimNodes/AnimSamplerNode.h @@ -38,10 +38,9 @@ struct AnimSamplerNode : public AnimNode { m_current_time += dt; if (!m_override_ratio) { if (m_is_time_synced) { - m_anim_ratio = fmodf((float)m_current_time, 1.0f); - m_current_time = m_anim_ratio; + m_anim_ratio = m_sync_track.CalcRatioFromSyncTime(m_current_time); } else { - m_anim_ratio = fmodf((float)m_current_time / m_sync_track.m_duration, 1.0f); + m_anim_ratio = fmodf((float)m_current_time / m_animation->duration(), 1.0f); } } diff --git a/src/AnimNodes/BlendNode.cc b/src/AnimNodes/BlendNode.cc index 1dfeb63..5d1b75b 100644 --- a/src/AnimNodes/BlendNode.cc +++ b/src/AnimNodes/BlendNode.cc @@ -49,7 +49,9 @@ void BlendNode::Evaluate(ozz::vector* local_matrices) { } void BlendNode::DrawDebugUi() { - ImGui::SliderFloat("Weight", &m_weight, 0.f, 1.f); + if (ImGui::SliderFloat("Weight", &m_weight, 0.f, 1.f)) { + m_sync_track = SyncTrack::Blend(m_weight, m_input_A->m_sync_track, m_input_B->m_sync_track); + } ImGui::Checkbox("Sync Inputs", &m_sync_inputs); ImGui::Text("SyncTrack"); diff --git a/src/AnimNodes/BlendNode.h b/src/AnimNodes/BlendNode.h index ab1d666..997ff87 100644 --- a/src/AnimNodes/BlendNode.h +++ b/src/AnimNodes/BlendNode.h @@ -45,20 +45,11 @@ struct BlendNode : public AnimNode { virtual void UpdateTime(float dt) { if (m_is_time_synced) { - float current_time_absolute = fmodf(m_current_time * m_sync_track.m_duration + dt, 1.0f); - float current_sync_time = m_sync_track.CalcSyncFromAbsTime(current_time_absolute); - m_current_time = m_sync_track.CalcRatioFromSyncTime(current_sync_time); + m_current_time = fmodf(m_current_time + dt, m_sync_track.m_duration); + float current_sync_time = m_sync_track.CalcSyncFromAbsTime(m_current_time); - float time_absolute_A = fmodf (m_input_A->m_current_time * m_input_A->m_sync_track.m_duration, 1.0f); - float sync_time_A = m_input_A->m_sync_track.CalcSyncFromAbsTime(time_absolute_A); - float m_ratio_A = m_input_A->m_sync_track.CalcSyncFromAbsTime(sync_time_A); - - float time_absolute_B = fmodf (m_input_B->m_current_time * m_input_B->m_sync_track.m_duration, 1.0f); - float sync_time_B = m_input_B->m_sync_track.CalcSyncFromAbsTime(time_absolute_B); - float m_ratio_B = m_input_B->m_sync_track.CalcSyncFromAbsTime(sync_time_B); - - m_input_A->UpdateTime(current_sync_time); - m_input_B->UpdateTime(current_sync_time); + m_input_A->UpdateTime(current_sync_time - m_input_A->m_current_time); + m_input_B->UpdateTime(current_sync_time - m_input_B->m_current_time); } else { m_current_time += dt; m_input_A->UpdateTime(dt); diff --git a/src/AnimationController.cc b/src/AnimationController.cc index 99f5f9a..cb266d3 100644 --- a/src/AnimationController.cc +++ b/src/AnimationController.cc @@ -17,7 +17,7 @@ #include "SkinnedMesh.h" AnimationController::AnimationController(SkinnedMesh* skinned_mesh) - : m_current_time(0.f), m_paused(false), m_skinned_mesh(skinned_mesh) { + : m_current_time(0.f), m_paused(true), m_skinned_mesh(skinned_mesh) { 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); @@ -32,7 +32,7 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh) AnimSamplerNode* sampler_node1 = new AnimSamplerNode(this); sampler_node1->m_name = "AnimSampler1"; - sampler_node1->SetAnimation(skinned_mesh->m_animations[2], skinned_mesh->m_animation_sync_track[1]); + sampler_node1->SetAnimation(skinned_mesh->m_animations[2], skinned_mesh->m_animation_sync_track[2]); m_anim_nodes.push_back(sampler_node1); // SpeedScaleNode* speed_node = new SpeedScaleNode(this); @@ -52,10 +52,11 @@ AnimationController::AnimationController(SkinnedMesh* skinned_mesh) speed_node1->m_input_node = blend_node; m_anim_nodes.push_back(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; + 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_anim_nodes.push_back(lock_node); m_output_node = m_anim_nodes.back(); diff --git a/src/SyncTrack.h b/src/SyncTrack.h index ead2844..00fd44f 100644 --- a/src/SyncTrack.h +++ b/src/SyncTrack.h @@ -11,6 +11,14 @@ constexpr int cSyncTrackMaxIntervals = 8; struct SyncTrack { + SyncTrack() : m_duration(0.f), m_num_intervals(0) { + for (int i = 0; i < cSyncTrackMaxIntervals; i++) { + m_sync_markers[i] = 0.f; + m_interval_ratio[i] = 0.f; + m_interval_ratio[i] = 0.f; + } + } + float m_duration; int m_num_intervals; float m_sync_markers[cSyncTrackMaxIntervals];