The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
Overlapping media drawn together: the ray a stretch at a time, the media it is in summed (ROADMAP § 1a) (#40)
Yujia50c077e637fd
diff --git a/docs/MATERIAL_FUNCTIONS.md b/docs/MATERIAL_FUNCTIONS.mdindex d015dec..e2c4ad5 100644--- a/docs/MATERIAL_FUNCTIONS.md+++ b/docs/MATERIAL_FUNCTIONS.md@@ -329,7 +329,8 @@ A `VolumeMesh` has these settings:- `anisotropy`: the Henyey-Greenstein g.The path tracer samples collisions at the majorant's rate. A loose bound is slower, never wrong; a function above it is-clamped. Volumes must not overlap. They are lit by every light, cast shadows on what is behind them and scatter light+clamped. Volumes may overlap (a fire inside smoke): where they do they are one medium, their coefficients summed, and a+collision there is one of theirs by its extinction. They are lit by every light, cast shadows on what is behind them and scatter lightany number of times:- delta tracking for paths;- ratio tracking for shadow rays (Novák et al. 2018);diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex a7d2b3d..7093ed2 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -1836,8 +1836,8 @@ hid two faults of the live path's own:- Elsewhere: bench-ghosting passes with the numbers of the table above for Bistro and 2.42, 3.27, 1.17, 1.22, 2.48 forKabukichō; the Bistro slice half a second after its explosion 6.03 / SSIM 0.832 in both builds (its fireball is asmall part of that view). test-render 179 pass.-- Not done: live GI, reflection and shadow rays still see no media (the stand-in light stays), overlapping media are- still taken whole in entry order, and a medium has no motion vector (smoke that drifts is reactive, not followed).+- Not done: live GI, reflection and shadow rays still see no media (the stand-in light stays), and a medium has no+ motion vector (smoke that drifts is reactive, not followed). (Overlapping media: below.)**The specular history per pixel (2026-11-13, after it).** `stemporal` still set every pixel's history by what thecamera did: 2 .. 12 frames by roughness whenever it moved or turned at all, 4 for the 8 frames after it translated@@ -1877,6 +1877,31 @@ specular history per pixel by parallax too (its source, `REBLUR_TemporalAccumulano bench here counts in frames. It waits for a lockstep lighting-edit measure.- Gone with it: the frames-since-translation counter, temporal flag 16384 and the Playground's H switch.+**Overlapping media (2026-11-13, after it).** The kernel took each medium on a ray whole: `media_track` (offline) and+`media_march` (live camera rays, the cache's gather) in the order the ray enters them, `media_homogeneous` with its+light (live paths through water) in the table's order. A medium inside another was drawn behind all of it: an+emitter (2 nits a metre, 1 m) inside an absorber (1/m, 2 m) read 2 e⁻² = 0.27 nits for 2 (e⁻⁰·⁵ − e⁻¹·⁵) = 0.77, offline+and live; hence the rule that volumes must not overlap, and no smoke grid around explosions (§ 6 step 10 b). Now the+ray is taken a stretch at a time, between the points where it enters or leaves a medium, and the media it is in+there are one medium:+- offline, delta tracking at the sum of their majorants; a tentative collision evaluates each, sums extinction and+ emission, and a real one is one of theirs by its extinction (its albedo, its phase function);+- live, the march sums their coefficients a step, at the finest of their steps (a medium's crossing over 8, over 64+ with a density grid: the grid's two source patches are in the kernel's source now); the share of the pixel that+ media changing with time take is summed the same way;+- homogeneous media with their light the same way, analytically a stretch; their transmittance alone stays a product.+Shadow rays' ratio tracking was right already (a product). One medium on a ray draws the random numbers it drew: the+fire shot reads 6.28 / 5.56 / 3.69 as before, to the digit.++- `volume/overlap-emission` and `-live`: 0.7674 (8,192 spp) and 0.7668 of 0.7668; main's kernel 0.2708 and 0.2705.+- A fireball inside a smoke column (a scratch scene, 0.6 s after it starts): drawn dim behind the smoke before,+ offline and live; in it now. Live against its own offline dE 1.63 / SSIM 0.972 (1.59 / 0.972 before: what is left+ is the smoke's shadow on the floor, which live shadow rays do not cast).+- Cost: none that shows. Thames (the river is a homogeneous medium on every path) at 0.5, this build and main's+ alternated on a machine under load, GPU p50: 22.6, 20.6, 19.7, 18.6 ms (main, this, main, this).+- test-render 181 pass. Not done: `Explosions` still refuses a smoke grid and says why in the old words (the voxel+ line's to change, with the comparison that found it); at most 32 media a scene, as before.+Not done here: filters sized by hit distance, which is what would let a short history stand without noise; thePlayground and the page still open a 1280 × 720 view; the live buffers are sized by the output (about 1.7 GB at2560 × 1440), though the traced grid rarely fills it.diff --git a/docs/VOXEL.md b/docs/VOXEL.mdindex 782d865..835c565 100644--- a/docs/VOXEL.md+++ b/docs/VOXEL.md@@ -558,8 +558,9 @@ An explosion's fireball and smoke, on the existing volume and particle paths (thspreading along burnable voxels, no smoke stopped by voxels). `Explosions` (`@threert/voxel`) is a group made once insetup: a pool of fireballs (`Fireball`) and a pool of point lights. It adds no engine call. **The smoke is eachfireball's own**: it cools to dark smoke over its life. `Explosions` takes no smoke grid (it throws if given one): the-grid's box is a volume, a fireball in it would overlap it, and volumes must not overlap (the media are taken whole,-in the order a ray enters them; why, below, "Why volumes must not overlap"). A game may keep its own `SimulatedSmoke`+grid's box is a volume, a fireball in it would overlap it, and when this was built volumes could not overlap (the+media were taken whole, in the order a ray enters them: below, "Why volumes must not overlap"; the engine draws+overlapping media together since 2026-11-13, and `Explosions` has not been changed for it yet). A game may keep its own `SimulatedSmoke`where nothing blows up.```ts@@ -659,6 +660,9 @@ Where it falls short, and why:#### Why volumes must not overlap (measured 2026-11-12, kept as the evidence)+(Since 2026-11-13 the kernel draws overlapping media together, offline and live: ROADMAP § 1a "Overlapping media".+What follows is of the kernel before, and is why `Explosions` was built without a grid.)+The first `-fire` had the fireballs inside the smoke grid's box and puffed hot smoke at each fireball's centre. Liveand offline both take the media whole in the order a ray enters them: the smoke box first, so each fireball was drawnbehind all the smoke on that ray. With the grid (fire at 0 s offline, at 2 s live, the same age;@@ -770,7 +774,7 @@ above.)| Idle fireballs parked | at y = −10,000, scale 1e-3, instead of hidden | `World::set_visible` (`world.rs` line 750): hiding anything but particles rebuilds the live scene | parked fireballs keep their slots among the kernel's 32 volumes, and every pixel's march still tests their boxes | hiding a volume or a light is done in place | `visible = false` instead of parking; the bench's cost runs report no rebuild after an explosion || Idle lights reach 1 mm | an idle light's `distance` is 1 mm where it is parked | `light_reach` (`lib.rs` line 211): a black light without a range reaches without limit, and the kernel does not skip black lights before the group test (`pathtrace.wgsl` line 1578) | parked lights 10 km away share a group of 16 with lit ones, and groups are not sorted again when lights move (`lib.rs` line 277): the group's sphere is about 5 km wide while any fire burns (the per-light range test still works) | black lights reach nothing (or are hidden in place) and moved lights are grouped again | distance 0 on idle lights; the fire route's GPU time before any explosion within 0.1 ms of the smoke route's || The smoke kept by never rebuilding | the pool's objects movable from the start; idle ones parked, not hidden (`Explosions` has no grid now: this keeps a game's own `SimulatedSmoke`) | `LiveCore::rebuild` (`live.rs` line 3474) → `load_scene` → `Gpu::upload` makes new grid textures (`gpu.rs` line 1664): the simulated state is not carried over | any other structural change in the game (an object added, a texture, an edit's fallback) wipes the smoke and the fire burning in it | a rebuild carries every simulated grid's state over | the bench's smoke check with a rebuild forced at 1 s (an object added): the smoke at 3 s as without it |-| Volumes must not overlap | `Explosions` has no smoke grid (it refuses one); the smoke is each fireball's own. Two fires close together still make two fireballs that overlap: nothing keeps them apart (no distance rule) | `media_track` (offline) and `media_march` (live) in `pathtrace.wgsl` take the media whole, in the order a ray enters them (at most 32, a 32-bit mask), not their coefficients summed along the ray; hence the rule (`core.ts` `VolumeMesh`, MATERIAL_FUNCTIONS.md) | a fire inside smoke is drawn behind all of it (measured: not seen at all at 0.2 and 0.5 s, "Why volumes must not overlap"); neighbouring fireballs hide each other in the same way; no smoke grid around explosions | overlapping media are drawn together: one march summing every medium's coefficients per step along the ray (or a froxel volume), offline and live | puff a `SimulatedSmoke` around fireballs again (`Explosions` taking a grid) and run the comparison that found it (`bench-voxel-fire` at d7b4e4a, `--only smoke --ages 0.2,0.5`): the fire seen in its smoke offline and live; two overlapping fireballs both seen |+| Volumes must not overlap | `Explosions` has no smoke grid (it refuses one); the smoke is each fireball's own. Two fires close together still make two fireballs that overlap: nothing keeps them apart (no distance rule) | `media_track` (offline) and `media_march` (live) in `pathtrace.wgsl` take the media whole, in the order a ray enters them (at most 32, a 32-bit mask), not their coefficients summed along the ray; hence the rule (`core.ts` `VolumeMesh`, MATERIAL_FUNCTIONS.md) | a fire inside smoke is drawn behind all of it (measured: not seen at all at 0.2 and 0.5 s, "Why volumes must not overlap"); neighbouring fireballs hide each other in the same way; no smoke grid around explosions | **now** (2026-11-13: the kernel takes the ray a stretch at a time and sums the coefficients of the media it is in, offline and live; `volume/overlap-emission`, `-live`). What is left is `Explosions`' own: taking a grid again, in a PR of the voxel line's | puff a `SimulatedSmoke` around fireballs again (`Explosions` taking a grid) and run the comparison that found it (`bench-voxel-fire` at d7b4e4a, `--only smoke --ages 0.2,0.5`): the fire seen in its smoke offline and live; two overlapping fireballs both seen |### Where the engine works round its dependenciesdiff --git a/packages/scene/src/core.ts b/packages/scene/src/core.tsindex d067a1f..d6a48d4 100644--- a/packages/scene/src/core.ts+++ b/packages/scene/src/core.ts@@ -1537,7 +1537,8 @@ export class DensityGrid {* A bounded participating medium (smoke, local fog, a fireball): the unit box or sphere (diameter 1) under the node's* transform (scale = size), filled by `volume`. `majorant` (1/m) must bound the function's extinction everywhere (the* path tracer samples collisions at that rate; larger is slower, never wrong); `anisotropy` is the Henyey-Greenstein g- * (0 = isotropic). Volumes must not overlap. Rendered by the path tracer: they shadow, scatter and emit.+ * (0 = isotropic). Volumes may overlap: where they do they are one medium, their coefficients summed. Rendered by the+ * path tracer: they shadow, scatter and emit.*/export class VolumeMesh extends Object3D {override type = "VolumeMesh";diff --git a/renderer/shaders/pathtrace.wgsl b/renderer/shaders/pathtrace.wgslindex ef66876..20fe2ff 100644--- a/renderer/shaders/pathtrace.wgsl+++ b/renderer/shaders/pathtrace.wgsl@@ -763,30 +763,63 @@ fn occluded_sky(o: vec3<f32>, d: vec3<f32>) -> bool {fn media_homogeneous(o: vec3<f32>, d: vec3<f32>, tmax: f32, light: bool) -> March {var r = March(1.0, vec3<f32>(0.0), vec2<f32>(1.0, 0.0));let n = min(u32(P.time.w), 32u);- for (var k = 0u; k < n; k++) {- let m = media[k];- if (m.a.z == 0u) { continue; }- let sp = medium_span(m, o, d);- let a = max(sp.x, 0.0);- let b = min(sp.y, tmax);- if (a >= b) { continue; }- let st = m.p[0].w;- let tr = exp(-st * (b - a));- if (light) {+ if (!light) {+ // Transmittance alone: the media's product, in any order.+ for (var k = 0u; k < n; k++) {+ let m = media[k];+ if (m.a.z == 0u) { continue; }+ let sp = medium_span(m, o, d);+ let a = max(sp.x, 0.0);+ let b = min(sp.y, tmax);+ if (a < b) { r.tr *= exp(-m.p[0].w * (b - a)); }+ }+ return r;+ }+ // With their light: along the ray, a stretch at a time between the points where it enters or leaves a medium, the+ // coefficients of the media it is in there summed (media may overlap, and the table's order is not the ray's).+ var t = 0.0;+ for (var s = 0u; s <= 2u * n; s++) {+ var st = 0.0;+ var sc = vec3<f32>(0.0);+ var em = vec3<f32>(0.0);+ var end = tmax;+ var inside = false;+ for (var k = 0u; k < n; k++) {+ let m = media[k];+ if (m.a.z == 0u) { continue; }+ let sp = medium_span(m, o, d);+ let a = max(sp.x, 0.0);+ let b = min(sp.y, tmax);+ if (a >= b) { continue; }+ if (a > t) {+ end = min(end, a);+ } else if (b > t) {+ inside = true;+ end = min(end, b);+ st += m.p[0].w;+ sc += m.p[0].rgb * m.p[0].w;+ em += m.p[1].rgb;+ }+ }+ if (inside) {+ let len = end - t;+ let tr = exp(-st * len);// In-scattering at two points stratified by transmittance (half of what scatters toward o happens before// the first, ...): one lookup in the middle lit a deep crossing with its dimmer depth.var lin = vec3<f32>(0.0);if (cache_on()) {for (var j = 0u; j < 2u; j++) {let q = (f32(j) + rnd()) * 0.5 * (1.0 - tr);- let t = select(-log(1.0 - q) / st, (f32(j) + 0.5) * 0.5 * (b - a), st <= 1e-6);- lin += 0.5 * cache_point(o + d * (a + t)).rgb;+ let tt = select(-log(1.0 - q) / st, (f32(j) + 0.5) * 0.5 * len, st <= 1e-6);+ lin += 0.5 * cache_point(o + d * (t + tt)).rgb;}}- let w = select(b - a, (1.0 - tr) / st, st > 1e-6);- r.light += r.tr * (m.p[0].rgb * st * lin + m.p[1].rgb) * w;+ let w = select(len, (1.0 - tr) / st, st > 1e-6);+ r.light += r.tr * (sc * lin + em) * w;+ r.tr *= tr;}- r.tr *= tr;+ if (end >= tmax) { break; }+ t = end;}return r;}@@ -2014,43 +2047,61 @@ fn medium_eval(k: u32, x: vec3<f32>) -> VOut {struct Track { t: f32, albedo: vec3<f32>, g: f32, emission: vec3<f32> };// Delta tracking along o + d t, t < tmax: the first real collision (t < 0: none) with its albedo and HG anisotropy,-// and the emission met on the way (emission / majorant at every tentative collision). The media are taken in the-// order the ray enters them (they do not overlap).+// and the emission met on the way (emission / majorant at every tentative collision). The ray is taken a stretch at+// a time, between the points where it enters or leaves a medium: in each the media it is in are one medium, their+// majorants and their coefficients summed (media may overlap). A collision is one of theirs, by extinction.+// (Until 2026-11-13 each medium was tracked whole, in the order the ray entered them: a collision in the first hid+// what a medium inside it emitted or scattered before that point: a fire inside smoke was drawn behind the smoke.)fn media_track(o: vec3<f32>, d: vec3<f32>, tmax: f32) -> Track {var r = Track(-1.0, vec3<f32>(1.0), 0.0, vec3<f32>(0.0));let n = min(u32(P.time.w), 32u);- var done = 0u;- for (var it = 0u; it < n; it++) {- var best = 0xffffffffu;- var span = vec2<f32>(0.0);+ var t = 0.0;+ for (var s = 0u; s <= 2u * n; s++) {+ var mask = 0u;+ var mj = 0.0;+ var end = tmax;for (var k = 0u; k < n; k++) {- if (((done >> k) & 1u) != 0u) { continue; }let sp = medium_span(media[k], o, d);let a = max(sp.x, 0.0);let b = min(sp.y, tmax);- if (a < b && (best == 0xffffffffu || a < span.x)) {- best = k;- span = vec2<f32>(a, b);+ if (a >= b) { continue; }+ if (a > t) {+ end = min(end, a);+ } else if (b > t) {+ mask |= 1u << k;+ mj += media[k].b.x;+ end = min(end, b);}}- if (best == 0xffffffffu) { break; }- done |= 1u << best;- let mj = media[best].b.x;- if (mj <= 0.0) { continue; }- var t = span.x;- loop {- t -= log(1.0 - rnd()) / mj;- if (t >= span.y) { break; }- let v = medium_eval(best, o + d * t);- r.emission += v.emission / mj;- if (rnd() * mj < v.extinction) {- r.t = t;- r.albedo = v.albedo;- r.g = media[best].b.y;- return r;+ if (mask != 0u && mj > 0.0) {+ loop {+ t -= log(1.0 - rnd()) / mj;+ if (t >= end) { break; }+ var st = 0.0;+ var em = vec3<f32>(0.0);+ for (var k = 0u; k < n; k++) {+ if (((mask >> k) & 1u) == 0u) { continue; }+ let v = medium_eval(k, o + d * t);+ st += v.extinction;+ em += v.emission;+ // The medium a collision here is in: each by its extinction (the first without a draw).+ if (v.extinction > 0.0 && (st <= v.extinction || rnd() * st < v.extinction)) {+ r.albedo = v.albedo;+ r.g = media[k].b.y;+ }+ }+ r.emission += em / mj;+ if (rnd() * mj < st) {+ r.t = t;+ return r;+ }}}+ if (end >= tmax) { break; }+ t = end;}+ r.albedo = vec3<f32>(1.0);+ r.g = 0.0;return r;}@@ -2090,47 +2141,78 @@ fn media_march(o: vec3<f32>, d: vec3<f32>, tmax: f32, camera: bool) -> March {var r = March(1.0, vec3<f32>(0.0), vec2<f32>(1.0, 0.0));let n = min(u32(P.time.w), 32u);let u = rnd();- var done = 0u;- for (var it = 0u; it < n; it++) {- var best = 0xffffffffu;- var span = vec2<f32>(0.0);+ let per = f32(select(MEDIA_STEPS_INDIRECT, MEDIA_STEPS, camera));+ let lw = vec3<f32>(0.2126, 0.7152, 0.0722);+ // A stretch at a time, between the points where the ray enters or leaves a medium (as `media_track`): the media+ // it is in there are marched together, their coefficients summed, at the finest of their steps: a medium's is its+ // crossing over `per`, and an eighth of that with a density grid (b.w: its cells; a grid has detail 8 steps miss).+ var t = 0.0;+ for (var s = 0u; s <= 2u * n; s++) {+ var mask = 0u;+ var end = tmax;+ var dt = 1.0e30;+ var gridded = false;for (var k = 0u; k < n; k++) {- if (((done >> k) & 1u) != 0u) { continue; }let sp = medium_span(media[k], o, d);let a = max(sp.x, 0.0);let b = min(sp.y, tmax);- if (a < b && (best == 0xffffffffu || a < span.x)) {- best = k;- span = vec2<f32>(a, b);+ if (a >= b) { continue; }+ if (a > t) {+ end = min(end, a);+ } else if (b > t) {+ mask |= 1u << k;+ end = min(end, b);+ let grid = bitcast<u32>(media[k].b.w) != 0u;+ gridded = gridded || grid;+ dt = min(dt, (b - a) / (per * select(1.0, 8.0, grid)));}}- if (best == 0xffffffffu) { break; }- done |= 1u << best;- let steps = select(MEDIA_STEPS_INDIRECT, MEDIA_STEPS, camera);- let dt = (span.y - span.x) / f32(steps);- for (var k = 0u; k < steps; k++) {- let x = o + d * (span.x + (f32(k) + u) * dt);- let v = medium_eval(best, x);- let st = v.extinction;- let ti = exp(-st * dt);- // Over the step (constant medium): emitted + scattered radiance x (1 - e^-st dt) / st.- let w = select(dt, (1.0 - ti) / st, st > 1e-6);- var lin = vec3<f32>(0.0);- if (camera && cache_on()) {- // Looked up at a point jittered within a voxel: the accumulation filters the voxels instead of- // showing them as blocks. A jitter out of the medium finds a voxel no training point reached (dark):- // then the voxel of the point itself. Near the boundary that lost light, at every scattering order- // through the training's feedback (volume/furnace-live 0.931 of 1 without the emission clamp).- let vs = exp2(floor(log2(max(length(x - P.cam_pos.xyz) * CACHE_ANGLE, CACHE_MIN_VOXEL))) + VOLUME_FINE);- let jv = cache_point(x + (vec3<f32>(rnd(), rnd(), rnd()) - 0.5) * vs);- lin = jv.rgb;- if (jv.w < CACHE_WARM) { lin = cache_point(x).rgb; }+ if (mask != 0u) {+ let steps = max(u32(ceil((end - t) / dt - 1e-3)), 1u);+ let ds = (end - t) / f32(steps);+ for (var j = 0u; j < steps; j++) {+ let x = o + d * (t + (f32(j) + u) * ds);+ // All the media here, and those of them that change with time (`March::moving`).+ var st = 0.0;+ var sc = vec3<f32>(0.0);+ var em = vec3<f32>(0.0);+ var st_m = 0.0;+ var sc_m = vec3<f32>(0.0);+ var em_m = vec3<f32>(0.0);+ for (var k = 0u; k < n; k++) {+ if (((mask >> k) & 1u) == 0u) { continue; }+ let v = medium_eval(k, x);+ st += v.extinction;+ sc += v.albedo * v.extinction;+ em += v.emission;+ if (media[k].a.w != 0u) {+ st_m += v.extinction;+ sc_m += v.albedo * v.extinction;+ em_m += v.emission;+ }+ }+ let ti = exp(-st * ds);+ // Over the step (constant medium): emitted + scattered radiance x (1 - e^-st dt) / st.+ let w = select(ds, (1.0 - ti) / st, st > 1e-6);+ var lin = vec3<f32>(0.0);+ // (No lookup where a step through a grid finds nothing: most of a grid is empty air.)+ if (camera && cache_on() && (st > 0.0 || !gridded)) {+ // Looked up at a point jittered within a voxel: the accumulation filters the voxels instead of+ // showing them as blocks. A jitter out of the medium finds a voxel no training point reached (dark):+ // then the voxel of the point itself. Near the boundary that lost light, at every scattering order+ // through the training's feedback (volume/furnace-live 0.931 of 1 without the emission clamp).+ let vs = exp2(floor(log2(max(length(x - P.cam_pos.xyz) * CACHE_ANGLE, CACHE_MIN_VOXEL))) + VOLUME_FINE);+ let jv = cache_point(x + (vec3<f32>(rnd(), rnd(), rnd()) - 0.5) * vs);+ lin = jv.rgb;+ if (jv.w < CACHE_WARM) { lin = cache_point(x).rgb; }+ }+ r.light += r.tr * (sc * lin + em) * w;+ r.moving = vec2<f32>(r.moving.x * exp(-st_m * ds), r.moving.y + dot(r.tr * (sc_m * lin + em_m) * w, lw));+ r.tr *= ti;}- let seen = r.tr * (v.albedo * st * lin + v.emission) * w;- r.light += seen;- r.tr *= ti;- if (media[best].a.w != 0u) { r.moving = vec2<f32>(r.moving.x * ti, r.moving.y + dot(seen, vec3<f32>(0.2126, 0.7152, 0.0722))); }}+ if (end >= tmax) { break; }+ t = end;}return r;}diff --git a/renderer/src/gpu.rs b/renderer/src/gpu.rsindex c0b99ae..b27b6a5 100644--- a/renderer/src/gpu.rs+++ b/renderer/src/gpu.rs@@ -702,13 +702,6 @@ pub fn kernel_source(src: &str, features: u32) -> String {"// The node's density grid at `lp` (the volume's own space, the box [-0.5, 0.5]^3): field A's four numbers, trilinear\n// between cell centres, the border cells' beyond them; zeros for a node without a grid.\nfn vgrid(i: VIn, lp: vec3<f32>) -> vec4<f32> {\n if (i.grid.y == 0u) { return vec4<f32>(0.0); }\n let n = vec3<f32>(vec3<u32>(i.grid.y & 1023u, (i.grid.y >> 10u) & 1023u, (i.grid.y >> 20u) & 1023u));\n let c = clamp((lp + 0.5) * n, vec3<f32>(0.5), n - 0.5) + vec3<f32>(0.0, 0.0, f32(i.grid.x));\n return textureSampleLevel(grid_tex, samp, c / vec3<f32>(textureDimensions(grid_tex)), 0.0);\n}\n\n// @volume-functions-begin\n",);patch(" i.custom = m.c;\n var r = volume_function(m.a.x, i, m.p);\n", " i.custom = m.c;\n i.grid = vec2<u32>(bitcast<u32>(m.b.z), bitcast<u32>(m.b.w));\n var r = volume_function(m.a.x, i, m.p);\n");- // A grid has detail a medium's 8 live steps would miss: eight times as many through a medium with one, and no- // voxel lookup where a step finds nothing (most of a grid is empty air).- patch(- " let steps = select(MEDIA_STEPS_INDIRECT, MEDIA_STEPS, camera);\n",- " let steps = select(MEDIA_STEPS_INDIRECT, MEDIA_STEPS, camera) * select(1u, 8u, bitcast<u32>(media[best].b.w) != 0u);\n",- );- patch(" if (camera && cache_on()) {\n", " if (camera && cache_on() && (st > 0.0 || bitcast<u32>(media[best].b.w) == 0u)) {\n");}if features & F_TINT != 0 {patch(diff --git a/tests/render/volume-overlap-emission-live.ts b/tests/render/volume-overlap-emission-live.tsnew file mode 100644index 0000000..971fe0a--- /dev/null+++ b/tests/render/volume-overlap-emission-live.ts@@ -0,0 +1,8 @@+// volume/overlap-emission in the real-time renderer: the camera ray is marched a stretch at a time, the media it is in+// together (pathtrace.wgsl media_march). Same analytic value.+import def, { test as base } from "./volume-overlap-emission.ts";+import type { TestMeta } from "./lib.ts";++export const test: TestMeta = { ...base, id: "volume/overlap-emission-live", temporal: true, live: 240, metric: { ...base.metric, tolerance: 0.02 } as TestMeta["metric"] };++export default def;diff --git a/tests/render/volume-overlap-emission.ts b/tests/render/volume-overlap-emission.tsnew file mode 100644index 0000000..e4d42d2--- /dev/null+++ b/tests/render/volume-overlap-emission.ts@@ -0,0 +1,23 @@+// Media that overlap are one medium where they do: an emitter (2 nits a metre, no extinction, 1 m thick) inside an+// absorber (extinction 1/m, 2 m thick) over a black sky. The emitter's light crosses the absorber in front of each of+// its points: 2 (e^-0.5 - e^-1.5) = 0.7668 nits. Taken whole in the order the ray enters them (until 2026-11-13), the+// emitter was drawn behind all of the absorber: 2 e^-2 = 0.2707.+import { VolumeMesh } from "@threert/scene";+import { UNIFORM } from "./volume-box-transmittance.ts";+import { CENTER, camera, result, sceneWithSky, type TestMeta } from "./lib.ts";++export const test: TestMeta = {+ id: "volume/overlap-emission", features: ["volume"], reference: "analytic", temporal: false,+ metric: { kind: "radiance", region: CENTER, expected: 2 * (Math.exp(-0.5) - Math.exp(-1.5)), tolerance: 0.02 },+ spp: 256,+ note: "absorber 2 x 2 x 2 m and emitter 2 x 2 x 1 m, both at the origin, camera at z = 5, nothing behind",+};++export default function () {+ const scene = sceneWithSky(0);+ const absorber = new VolumeMesh({ volume: UNIFORM.with({ Density: 1 }), majorant: 1 });+ absorber.scale.set(2, 2, 2);+ const emitter = new VolumeMesh({ volume: UNIFORM.with({ Density: 0, Emission: [2, 2, 2, 0] }), majorant: 1 });+ emitter.scale.set(2, 2, 1);+ return result(scene, camera(scene, [0, 0, 5], [0, 0, 0]), [absorber, emitter]);+}