Log inGet started
Package

mediaTransmittance.module

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

mediaTransmittance

Participating media (fog, smoke, dust, steam) as the camera and the scene's sun see them. A ray crossing d units of a medium of density k keeps exp(-k * d) of its light, and every view of the frame reads the registered media two ways:

  • Toward the sun, from every surface. The share of the scene's sun the media take on the way to the surface is written to the scene's sun occlusion channel, and the sun's term reads it. The medium dims the sun alone: the sky, the ambient term and the other lights still reach the surface, so the ground under a thick bank reads like the ground in an opaque blocker's shadow. A surface standing inside a medium is dimmed by the part between it and the sun.
  • Along each view ray. What is behind a medium is dimmed by the media in front of it, and the media add the light they scatter toward the camera from the scene's sun (itself dimmed by the media between each point and the sun) and from their surroundings: the light a surface standing there takes from the ambient term, the sky and reflection probes the environment captured, and the baked light-probe volumes, averaged over every direction.

The sun is the one the scene is lit by, read in the same frame the surfaces read it: a medium follows a day cycle and a scripted sun, and a scene with the sun, the ambient and the environment dark leaves it black. Optical depth is additive, so overlapping media stack. A view ray samples the stretch of medium it crosses steps times, so a thin medium is sampled as finely as a thick one, and the optical depth toward the sun is read in one intersection per medium (media_common).

Direct use

local Media = require("@builtin::systems.volumetrics.mediaTransmittance")

Media.set({ steps = 32 })
Media.setMedium("bank", {
    position = { 0, 8, 0 },
    size = { 20, 6, 20 },
    density = 0.15,
})

Most scenes attach ParticipatingMedium to an entity instead and let it drive this module.

API

CallEffect
set(opts)Scene-wide settings; omitted fields keep their value, and a field that is not a setting is refused by name
setMedium(key, opts)Register or move a medium under a stable key
removeMedium(key)Drop one medium
get()A copy of the settings in force
clear()Drop everything and release the passes
active()Whether the passes are running
buffers()The two buffers the passes read

Settings

FieldMeaning
strengthHow much of the media's effect is applied, [0, 1]
stepsSamples a view ray takes across each stretch of medium it crosses, [4, 128] (default 24)
qualityHow finely the view rays sample: auto (the default) follows the device's quality tier, high takes steps, medium 67 % of them (16 of 24) and low 50 % (12). drawn() reads the level in force and the steps it takes.

Media

FieldMeaning
positionWorld-space centre
shape"box" (default) or "sphere"
sizeHalf-extents, for a box
radiusRadius, for a sphere
densityExtinction per world unit

Up to 32 media are packed into the records buffer. The outer tenth of each shape is feathered so a bank does not end at a visible face.

Cost

A medium costs in proportion to the pixels it covers. Every pass works per pixel of the view being drawn, and only a view ray that crosses a medium samples: steps samples across each stretch of medium in front of its surface, each reading the density of the media that ray crosses and the optical depth toward the sun through the media the sun's light can cross on its way to that stretch, one intersection per medium. The share of the sun a surface keeps is one intersection per medium as well. The light of the surroundings is stated once per render at the nodes of the surroundings grid (surroundings_grid, 16384 nodes) and read at each stretch for eight loads. While any medium is registered the passes cost about 0.4 ms a frame on a desktop GPU at 2097x1409 before a medium covers a pixel; a medium filling the whole view adds about 1.5 ms more there, at the default 24 steps (systems/volumetrics/documentation/README has the figures). With nothing registered the passes are released entirely, so a scene with no media pays nothing.

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. The graphics-quality guide lists what each level costs.