Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commit43c995dce8

README.md

cloudShadow

The shadow the cloud layer (VolumetricSky) casts on everything the sun lights under it. A cloud is density rather than a surface, so the shadow is the share of the sun's light that crosses the cloud between each point and the sun (exp(-tau) of the cloud's optical depth tau, scaled by the sky's shadowStrength).

The map holds one line toward the sun per texel, packed most closely around the camera and still reaching as far as the layer is drawn (cloud_shadow_space). Each view the frame draws (the viewport, a camera rendering into a texture, a capture) keeps its own map from one render to the next, and a render marches only a share of it: the map is split into blocks of 4 x 4 texels, each render marches one line of every block, and the rest is carried over from the view's previous render. A whole map is marched again every 16 renders of a view, whatever cadence the view is drawn at, while each render pays a sixteenth of a full march. A moving camera, a turning sun and the wind carrying the clouds move the carried lines with them; what the previous map cannot answer for (a view's first render, a camera that jumped, a change of the clouds' form, the sun turning more than a quarter of a degree in one render, a view drawn again after the wind carried its clouds far through their noise, a wind sinking the clouds through their noise faster than a carried line can stand) is marched in the render that needs it.

Each frame the cloud layer draws, enqueue runs these passes for every view, at the afterDepthPrepass seam:

  1. cloud_shadow_recall: what the view's previous map was written for (its camera, its sun, the form of the clouds), read out of its record, and the step of the refresh this render takes.
  2. cloud_shadow_carry: the view's previous map carried into this render, marching every line it cannot carry.
  3. cloud_shadow_map: one line of every block marched anew across the layer through the same clouds the raymarcher draws (cloud_density, through cloud_shadow_march), recording the share of the sun crossing the cloud above the layer's base and above a quarter, a half and three quarters of the way up.
  4. cloud_shadow_filter: the map its readers sample, the lines drawn smooth between them (a cubic B-spline) at twice their density, so a cloud's shadow fades over a line or so at its edge rather than following the map's grid.
  5. cloud_shadow_apply: the share of the sun the clouds take from each surface in view.
  6. A composite laying that share into the scene's sun occlusion channel (@scene.sunOcclusion) with the union blend, so it stands with a participating medium's and a contact shadow's share as a + b - ab: the two transmittances multiply, and neither is taken twice. The sun's term reads the channel, so the clouds dim the sun's diffuse and specular light alone and the sky, the ambient term and the other lights still reach the surface.

marched() reads back how many lines the passes have marched and how many renders have carried the map; their ratio is the lines a render marches.

The air reads the same map. A pass marching it merges what bindings() names into its own inputs and buffers, includes @builtin::systems.volumetrics.shaders.cloud_shadow and multiplies the sun's light at each of its samples by cloud_shadow_transmittance(p, cam, to_sun). The height fog, the volumetric light (lightScattering), the participating media's view march (mediaShadow) and the march of every standing volume (volume_draw: Volume, Smoke, Fire) do, so the air and the smoke in a cloud's shadow are dark and the sun falls through the gaps.

With no cloud layer drawing, nothing is enqueued, the map's parameters say no shadow is cast, and cloud_shadow_transmittance answers 1 without reading the map.

Quality levels

levelmap texelssamples per linelines a still view marches per render
high384 x 384169216
medium256 x 256124096
low192 x 19282304

The level is the cloud layer's own (VolumetricSky's quality).

Direct use

local CloudShadow = require("@builtin::systems.volumetrics.cloudShadow")
local shadow = CloudShadow.bindings()
ctx.enqueue {
    kind = "compute", program = myAirMarch,
    inputs = { frame_cam = "@frame.camera", cloud_shadow_map = shadow.inputs.cloud_shadow_map },
    buffers = { cloud_shadow = shadow.buffers.cloud_shadow },
}
print(CloudShadow.state().casting)
local m = CloudShadow.marched()
print(m.lines, m.renders)