From c03d81fcfd08e02daabfbb12a51d3c0299a29fb0 Mon Sep 17 00:00:00 2001 From: Digvijaysinh Gohil Date: Thu, 12 Oct 2023 19:46:55 +0530 Subject: [PATCH] Voronoi node added --- addons/ShaderLib/Procedural/Noise/Voronoi.gd | 72 +++++++++++++++++++ .../Procedural/Noise/Voronoi.gdshaderinc | 25 +++++++ 2 files changed, 97 insertions(+) create mode 100644 addons/ShaderLib/Procedural/Noise/Voronoi.gd create mode 100644 addons/ShaderLib/Procedural/Noise/Voronoi.gdshaderinc diff --git a/addons/ShaderLib/Procedural/Noise/Voronoi.gd b/addons/ShaderLib/Procedural/Noise/Voronoi.gd new file mode 100644 index 0000000..e050378 --- /dev/null +++ b/addons/ShaderLib/Procedural/Noise/Voronoi.gd @@ -0,0 +1,72 @@ +@tool +class_name VisualShaderNodeProceduralVoronoi extends VisualShaderNodeCustom + +func _init() -> void: + _set_input_port_default_value(1, 5.0) + _set_input_port_default_value(2, 2.0) + + output_port_for_preview = 0 + +func _get_name() -> String: + return "Voronoi" + +func _get_category() -> String: + return "Procedural/Noise" + +func _get_description() -> String: + return "Generates a Voronoi or Worley noise based on input UV." + +func _get_return_icon_type() -> VisualShaderNode.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 "uv" + 1: + return "cell density" + 2: + return "angle offset" + return "" + +func _get_input_port_type(port: int) -> VisualShaderNode.PortType: + match port: + 0: + return PORT_TYPE_VECTOR_2D + 1, 2: + return PORT_TYPE_SCALAR + return PORT_TYPE_SCALAR + +func _get_output_port_count() -> int: + return 2 + +func _get_output_port_name(port: int) -> String: + match port: + 0: + return "output" + 1: + return "cells" + return "" + +func _get_output_port_type(port: int) -> VisualShaderNode.PortType: + return PORT_TYPE_SCALAR + +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("Voronoi.gdshaderinc").code + return code + +func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: + var uv: String = "UV" + + if input_vars[0]: + uv = input_vars[0] + + var cell_density: String = input_vars[1] + var angle_offset: String = input_vars[2] + var output: String = output_vars[0] + var cells: String = output_vars[1] + + return "voronoi_noise(%s,%s, %s, %s, %s);" % [uv, cell_density, angle_offset, output, cells] diff --git a/addons/ShaderLib/Procedural/Noise/Voronoi.gdshaderinc b/addons/ShaderLib/Procedural/Noise/Voronoi.gdshaderinc new file mode 100644 index 0000000..ecbc336 --- /dev/null +++ b/addons/ShaderLib/Procedural/Noise/Voronoi.gdshaderinc @@ -0,0 +1,25 @@ +void voronoi_noise(vec2 uv, float cell_density, float angle_offset, out float output, out float cells){ + vec2 g = floor(uv * cell_density); + vec2 f = fract(uv * cell_density); + float t = 8.0; + vec3 res = vec3(8.0, 0.0, 0.0); + mat2 m = mat2(vec2(15.27, 47.63), vec2(99.41, 89.98)); + + for(int y=-1; y<=1; y++) + { + for(int x=-1; x<=1; x++) + { + vec2 lattice = vec2(float(x), float(y)); + vec2 new_uv = lattice + g; + new_uv = fract(sin(new_uv * m) * 46839.32); + vec2 offset = vec2(sin(new_uv.y * angle_offset) * 0.5 + 0.5, cos(new_uv.x * angle_offset) * 0.5 + 0.5); + float d = distance(lattice + offset, f); + if(d < res.x) + { + res = vec3(d, offset.x, offset.y); + output = res.x; + cells = res.y; + } + } + } +} \ No newline at end of file