From d500e1445b59f7eaf74b3ae60a23c9bc1c9aae44 Mon Sep 17 00:00:00 2001 From: Digvijaysinh Gohil Date: Fri, 13 Oct 2023 00:16:04 +0530 Subject: [PATCH] CheckerBoard node added --- addons/ShaderLib/Procedural/CheckerBoard.gd | 69 +++++++++++++++++++ .../Procedural/CheckerBoard.gdshaderinc | 12 ++++ 2 files changed, 81 insertions(+) create mode 100644 addons/ShaderLib/Procedural/CheckerBoard.gd create mode 100644 addons/ShaderLib/Procedural/CheckerBoard.gdshaderinc diff --git a/addons/ShaderLib/Procedural/CheckerBoard.gd b/addons/ShaderLib/Procedural/CheckerBoard.gd new file mode 100644 index 0000000..a2e8701 --- /dev/null +++ b/addons/ShaderLib/Procedural/CheckerBoard.gd @@ -0,0 +1,69 @@ +@tool +class_name VisualShaderNodeProceduralCheckerBoard extends VisualShaderNodeCustom + +func _init() -> void: + _set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0)) + _set_input_port_default_value(2, Vector3(0.4, 0.4, 0.4)) + _set_input_port_default_value(3, Vector2(1.0, 1.0)) + + output_port_for_preview = 0 + +func _get_name() -> String: + return "CheckerBoard" + +func _get_category() -> String: + return "Procedural" + +func _get_description() -> String: + return "Generates a checkerboard of alternating colors between inputs Color A and Color B based on input UV." + +func _get_return_icon_type() -> VisualShaderNode.PortType: + return PORT_TYPE_VECTOR_3D + +func _get_input_port_count() -> int: + return 4 + +func _get_input_port_name(port: int) -> String: + match port: + 0: + return "uv" + 1: + return "color A" + 2: + return "color B" + 3: + return "frequency" + return "" + +func _get_input_port_type(port: int) -> VisualShaderNode.PortType: + match port: + 0, 3: + return PORT_TYPE_VECTOR_2D + 1, 2: + return PORT_TYPE_VECTOR_3D + return PORT_TYPE_SCALAR + +func _get_output_port_count() -> int: + return 1 + +func _get_output_port_name(port: int) -> String: + return "output" + +func _get_output_port_type(port: int) -> VisualShaderNode.PortType: + return PORT_TYPE_VECTOR_3D + +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("CheckerBoard.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 color_a: String = input_vars[1] + var color_b: String = input_vars[2] + var frequency: String = input_vars[3] + + return output_vars[0] + " = checker_board(%s, %s, %s, %s);" % [uv, color_a, color_b, frequency] diff --git a/addons/ShaderLib/Procedural/CheckerBoard.gdshaderinc b/addons/ShaderLib/Procedural/CheckerBoard.gdshaderinc new file mode 100644 index 0000000..3f6767f --- /dev/null +++ b/addons/ShaderLib/Procedural/CheckerBoard.gdshaderinc @@ -0,0 +1,12 @@ +vec3 checker_board(vec2 uv, vec3 color_a, vec3 color_b, vec2 frequency){ + uv = (uv.xy + 0.5) * frequency; + vec4 _derivatives = vec4(dFdx(uv), dFdy(uv)); + vec2 _duv_length = sqrt(vec2(dot(_derivatives.xz, _derivatives.xz), dot(_derivatives.yw, _derivatives.yw))); + float _width = 1.0; + vec2 _distance3 = 4.0 * abs(fract(uv + 0.25) - 0.5) - _width; + vec2 _scale = 0.35 / _duv_length.xy; + float _frequency_limiter = sqrt(clamp(1.1f - max(_duv_length.x, _duv_length.y), 0.0, 1.0)); + vec2 _vector_alpha = clamp(_distance3 * _scale.xy, -1.0, 1.0); + float _alpha = clamp(0.5f + 0.5f * _vector_alpha.x * _vector_alpha.y * _frequency_limiter, 0.0, 1.0); + return mix(color_b, color_a, _alpha); +} \ No newline at end of file