Skip to content

Instantly share code, notes, and snippets.

@danilw
Created January 17, 2024 21:34
Show Gist options
  • Save danilw/71a5c37cc124bc52ba5f171e4ac148e6 to your computer and use it in GitHub Desktop.
Save danilw/71a5c37cc124bc52ba5f171e4ac148e6 to your computer and use it in GitHub Desktop.
Godot 4 depth fog on unshaded
// depth fog is something like this
shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx,unshaded;
uniform sampler2D depth_tex:hint_depth_texture;
void vertex() {
}
void fragment() {
ALBEDO = vec3(1.,0.,0.);
const vec3 fog_color = vec3(0.,1.,1.); //replace with sky texture or anything
const float depth_far = 10.;
float depth = texture(depth_tex, SCREEN_UV).r;
depth = depth * 2.0 - 1.0;
float z = -PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]);
float x = (SCREEN_UV.x * 2.0 - 1.0) * z / PROJECTION_MATRIX[0][0];
float y = (SCREEN_UV.y * 2.0 - 1.0) * z / PROJECTION_MATRIX[1][1];
z*=1./depth_far;
depth=1.+z;
depth=clamp(depth,0.,1.);
ALBEDO = mix(fog_color,ALBEDO,depth);
METALLIC = 0.001;
ROUGHNESS = 0.001;
SPECULAR = 0.001;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment