From b8d863c8e10fdef74619906fa75d5e6da0260ccb Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 3 Dec 2021 15:58:20 +0100 Subject: [PATCH] Renamed m_current_time to m_time_current to simplify root bone sampling. --- src/AnimNode.h | 6 ++--- src/AnimNodes/AnimSamplerNode.h | 42 +++++++++++++++++------------ src/AnimNodes/BlendNode.h | 27 ++++++++++--------- src/AnimNodes/BlendSpace1D.h | 38 +++++++++++++++----------- src/AnimNodes/LockTranslationNode.h | 2 +- src/AnimNodes/SpeedScaleNode.h | 6 ++--- src/AnimationController.cc | 4 +-- tests/AnimSampleNodeTests.cc | 2 +- 8 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/AnimNode.h b/src/AnimNode.h index f35f241..446fcf2 100644 --- a/src/AnimNode.h +++ b/src/AnimNode.h @@ -17,7 +17,7 @@ enum class AnimNodeType { Blend, SpeedScale, AnimSampler }; struct AnimNode { AnimNode(AnimationController* animation_controller) : m_animation_controller(animation_controller), - m_current_time(0.f), + m_time_current(0.f), m_is_time_synced(false) {} virtual ~AnimNode(){}; @@ -27,10 +27,10 @@ struct AnimNode { // When synced then current time is relative to the node's anim duration.:w bool m_is_time_synced; - float m_current_time; + float m_time_current; SyncTrack m_sync_track; - virtual void Reset() { m_current_time = 0.f; } + virtual void Reset() { m_time_current = 0.f; } // Mark current node according to is_synced and propagate flag to animation inputs. virtual void UpdateIsSynced(bool is_synced) = 0; diff --git a/src/AnimNodes/AnimSamplerNode.h b/src/AnimNodes/AnimSamplerNode.h index 4fc8c69..58b3acd 100644 --- a/src/AnimNodes/AnimSamplerNode.h +++ b/src/AnimNodes/AnimSamplerNode.h @@ -10,16 +10,18 @@ struct AnimSamplerNode : public AnimNode { AnimSamplerNode(AnimationController* animation_controller) : AnimNode(animation_controller), + m_time_prev(0.f), m_override_ratio(false), m_anim_ratio(0.f), m_root_bone_index(0) { - assert(m_current_time < 100.0f); + assert(m_time_current < 100.0f); m_anim_node_type = AnimNodeType::AnimSampler; }; virtual ~AnimSamplerNode() {} ozz::animation::Animation* m_animation; + float m_time_prev; bool m_override_ratio; float m_anim_ratio; float m_anim_ratio_prev; @@ -40,22 +42,28 @@ struct AnimSamplerNode : public AnimNode { virtual void UpdateSyncTrack() override {} virtual void UpdateTime(float dt) override { - m_current_time += dt; - if (!m_override_ratio) { - if (m_is_time_synced) { - m_anim_ratio = m_sync_track.CalcRatioFromSyncTime(m_current_time); - float prev_sync_time = m_current_time - dt; - if (m_current_time < 0) { - prev_sync_time += m_sync_track.m_num_intervals; - } - m_anim_ratio_prev = m_sync_track.CalcRatioFromSyncTime(prev_sync_time); - } else { - m_anim_ratio = - fmodf((float)m_current_time / m_animation->duration(), 1.0f); - m_anim_ratio_prev = fmodf((float)m_current_time / m_animation->duration(), 1.0f); - if (m_anim_ratio_prev < 0) { - m_anim_ratio_prev += 1.0f; - } + if (m_override_ratio) { + return; + } + + m_time_prev = m_time_current; + m_time_current += dt; + + if (m_is_time_synced) { + m_anim_ratio = m_sync_track.CalcRatioFromSyncTime(m_time_current); + float prev_sync_time = m_time_current - dt; + if (m_time_current < 0) { + prev_sync_time += m_sync_track.m_num_intervals; + } + m_anim_ratio_prev = m_sync_track.CalcRatioFromSyncTime(prev_sync_time); + } else { + m_anim_ratio = + fmodf((float)m_time_current / m_animation->duration(), 1.0f); + m_anim_ratio_prev = + fmodf((float)m_time_prev / m_animation->duration(), 1.0f); + + if (m_anim_ratio_prev < 0) { + m_anim_ratio_prev += 1.0f; } } diff --git a/src/AnimNodes/BlendNode.h b/src/AnimNodes/BlendNode.h index 76b97b3..7d187b5 100644 --- a/src/AnimNodes/BlendNode.h +++ b/src/AnimNodes/BlendNode.h @@ -22,9 +22,7 @@ struct BlendNode : public AnimNode { ozz::math::Transform m_root_transform_A; ozz::math::Transform m_root_transform_B; - virtual void Reset() { - m_current_time = 0.f; - } + virtual void Reset() { m_time_current = 0.f; } virtual void UpdateIsSynced(bool is_synced) override { m_is_time_synced = is_synced; @@ -39,24 +37,28 @@ struct BlendNode : public AnimNode { virtual void UpdateSyncTrack() override { if (m_is_time_synced) { - m_sync_track = SyncTrack::Blend(m_weight, m_input_A->m_sync_track, m_input_B->m_sync_track); + m_sync_track = SyncTrack::Blend( + m_weight, + m_input_A->m_sync_track, + m_input_B->m_sync_track); } else { - assert (false); + assert(false); } } virtual void UpdateTime(float dt) { if (m_is_time_synced) { - float sync_time_old = m_sync_track.CalcSyncFromAbsTime(m_current_time); - m_current_time = fmodf(m_current_time + dt, m_sync_track.m_duration); - float sync_time_dt = m_sync_track.CalcSyncFromAbsTime(m_current_time) - sync_time_old; + float sync_time_old = m_sync_track.CalcSyncFromAbsTime(m_time_current); + m_time_current = fmodf(m_time_current + dt, m_sync_track.m_duration); + float sync_time_dt = + m_sync_track.CalcSyncFromAbsTime(m_time_current) - sync_time_old; - m_input_A->m_current_time = sync_time_old; - m_input_B->m_current_time = sync_time_old; + m_input_A->m_time_current = sync_time_old; + m_input_B->m_time_current = sync_time_old; m_input_A->UpdateTime(sync_time_dt); m_input_B->UpdateTime(sync_time_dt); } else { - m_current_time += dt; + m_time_current += dt; m_input_A->UpdateTime(dt); m_input_B->UpdateTime(dt); } @@ -66,7 +68,8 @@ struct BlendNode : public AnimNode { ozz::vector* local_matrices, ozz::math::Transform* root_transform) override; - virtual void GetInputNodes(std::vector& input_nodes) const override { + virtual void GetInputNodes( + std::vector& input_nodes) const override { input_nodes.push_back(m_input_A); input_nodes.push_back(m_input_B); }; diff --git a/src/AnimNodes/BlendSpace1D.h b/src/AnimNodes/BlendSpace1D.h index 345fc2b..6cfe4d0 100644 --- a/src/AnimNodes/BlendSpace1D.h +++ b/src/AnimNodes/BlendSpace1D.h @@ -29,9 +29,7 @@ struct BlendSpace1D : public AnimNode { ozz::math::Transform m_root_transform_0; ozz::math::Transform m_root_transform_1; - virtual void Reset() { - m_current_time = 0.f; - } + virtual void Reset() { m_time_current = 0.f; } virtual void UpdateIsSynced(bool is_synced) override { m_is_time_synced = is_synced; @@ -40,12 +38,15 @@ struct BlendSpace1D : public AnimNode { 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]); + 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) { + 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]; @@ -65,21 +66,28 @@ struct BlendSpace1D : public AnimNode { 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); + m_sync_track = SyncTrack::Blend( + m_normalized_weight, + m_input_0->m_sync_track, + m_input_1->m_sync_track); } else { - assert (false); + 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); + float sync_time_old = m_sync_track.CalcSyncFromAbsTime(m_time_current); + m_time_current = fmodf(m_time_current + dt, m_sync_track.m_duration); + float sync_time_dt = + m_sync_track.CalcSyncFromAbsTime(m_time_current) - sync_time_old; - 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); + m_input_0->m_time_current = sync_time_old; + m_input_1->m_time_current = sync_time_old; + m_input_0->UpdateTime(sync_time_dt); + m_input_1->UpdateTime(sync_time_dt); } else { - m_current_time += dt; + m_time_current += dt; m_input_0->UpdateTime(dt); m_input_1->UpdateTime(dt); } @@ -89,7 +97,8 @@ struct BlendSpace1D : public AnimNode { ozz::vector* local_matrices, ozz::math::Transform* root_transform) override; - virtual void GetInputNodes(std::vector& input_nodes) const override { + virtual void GetInputNodes( + std::vector& input_nodes) const override { for (int i = 0; i < m_inputs.size(); i++) { input_nodes.push_back(m_inputs[i]); } @@ -98,5 +107,4 @@ struct BlendSpace1D : public AnimNode { virtual void DrawDebugUi(); }; - #endif //ANIMTESTBED_BLENDSPACE1D_H diff --git a/src/AnimNodes/LockTranslationNode.h b/src/AnimNodes/LockTranslationNode.h index 6708cfa..86d8997 100644 --- a/src/AnimNodes/LockTranslationNode.h +++ b/src/AnimNodes/LockTranslationNode.h @@ -24,7 +24,7 @@ struct LockTranslationNode : public AnimNode { bool m_lock_y; bool m_lock_z; - virtual void Reset() { m_current_time = 0.f; } + virtual void Reset() { m_time_current = 0.f; } virtual void UpdateIsSynced(bool is_synced) override { m_is_time_synced = is_synced; diff --git a/src/AnimNodes/SpeedScaleNode.h b/src/AnimNodes/SpeedScaleNode.h index 5addcf3..04b98c3 100644 --- a/src/AnimNodes/SpeedScaleNode.h +++ b/src/AnimNodes/SpeedScaleNode.h @@ -16,7 +16,7 @@ struct SpeedScaleNode : public AnimNode { float m_time_scale; AnimNode* m_input_node; - virtual void Reset() { m_current_time = 0.f; } + virtual void Reset() { m_time_current = 0.f; } virtual void UpdateIsSynced(bool is_synced) override { m_is_time_synced = is_synced; @@ -32,10 +32,10 @@ struct SpeedScaleNode : public AnimNode { virtual void UpdateTime(float dt) { if (!m_is_time_synced) { - m_current_time += dt * m_time_scale; + m_time_current += dt * m_time_scale; m_input_node->UpdateTime(dt * m_time_scale); } else { - m_current_time += dt; + m_time_current += dt; m_input_node->UpdateTime(dt); } } diff --git a/src/AnimationController.cc b/src/AnimationController.cc index b6e1fbe..43d4136 100644 --- a/src/AnimationController.cc +++ b/src/AnimationController.cc @@ -229,8 +229,8 @@ void AnimationController::DrawDebugUi() { ImGui::NextColumn(); ImGui::PopID(); - ImGui::PushID((void*)&node->m_current_time); - ImGui::Text("%2.3f", node->m_current_time); + ImGui::PushID((void*)&node->m_time_current); + ImGui::Text("%2.3f", node->m_time_current); ImGui::NextColumn(); ImGui::PopID(); } diff --git a/tests/AnimSampleNodeTests.cc b/tests/AnimSampleNodeTests.cc index 847bbf6..daa2a7e 100644 --- a/tests/AnimSampleNodeTests.cc +++ b/tests/AnimSampleNodeTests.cc @@ -125,7 +125,7 @@ TEST_CASE("Sample single bone channel", "[AnimSamplerNode]") { local_matrices, root_transform_0); - int n_samples = 20; + int n_samples = 53; for (int i = 0; i <= n_samples; i++) { float ratio = i * 1.f / n_samples; ozz::math::Transform sampled_root_transform;