From 865da09871b0242471e7426ed54e9815193df5ae Mon Sep 17 00:00:00 2001 From: Silc 'Tokage' Renew Date: Fri, 11 Feb 2022 12:43:21 +0900 Subject: Implement cubic_interpolate() as MathFunc for refactoring --- .../doc_classes/VisualScriptBuiltinFunc.xml | 84 +++++++++++----------- .../visual_script/visual_script_builtin_funcs.cpp | 26 +++++++ .../visual_script/visual_script_builtin_funcs.h | 1 + 3 files changed, 70 insertions(+), 41 deletions(-) (limited to 'modules/visual_script') diff --git a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml index 1ac5768755..55afacbea1 100644 --- a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml +++ b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml @@ -96,123 +96,125 @@ Returns a number linearly interpolated between the first two inputs, based on the third input. Uses the formula [code]a + (a - b) * t[/code]. - + - + - + + + Moves the number toward a value, based on the third input. - + Randomize the seed (or the internal state) of the random number generator. Current implementation reseeds using a number based on time. - + Returns a random 32 bits integer value. To obtain a random value between 0 to N (where N is smaller than 2^32 - 1), you can use it with the remainder function. - + Returns a random floating-point value between 0 and 1. To obtain a random value between 0 to N, you can use it with multiplication. - + Returns a random 32-bit integer value between the two inputs. - + Returns a random floating-point value between the two inputs. - + Returns a normally-distributed pseudo-random number, using Box-Muller transform with the specified mean and a standard deviation. This is also called Gaussian distribution. - + Set the seed for the random number generator. - + Returns a random value from the given seed, along with the new seed. - + Convert the input from degrees to radians. - + Convert the input from radians to degrees. - + Convert the input from linear volume to decibel volume. - + Convert the input from decibel volume to linear volume. - + - + - + Returns the [code]value[/code] wrapped between [code]0[/code] and the [code]length[/code]. If the limit is reached, the next value the function returned is decreased to the [code]0[/code] side or increased to the [code]length[/code] side (like a triangle wave). If [code]length[/code] is less than zero, it becomes positive. - + Returns the greater of the two numbers, also known as their maximum. - + Returns the lesser of the two numbers, also known as their minimum. - + Returns the input clamped inside the given range, ensuring the result is never outside it. Equivalent to [code]min(max(input, range_low), range_high)[/code]. - + Returns the nearest power of 2 to the input. - + Create a [WeakRef] from the input. - + Convert between types. - + Returns the type of the input as an integer. Check [enum Variant.Type] for the integers that might be returned. - + Checks if a type is registered in the [ClassDB]. - + Returns a character with the given ascii value. - + Convert the input to a string. - + Print the given string to the output window. - + Print the given string to the standard error output. - + Print the given string to the standard output, without adding a newline. - + - + Serialize a [Variant] to a string. - + Deserialize a [Variant] from a string serialized using [constant VAR_TO_STR]. - + Serialize a [Variant] to a [PackedByteArray]. - + Deserialize a [Variant] from a [PackedByteArray] serialized using [constant VAR_TO_BYTES]. - + Returns a number smoothly interpolated between the first two inputs, based on the third input. Similar to [constant MATH_LERP], but interpolates faster at the beginning and slower at the end. Using Hermite interpolation formula: [codeblock] var t = clamp((weight - from) / (to - from), 0.0, 1.0) return t * t * (3.0 - 2.0 * t) [/codeblock] - + - + - + - + Represents the size of the [enum BuiltinFunc] enum. diff --git a/modules/visual_script/visual_script_builtin_funcs.cpp b/modules/visual_script/visual_script_builtin_funcs.cpp index fd55796a66..f6bc855a50 100644 --- a/modules/visual_script/visual_script_builtin_funcs.cpp +++ b/modules/visual_script/visual_script_builtin_funcs.cpp @@ -65,6 +65,7 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX "step_decimals", "snapped", "lerp", + "cubic_interpolate", "inverse_lerp", "range_lerp", "move_toward", @@ -212,6 +213,7 @@ int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) { case MATH_WRAPF: case LOGIC_CLAMP: return 3; + case MATH_CUBIC_INTERPOLATE: case MATH_RANGE_LERP: return 5; case FUNC_MAX: { @@ -329,6 +331,19 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const return PropertyInfo(Variant::FLOAT, "weight"); } } break; + case MATH_CUBIC_INTERPOLATE: { + if (p_idx == 0) { + return PropertyInfo(Variant::FLOAT, "from"); + } else if (p_idx == 1) { + return PropertyInfo(Variant::FLOAT, "to"); + } else if (p_idx == 2) { + return PropertyInfo(Variant::FLOAT, "pre"); + } else if (p_idx == 3) { + return PropertyInfo(Variant::FLOAT, "post"); + } else { + return PropertyInfo(Variant::FLOAT, "weight"); + } + } break; case MATH_RANGE_LERP: { if (p_idx == 0) { return PropertyInfo(Variant::FLOAT, "value"); @@ -525,6 +540,7 @@ PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) cons } break; case MATH_SNAPPED: case MATH_LERP: + case MATH_CUBIC_INTERPOLATE: case MATH_LERP_ANGLE: case MATH_INVERSE_LERP: case MATH_RANGE_LERP: @@ -795,6 +811,14 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in VALIDATE_ARG_NUM(2); *r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); } break; + case VisualScriptBuiltinFunc::MATH_CUBIC_INTERPOLATE: { + VALIDATE_ARG_NUM(0); + VALIDATE_ARG_NUM(1); + VALIDATE_ARG_NUM(2); + VALIDATE_ARG_NUM(3); + VALIDATE_ARG_NUM(4); + *r_return = Math::cubic_interpolate((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2], (double)*p_inputs[3], (double)*p_inputs[4]); + } break; case VisualScriptBuiltinFunc::MATH_LERP_ANGLE: { VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); @@ -1220,6 +1244,7 @@ void VisualScriptBuiltinFunc::_bind_methods() { BIND_ENUM_CONSTANT(MATH_STEP_DECIMALS); BIND_ENUM_CONSTANT(MATH_SNAPPED); BIND_ENUM_CONSTANT(MATH_LERP); + BIND_ENUM_CONSTANT(MATH_CUBIC_INTERPOLATE); BIND_ENUM_CONSTANT(MATH_INVERSE_LERP); BIND_ENUM_CONSTANT(MATH_RANGE_LERP); BIND_ENUM_CONSTANT(MATH_MOVE_TOWARD); @@ -1309,6 +1334,7 @@ void register_visual_script_builtin_func_node() { VisualScriptLanguage::singleton->add_register_func("functions/built_in/step_decimals", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/snapped", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp", create_builtin_func_node); + VisualScriptLanguage::singleton->add_register_func("functions/built_in/cubic_interpolate", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp_angle", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/inverse_lerp", create_builtin_func_node); VisualScriptLanguage::singleton->add_register_func("functions/built_in/range_lerp", create_builtin_func_node); diff --git a/modules/visual_script/visual_script_builtin_funcs.h b/modules/visual_script/visual_script_builtin_funcs.h index d689296233..18935b9995 100644 --- a/modules/visual_script/visual_script_builtin_funcs.h +++ b/modules/visual_script/visual_script_builtin_funcs.h @@ -65,6 +65,7 @@ public: MATH_STEP_DECIMALS, MATH_SNAPPED, MATH_LERP, + MATH_CUBIC_INTERPOLATE, MATH_INVERSE_LERP, MATH_RANGE_LERP, MATH_MOVE_TOWARD, -- cgit v1.2.3