Builtin Library
- ..
- README.md3.2 KB
- module.wgsl4.1 KB
README.md
media_trace
Reads and writes the media trace: what the participating media and the
see-through draws laid over each pixel, in front of the opaque surface
@scene.depth holds. A full-frame pass (an outline, a toon ramp, a style
block) tells a cloud from the sky behind it, smoke from the wall behind it and
a water surface from the lake bed under it through it.
| Channel | .r | .g | .b | .a |
|---|---|---|---|---|
@scene.media | cloud layer share | volumetric medium share (volume, smoke, fire, fog bank) | see-through surface share (blended draw, fluid surface) | coverage |
@scene.mediaSurface | distance from the camera | world normal on the octahedron, x | y | coverage |
Every value but .a is premultiplied by the coverage. Each producer blends
its trace source-over what the draws behind it laid, so where several stand
in front of one another the nearer hold the larger share. A medium states the
direction back toward the camera as its normal. Both channels are zero where
nothing stands in front of the opaque surface.
A view keeps the trace only while a pass reads it: name @scene.media or
@scene.mediaSurface in a pass's inputs, at any phase from afterSky on,
the post-scene phases included. A pass writing the trace alone runs only on a
view that keeps it.
Reading it
# bindings.yaml of a compute pass
- { name: media, kind: texture2d }
- { name: media_surface, kind: texture2d }
ctx.enqueue {
kind = "compute", program = prog,
inputs = { media = "@scene.media", media_surface = "@scene.mediaSurface" },
stage = "postTemporal",
}
#include "@builtin::shaderModules.media_trace"
let m = textureLoad(media, px, 0);
let s = textureLoad(media_surface, px, 0);
let covered = media_trace_coverage(m); // 0..1
let kinds = media_trace_kinds(m); // cloud, medium, surface shares
let dist = media_trace_distance(s); // world units
let n = media_trace_normal(s); // world normal
Writing it
A producer lays its trace with a raster pass whose fs_main returns
@location(0) for @scene.media and @location(1) for
@scene.mediaSurface, both blended "premultiplied":
let t = media_trace_lay(coverage, MEDIA_TRACE_MEDIUM, distance, -view_dir);
out.media = t.media;
out.surface = t.surface;
ctx.enqueue {
kind = "raster", shader = traceShader, vertexCount = 3,
outputs = {
{ target = "@scene.media", blend = "premultiplied" },
{ target = "@scene.mediaSurface", blend = "premultiplied" },
},
...
}
Every blended material draw lays its trace itself, as a see-through surface at its own distance and normal, covering the share of the pixel its blend equation changes:
| Blend | Coverage |
|---|---|
alpha, premultiplied | the colour's alpha |
additive | the strength of the light it adds: its brightest channel, saturating at 1 |
multiply | how far the factor it scales by departs from 1, in its farthest channel, saturating at 1 |
So an additive glow card lays its trace where it glows and none across the dark of its quad, and a multiply grime card lays none where its factor is white.