diff --git a/addons/ShaderLib/Maths/Vector/Distance/Chebyshev2D.gdshaderinc b/addons/ShaderLib/Maths/Vector/Distance/Chebyshev2D.gdshaderinc new file mode 100644 index 0000000..a270d5b --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/Distance/Chebyshev2D.gdshaderinc @@ -0,0 +1,4 @@ +float chebyshev_distance_2d(vec2 point1, vec2 point2, float power) { + vec2 p = abs(point1 - point2); + return pow(pow(p.x, power) + pow(p.y, power), 1. / power); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Vector/Distance/Chebyshev3D.gdshaderinc b/addons/ShaderLib/Maths/Vector/Distance/Chebyshev3D.gdshaderinc new file mode 100644 index 0000000..d5e525b --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/Distance/Chebyshev3D.gdshaderinc @@ -0,0 +1,4 @@ +float chebyshev_distance_3d(vec3 point1, vec3 point2, float power) { + vec3 p = abs(point1 - point2); + return pow(pow(p.x, power) + pow(p.y, power) + pow(p.z, power), 1. / power); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Vector/Distance/ChebyshevDistance.gd b/addons/ShaderLib/Maths/Vector/Distance/ChebyshevDistance.gd new file mode 100644 index 0000000..bd89d87 --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/Distance/ChebyshevDistance.gd @@ -0,0 +1,102 @@ +@tool +class_name VisualShaderNodeMathsChebyshevDistance extends VisualShaderNodeCustom + +func _get_name() -> String: + return "ChebyshevDistance" + +func _get_category() -> String: + return "Maths/Vector/Distance" + +func _get_description() -> String: + return "Returns the distance between two points using Chebyshev distance matrix." + +func _get_return_icon_type() -> PortType: + return PORT_TYPE_SCALAR + +func _get_input_port_count() -> int: + return 3 + +func _get_input_port_name(port: int) -> String: + match port: + 0: + return "a" + 1: + return "b" + _: + return "power" + +func _get_input_port_type(port: int) -> PortType: + var vector_index: int = get_option_index(0) + match port: + 2: + return PORT_TYPE_SCALAR + _: + match vector_index: + 0: + return PORT_TYPE_VECTOR_2D + _: + return PORT_TYPE_VECTOR_3D + +func _get_input_port_default_value(port: int) -> Variant: + match port: + 2: + return 2.0 + _: + return null + +func _get_output_port_count() -> int: + return 1 + +func _get_output_port_name(port: int) -> String: + return "distance" + +func _get_output_port_type(port: int) -> PortType: + return PORT_TYPE_SCALAR + +func _get_property_count() -> int: + return 1 + +func _get_property_name(index: int) -> String: + return "" + +func _get_property_default_index(index: int) -> int: + return 0 + +func _get_property_options(index: int) -> PackedStringArray: + return ["Vector2", "Vector3"] + +func _get_global_code(mode: Shader.Mode) -> String: + var code: String + var vector_index: int = get_option_index(0) + match vector_index: + 0: + code = preload("Chebyshev2D.gdshaderinc").code + _: + code = preload("Chebyshev3D.gdshaderinc").code + return code + +func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: + var point_a: String + var point_b: String + var power: String = input_vars[2] + var vector_index: int = get_option_index(0) + match vector_index: + 0: + point_a = "vec2(0)" + point_b = "vec2(0)" + _: + point_b = "vec3(0)" + point_a = "vec3(0)" + + if input_vars[0]: + point_a = input_vars[0] + + if input_vars[1]: + point_b = input_vars[1] + + match vector_index: + 0: + return output_vars[0] + " = chebyshev_distance_2d(%s, %s, %s);" % [point_a, point_b, power] + _: + return output_vars[0] + " = chebyshev_distance_3d(%s, %s, %s);" % [point_a, point_b, power] + diff --git a/documentation/Documentation.md b/documentation/Documentation.md index 1d2adc4..4ae293b 100644 --- a/documentation/Documentation.md +++ b/documentation/Documentation.md @@ -34,6 +34,7 @@ For example if you want to rotate UV in your **_.gdshader_** file, you can use `