From 23fc704cbc79c77fb3e8bda8c58a8bbb4815951e Mon Sep 17 00:00:00 2001 From: Malcolm Nixon Date: Fri, 14 Jun 2024 03:08:16 -0400 Subject: This PR handles the smoothstep degenerate case where the range is empty. It also updates the documentation to describe positive and negative ranges. Co-Authored-By: Hugo Locurcio Co-Authored-By: kleonc <9283098+kleonc@users.noreply.github.com> --- core/math/math_funcs.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'core/math/math_funcs.h') diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 3060f31970..fd53ed28fd 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -447,14 +447,22 @@ public: static _ALWAYS_INLINE_ double smoothstep(double p_from, double p_to, double p_s) { if (is_equal_approx(p_from, p_to)) { - return p_from; + if (likely(p_from <= p_to)) { + return p_s <= p_from ? 0.0 : 1.0; + } else { + return p_s <= p_to ? 1.0 : 0.0; + } } double s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0, 1.0); return s * s * (3.0 - 2.0 * s); } static _ALWAYS_INLINE_ float smoothstep(float p_from, float p_to, float p_s) { if (is_equal_approx(p_from, p_to)) { - return p_from; + if (likely(p_from <= p_to)) { + return p_s <= p_from ? 0.0f : 1.0f; + } else { + return p_s <= p_to ? 1.0f : 0.0f; + } } float s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0f, 1.0f); return s * s * (3.0f - 2.0f * s); -- cgit v1.2.3