lightScattering.renderFeature
This package holds more than this page lists. See all of its files in its world.
README.md
lightScattering
The render passes behind the VolumetricLighting component and the
volumetricLighting module. Light shafts and glowing air in a scene come from
adding that component to an entity; this feature runs while one stands.
Three compute passes in the medium stage, after the fog.
vol_scatter marches each view ray at half resolution, from the camera to
the surface the ray hits, or to the medium's reach when it escapes to sky, since
an unbounded march would integrate the whole horizon into every sky pixel. At
each step it evaluates the medium's density and accumulates the fraction of the
sun's light sent toward the camera, attenuating by everything already crossed.
The medium's surroundings light it too, as they light a surface standing in it:
the scene's ambient term, the sky and reflection probes the environment captured
and the baked light-probe volumes (@scene.environment,
@scene.environmentSampler, @scene.reflectionProbes), averaged over every
direction through @builtin::shaderModules.ambient_light. They change over the
room, the canopy or the sky rather than over a step, so each ray reads them once
per stretch of four, where the medium that stretch holds stands. The sky reaches
that point as far as the point sees it, past the static geometry (the
sky-visibility field) and past every surface standing over it, movable and
instanced ones included: the feature draws the overhead cover map
(@builtin::systems.skyVisibility.overheadCover) at afterLighting, ahead of
the march, and the march reads it as overhead_map. The air under a canopy or a
roof so takes the share of the sky it sees, and the medium's skyLight scales
what it takes. Haze in front of the sky stands in for the sky it hides, by day
and by night, and with the sun, every light and the surroundings dark the
medium scatters nothing.
A surface-only renderer draws the pool of light a lamp throws on the floor but not the beam that carried it there, because nothing between the lamp and the floor is ever shaded. Marching the ray shades that space.
The sun's contribution is gated by a short march back toward it, tested against the depth buffer. That is what turns uniform haze into shafts: the medium is lit only where the path back to the sun is clear, so an occluder carves a dark volume out of the beam rather than only shading the surface behind it. Because the test reads the depth buffer, an occluder outside the view frustum does not cast into the medium.
vol_lights marches the same rays for the scene's lights that stand
somewhere. The lights are the scene's own. @scene.lights is the same buffer
the lit passes read, so a light added, moved or recoloured at runtime changes the
beam it throws with no bookkeeping here. A spot's cone gate is evaluated at the
sample itself rather than at a surface, which is what makes the cone visible as a
volume instead of only where it lands, and a rect lights the sample from the part
of its face nearest to it. Attenuation matches the lit passes' windowed falloff,
so a beam and the pool of light it lands in agree about how far the source
reaches, and the pass spends its work only where that is: a tile of rays marches
with the lights whose reach meets it, a ray with the ones whose reach it crosses,
over the stretch of the ray they reach. A light whose radius reaches no ray costs
the march nothing.
Scattering follows Henyey-Greenstein, so the medium brightens toward a source you are looking at and stays dim when it is behind you: the difference between haze that reads as air and haze that reads as a grey wash.
Each ray's first sample is offset by a per-pixel fraction of a step, trading the banding a fixed step pattern produces for noise.
Consecutive renders of a view share each ray's samples. Each render marches every other sample of a ray, and lays its half beside the half the view's previous render marched, taken from where the end of the ray stood in that render, so a camera holding still sees every sample of the ray for the price of half. The halves are held in history targets, one pair of images per view, beside the length of each ray: a ray whose previous render ended at another distance (an object moved across it), one that falls off the previous picture, and every ray of a view with no previous render (its first, its first after a cut, a one-shot capture) march all of their samples.
vol_composite upsamples both and composites them. The taps are weighted by
how closely their depth matches the full-resolution pixel's, which keeps a beam
from bleeding across a silhouette: the halo a plain bilinear upsample leaves
behind.
The camera comes from @frame.camera, the camera of the render target being
drawn, its inverse view-projection to unproject a pixel and its view-projection to
project a point back, so an offscreen capture or an RTT camera marches through
its own matrices at its own resolution.
Parameters arrive through the vol_params buffer, packed by the
volumetricLighting module.