Skip to content

Instantly share code, notes, and snippets.

@realkotob
Last active August 14, 2019 10:41
Show Gist options
  • Save realkotob/78aa403456d1163126da931c46d1440f to your computer and use it in GitHub Desktop.
Save realkotob/78aa403456d1163126da931c46d1440f to your computer and use it in GitHub Desktop.
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