Advanced shaders · lesson 13 of 19 · Advanced · about 5 min
Snow 3: sparkle
You will build: Add flickering ice glints to the snow: a very fine random grid, a few cells flash, each with its own phase, only in sunlit snow.
The last ingredient is the one you notice when you walk in real snow: glints. Tiny ice crystals catch the sun at just the right angle and flash for a moment.
We cannot simulate every crystal, but we can fake their look. Chop the ground into a very fine grid (floor(pos.xz * 110.0) gives every little cell a number), ask the hash21 for a random value of the cell, and let only the luckiest 0.4% of cells sparkle: step(0.996, g).
If the glints stood still they would look like white dust. Give each its own flicker: 0.5 + 0.5 * sin(time * 6.0 + g * 40.0). The random number g works as a phase, so the cells blink out of step.
Finally, ice crystals only flash where the sun reaches, so multiply by how much the point is lit: saturate(diff * 2.0 + 0.2). Sparkle in shadow would look wrong.
float g = hash21(floor(pos.xz * 110.0)); float glint = step(0.996, g) * (0.5 + 0.5 * sin(time * 6.0 + g * 40.0)); col += float3(1.0, 1.0, 1.0) * glint * saturate(diff * 2.0 + 0.2);
Add this just after the snow color, before the fog (so distant sparkles fade away, too).
The numbers you need
Everything the picture depends on is given, so the challenge is the code, not guessing.
- Cell
g = hash21(floor(pos.xz * 110.0))- Glint
step(0.996, g) * (0.5 + 0.5 * sin(time * 6.0 + g * 40.0))- Added to the color
col += float3(1.0, 1.0, 1.0) * glint * saturate(diff * 2.0 + 0.2)- Order
snow color, then the glint, then the fog- Everything else
as in Snow 2
Try it in your own language
The lesson is written once and shown in every language. Pick yours: Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.
Start coding