medium_light.shaderModule
- README.md6.1 KB
- module.wgsl1.7 KB
This package holds more than this page lists. See all of its files in its world.
README.md
medium_light
What a medium's light response is handed, and the functions a response
module defines. The cloud layer (VolumetricSky), a Volume draw (Volume,
VolumeSequence, Smoke, Fire) and the participating media
(ParticipatingMedium) each take the response their component names in
lightResponse: a .shaderModule that includes this module and defines
medium_shape(p: MediumPoint) -> f32: the density the march takes at a point.p.densitykeeps the medium's own shape; a step turns a soft edge into a hard silhouette.medium_light(s: MediumSample) -> vec3<f32>: the light one unit of coverage at a lit sample sends toward the camera.s.physicalkeeps the engine's light.medium_flame(s: MediumSample) -> vec3<f32>: the light a burning sample emits per world unit.s.flamekeeps the engine's blackbody flame.
The march accumulates what the response returns front to back, so a sample nearer the camera covers the ones behind it as far as its density does.
medium_shape thins, hardens or thickens the medium where it stands, and
answers 0 where p.density is 0: the marchers step over the empty space
around a medium without asking it, so density answered there shows only at
the samples that land in it. The density it answers is what the view's march
takes, and what the march toward the sun takes inside the clouds and a
volume, so their self-shadow follows the shape.
The shadows the medium casts beyond itself are cast by its own density: the
clouds' shadow on the ground and in each view's shadow map, and a fog bank's
shadow, on itself and on the surfaces under it.
What a sample carries
MediumSample: physical (the engine's own light), sun (the sun's light
arriving at the medium) with sun_kept (the share the medium toward the sun
lets through, 1 on the lit edge) and phase, surroundings (sky, ambient and
surroundings), lamps (the scene's local lights, for a Volume), color
(the medium's own colour), density and extinction (that density times the
medium's absorption, per world unit), height (0 at the bottom of the layer or
box, 1 at its top), heat (a flame's temperature as a share of white-hot) and
flame, view and toward_sun, kind (MEDIUM_CLOUD, MEDIUM_VOLUME,
MEDIUM_FOG) and params.
MediumPoint: density, height, heat, kind and params.
params (MediumParams, four vec4s a, b, c, d) holds the sixteen
numbers the component states in lightResponseParams, in order; a number it
leaves out reads 0. A palette, band thresholds and rim colours travel there,
so one response module draws many looks.
medium_luma(c) is the luminance of a light.
s.sun carries the phase, so a sample seen toward the sun is brighter than
one seen away from it; s.sun * (0.0795775 / s.phase) is the sun's light with
the phase taken out (as even scattering sends it), the same from every view,
for a lit tone that stays flat as the camera turns.
These values are stated in shaders.medium_types, which this module
includes. This module also states MEDIUM_AUTHORED true: a marcher hands its
samples to a response's functions only in a program drawn with one.
A two-tone response
#include "@builtin::systems.volumetrics.shaders.medium_light"
fn medium_shape(p: MediumPoint) -> f32 {
// A hard silhouette: thin medium is gone, the rest is solid.
return select(0.0, max(p.density, 0.05) * p.params.c.x, p.density > p.params.c.y);
}
fn medium_light(s: MediumSample) -> vec3<f32> {
// The lit tone where the sample keeps more of the sun than a.w, the shade
// tone elsewhere, at the brightness of the light reaching it.
let level = medium_luma(s.sun + s.surroundings + s.lamps);
return mix(s.params.b.rgb, s.params.a.rgb, step(s.params.a.w, s.sun_kept)) * level;
}
fn medium_flame(s: MediumSample) -> vec3<f32> {
return s.flame;
}
A component names it by identity in lightResponse, with
lightResponseParams = { 1, 0.97, 0.9, 0.5, 0.45, 0.5, 0.7, 0, 300, 0.08, 0, 0 }
(the lit tone and its edge, the shade tone, the silhouette's gain and
threshold).
A hard silhouette and a flat flame
The march adds a sample's light over the coverage the sample adds, so an edge
reads hard only where one step covers everything behind it: where
s.extinction times the step reaches well past 1. A gain too small for that
leaves stair-step stripes inside the silhouette, each step only partly
covering the one behind; the clouds need a gain in the tens, smoke and fire in
the hundreds.
medium_flame is emission per world unit the ray crosses, which the march
integrates over each step through the medium the step crosses, so an opaque
flame shows its emission over its extinction. A flame band returns its colour
times s.extinction to show as that colour wherever the flame is opaque, at
any gain and any step:
fn medium_flame(s: MediumSample) -> vec3<f32> {
if (s.heat <= s.params.d.y) { return vec3<f32>(0.0); }
var band = vec3<f32>(0.9, 0.2, 0.05);
if (s.heat >= s.params.c.z) { band = vec3<f32>(1.0, 0.55, 0.1); }
if (s.heat >= s.params.c.w) { band = vec3<f32>(1.0, 0.9, 0.5); }
return band * s.params.d.x * s.extinction;
}
The frame's tone mapping takes the flame like any other light, so a band brighter than about 1 reads white.
A burning sample is lit as well as burning: the march adds what
medium_light returns for the light it scatters and what medium_flame
returns for its flame. A response that draws a flame in its bands alone
returns black from medium_light where s.heat says the sample burns, so a
smoke tone does not wash over the flame; and a silhouette cut in
medium_shape that thins smoke away thins a flame's base too unless it keeps
a burning point (p.heat above where the flame starts) solid.
A hard edge shows the resolution a medium is drawn at: at the low and
medium quality levels a Smoke or Fire draws over half the view's width
and height, so a silhouette there steps at that size; high draws it at the
view's own.