summaryrefslogtreecommitdiff
path: root/scene/animation
diff options
context:
space:
mode:
Diffstat (limited to 'scene/animation')
-rw-r--r--scene/animation/SCsub3
-rw-r--r--scene/animation/animation_blend_tree.cpp15
-rw-r--r--scene/animation/animation_player.cpp3
-rw-r--r--scene/animation/animation_tree.cpp2
-rw-r--r--scene/animation/easing_equations.h405
-rw-r--r--scene/animation/tween.cpp36
-rw-r--r--scene/animation/tween.h4
7 files changed, 451 insertions, 17 deletions
diff --git a/scene/animation/SCsub b/scene/animation/SCsub
index cc33a5af84..d0aa0bc8aa 100644
--- a/scene/animation/SCsub
+++ b/scene/animation/SCsub
@@ -6,11 +6,8 @@ Import("env")
thirdparty_obj = []
-thirdparty_sources = "#thirdparty/misc/easing_equations.cpp"
-
env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()
-env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
env.scene_sources += thirdparty_obj
# Godot source files
diff --git a/scene/animation/animation_blend_tree.cpp b/scene/animation/animation_blend_tree.cpp
index d11387902a..af186072ac 100644
--- a/scene/animation/animation_blend_tree.cpp
+++ b/scene/animation/animation_blend_tree.cpp
@@ -248,27 +248,26 @@ double AnimationNodeOneShot::process(double p_time, bool p_seek) {
if (fade_in > 0) {
blend = time / fade_in;
} else {
- blend = 0; //wtf
+ blend = 0;
}
-
- } else if (!do_start && remaining < fade_out) {
- if (fade_out) {
+ } else if (!do_start && remaining <= fade_out) {
+ if (fade_out > 0) {
blend = (remaining / fade_out);
} else {
- blend = 1.0;
+ blend = 0;
}
} else {
blend = 1.0;
}
- float main_rem;
+ double main_rem;
if (mix == MIX_MODE_ADD) {
main_rem = blend_input(0, p_time, p_seek, 1.0, FILTER_IGNORE, !sync);
} else {
main_rem = blend_input(0, p_time, p_seek, 1.0 - blend, FILTER_BLEND, !sync);
}
- float os_rem = blend_input(1, os_seek ? time : p_time, os_seek, blend, FILTER_PASS, false);
+ double os_rem = blend_input(1, os_seek ? time : p_time, os_seek, blend, FILTER_PASS, false);
if (do_start) {
remaining = os_rem;
@@ -718,7 +717,7 @@ double AnimationNodeTransition::process(double p_time, bool p_seek) {
} else { // cross-fading from prev to current
- float blend = xfade ? (prev_xfading / xfade) : 1;
+ float blend = xfade == 0 ? 0 : (prev_xfading / xfade);
if (!p_seek && switched) { //just switched, seek to start of current
diff --git a/scene/animation/animation_player.cpp b/scene/animation/animation_player.cpp
index f6091f224c..5825a35030 100644
--- a/scene/animation/animation_player.cpp
+++ b/scene/animation/animation_player.cpp
@@ -737,7 +737,7 @@ void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, double
if (anim->has_loop()) {
at_anim_pos = Math::fposmod(p_time - pos, (double)anim->get_length()); //seek to loop
} else {
- at_anim_pos = MAX((double)anim->get_length(), p_time - pos); //seek to end
+ at_anim_pos = MIN((double)anim->get_length(), p_time - pos); //seek to end
}
if (player->is_playing() || p_seeked) {
@@ -765,6 +765,7 @@ void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, double
}
} else {
player->play(anim_name);
+ player->seek(0.0, true);
nc->animation_playing = true;
playing_caches.insert(nc);
}
diff --git a/scene/animation/animation_tree.cpp b/scene/animation/animation_tree.cpp
index 88fb960164..9ca8d478b1 100644
--- a/scene/animation/animation_tree.cpp
+++ b/scene/animation/animation_tree.cpp
@@ -958,7 +958,7 @@ void AnimationTree::_process_graph(real_t p_delta) {
Variant::interpolate(t->value, value, blend, t->value);
- } else if (delta != 0) {
+ } else {
List<int> indices;
a->value_track_get_key_indices(i, time, delta, &indices);
diff --git a/scene/animation/easing_equations.h b/scene/animation/easing_equations.h
new file mode 100644
index 0000000000..c38d083b7f
--- /dev/null
+++ b/scene/animation/easing_equations.h
@@ -0,0 +1,405 @@
+/*************************************************************************/
+/* easing_equations.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+/*
+ * Derived from Robert Penner's easing equations: http://robertpenner.com/easing/
+ *
+ * Copyright (c) 2001 Robert Penner
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef EASING_EQUATIONS_H
+#define EASING_EQUATIONS_H
+
+namespace linear {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ return c * t / d + b;
+}
+}; // namespace linear
+
+namespace sine {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ return -c * cos(t / d * (Math_PI / 2)) + c + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ return c * sin(t / d * (Math_PI / 2)) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ return -c / 2 * (cos(Math_PI * t / d) - 1) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace sine
+
+namespace quint {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ return c * pow(t / d, 5) + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ return c * (pow(t / d - 1, 5) + 1) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ t = t / d * 2;
+
+ if (t < 1) {
+ return c / 2 * pow(t, 5) + b;
+ }
+ return c / 2 * (pow(t - 2, 5) + 2) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace quint
+
+namespace quart {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ return c * pow(t / d, 4) + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ return -c * (pow(t / d - 1, 4) - 1) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ t = t / d * 2;
+
+ if (t < 1) {
+ return c / 2 * pow(t, 4) + b;
+ }
+ return -c / 2 * (pow(t - 2, 4) - 2) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace quart
+
+namespace quad {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ return c * pow(t / d, 2) + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ t /= d;
+ return -c * t * (t - 2) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ t = t / d * 2;
+
+ if (t < 1) {
+ return c / 2 * pow(t, 2) + b;
+ }
+ return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace quad
+
+namespace expo {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ if (t == 0) {
+ return b;
+ }
+ return c * pow(2, 10 * (t / d - 1)) + b - c * 0.001;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ if (t == d) {
+ return b + c;
+ }
+ return c * 1.001 * (-pow(2, -10 * t / d) + 1) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ if (t == 0) {
+ return b;
+ }
+
+ if (t == d) {
+ return b + c;
+ }
+
+ t = t / d * 2;
+
+ if (t < 1) {
+ return c / 2 * pow(2, 10 * (t - 1)) + b - c * 0.0005;
+ }
+ return c / 2 * 1.0005 * (-pow(2, -10 * (t - 1)) + 2) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace expo
+
+namespace elastic {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ if (t == 0) {
+ return b;
+ }
+
+ t /= d;
+ if (t == 1) {
+ return b + c;
+ }
+
+ t -= 1;
+ float p = d * 0.3f;
+ float a = c * pow(2, 10 * t);
+ float s = p / 4;
+
+ return -(a * sin((t * d - s) * (2 * Math_PI) / p)) + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ if (t == 0) {
+ return b;
+ }
+
+ t /= d;
+ if (t == 1) {
+ return b + c;
+ }
+
+ float p = d * 0.3f;
+ float s = p / 4;
+
+ return (c * pow(2, -10 * t) * sin((t * d - s) * (2 * Math_PI) / p) + c + b);
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ if (t == 0) {
+ return b;
+ }
+
+ if ((t /= d / 2) == 2) {
+ return b + c;
+ }
+
+ float p = d * (0.3f * 1.5f);
+ float a = c;
+ float s = p / 4;
+
+ if (t < 1) {
+ t -= 1;
+ a *= pow(2, 10 * t);
+ return -0.5f * (a * sin((t * d - s) * (2 * Math_PI) / p)) + b;
+ }
+
+ t -= 1;
+ a *= pow(2, -10 * t);
+ return a * sin((t * d - s) * (2 * Math_PI) / p) * 0.5f + c + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace elastic
+
+namespace cubic {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ t /= d;
+ return c * t * t * t + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ t = t / d - 1;
+ return c * (t * t * t + 1) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ t /= d / 2;
+ if (t < 1) {
+ return c / 2 * t * t * t + b;
+ }
+
+ t -= 2;
+ return c / 2 * (t * t * t + 2) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace cubic
+
+namespace circ {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ t /= d;
+ return -c * (sqrt(1 - t * t) - 1) + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ t = t / d - 1;
+ return c * sqrt(1 - t * t) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ t /= d / 2;
+ if (t < 1) {
+ return -c / 2 * (sqrt(1 - t * t) - 1) + b;
+ }
+
+ t -= 2;
+ return c / 2 * (sqrt(1 - t * t) + 1) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace circ
+
+namespace bounce {
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ t /= d;
+
+ if (t < (1 / 2.75f)) {
+ return c * (7.5625f * t * t) + b;
+ }
+
+ if (t < (2 / 2.75f)) {
+ t -= 1.5f / 2.75f;
+ return c * (7.5625f * t * t + 0.75f) + b;
+ }
+
+ if (t < (2.5 / 2.75)) {
+ t -= 2.25f / 2.75f;
+ return c * (7.5625f * t * t + 0.9375f) + b;
+ }
+
+ t -= 2.625f / 2.75f;
+ return c * (7.5625f * t * t + 0.984375f) + b;
+}
+
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ return c - out(d - t, 0, c, d) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return in(t * 2, b, c / 2, d);
+ }
+ return out(t * 2 - d, b + c / 2, c / 2, d);
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace bounce
+
+namespace back {
+static real_t in(real_t t, real_t b, real_t c, real_t d) {
+ float s = 1.70158f;
+ t /= d;
+
+ return c * t * t * ((s + 1) * t - s) + b;
+}
+
+static real_t out(real_t t, real_t b, real_t c, real_t d) {
+ float s = 1.70158f;
+ t = t / d - 1;
+
+ return c * (t * t * ((s + 1) * t + s) + 1) + b;
+}
+
+static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
+ float s = 1.70158f * 1.525f;
+ t /= d / 2;
+
+ if (t < 1) {
+ return c / 2 * (t * t * ((s + 1) * t - s)) + b;
+ }
+
+ t -= 2;
+ return c / 2 * (t * t * ((s + 1) * t + s) + 2) + b;
+}
+
+static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
+ if (t < d / 2) {
+ return out(t * 2, b, c / 2, d);
+ }
+ return in(t * 2 - d, b + c / 2, c / 2, d);
+}
+}; // namespace back
+
+#endif
diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp
index 3018fd3ae6..c43b83747b 100644
--- a/scene/animation/tween.cpp
+++ b/scene/animation/tween.cpp
@@ -30,8 +30,23 @@
#include "tween.h"
+#include "scene/animation/easing_equations.h"
#include "scene/main/node.h"
+Tween::interpolater Tween::interpolaters[Tween::TRANS_MAX][Tween::EASE_MAX] = {
+ { &linear::in, &linear::in, &linear::in, &linear::in }, // Linear is the same for each easing.
+ { &sine::in, &sine::out, &sine::in_out, &sine::out_in },
+ { &quint::in, &quint::out, &quint::in_out, &quint::out_in },
+ { &quart::in, &quart::out, &quart::in_out, &quart::out_in },
+ { &quad::in, &quad::out, &quad::in_out, &quad::out_in },
+ { &expo::in, &expo::out, &expo::in_out, &expo::out_in },
+ { &elastic::in, &elastic::out, &elastic::in_out, &elastic::out_in },
+ { &cubic::in, &cubic::out, &cubic::in_out, &cubic::out_in },
+ { &circ::in, &circ::out, &circ::in_out, &circ::out_in },
+ { &bounce::in, &bounce::out, &bounce::in_out, &bounce::out_in },
+ { &back::in, &back::out, &back::in_out, &back::out_in },
+};
+
void Tweener::set_tween(Ref<Tween> p_tween) {
tween = p_tween;
}
@@ -60,6 +75,11 @@ Ref<PropertyTweener> Tween::tween_property(Object *p_target, NodePath p_property
ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree.");
ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first.");
+#ifdef DEBUG_ENABLED
+ Variant::Type property_type = p_target->get_indexed(p_property.get_as_property_path().get_subnames()).get_type();
+ ERR_FAIL_COND_V_MSG(property_type != p_to.get_type(), Ref<PropertyTweener>(), "Type mismatch between property and final value: " + Variant::get_type_name(property_type) + " and " + Variant::get_type_name(p_to.get_type()));
+#endif
+
Ref<PropertyTweener> tweener = memnew(PropertyTweener(p_target, p_property, p_to, p_duration));
append(tweener);
return tweener;
@@ -83,7 +103,7 @@ Ref<CallbackTweener> Tween::tween_callback(Callable p_callback) {
return tweener;
}
-Ref<MethodTweener> Tween::tween_method(Callable p_callback, float p_from, float p_to, float p_duration) {
+Ref<MethodTweener> Tween::tween_method(Callable p_callback, Variant p_from, Variant p_to, float p_duration) {
ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree.");
ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first.");
@@ -312,6 +332,16 @@ bool Tween::should_pause() {
return pause_mode != TWEEN_PAUSE_PROCESS;
}
+real_t Tween::run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t p_time, real_t p_initial, real_t p_delta, real_t p_duration) {
+ if (p_duration == 0) {
+ // Special case to avoid dividing by 0 in equations.
+ return p_initial + p_delta;
+ }
+
+ interpolater func = interpolaters[p_trans_type][p_ease_type];
+ return func(p_time, p_initial, p_delta, p_duration);
+}
+
Variant Tween::interpolate_variant(Variant p_initial_val, Variant p_delta_val, float p_time, float p_duration, TransitionType p_trans, EaseType p_ease) {
ERR_FAIL_INDEX_V(p_trans, TransitionType::TRANS_MAX, Variant());
ERR_FAIL_INDEX_V(p_ease, EaseType::EASE_MAX, Variant());
@@ -496,6 +526,8 @@ Variant Tween::interpolate_variant(Variant p_initial_val, Variant p_delta_val, f
}
Variant Tween::calculate_delta_value(Variant p_intial_val, Variant p_final_val) {
+ ERR_FAIL_COND_V_MSG(p_intial_val.get_type() != p_final_val.get_type(), p_intial_val, "Type mismatch between initial and final value: " + Variant::get_type_name(p_intial_val.get_type()) + " and " + Variant::get_type_name(p_final_val.get_type()));
+
switch (p_intial_val.get_type()) {
case Variant::BOOL: {
return (int)p_final_val - (int)p_intial_val;
@@ -888,7 +920,7 @@ void MethodTweener::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_ease", "ease"), &MethodTweener::set_ease);
}
-MethodTweener::MethodTweener(Callable p_callback, float p_from, float p_to, float p_duration) {
+MethodTweener::MethodTweener(Callable p_callback, Variant p_from, Variant p_to, float p_duration) {
callback = p_callback;
initial_val = p_from;
delta_val = tween->calculate_delta_value(p_from, p_to);
diff --git a/scene/animation/tween.h b/scene/animation/tween.h
index 953d573539..6a48d332b8 100644
--- a/scene/animation/tween.h
+++ b/scene/animation/tween.h
@@ -128,7 +128,7 @@ public:
Ref<PropertyTweener> tween_property(Object *p_target, NodePath p_property, Variant p_to, float p_duration);
Ref<IntervalTweener> tween_interval(float p_time);
Ref<CallbackTweener> tween_callback(Callable p_callback);
- Ref<MethodTweener> tween_method(Callable p_callback, float p_from, float p_to, float p_duration);
+ Ref<MethodTweener> tween_method(Callable p_callback, Variant p_from, Variant p_to, float p_duration);
void append(Ref<Tweener> p_tweener);
bool custom_step(float p_delta);
@@ -258,7 +258,7 @@ public:
void start() override;
bool step(float &r_delta) override;
- MethodTweener(Callable p_callback, float p_from, float p_to, float p_duration);
+ MethodTweener(Callable p_callback, Variant p_from, Variant p_to, float p_duration);
MethodTweener();
protected: