From 0d5c68fb1fa7bfcd1547b4bcff33d457123eb896 Mon Sep 17 00:00:00 2001 From: Digvijaysinh Gohil Date: Tue, 19 Mar 2024 19:43:02 +0530 Subject: [PATCH] Manhattan Distance node added --- .../Vector/Distance/Manhattan2D.gdshaderinc | 4 + .../Vector/Distance/Manhattan3D.gdshaderinc | 4 + .../Vector/Distance/ManhattanDistance.gd | 88 +++++++++++++++++++ documentation/Documentation.md | 4 + .../Vector/Distance/ManhattanDistance.md | 20 +++++ 5 files changed, 120 insertions(+) create mode 100644 addons/ShaderLib/Maths/Vector/Distance/Manhattan2D.gdshaderinc create mode 100644 addons/ShaderLib/Maths/Vector/Distance/Manhattan3D.gdshaderinc create mode 100644 addons/ShaderLib/Maths/Vector/Distance/ManhattanDistance.gd create mode 100644 documentation/Nodes/Maths/Vector/Distance/ManhattanDistance.md diff --git a/addons/ShaderLib/Maths/Vector/Distance/Manhattan2D.gdshaderinc b/addons/ShaderLib/Maths/Vector/Distance/Manhattan2D.gdshaderinc new file mode 100644 index 0000000..b2d6d7e --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/Distance/Manhattan2D.gdshaderinc @@ -0,0 +1,4 @@ +float manhattan_distance_2d(vec2 point1, vec2 point2) { + vec2 d = point1 - point2; + return abs(d.x) + abs(d.y); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Vector/Distance/Manhattan3D.gdshaderinc b/addons/ShaderLib/Maths/Vector/Distance/Manhattan3D.gdshaderinc new file mode 100644 index 0000000..78ba265 --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/Distance/Manhattan3D.gdshaderinc @@ -0,0 +1,4 @@ +float manhattan_distance_3d(vec3 point1, vec3 point2) { + vec3 d = point1 - point2; + return abs(d.x) + abs(d.y) + abs(d.z); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Vector/Distance/ManhattanDistance.gd b/addons/ShaderLib/Maths/Vector/Distance/ManhattanDistance.gd new file mode 100644 index 0000000..ce99534 --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/Distance/ManhattanDistance.gd @@ -0,0 +1,88 @@ +@tool +class_name VisualShaderNodeMathsManhattanDistance extends VisualShaderNodeCustom + +func _get_name() -> String: + return "ManhattanDistance" + +func _get_category() -> String: + return "Maths/Vector/Distance" + +func _get_description() -> String: + return "Returns the distance between two points using Manhattan distance matrix." + +func _get_return_icon_type() -> PortType: + return PORT_TYPE_SCALAR + +func _get_input_port_count() -> int: + return 2 + +func _get_input_port_name(port: int) -> String: + match port: + 0: + return "a" + _: + return "b" + +func _get_input_port_type(port: int) -> PortType: + var vector_index: int = get_option_index(0) + match vector_index: + 0: + return PORT_TYPE_VECTOR_2D + _: + return PORT_TYPE_VECTOR_3D + +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("Manhattan2D.gdshaderinc").code + _: + code = preload("Manhattan3D.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 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] + " = manhattan_distance_2d(%s, %s);" % [point_a, point_b] + _: + return output_vars[0] + " = manhattan_distance_3d(%s, %s);" % [point_a, point_b] + diff --git a/documentation/Documentation.md b/documentation/Documentation.md index 90f5a2a..1d2adc4 100644 --- a/documentation/Documentation.md +++ b/documentation/Documentation.md @@ -32,6 +32,10 @@ For example if you want to rotate UV in your **_.gdshader_** file, you can use `

 Vector

+

  Distance

+ +

   Manhattan Distance node

+

  Project node

  Project On Plane node

  Vector Transform node

diff --git a/documentation/Nodes/Maths/Vector/Distance/ManhattanDistance.md b/documentation/Nodes/Maths/Vector/Distance/ManhattanDistance.md new file mode 100644 index 0000000..a1a2d4a --- /dev/null +++ b/documentation/Nodes/Maths/Vector/Distance/ManhattanDistance.md @@ -0,0 +1,20 @@ +# Manhattan Distance node +Returns the distance between two points using Manhattan distance matrix. +
+ +**Controls** +|Options|Description| +|---|---| +|Vector2, Vector3|Vector type to use for calculation| + +**Inputs** +|Name|Type|Binding|Description| +|---|---|---|---| +|a|Dynamic vector|none|Point a| +|b|Dynamic vector|none|Point b| + +**Outputs** +|Name|Type|Binding|Description| +|---|---|---|---| +|distance|float|None|Distance between 2 points| +___ \ No newline at end of file