diff --git a/addons/ShaderLib/Procedural/HeightToNormal.gd b/addons/ShaderLib/Procedural/HeightToNormal.gd new file mode 100644 index 0000000..e7d13cb --- /dev/null +++ b/addons/ShaderLib/Procedural/HeightToNormal.gd @@ -0,0 +1,65 @@ +@tool +class_name VisualShaderNodeProceduralHeightToNormal extends VisualShaderNodeCustom + +func _get_name() -> String: + return "HeightToNormal" + +func _get_category() -> String: + return "Procedural" + +func _get_description() -> String: + return "Generates a normal map from a height map." + +func _get_return_icon_type() -> PortType: + return PORT_TYPE_VECTOR_3D + +func _get_input_port_count() -> int: + return 3 + +func _get_input_port_name(port: int) -> String: + match port: + 0: + return "height map" + 1: + return "uv" + 2: + return "bump strength" + return "" + +func _get_input_port_type(port: int) -> PortType: + match port: + 0: + return PORT_TYPE_SAMPLER + 1: + return PORT_TYPE_VECTOR_2D + _: + return PORT_TYPE_SCALAR + +func _get_input_port_default_value(port: int) -> Variant: + match port: + 2: + return 8.0 + _: + return null + +func _get_output_port_count() -> int: + return 1 + +func _get_output_port_name(port: int) -> String: + return "normal" + +func _get_output_port_type(port: int) -> PortType: + return PORT_TYPE_VECTOR_3D + +func _get_global_code(mode: Shader.Mode) -> String: + return "#include \"res://addons/ShaderLib/Procedural/Procedural.gdshaderinc\"" + +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[1]: + uv = input_vars[1] + + var sampler: String = input_vars[0] + var bump: String = input_vars[2] + + return output_vars[0] + " = heigth_to_normal(%s, %s, %s);" % [sampler, uv, bump] diff --git a/addons/ShaderLib/Procedural/Procedural.gdshaderinc b/addons/ShaderLib/Procedural/Procedural.gdshaderinc index 9b4b7ca..594a9ae 100644 --- a/addons/ShaderLib/Procedural/Procedural.gdshaderinc +++ b/addons/ShaderLib/Procedural/Procedural.gdshaderinc @@ -229,4 +229,19 @@ float rounded_rectangle_shape(vec2 uv, float width, float height, float radius){ uv = abs(uv * 2.0 - 1.0) - vec2(width, height) + radius; float dist = length(max(vec2(0.0), uv)) / radius; return clamp((1.0 - dist) / fwidth(dist), 0.0, 1.0); +} + +vec3 heigth_to_normal(sampler2D height_map, vec2 uv, float bump_strength) { + float pixel_width = .002; + float height = texture(height_map, uv).r; + float r = height - texture(height_map, uv + vec2(pixel_width, 0)).r; + float l = height - texture(height_map, uv - vec2(pixel_width, 0)).r; + float u = height - texture(height_map, uv + vec2(0, pixel_width)).r; + float d = height - texture(height_map, uv - vec2(0, pixel_width)).r; + float h = (r - l) / pixel_width; + float v = (u - d) / pixel_width; + vec3 n = vec3(h, v, 1.); + n.x = n.x * (pixel_width * bump_strength * .5) + .5; + n.y = n.y * (pixel_width * bump_strength * .5) + .5; + return normalize(n); } \ No newline at end of file diff --git a/project.godot b/project.godot index 32979cd..245a2ff 100644 --- a/project.godot +++ b/project.godot @@ -12,5 +12,5 @@ config_version=5 config/name="Godot-shader-lib" config/tags=PackedStringArray("addons") -config/features=PackedStringArray("4.2", "Forward Plus") +config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://icons/icon.png"