← Pixel Dreamer Studio

Snow that stays on the roofs

Work out how much a surface faces up from its world normal, made in vertex(), not from NORMAL in fragment().

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.

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

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