diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index c1a0ad73b09..4b38bf5716e 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -235,7 +235,44 @@ struct VariantUtilityFunctions { return Math::snapped(value, step); } - static inline double lerp(double from, double to, double weight) { + static inline Variant lerp(const Variant &from, const Variant &to, double weight, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + if (from.get_type() != to.get_type()) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 1; + return Variant(); + } + + switch (from.get_type()) { + case Variant::FLOAT: { + return lerpf(VariantInternalAccessor::get(&from), to, weight); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor::get(&from).lerp(VariantInternalAccessor::get(&to), weight); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor::get(&from).lerp(VariantInternalAccessor::get(&to), weight); + } break; + case Variant::VECTOR4: { + return VariantInternalAccessor::get(&from).lerp(VariantInternalAccessor::get(&to), weight); + } break; + case Variant::QUATERNION: { + return VariantInternalAccessor::get(&from).slerp(VariantInternalAccessor::get(&to), weight); + } break; + case Variant::BASIS: { + return VariantInternalAccessor::get(&from).slerp(VariantInternalAccessor::get(&to), weight); + } break; + case Variant::COLOR: { + return VariantInternalAccessor::get(&from).lerp(VariantInternalAccessor::get(&to), weight); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double lerpf(double from, double to, double weight) { return Math::lerp(from, to, weight); } @@ -1262,7 +1299,6 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(absi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDVR(sign, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); @@ -1280,7 +1316,8 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(snapped, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDVR3(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(lerpf, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(cubic_interpolate, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(bezier_interpolate, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index 4a16244235a..6c7199c689b 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -439,16 +439,18 @@ - - - - + + + + Linearly interpolates between two values by the factor defined in [code]weight[/code]. To perform interpolation, [code]weight[/code] should be between [code]0.0[/code] and [code]1.0[/code] (inclusive). However, values outside this range are allowed and can be used to perform [i]extrapolation[/i]. Use [method clamp] on the result of [method lerp] if this is not desired. + Both [code]from[/code] and [code]to[/code] must have matching types. Supported types: [float], [Vector2], [Vector3], [Vector4], [Color], [Quaternion], [Basis]. [codeblock] lerp(0, 4, 0.75) # Returns 3.0 [/codeblock] See also [method inverse_lerp] which performs the reverse of this operation. To perform eased interpolation with [method lerp], combine it with [method ease] or [method smoothstep]. See also [method range_lerp] to map a continuous series of values to another. + [b]Note:[/b] For better type safety, you can use [method lerpf], [method Vector2.lerp], [method Vector3.lerp], [method Vector4.lerp], [method Color.lerp], [method Quaternion.slerp] or [method Basis.slerp] instead. @@ -471,6 +473,19 @@ [b]Note:[/b] This method lerps through the shortest path between [code]from[/code] and [code]to[/code]. However, when these two angles are approximately [code]PI + k * TAU[/code] apart for any integer [code]k[/code], it's not obvious which way they lerp due to floating-point precision errors. For example, [code]lerp_angle(0, PI, weight)[/code] lerps counter-clockwise, while [code]lerp_angle(0, PI + 5 * TAU, weight)[/code] lerps clockwise. + + + + + + + Linearly interpolates between two values by the factor defined in [code]weight[/code]. To perform interpolation, [code]weight[/code] should be between [code]0.0[/code] and [code]1.0[/code] (inclusive). However, values outside this range are allowed and can be used to perform [i]extrapolation[/i]. + [codeblock] + lerp(0, 4, 0.75) # Returns 3.0 + [/codeblock] + See also [method inverse_lerp] which performs the reverse of this operation. To perform eased interpolation with [method lerp], combine it with [method ease] or [method smoothstep]. + +