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

HeightToNormal Node added for feature request #5

This commit is contained in:
Digvijaysinh Gohil 2024-09-27 23:58:41 +05:30
parent d72153b7c4
commit fb19605155
3 changed files with 81 additions and 1 deletions

View File

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

View File

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

View File

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