mirror of
https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git
synced 2025-01-07 01:43:35 +08:00
Parallax mapping node initial implementation
This commit is contained in:
parent
7e9953230a
commit
9b79e5fca3
56
addons/ShaderLib/UV/ParallaxMappingUV.gd
Normal file
56
addons/ShaderLib/UV/ParallaxMappingUV.gd
Normal file
@ -0,0 +1,56 @@
|
||||
@tool
|
||||
class_name VisualShaderNodeUVParallaxMapping extends VisualShaderNodeCustom
|
||||
|
||||
func _init() -> void:
|
||||
_set_input_port_default_value(1, 1.0)
|
||||
|
||||
func _get_name() -> String:
|
||||
return "ParallaxMapping"
|
||||
|
||||
func _get_category() -> String:
|
||||
return "UV"
|
||||
|
||||
func _get_description() -> String:
|
||||
return "The Parallax Mapping node lets you create a parallax effect that displaces a Material's UVs to create the illusion of depth inside a Material."
|
||||
|
||||
func _get_return_icon_type() -> VisualShaderNode.PortType:
|
||||
return PORT_TYPE_VECTOR_2D
|
||||
|
||||
func _get_input_port_count() -> int:
|
||||
return 2
|
||||
|
||||
func _get_input_port_name(port: int) -> String:
|
||||
match port:
|
||||
0:
|
||||
return "heightMap"
|
||||
1:
|
||||
return "amplitude"
|
||||
return ""
|
||||
|
||||
func _get_input_port_type(port: int) -> VisualShaderNode.PortType:
|
||||
match port:
|
||||
0:
|
||||
return PORT_TYPE_SAMPLER
|
||||
_:
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
func _get_output_port_count() -> int:
|
||||
return 1
|
||||
|
||||
func _get_output_port_name(port: int) -> String:
|
||||
return "uv"
|
||||
|
||||
func _get_output_port_type(port: int) -> VisualShaderNode.PortType:
|
||||
return PORT_TYPE_VECTOR_2D
|
||||
|
||||
func _get_global_code(mode: Shader.Mode) -> String:
|
||||
var code: String = preload("ParallaxMappingUV.gdshaderinc").code
|
||||
return code
|
||||
|
||||
func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String:
|
||||
var height_map: String = input_vars[0]
|
||||
var amplitude: String = input_vars[1]
|
||||
if !height_map:
|
||||
return output_vars[0] + " = UV;"
|
||||
|
||||
return output_vars[0] + " = parallax_mapping_uv(%s, %s, CAMERA_DIRECTION_WORLD, INV_VIEW_MATRIX, UV);" % [height_map, amplitude];
|
11
addons/ShaderLib/UV/ParallaxMappingUV.gdshaderinc
Normal file
11
addons/ShaderLib/UV/ParallaxMappingUV.gdshaderinc
Normal file
@ -0,0 +1,11 @@
|
||||
vec2 parallax_mapping_uv(sampler2D height_map, float bump_scale, vec3 view_direction, mat4 inv_view_matrix, vec2 uv){
|
||||
vec3 _eye = -(inv_view_matrix * vec4(view_direction, 0.0)).xyz;
|
||||
_eye = normalize(view_direction);
|
||||
float _depth = texture(height_map, uv).w;
|
||||
vec2 _half_offset = normalize(_eye).xy * (_depth) * bump_scale;
|
||||
_depth = (_depth + texture(height_map, uv + _half_offset).x) * 0.5;
|
||||
_half_offset = normalize(_eye).xy * (_depth) * bump_scale;
|
||||
_depth = (_depth + texture(height_map, uv + _half_offset).x) * 0.5;
|
||||
_half_offset = normalize(_eye).xy * (_depth) * bump_scale;
|
||||
return uv + _half_offset;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user