Snow, moss or dust on the tops of things is usually a shader that checks how much a surface faces up. In fragment() Godot's NORMAL is in view space, so its y means up on the screen, not up in the world. In our Godot 4.7 test a flat roof was 100% snow with the camera a little above it, and 0% when the camera looked down on it steeply: from up there, the roof faces the camera, not the top of the screen. The script below turns the normal into world space in vertex() with MODEL_NORMAL_MATRIX: the roof stayed 100% snow from all 3 camera positions and the wall 0%.
The code
Save it as snow_cover.gd and load it with const Snow := preload("res://snow_cover.gd"). Then $House.material_override = Snow.make(Color(0.4, 0.3, 0.25), 0.5): the colour under the snow and how much snow. The shader code is in the script, so you can also copy it into a .gdshader file.
extends RefCounted
## Snow on everything that faces up, measured in the world, so it stays on the roofs
## when the camera moves or looks down.
## const Snow := preload("res://snow_cover.gd")
## $House.material_override = Snow.make(Color(0.4, 0.3, 0.25), 0.5)
const CODE := """
shader_type spatial;
uniform vec4 base_color : source_color = vec4(0.4, 0.3, 0.25, 1.0);
uniform vec4 snow_color : source_color = vec4(0.95, 0.97, 1.0, 1.0);
uniform float snow_amount : hint_range(0.0, 1.0) = 0.5; // 0 = no snow, 1 = snow on every slope
uniform float edge : hint_range(0.0, 0.5) = 0.05;
varying vec3 world_normal;
void vertex() {
// NORMAL here is in the mesh's own space; this turns it into the world's
world_normal = normalize(MODEL_NORMAL_MATRIX * NORMAL);
}
void fragment() {
float up = normalize(world_normal).y; // 1 = flat top, 0 = wall
float limit = 1.0 - snow_amount;
float s = snow_amount <= 0.0 ? 0.0 : smoothstep(limit - edge, limit + edge, up);
ALBEDO = mix(base_color.rgb, snow_color.rgb, s);
}
"""
static func make(base := Color(0.4, 0.3, 0.25), amount := 0.5) -> ShaderMaterial:
var sh := Shader.new()
sh.code = CODE
var m := ShaderMaterial.new()
m.shader = sh
m.set_shader_parameter("base_color", base)
m.set_shader_parameter("snow_amount", amount)
return m
What we measured
A flat roof and a wall side by side, lit by one directional light. On the rendered screen we counted which of their pixels were snow coloured, with the camera a little above, looking down steeply, and level with the roof. Two runs gave the same numbers.
- NORMAL.y in fragment(): camera a little above, roof 100%, wall 0%. Looking down steeply, roof 0%, wall 0%. Level with the roof, roof 100%.
- The script: roof 100% and wall 0% from all three.
- The script with snow_amount 0.5 on a roof tilted 40 degrees: 100% snow. Tilted 70 degrees: 0%.
Why it happens
NORMAL in fragment() is turned with the camera, so that x is right and y is up on the screen. When the camera looks straight down at a roof, the roof faces the camera, and its normal points out of the screen with almost nothing left in y. In vertex() the normal is still the mesh's own, and MODEL_NORMAL_MATRIX turns it into the world's, where y is the sky whatever the camera does. A varying passes it on to fragment().
What to change
- snow_amount: at 0.5, slopes up to about 60 degrees from flat get snow, in our test 40 did and 70 did not. Higher gives snow on steeper slopes.
- The edge value softens the line between snow and roof.
- The same space mix-up the other way round: a MatCap needs the view normal. Snow that piles up as particles: snow that settles on roofs.
Winter effects to go with it: Winter & Christmas VFX for Godot 4 has Christmas lights that build themselves along any Path3D, gifts that burst open, snow globes you can shake, aurora skies and frost on the screen.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Winter & Christmas VFX for Godot 4 has Christmas lights that build themselves along any Path3D, gifts that burst open, snow globes you can shake, aurora skies and frost on the screen.
More how-tos for Winter & Christmas VFX for Godot 4: Snow particles not falling · Snow that stays on roofs · Emit particles along a Path3D · Space lights, posts or coins evenly along a Path3D
More how-tos: Shaders, materials and lights
- A 3D outline that stays the same width on screen
- Find and fix the seam in a repeating texture
- PS1 vertex wobble that is the same near and far
- Packed ORM maps on a StandardMaterial3D
- Neon signs that share a material but not a colour
- A MatCap that stays the right way up from every side
- Scroll or tile a 2D texture without stretched edge lines
- Keep floors, roads and roofs sharp when the camera looks along them
- Make WorldEnvironment color grading apply to a 2D game
- Get a hard cel-shaded light edge from StandardMaterial3D's Toon mode
- Make metallic materials stop looking black or flat
- Make a glowing sign or screen actually light the wall around it
- Use a LUT strip for color grading in Godot 4
- Tiling textures that keep their size on any mesh
- A sharp low-resolution PS1 look
- Only 8 lights show on a long wall in Compatibility
- Hit flash shader for 2D sprites
- Flash a 3D enemy white when it takes a hit
- Dissolve shader for 3D models
- 2D outline shader that is not cut off
- 3D outline shader without gaps at the corners
- A force field shield with a hit ripple
- Normal map inverted: DirectX vs OpenGL
- Full-screen post-process shader in 3D
- Scroll a shader texture for hours without the once-an-hour jump