Distance Gradient Fog 4.3+

This shader is inspired by the atmospheric fog and stylized visual design of Among Trees.

This is a full-screen postprocessing shader for Godot 4.3+ that applies distance-based fog using the DEPTH_TEXTURE and a customizable GradientTexture2D. It blends the scene with a color gradient based on the camera depth, creating atmospheric effects like haze, mist, or stylized fog for outdoor and indoor scenes.

⚠️ Requires Godot 4.3+ (due to changes in clip-space and screen texture handling).

Ideal for:

  • Open world environments

  • Distant terrain fading

  • Atmospheric dungeons

📋 Setup:

  • Add a Quadmesh to your camera size 2×2
  • Flip its faces
  • Apply the shader as a ShaderMaterial to the QuadMesh
  • Create a GradientTexture2D
  • Add colors from left (near) to right (far)
  • Add alpha if you want fading

 

✅ Done! You can now control shader parameters as needed


credits: 
i used the abandoned spaceship demo scene you can get it by clicking on the link

Shader code
shader_type spatial;
render_mode unshaded, cull_disabled, depth_draw_always;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;
uniform sampler2D DEPTH_TEXTURE : hint_depth_texture, filter_linear;
uniform sampler2D gradient;

uniform float fog_strength : hint_range(0.0, 1.0) = 1.0;
uniform float fog_amount : hint_range(0.0, 1.0) = 0.15;
uniform float fog_near = 1.0;
uniform float fog_far = 150.0;

void vertex() {
    POSITION = vec4(VERTEX.xy, 1.0, 1.0);
}

void fragment() {
    vec4 original = texture(SCREEN_TEXTURE, SCREEN_UV);
    float raw_depth = texture(DEPTH_TEXTURE, SCREEN_UV).x;

    vec4 clip = vec4(SCREEN_UV * 2.0 - 1.0, raw_depth, 1.0);
    vec4 view = INV_PROJECTION_MATRIX * clip;
    view /= view.w;
    float linear_depth = -view.z;

   
    float fog_factor = clamp((linear_depth - fog_near) / (fog_far - fog_near), 0.0, 1.0);
    fog_factor *= fog_amount;

   
    vec4 fog_color = texture(gradient, vec2(fog_factor, 0.0));

    
    vec3 final_color = mix(original.rgb, fog_color.rgb, fog_color.a * fog_strength);
    ALBEDO = final_color;
}
Live Preview
Tags
among trees, atmospheric, depth, Distance, effect, firewatch, Fog, fullscreen, Godot4.3, gradient, postprocessing, shader
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments