Synced animations looks good.
This commit is contained in:
@@ -10,26 +10,47 @@
|
||||
struct AnimSamplerNode : public AnimNode {
|
||||
AnimSamplerNode(AnimationController* animation_controller)
|
||||
: AnimNode(animation_controller),
|
||||
m_anim_ratio(0.f),
|
||||
m_override_ratio(false){};
|
||||
m_override_ratio(false),
|
||||
m_anim_ratio(0.f) {
|
||||
assert(m_current_time < 100.0f);
|
||||
m_anim_node_type = AnimNodeType::AnimSampler;
|
||||
};
|
||||
virtual ~AnimSamplerNode() {}
|
||||
|
||||
ozz::animation::Animation* m_animation;
|
||||
|
||||
float m_anim_ratio;
|
||||
bool m_override_ratio;
|
||||
float m_anim_ratio;
|
||||
|
||||
ozz::animation::SamplingCache m_sampling_cache;
|
||||
|
||||
void SetAnimation(ozz::animation::Animation* animation);
|
||||
|
||||
virtual void Update(float dt) override {
|
||||
virtual void UpdateIsSynced(bool is_synced) override {
|
||||
m_is_time_synced = is_synced;
|
||||
}
|
||||
|
||||
virtual void EvalAnimDuration() override {
|
||||
m_anim_duration = m_animation->duration();
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float dt) override {
|
||||
m_current_time += dt;
|
||||
if (!m_override_ratio) {
|
||||
const float duration = m_animation->duration();
|
||||
m_anim_ratio = fmodf((float)m_current_time / duration, 1.0f);
|
||||
if (m_is_time_synced) {
|
||||
m_anim_ratio = fmodf((float)m_current_time, 1.0f);
|
||||
m_current_time = m_anim_ratio;
|
||||
} else {
|
||||
const float duration = m_animation->duration();
|
||||
m_anim_ratio = fmodf((float)m_current_time / duration, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_anim_ratio < 0.f) {
|
||||
m_anim_ratio += 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) override;
|
||||
|
||||
|
||||
@@ -13,7 +13,9 @@ BlendNode::BlendNode(AnimationController* animation_controller)
|
||||
: AnimNode(animation_controller),
|
||||
m_input_A(nullptr),
|
||||
m_input_B(nullptr),
|
||||
m_weight(0.f) {
|
||||
m_weight(0.f),
|
||||
m_sync_inputs(false) {
|
||||
m_anim_node_type = AnimNodeType::Blend;
|
||||
const SkinnedMesh* skinned_mesh = m_animation_controller->m_skinned_mesh;
|
||||
const int num_soa_joints = skinned_mesh->m_skeleton.num_soa_joints();
|
||||
const int num_joints = skinned_mesh->m_skeleton.num_joints();
|
||||
@@ -54,6 +56,7 @@ void BlendNode::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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,13 @@
|
||||
|
||||
struct BlendNode : public AnimNode {
|
||||
BlendNode(AnimationController* animation_controller);
|
||||
|
||||
virtual ~BlendNode() {}
|
||||
|
||||
AnimNode* m_input_A;
|
||||
AnimNode* m_input_B;
|
||||
float m_weight;
|
||||
bool m_sync_inputs;
|
||||
|
||||
ozz::vector<ozz::math::SoaTransform> m_local_matrices_A;
|
||||
ozz::vector<ozz::math::SoaTransform> m_local_matrices_B;
|
||||
@@ -22,9 +24,37 @@ struct BlendNode : public AnimNode {
|
||||
m_current_time = 0.f;
|
||||
}
|
||||
|
||||
virtual void Update(float dt) {
|
||||
m_input_A->Update(dt);
|
||||
m_input_B->Update(dt);
|
||||
virtual void UpdateIsSynced(bool is_synced) override {
|
||||
m_is_time_synced = is_synced;
|
||||
|
||||
if (m_sync_inputs) {
|
||||
m_is_time_synced = true;
|
||||
}
|
||||
|
||||
m_input_B->UpdateIsSynced(m_is_time_synced);
|
||||
m_input_A->UpdateIsSynced(m_is_time_synced);
|
||||
}
|
||||
|
||||
virtual void EvalAnimDuration() override {
|
||||
if (m_is_time_synced) {
|
||||
m_anim_duration = (1.0f - m_weight) * m_input_A->m_anim_duration + m_weight * m_input_B->m_anim_duration;
|
||||
} else {
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void UpdateTime(float dt) {
|
||||
if (m_is_time_synced) {
|
||||
float current_time_absolute = m_current_time * m_anim_duration + dt;
|
||||
m_current_time = fmodf(current_time_absolute / m_anim_duration, 1.0);
|
||||
|
||||
m_input_A->UpdateTime(m_current_time - m_input_A->m_current_time);
|
||||
m_input_B->UpdateTime(m_current_time - m_input_B->m_current_time);
|
||||
} else {
|
||||
m_current_time += dt;
|
||||
m_input_A->UpdateTime(dt);
|
||||
m_input_B->UpdateTime(dt);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Evaluate(
|
||||
|
||||
@@ -9,7 +9,16 @@
|
||||
void SpeedScaleNode::DrawDebugUi() {
|
||||
std::string node_name = "SpeedScaleNode: " + m_name;
|
||||
if (ImGui::TreeNode(node_name.c_str())) {
|
||||
ImGui::SliderFloat("Time Scale", &m_time_scale, -5.f, 5.f);
|
||||
bool is_negative = m_time_scale < 0.f;
|
||||
if (ImGui::Checkbox("Reverse Time", &is_negative)) {
|
||||
m_time_scale = m_time_scale * -1.f;
|
||||
}
|
||||
|
||||
// ensure m_time_scale is positive
|
||||
m_time_scale = m_time_scale * (is_negative ? -1.f : 1.f);
|
||||
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();
|
||||
|
||||
|
||||
@@ -8,25 +8,43 @@
|
||||
#include "../AnimNode.h"
|
||||
|
||||
struct SpeedScaleNode : public AnimNode {
|
||||
SpeedScaleNode(AnimationController* animation_controller): AnimNode (animation_controller), m_time_scale(1.f) {}
|
||||
SpeedScaleNode(AnimationController* animation_controller)
|
||||
: AnimNode(animation_controller), m_time_scale(1.f) {
|
||||
m_anim_node_type = AnimNodeType::SpeedScale;
|
||||
}
|
||||
|
||||
float m_time_scale;
|
||||
AnimNode* m_input_node;
|
||||
|
||||
virtual void Reset() {
|
||||
m_current_time = 0.f;
|
||||
virtual void Reset() { m_current_time = 0.f; }
|
||||
|
||||
virtual void UpdateIsSynced(bool is_synced) override {
|
||||
m_is_time_synced = is_synced;
|
||||
m_input_node->UpdateIsSynced(is_synced);
|
||||
}
|
||||
|
||||
virtual void Update(float dt) {
|
||||
m_current_time += dt * m_time_scale;
|
||||
m_input_node->Update(dt * m_time_scale);
|
||||
virtual void EvalAnimDuration() override {
|
||||
assert(fabs(m_time_scale) >= 0.01f);
|
||||
m_anim_duration = fabsf(m_input_node->m_anim_duration / m_time_scale);
|
||||
}
|
||||
|
||||
virtual void Evaluate(ozz::vector<ozz::math::SoaTransform>* local_matrices) override {
|
||||
virtual void UpdateTime(float dt) {
|
||||
if (!m_is_time_synced) {
|
||||
m_current_time += dt * m_time_scale;
|
||||
m_input_node->UpdateTime(dt * m_time_scale);
|
||||
} else {
|
||||
m_current_time += dt;
|
||||
m_input_node->UpdateTime(dt);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Evaluate(
|
||||
ozz::vector<ozz::math::SoaTransform>* local_matrices) override {
|
||||
m_input_node->Evaluate(local_matrices);
|
||||
};
|
||||
|
||||
virtual void CollectNodeOrdering (std::vector<AnimNode*>& anim_nodes) override {
|
||||
virtual void CollectNodeOrdering(
|
||||
std::vector<AnimNode*>& anim_nodes) override {
|
||||
anim_nodes.push_back(this);
|
||||
m_input_node->CollectNodeOrdering(anim_nodes);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user