Skip to content

Commit

Permalink
Determine break_loop_at_end 1 frame earlier using prediction by delta
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Jul 28, 2024
1 parent 607b230 commit b73531c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
9 changes: 2 additions & 7 deletions scene/animation/animation_blend_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,11 @@ AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixe
bool p_seek = p_playback_info.seeked;
bool p_is_external_seeking = p_playback_info.is_external_seeking;

bool is_just_looped = false;

// 1. Progress for AnimationNode.
bool will_end = Animation::is_greater_or_equal_approx(cur_time + cur_delta, cur_len);
if (cur_loop_mode != Animation::LOOP_NONE) {
if (cur_loop_mode == Animation::LOOP_LINEAR) {
if (!Math::is_zero_approx(cur_len)) {
if (Animation::is_less_or_equal_approx(prev_time, cur_len) && Animation::is_greater_approx(cur_time, cur_len)) {
is_just_looped = true; // Don't break with negative timescale since remain will not be 0.
}
cur_time = Math::fposmod(cur_time, cur_len);
}
backward = false;
Expand All @@ -156,7 +152,6 @@ AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixe
backward = !backward;
} else if (Animation::is_less_or_equal_approx(prev_time, cur_len) && Animation::is_greater_approx(cur_time, cur_len)) {
backward = !backward;
is_just_looped = true; // Don't break with negative timescale since remain will not be 0.
}
cur_time = Math::pingpong(cur_time, cur_len);
}
Expand Down Expand Up @@ -190,7 +185,7 @@ AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixe
nti.position = cur_time;
nti.delta = cur_delta;
nti.loop_mode = cur_loop_mode;
nti.is_just_looped = is_just_looped;
nti.will_end = will_end;

// 3. Progress for Animation.
double prev_playback_time = prev_time + start_offset;
Expand Down
4 changes: 2 additions & 2 deletions scene/animation/animation_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AnimationNode : public Resource {

// Needs internally to estimate remain time, the previous frame values are not retained.
Animation::LoopMode loop_mode = Animation::LOOP_NONE;
bool is_just_looped = false; // For breaking loop, it is true when just looped.
bool will_end = false; // For breaking loop, it is true when just looped.
bool is_infinity = false; // For unpredictable state machine's end.

bool is_looping() {
Expand All @@ -84,7 +84,7 @@ class AnimationNode : public Resource {
if ((is_looping() && !p_break_loop) || is_infinity) {
return HUGE_LENGTH;
}
if (p_break_loop && is_just_looped) {
if (is_looping() && p_break_loop && will_end) {
return 0;
}
double remain = length - position;
Expand Down

0 comments on commit b73531c

Please sign in to comment.