VolumetricLighting.component
This package holds more than this page lists. See all of its files in its world.
README.md
VolumetricLighting
Light shafts, god rays and glowing air, as a component. It is the air between the camera and the scene: add it to an entity and light travelling through that air scatters toward the viewer, so a lamp becomes a glow rather than an invisible point and the sun cuts shafts where something crosses it, through the trees of a forest, a window or a gap in a wall.
local id = entity.spawn("air").id
entity(id).component.add("VolumetricLighting", {
density = 0.06,
anisotropy = 0.7,
})
Every field is live — editing one in the inspector changes the air on the next
frame, and the values persist with the scene. Setting density to 0 empties the
air and releases the passes while keeping the rest authored; removing the
last one in the scene clears it.
| field | effect |
|---|---|
density | How much medium there is per world unit at the reference altitude. 0 is off. |
heightFalloff / heightRef | How fast density thins with altitude, and the altitude density is quoted at. 0 falloff fills the scene evenly. |
steps | Samples along each view ray. More resolves a beam's edge more cleanly. |
colorR/G/B | What the medium scatters. Warm dust and cold mist differ here. |
anisotropy | How forward-biased the scattering is, in (-1, 1). Positive values throw light onward, so haze blooms around a source you look toward. |
sunIntensity / sunColorR/G/B | A scale and a tint on the scene's sun as it lights the medium: at 1 and white the medium takes the sun as the surfaces do, so a setting sun dims the haze and a sun switched off lights none of it. |
lightScale | The scene lights' contribution to the medium. |
skyLight | How much of its surroundings' light the medium takes: the sky, the ambient term and the probes. 1 (the default) lights it as the surroundings light a surface standing in it; 0 leaves the sun and the lights alone. |
shadowSteps / shadowDistance | Samples taken back toward the sun to decide whether a point in the air is lit, and how far they reach. 0 leaves the sun's contribution unshadowed, so the haze glows evenly and no shafts form. |
maxDistance | How far along the view ray the march runs. A ray that escapes to sky stops here. |
quality | How much the march draws: auto (the default) follows the device's quality tier, high marches steps and shadowSteps as stated, medium and low a coarser grid with a share of them (below). |
The march runs on a grid coarser than the frame and is upsampled over it with a depth-aware filter. The quality level sets that grid and the share of the stated samples each ray takes:
| level | grid (each axis of the frame) | steps | shadowSteps |
|---|---|---|---|
high | 1/2 | all (40 by default) | all (6) |
medium | 1/3 | 60 % (24) | 67 % (4) |
low | 1/4 | 50 % (20) | 50 % (3) |
Under auto a desktop's or a browser's high tier draws high, a handheld's
high tier and every medium tier medium, and the low tier low
(renderer.qualityTier(), @builtin::modules.qualityLevels), and
renderer.setQualityTier moves it on the next frame.
volumetricLighting.drawn() reads the level in force and what the march
draws at it; the graphics-quality guide lists what each level costs.
The lights are the scene's own, each scattering on the terms its kind emits on:
a point fills the air around it, a spot fills its cone, a rect reaches the air on
the side its face emits along, and a parallel light lays an even glow across the
whole medium. One added, moved, re-aimed or recoloured at runtime changes the
glow it throws with nothing to keep in step here. The sun is the scene's sun,
lighting the air as it lights the surfaces, so haze under a daylight sun is as
bright as that sun makes it and a setting sun dims it. The air also takes the
light of its surroundings, the light a surface standing in it takes from the
scene's ambient term, the sky and reflection probes the environment captured and
the baked light-probe volumes. The ambient term and the sky reach the air as
far as it 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
in front of the sky stands in for the sky it hides, by day and by night, and the
air under a canopy takes the share of the sky the canopy leaves open; the probes
light it as they light a surface there. skyLight scales what it takes. With
the sun, every light, the ambient term and the sky dark it scatters nothing.
One body of air fills the scene, so one of these per scene is enough — the last one to push wins.
The work runs in lightScattering.renderFeature; the settings live in
@builtin::systems.volumetrics.volumetricLighting, which is also how to drive
them from a script.