aerialPerspective.renderFeature
This package holds more than this page lists. See all of its files in its world.
README.md
aerialPerspective
The render feature behind PhysicalAtmosphere and
@builtin::systems.atmosphere.atmosphere. A physical sky and distance haze in
a scene come from adding the PhysicalAtmosphere component to an entity. Its passes run at two seams of every
view the frame draws.
Right after the sky pass (afterSky), before any geometry:
| order | pass | output |
|---|---|---|
| 0 | atmo_transmittance | 256x64 transmittance from a point out to the top of the atmosphere, in the Bruneton (r, mu) parameterisation |
| 1 | atmo_multiscatter | 32x32 multiple-scattering transfer function, integrated over the sphere of directions |
| 2 | atmo_skyview | 192x108 sky radiance per view direction from the camera's altitude, horizon-concentrated |
| 3 | atmo_aerial | the aerial perspective volume: the light the air scatters toward the camera and its optical depth, out to each of 32 depth boundaries along 32x64 view directions |
| 4 | atmo_sky | with sky on: the sky and the solar disc, where the depth pre-pass left sky |
In the medium stage, after every lighting term and ahead of the fog:
| local order | pass | output |
|---|---|---|
| 6 | atmo_composite | scene depth in, aerial perspective on every surface out, read from the volume |
The sky goes in under everything the frame draws over it. The cloud layer
composites at the same seam after it, then the geometry pass, the transparents
and the particles draw in front of it, and the aerial perspective later in the
view hazes the surfaces and leaves the sky pixels, and whatever was drawn over
them, as they stand. The cloud layer's clouds are seen through the same air:
its raymarch reads the volume (atmosphere.liveTables().aerial) at each
cloud's own distance, as the composite reads it at each surface's.
The sky pass samples the sky-view LUT and adds the solar disc, attenuated by
the transmittance between it and the camera, which is what warms a low sun.
The disc's brightest channel is held to a fixed ceiling above the sky's, so it
stays the brightest thing in the frame at every exposure and the display
curve keeps its colour.
The volume holds the air in front of the camera being drawn. The air seen along a direction depends on the camera's altitude, the direction's zenith angle, its azimuth from the sun and what stands in the scene between that air and the sun, so the volume's directions are those two angles: 64 azimuths all the way around, concentrated toward the sun's heading as the sky-view LUT's are, by 64 zenith angles concentrated at the horizon, where the air a ray crosses changes fastest. Each direction is cut at 32 depth boundaries over the air that direction crosses: from where the air begins (the camera, or for a camera above the air where the ray enters it) to where the ray leaves the air or meets the ground. The boundaries are spaced tightly near the camera and widen with distance, and on a ray that ends on the ground they tighten again toward it, where the air is densest; seen from orbit that end holds nearly all of the haze. A read filters between neighbouring directions at the same share of each one's path, and only between directions on the same side of the horizon. Each slice is marched by its own invocation and the slices of a direction are summed from the camera outward, so the whole volume costs one march of each slice.
Every step of the march is lit as the scene lets light reach its point of the
air (atmo_scene_light): the sun's single scattering by how much of the sun
reaches it past the occluders that shadow the surfaces (the sun's shadow map,
@scene.sunShadow, zero::sun_shadow), the sky's multiple scattering by how
much of the sky it sees (the sky-visibility field, zero::sky_visibility).
The air of a sealed room scatters no light, the air under a roof only what
reaches it past the roof's edge, and a gap in a wall throws a lit shaft
through the haze behind it. A frame reading the sun's shadow map keeps the
sun's pages under the air its view looks through. The sky-visibility field
is the one the scene's sky keeps (@builtin::systems.skyVisibility); a scene
whose sky keeps none, and air the field does not reach, light the multiple
scattering as the open sky does.
The composite reconstructs each pixel's world position from @scene.depth and
the drawn camera's inverse view-projection (@frame.camera), and reads the
air in front of the surface through atmo_aerial_lookup: the volume's light
and optical depth out to the last boundary in front of the surface, and the
stretch from there to the surface marched along the pixel's own ray in four
steps on top of them, each lit by what its point of the scene sees of the sun
and the sky, so a shaft in the air just in front of a surface is the pixel's
own. It applies surface * transmittance + in-scattering.
Because the camera comes from @frame.camera, the atmosphere is correct in
offscreen captures and render-to-texture cameras, not only the live viewport.
The sky-view LUT derives its own altitude and sun elevation from that same
camera, so a capture from a different viewpoint gets its own sky rather than
the live viewport's.
Sky and geometry come out of one integral, so they agree at the horizon by construction: as a surface recedes its transmittance goes to zero and its in-scattered term goes to exactly the sky radiance for that direction.
Scattering coefficients are Earth's, in kilometres and inverse kilometres.
worldUnitsPerKm maps the scene onto them, so the same physics serves a
metre-scale scene and a kilometre-scale one.
Costs
The transmittance and multiple-scattering LUTs follow from the medium alone
(the Rayleigh, Mie and ozone strengths, and for the multiple scattering the
ground albedo), so their passes state the version of those inputs
(atmosphere.tableVersions()) and run only in a view where the table does not
already hold that version's picture: a table is built in the first view after
its inputs change and costs nothing on the frames between. A moving sun, a
moving camera and a change of exposure, sky intensity or aerial strength build
neither. The sky-view LUT follows the medium, the sun and the position of the
camera being drawn: its pass states the version of the first two
(tableVersions().skyview) and names the camera's position as what it reads of
the view (viewReads = { "@frame.camera.position" }), so it is built in a view
drawn from where the table was not built, or after the sun or the medium moved,
and costs nothing in a frame where the camera and the sun stand still; a
change of exposure, sky intensity or aerial strength, which the passes drawing
with it apply, builds it neither. A capture or a render-to-texture camera
standing elsewhere builds it for its own camera, and the next view drawn from
the viewport builds it again for that one.
The march runs over the volume, whose size is fixed, so its cost is the same
at every output resolution and whatever share of the screen is geometry;
aerialSteps sets how finely each slice is marched (one step for every 8 of
it, rounded up). The composite reads the volume once per pixel and marches the
stretch past its last boundary in a fixed two steps, so its cost follows the
pixel count alone and aerialSteps does not move it; the cloud layer's read of
the same air costs the same whatever aerialSteps is set to, for the same
reason.
Colour
exposure is what maps the sky's radiance into the displayable range — raise
it for a brighter sky, and the aerial perspective brightens with it, since both
come from the same integral.
Parameters arrive through the atmo_params buffer, packed by
@builtin::systems.atmosphere.atmosphere.