Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active November 5, 2024 19:08
Show Gist options
  • Save Shilo/13bf65846678b88dbc544db8634fe3d0 to your computer and use it in GitHub Desktop.
Save Shilo/13bf65846678b88dbc544db8634fe3d0 to your computer and use it in GitHub Desktop.
Godot 4 shader for 2d fog for ColorRect. Useful for top down games. Note: Use Parallax2D and seamless NoiseTexture2D for best results.
shader_type canvas_item;
uniform sampler2D noise_texture: repeat_enable, filter_nearest;
uniform vec2 speed = vec2(0.01, 0.01);
uniform float sparsity: hint_range(0.0, 5.0) = 1.0;
uniform float alpha: hint_range(0.0, 1.0) = 0.5;
uniform float edge_falloff: hint_range(0.0, 1.0) = 0.0;
uniform float edge_falloff_sensitivity: hint_range(1.0, 20.0) = 1.0;
void fragment() {
vec2 uv = UV + speed * TIME;
float noise = texture(noise_texture, uv).r;
float fog = clamp(noise * (1.0 + sparsity) - sparsity, 0.0, 1.0);
vec2 dist_to_edge = vec2(1.0 - abs(UV.x * 2.0 - 1.0), 1.0 - abs(UV.y * 2.0 - 1.0)) * edge_falloff_sensitivity;
float edge_factor = clamp(pow(min(dist_to_edge.x, dist_to_edge.y), edge_falloff), 0.0, 1.0);
COLOR.a *= fog * alpha * edge_factor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment