Renamed m_current_time to m_time_current to simplify root bone sampling.

AnimGraphEditor
Martin Felis 2021-12-03 15:58:20 +01:00
parent 1e1d36c5b1
commit b8d863c8e1
8 changed files with 73 additions and 54 deletions

View File

@ -17,7 +17,7 @@ enum class AnimNodeType { Blend, SpeedScale, AnimSampler };
struct AnimNode { struct AnimNode {
AnimNode(AnimationController* animation_controller) AnimNode(AnimationController* animation_controller)
: m_animation_controller(animation_controller), : m_animation_controller(animation_controller),
m_current_time(0.f), m_time_current(0.f),
m_is_time_synced(false) {} m_is_time_synced(false) {}
virtual ~AnimNode(){}; virtual ~AnimNode(){};
@ -27,10 +27,10 @@ struct AnimNode {
// When synced then current time is relative to the node's anim duration.:w // When synced then current time is relative to the node's anim duration.:w
bool m_is_time_synced; bool m_is_time_synced;
float m_current_time; float m_time_current;
SyncTrack m_sync_track; 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. // Mark current node according to is_synced and propagate flag to animation inputs.
virtual void UpdateIsSynced(bool is_synced) = 0; virtual void UpdateIsSynced(bool is_synced) = 0;

View File

@ -10,16 +10,18 @@
struct AnimSamplerNode : public AnimNode { struct AnimSamplerNode : public AnimNode {
AnimSamplerNode(AnimationController* animation_controller) AnimSamplerNode(AnimationController* animation_controller)
: AnimNode(animation_controller), : AnimNode(animation_controller),
m_time_prev(0.f),
m_override_ratio(false), m_override_ratio(false),
m_anim_ratio(0.f), m_anim_ratio(0.f),
m_root_bone_index(0) { m_root_bone_index(0) {
assert(m_current_time < 100.0f); assert(m_time_current < 100.0f);
m_anim_node_type = AnimNodeType::AnimSampler; m_anim_node_type = AnimNodeType::AnimSampler;
}; };
virtual ~AnimSamplerNode() {} virtual ~AnimSamplerNode() {}
ozz::animation::Animation* m_animation; ozz::animation::Animation* m_animation;
float m_time_prev;
bool m_override_ratio; bool m_override_ratio;
float m_anim_ratio; float m_anim_ratio;
float m_anim_ratio_prev; float m_anim_ratio_prev;
@ -40,24 +42,30 @@ struct AnimSamplerNode : public AnimNode {
virtual void UpdateSyncTrack() override {} virtual void UpdateSyncTrack() override {}
virtual void UpdateTime(float dt) override { virtual void UpdateTime(float dt) override {
m_current_time += dt; if (m_override_ratio) {
if (!m_override_ratio) { return;
}
m_time_prev = m_time_current;
m_time_current += dt;
if (m_is_time_synced) { if (m_is_time_synced) {
m_anim_ratio = m_sync_track.CalcRatioFromSyncTime(m_current_time); m_anim_ratio = m_sync_track.CalcRatioFromSyncTime(m_time_current);
float prev_sync_time = m_current_time - dt; float prev_sync_time = m_time_current - dt;
if (m_current_time < 0) { if (m_time_current < 0) {
prev_sync_time += m_sync_track.m_num_intervals; prev_sync_time += m_sync_track.m_num_intervals;
} }
m_anim_ratio_prev = m_sync_track.CalcRatioFromSyncTime(prev_sync_time); m_anim_ratio_prev = m_sync_track.CalcRatioFromSyncTime(prev_sync_time);
} else { } else {
m_anim_ratio = m_anim_ratio =
fmodf((float)m_current_time / m_animation->duration(), 1.0f); fmodf((float)m_time_current / m_animation->duration(), 1.0f);
m_anim_ratio_prev = fmodf((float)m_current_time / 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) { if (m_anim_ratio_prev < 0) {
m_anim_ratio_prev += 1.0f; m_anim_ratio_prev += 1.0f;
} }
} }
}
if (m_anim_ratio < 0.f) { if (m_anim_ratio < 0.f) {
m_anim_ratio += 1.0f; m_anim_ratio += 1.0f;

View File

@ -22,9 +22,7 @@ struct BlendNode : public AnimNode {
ozz::math::Transform m_root_transform_A; ozz::math::Transform m_root_transform_A;
ozz::math::Transform m_root_transform_B; ozz::math::Transform m_root_transform_B;
virtual void Reset() { virtual void Reset() { m_time_current = 0.f; }
m_current_time = 0.f;
}
virtual void UpdateIsSynced(bool is_synced) override { virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced; m_is_time_synced = is_synced;
@ -39,7 +37,10 @@ struct BlendNode : public AnimNode {
virtual void UpdateSyncTrack() override { virtual void UpdateSyncTrack() override {
if (m_is_time_synced) { 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 { } else {
assert(false); assert(false);
} }
@ -47,16 +48,17 @@ struct BlendNode : public AnimNode {
virtual void UpdateTime(float dt) { virtual void UpdateTime(float dt) {
if (m_is_time_synced) { if (m_is_time_synced) {
float sync_time_old = m_sync_track.CalcSyncFromAbsTime(m_current_time); float sync_time_old = m_sync_track.CalcSyncFromAbsTime(m_time_current);
m_current_time = fmodf(m_current_time + dt, m_sync_track.m_duration); m_time_current = fmodf(m_time_current + dt, m_sync_track.m_duration);
float sync_time_dt = m_sync_track.CalcSyncFromAbsTime(m_current_time) - sync_time_old; 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_A->m_time_current = sync_time_old;
m_input_B->m_current_time = sync_time_old; m_input_B->m_time_current = sync_time_old;
m_input_A->UpdateTime(sync_time_dt); m_input_A->UpdateTime(sync_time_dt);
m_input_B->UpdateTime(sync_time_dt); m_input_B->UpdateTime(sync_time_dt);
} else { } else {
m_current_time += dt; m_time_current += dt;
m_input_A->UpdateTime(dt); m_input_A->UpdateTime(dt);
m_input_B->UpdateTime(dt); m_input_B->UpdateTime(dt);
} }
@ -66,7 +68,8 @@ struct BlendNode : public AnimNode {
ozz::vector<ozz::math::SoaTransform>* local_matrices, ozz::vector<ozz::math::SoaTransform>* local_matrices,
ozz::math::Transform* root_transform) override; ozz::math::Transform* root_transform) override;
virtual void GetInputNodes(std::vector<AnimNode*>& input_nodes) const override { virtual void GetInputNodes(
std::vector<AnimNode*>& input_nodes) const override {
input_nodes.push_back(m_input_A); input_nodes.push_back(m_input_A);
input_nodes.push_back(m_input_B); input_nodes.push_back(m_input_B);
}; };

View File

@ -29,9 +29,7 @@ struct BlendSpace1D : public AnimNode {
ozz::math::Transform m_root_transform_0; ozz::math::Transform m_root_transform_0;
ozz::math::Transform m_root_transform_1; ozz::math::Transform m_root_transform_1;
virtual void Reset() { virtual void Reset() { m_time_current = 0.f; }
m_current_time = 0.f;
}
virtual void UpdateIsSynced(bool is_synced) override { virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced; m_is_time_synced = is_synced;
@ -41,11 +39,14 @@ struct BlendSpace1D : public AnimNode {
} }
assert(m_input_weights.size() > 0); 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_weight >= m_input_weights[0]
&& m_weight <= m_input_weights[m_input_weights.size() - 1]);
int prev_idx = 0; int prev_idx = 0;
for (int next_idx = 1; next_idx < m_input_weights.size(); next_idx++) { 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_input_0 = m_inputs[prev_idx];
m_weight_0 = m_input_weights[prev_idx]; m_weight_0 = m_input_weights[prev_idx];
@ -65,7 +66,10 @@ struct BlendSpace1D : public AnimNode {
virtual void UpdateSyncTrack() override { virtual void UpdateSyncTrack() override {
if (m_is_time_synced) { 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 { } else {
assert(false); assert(false);
} }
@ -73,13 +77,17 @@ struct BlendSpace1D : public AnimNode {
virtual void UpdateTime(float dt) { virtual void UpdateTime(float dt) {
if (m_is_time_synced) { if (m_is_time_synced) {
m_current_time = fmodf(m_current_time + dt, m_sync_track.m_duration); float sync_time_old = m_sync_track.CalcSyncFromAbsTime(m_time_current);
float current_sync_time = m_sync_track.CalcSyncFromAbsTime(m_current_time); 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_0->m_time_current = sync_time_old;
m_input_1->UpdateTime(current_sync_time - m_input_1->m_current_time); m_input_1->m_time_current = sync_time_old;
m_input_0->UpdateTime(sync_time_dt);
m_input_1->UpdateTime(sync_time_dt);
} else { } else {
m_current_time += dt; m_time_current += dt;
m_input_0->UpdateTime(dt); m_input_0->UpdateTime(dt);
m_input_1->UpdateTime(dt); m_input_1->UpdateTime(dt);
} }
@ -89,7 +97,8 @@ struct BlendSpace1D : public AnimNode {
ozz::vector<ozz::math::SoaTransform>* local_matrices, ozz::vector<ozz::math::SoaTransform>* local_matrices,
ozz::math::Transform* root_transform) override; ozz::math::Transform* root_transform) override;
virtual void GetInputNodes(std::vector<AnimNode*>& input_nodes) const override { virtual void GetInputNodes(
std::vector<AnimNode*>& input_nodes) const override {
for (int i = 0; i < m_inputs.size(); i++) { for (int i = 0; i < m_inputs.size(); i++) {
input_nodes.push_back(m_inputs[i]); input_nodes.push_back(m_inputs[i]);
} }
@ -98,5 +107,4 @@ struct BlendSpace1D : public AnimNode {
virtual void DrawDebugUi(); virtual void DrawDebugUi();
}; };
#endif //ANIMTESTBED_BLENDSPACE1D_H #endif //ANIMTESTBED_BLENDSPACE1D_H

View File

@ -24,7 +24,7 @@ struct LockTranslationNode : public AnimNode {
bool m_lock_y; bool m_lock_y;
bool m_lock_z; 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 { virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced; m_is_time_synced = is_synced;

View File

@ -16,7 +16,7 @@ struct SpeedScaleNode : public AnimNode {
float m_time_scale; float m_time_scale;
AnimNode* m_input_node; 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 { virtual void UpdateIsSynced(bool is_synced) override {
m_is_time_synced = is_synced; m_is_time_synced = is_synced;
@ -32,10 +32,10 @@ struct SpeedScaleNode : public AnimNode {
virtual void UpdateTime(float dt) { virtual void UpdateTime(float dt) {
if (!m_is_time_synced) { 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); m_input_node->UpdateTime(dt * m_time_scale);
} else { } else {
m_current_time += dt; m_time_current += dt;
m_input_node->UpdateTime(dt); m_input_node->UpdateTime(dt);
} }
} }

View File

@ -229,8 +229,8 @@ void AnimationController::DrawDebugUi() {
ImGui::NextColumn(); ImGui::NextColumn();
ImGui::PopID(); ImGui::PopID();
ImGui::PushID((void*)&node->m_current_time); ImGui::PushID((void*)&node->m_time_current);
ImGui::Text("%2.3f", node->m_current_time); ImGui::Text("%2.3f", node->m_time_current);
ImGui::NextColumn(); ImGui::NextColumn();
ImGui::PopID(); ImGui::PopID();
} }

View File

@ -125,7 +125,7 @@ TEST_CASE("Sample single bone channel", "[AnimSamplerNode]") {
local_matrices, local_matrices,
root_transform_0); root_transform_0);
int n_samples = 20; int n_samples = 53;
for (int i = 0; i <= n_samples; i++) { for (int i = 0; i <= n_samples; i++) {
float ratio = i * 1.f / n_samples; float ratio = i * 1.f / n_samples;
ozz::math::Transform sampled_root_transform; ozz::math::Transform sampled_root_transform;