volumetricLighting.module
This package holds more than this page lists. See all of its files in its world.
README.md
volumetricLighting
The settings behind the VolumetricLighting component, and the way to drive the
scene's participating medium from a script — fog that rolls in over a cutscene, a
quality slider that drops the step count, a room that fills with smoke.
local volumetricLighting = require("@builtin::systems.volumetrics.volumetricLighting")
volumetricLighting.set({ density = 0.06, anisotropy = 0.7 })
volumetricLighting.get() -- a copy of the settings in force
volumetricLighting.active() -- whether the march is running
volumetricLighting.clear() -- empty the air, keep the settings
set takes any subset of the fields; whatever is omitted keeps its current
value. A density of 0 releases the render feature rather than marching a medium
that scatters nothing, and clear is the named way to say the same thing.
The medium is lit by the scene's sun, its lights and its surroundings, read by
the passes every frame: the sun's direction, colour and intensity, every light
in the scene, and the light a surface standing in the air takes from the scene's
ambient term, the sky and reflection probes the environment captured and the
baked light-probe volumes (@builtin::shaderModules.ambient_light, the light a
ParticipatingMedium takes too). The sun lights the air as it lights the
surfaces, so a setting sun dims and reddens the haze in the frame the surfaces
darken; the sky lights it as it lights them, as far as each point of the air
sees the sky past the static geometry and past every surface standing over it
(a tree's crown or a roof, movable and instanced ones included), so haze stands
in for the sky it hides by day, haze under a canopy takes the sky the canopy
leaves open, a mist reads as dim haze against a night sky, and a longer
maxDistance changes how far the haze reaches. skyLight scales how much of
its surroundings' light the medium takes: 1, the default, lights it as the
surroundings light a surface standing in it. With the sun, every light, the
ambient term and the sky dark the medium scatters nothing. color is what the medium
scatters of all of that light. sunIntensity and sunColor are a scale and a
tint on the scene's sun: at 1 and white the medium takes the sun as the surfaces
do.
sunDirection follows the scene's sun. Pass one to set when a scene wants the
medium lit along a direction of its own; a set without it goes back to the
scene's sun.
The module packs the settings into the vol_params buffer the passes read, so a
change lands on the next frame without recompiling anything.
Cost
The march is the expensive part, and its cost is steps × (shadowSteps + the
number of live scene lights) per marched pixel. Those are the two knobs to reach
for on a budget: steps controls how cleanly a beam's edge resolves,
and shadowSteps controls whether the sun's contribution is shadowed at all.
Setting shadowSteps to 0 removes the per-sample visibility work entirely, which
is the single largest saving available — at the cost of the shafts, since the
medium is then lit evenly wherever it reaches.
quality scales both, and the grid the march runs on, by level: high
marches steps and shadowSteps on a grid of half the frame's resolution on
each axis, medium 60 % and 67 % of them on a third, low half of each on a
quarter (volumetricLighting.LEVELS). Under auto (the default) the level is
the one the device's quality tier draws the effect at, read again each frame, so
renderer.setQualityTier moves it; drawn() reads the level in force and what
the march draws at it.