A while ago I shared how one clip shader builds all the buildings in my game, and the comments were great, so here's another one from the same project: cloud shadows. (Clip: Venice at 5× game speed, UI hidden.)
The first version was wrong in a way that took me a while to see
It was a fullscreen pass that darkened the final image wherever a cloud was overhead. Fine in a screenshot, but it darkens *everything*: building shadows under a cloud went double-dark, the sea kept its sun glint (just greyer), and the sky light dropped too. A real cloud only blocks the direct sun.
The fix: let the sun carry the shadow
Render the clouds as seen from the sun into a small texture and assign it as the directional light's cookie. URP multiplies the cookie into the main light only, so under a cloud the direct light goes away (and with it the hard shadows and the glint) while ambient stays. Which is what happens outside.
1. The texture
512×512 R8_UNorm RenderTexture covering 2048 m in light space, so 4 m per texel. A fullscree triangle fills it every frame from a CommandBuffer.
2. One texel = one ray
Each texel is a point on the light plane. Map it to world space, slide it along the sun ray down to the ground, then march the cloud volume toward the sun:
float2 ls = (uv - 0.5) * _Rect.zw + _Rect.xy; // same mapping URP uses
float3 p = mul(_LightToWorld, float4(ls, 0, 1)).xyz;
p -= sunDir * (p.y / max(sunDir.y, 0.05)); // drop to y = 0 along the ray
float lit = exp(-CloudShadowDepth(p, camPos, sunDir));
The cookie is projected along the light direction, so one value is right for the whole ray: a roof and the street below it get the same shadow.
3. Hooking it to URP
sun.cookie = rt;
var data = sun.GetUniversalAdditionalLightData();
data.lightCookieSize = new Vector2(2048, 2048);
data.lightCookieOffset = snappedCenter;
URP looks it up as uv = (light-space position - offset) / size + 0.5. Your shader has to use exactly the same mapping, or the shadow lands shifted or mirrored.
4. Snap the offset to whole texels
The square follows the camera, but in steps of one texel (Mathf.Round(x / texel) * texel). Move it smoothly and every cloud edge shimmers while you pan.
5. Fade to lit at the border
Past the square, URP stretches the edge texel to the horizon. I fade the cookie to 1 over the last ~8% on each side: saturate((0.5 - abs(uv - 0.5)) * 12).
6. Custom shaders need one line
URP/Lit picks the cookie up on its own (30k+ renderers in one city scene, zero changes). My own shaders, water included, needed
#pragma multi_compile_fragment _ _LIGHT_COOKIES
GetMainLight(shadowCoord, positionWS, shadowMask) then applies it for you; where I build the light by
hand it's mainLight.color *= SampleMainLightCookie(positionWS). The sun glint on the water now
switches off under a cloud by itself.
Gotchas
- My helper GameObjects used HideFlags.HideAndDontSave and survived exiting Play mode: after four Plays I had four copies, each holding live RenderTextures. HideInHierarchy plus DestroyImmediate in OnDestroy fixed it.
- I kept the old overlay for edit mode (the cookie only runs in Play, and editor captures still need clouds). A static flag turns the overlay off whenever the cookie is active, otherwise you get both and the shadows are twice as dark.
- Cost: 512² is about 262k samples a frame, half of what the old half-resolution pass did at 1080p. I haven't profiled it in milliseconds yet.
It's from FONDACO, a merchant strategy game set in the Mediterranean of 1400 that I'm making solo.
One thing I haven't solved: with a low sun I clamp sunDir.y at 0.05 and the shadows get very long near sunset. How do you handle cloud shadows when the sun is low?