Last active
August 14, 2019 10:41
-
-
Save realkotob/78aa403456d1163126da931c46d1440f to your computer and use it in GitHub Desktop.
Source: https://www.reddit.com/r/godot/comments/cp9pq2/fog_portal_shader/ https://twitter.com/lentsius_bark/status/1161353797056258048
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shader_type spatial; | |
render_mode unshaded; | |
render_mode cull_disabled; | |
uniform float depth_distance = 1.0; | |
uniform float beer_factor : hint_range(0.0,0.2) = 0.2; | |
uniform vec4 fog_colour : hint_color; | |
uniform float cam_distance = 1.0; | |
uniform float cam_distance_multiplier = 1.0; | |
uniform float gradient; | |
uniform float distance_fade_min; | |
uniform float distance_fade_max = 5.0; | |
void fragment(){ | |
ALBEDO = fog_colour.rgb; | |
float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r; | |
depth = depth * 2.0 - 1.0; | |
depth = PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]); | |
depth = depth + VERTEX.z; | |
// beer's law | |
depth = exp(-depth * beer_factor); | |
depth *= depth_distance; | |
depth = clamp(1.0 - depth, 0.0, 1.0); | |
//proximity fade | |
float depth_tex = textureLod(DEPTH_TEXTURE,SCREEN_UV,0.0).r; | |
vec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0,depth_tex * 2.0-1.0,1.0); | |
world_pos.xyz /= world_pos.w; | |
depth *= clamp(1.0 - smoothstep(world_pos.z + gradient, world_pos.z, VERTEX.z), 0.0, 1.0); | |
depth *=clamp(smoothstep(distance_fade_min,distance_fade_max,-VERTEX.z),0.0,1.0); | |
ALPHA *= depth; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment