Skip to content

Commit

Permalink
Add use_self_delta_to_fade to NodeOneShot to fade finished animation
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Jan 19, 2025
1 parent 7b1ed52 commit b7566b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 6 additions & 2 deletions doc/classes/AnimationNodeOneShot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,22 @@
</member>
<member name="fadein_time" type="float" setter="set_fadein_time" getter="get_fadein_time" default="0.0">
The fade-in duration. For example, setting this to [code]1.0[/code] for a 5 second length animation will produce a cross-fade that starts at 0 second and ends at 1 second during the animation.
[b]Note:[/b] [AnimationNodeOneShot] transitions the current state after the end of the fading. When [AnimationNodeOutput] is considered as the most upstream, so the [member fadein_time] is scaled depending on the downstream delta. For example, if this value is set to [code]1.0[/code] and a [AnimationNodeTimeScale] with a value of [code]2.0[/code] is chained downstream, the actual processing time will be 0.5 second.
[b]Note:[/b] [AnimationNodeOneShot] transitions the current state after the end of the fading. The timing and running duration of the fading depends on [member use_self_delta_to_fade].
</member>
<member name="fadeout_curve" type="Curve" setter="set_fadeout_curve" getter="get_fadeout_curve">
Determines how cross-fading between animations is eased. If empty, the transition will be linear. Should be a unit [Curve].
</member>
<member name="fadeout_time" type="float" setter="set_fadeout_time" getter="get_fadeout_time" default="0.0">
The fade-out duration. For example, setting this to [code]1.0[/code] for a 5 second length animation will produce a cross-fade that starts at 4 second and ends at 5 second during the animation.
[b]Note:[/b] [AnimationNodeOneShot] transitions the current state after the end of the fading. When [AnimationNodeOutput] is considered as the most upstream, so the [member fadeout_time] is scaled depending on the downstream delta. For example, if this value is set to [code]1.0[/code] and an [AnimationNodeTimeScale] with a value of [code]2.0[/code] is chained downstream, the actual processing time will be 0.5 second.
[b]Note:[/b] [AnimationNodeOneShot] transitions the current state after the end of the fading. The timing and running duration of the fading depends on [member use_self_delta_to_fade].
</member>
<member name="mix_mode" type="int" setter="set_mix_mode" getter="get_mix_mode" enum="AnimationNodeOneShot.MixMode" default="0">
The blend type.
</member>
<member name="use_self_delta_to_fade" type="bool" setter="set_use_self_delta_to_fade" getter="is_using_self_delta_to_fade" default="false">
If [code]false[/code], when [AnimationNodeOutput] is considered as the most upstream, so the [member fadein_time] and the [member fadeout_time] are scaled depending on the downstream delta. For example, if this value is set to [code]1.0[/code] and an [AnimationNodeTimeScale] with a value of [code]2.0[/code] is chained downstream, the actual processing time will be 0.5 second.
If [code]true[/code], No longer depends on downstream time scale.
</member>
</members>
<constants>
<constant name="ONE_SHOT_REQUEST_NONE" value="0" enum="OneShotRequest">
Expand Down
14 changes: 13 additions & 1 deletion scene/animation/animation_blend_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,14 @@ bool AnimationNodeOneShot::is_loop_broken_at_end() const {
return break_loop_at_end;
}

void AnimationNodeOneShot::set_use_self_delta_to_fade(bool p_enable) {
use_self_delta_to_fade = p_enable;
}

bool AnimationNodeOneShot::is_using_self_delta_to_fade() const {
return use_self_delta_to_fade;
}

String AnimationNodeOneShot::get_caption() const {
return "OneShot";
}
Expand Down Expand Up @@ -686,7 +694,7 @@ AnimationNode::NodeTimeInfo AnimationNodeOneShot::_process(const AnimationMixer:
set_parameter(time_to_restart, restart_sec);
}
}
double d = Math::abs(os_nti.delta);
double d = use_self_delta_to_fade ? abs_delta : Math::abs(os_nti.delta);
if (!do_start) {
cur_fade_in_remaining = MAX(0, cur_fade_in_remaining - d); // Don't consider seeked delta by restart.
}
Expand Down Expand Up @@ -715,6 +723,9 @@ void AnimationNodeOneShot::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_break_loop_at_end", "enable"), &AnimationNodeOneShot::set_break_loop_at_end);
ClassDB::bind_method(D_METHOD("is_loop_broken_at_end"), &AnimationNodeOneShot::is_loop_broken_at_end);

ClassDB::bind_method(D_METHOD("set_use_self_delta_to_fade", "enable"), &AnimationNodeOneShot::set_use_self_delta_to_fade);
ClassDB::bind_method(D_METHOD("is_using_self_delta_to_fade"), &AnimationNodeOneShot::is_using_self_delta_to_fade);

ClassDB::bind_method(D_METHOD("set_autorestart", "active"), &AnimationNodeOneShot::set_auto_restart_enabled);
ClassDB::bind_method(D_METHOD("has_autorestart"), &AnimationNodeOneShot::is_auto_restart_enabled);

Expand All @@ -734,6 +745,7 @@ void AnimationNodeOneShot::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadeout_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadeout_time", "get_fadeout_time");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadeout_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadeout_curve", "get_fadeout_curve");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "break_loop_at_end"), "set_break_loop_at_end", "is_loop_broken_at_end");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_self_delta_to_fade"), "set_use_self_delta_to_fade", "is_using_self_delta_to_fade");

ADD_GROUP("Auto Restart", "autorestart_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autorestart"), "set_autorestart", "has_autorestart");
Expand Down
4 changes: 4 additions & 0 deletions scene/animation/animation_blend_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class AnimationNodeOneShot : public AnimationNodeSync {
double auto_restart_random_delay = 0.0;
MixMode mix = MIX_MODE_BLEND;
bool break_loop_at_end = false;
bool use_self_delta_to_fade = false;

StringName request = PNAME("request");
StringName active = PNAME("active");
Expand Down Expand Up @@ -193,6 +194,9 @@ class AnimationNodeOneShot : public AnimationNodeSync {
void set_break_loop_at_end(bool p_enable);
bool is_loop_broken_at_end() const;

void set_use_self_delta_to_fade(bool p_enable);
bool is_using_self_delta_to_fade() const;

virtual bool has_filter() const override;
virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;

Expand Down

0 comments on commit b7566b9

Please sign in to comment.