1
0
mirror of https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git synced 2025-01-07 01:43:35 +08:00

CheckerBoard node added

This commit is contained in:
Digvijaysinh Gohil 2023-10-13 00:16:04 +05:30
parent 8eed7bc1e5
commit d500e1445b
2 changed files with 81 additions and 0 deletions

View File

@ -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]

View File

@ -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);
}