Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/variant/variant_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,13 @@ double VariantUtilityFunctions::remap(double value, double istart, double istop,
return Math::remap(value, istart, istop, ostart, ostop);
}

double VariantUtilityFunctions::remap_default(double value, double istart, double istop, double ostart, double ostop, double default_value) {
if (istart == istop) {
Comment thread
Arctis-Fireblight marked this conversation as resolved.
Outdated
return default_value;
}
return Math::remap(value, istart, istop, ostart, ostop);
}

double VariantUtilityFunctions::smoothstep(double from, double to, double val) {
return Math::smoothstep(from, to, val);
}
Expand Down Expand Up @@ -1745,6 +1752,7 @@ void Variant::_register_variant_utility_functions() {
FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(inverse_lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(remap, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(remap_default, sarray("value", "istart", "istop", "ostart", "ostop", "default_value"), Variant::UTILITY_FUNC_TYPE_MATH);

FUNCBINDR(smoothstep, sarray("from", "to", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH);
Expand Down
1 change: 1 addition & 0 deletions core/variant/variant_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ struct VariantUtilityFunctions {
static double lerp_angle(double from, double to, double weight);
static double inverse_lerp(double from, double to, double weight);
static double remap(double value, double istart, double istop, double ostart, double ostop);
static double remap_default(double value, double istart, double istop, double ostart, double ostop, double default_value);
static double smoothstep(double from, double to, double val);
static double move_toward(double from, double to, double delta);
static double rotate_toward(double from, double to, double delta);
Expand Down
19 changes: 18 additions & 1 deletion doc/classes/@GlobalScope.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,24 @@
remap(75, 0, 100, -1, 1) # Returns 0.5
[/codeblock]
For complex use cases where multiple ranges are needed, consider using [Curve] or [Gradient] instead.
[b]Note:[/b] If [code]istart == istop[/code], the return value is undefined (most likely NaN, INF, or -INF).
[b]Note:[/b] If [code]istart == istop[/code], the return value is undefined (most likely NaN, INF, or -INF). See also [method remap_default].
</description>
</method>
<method name="remap_default" keywords="range, lerp, interpolate">
<return type="float" />
<param index="0" name="value" type="float" />
<param index="1" name="istart" type="float" />
<param index="2" name="istop" type="float" />
<param index="3" name="ostart" type="float" />
<param index="4" name="ostop" type="float" />
<param index="5" name="default_value" type="float" />
<description>
Maps a [param value] from range [code][istart, istop][/code] to [code][ostart, ostop][/code] and returns [param default_value] if [code]istart == istop[/code]. See also [method remap], [method lerp] and [method inverse_lerp]. If [param value] is outside [code][istart, istop][/code], then the resulting value will also be outside [code][ostart, ostop][/code]. If this is not desired, use [method clamp] on the result of this function.
[codeblock]
remap_default(75, 0, 100, -1, 1, 3) # Returns 0.5
remap_default(75, 0, 0, -1, 1, 3) # Returns 3.0
[/codeblock]
For complex use cases where multiple ranges are needed, consider using [Curve] or [Gradient] instead.
</description>
Comment thread
Arctis-Fireblight marked this conversation as resolved.
</method>
<method name="rid_allocate_id">
Expand Down
Loading