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
Kernel: a light sample through a cutout keeps its light (#95)
Yujia5fed36d0388b
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 2668ee2..9b012f8 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -2427,6 +2427,17 @@ layouts changes only the five tests that name bits (renumbered); bench-window'sTokyo too. Next: a camera hit's light through a cutout loses the BSDF ray's share of its MIS weight (the 18 %);then the cutout bits' defaults.+**A light sample through a cutout keeps its light (2026-11-16, after it).** Under `A_CUTOUT_PATHS` a camera hit's+shadow rays test cutouts and the ray it sends on does not, but the sample's MIS weight still shared the light with+that ray: under a card cut out everywhere a uniform sky gave a Lambert plane 18 % of its light. The sample now+takes the share the BSDF ray cannot bring (`light_mis`; a specular sample off a smooth surface still tests cutouts+and keeps its share): 50 for 50. None of our shots shows it (Bistro at night, the slice, Kabukichō, Tokyo: as+main's); the trace is equal (Bistro 8.70 / 8.70 ms, the slice 9.29 / 9.32, Zero Day 9.68 / 9.61; guarded, Tokyo+4.79 / 4.84 and Kabukichō 17.02 / 16.95); render-identity changes that one test. What is left of the bit is in the+radiance cache: its rays take cards as solid for their own light samples too, so what a cutout lights sends nothing+on (`sky/cutout-bounce`: a quarter of the sky offline, black live). Found on the way, not explained: with no card at+all the live view reads that bounce 16 % low (0.21 for 0.25).+## 2. Temporal fixesTargets (2026-11-08): bench-temporal, bench-ghosting and their UE MRQ references move to Bistro night (§ 0 step 7); thediff --git a/docs/RUNTIME.md b/docs/RUNTIME.mdindex c9d7238..aa1f106 100644--- a/docs/RUNTIME.md+++ b/docs/RUNTIME.md@@ -1355,7 +1355,7 @@ view, the way to hold a live picture to the offline one).|---|---|---|---|| 1 `A_GLASS_SHADOWS` | shadow rays to the analytic lights and the sky pass glass in full, untinted | light under tinted glass 4.3 x (`shadow/glass-tint`) | a glass scene +34 % (khronos-transmission 5.20 -> 6.97 ms) || 2 `A_MEDIA_SHADOWS` | only homogeneous media dim shadow rays: smoke and fire cast no shadow | light behind optical depth 1: e x (`shadow/medium`) | the Bistro slice +0.84 ms (9 %), the fire scene +0.10 |-| 4 `A_CUTOUT_PATHS` | cutouts are opaque to rays past a camera hit's own, off mirror paths | the sky under a clear card: 18 % of it (`sky/cutout-clear`) | Bistro +1.48 ms of 9.06 |+| 4 `A_CUTOUT_PATHS` | cutouts are opaque to rays past a camera hit's own, off mirror paths | a camera hit's own light is whole (`sky/cutout-clear-live`: its samples take the share those rays cannot bring, `light_mis`); what a cutout lights sends nothing on (`sky/cutout-bounce`: black) | Bistro +1.48 ms of 9.06 || 8 `A_CUTOUT_EMITTERS` | cutouts are opaque to emitters' shadow rays | an emitter behind a clear card: black (`shadow/cutout-emitter`) | Bistro +0.62 ms || 16 `A_FAR_EMITTERS` | the light tree proposes no emitter beyond 120 m; without a tree, the emitter cull (60 m, 120 m in view) | panels 150 m up: 3 % of their light (`light/emitter-far`); Kabukichō's and Tokyo's shots unchanged | Tokyo from the air 4.7 -> 9.2 ms; the 3 x 3 district +0.15 |diff --git a/renderer/shaders/pathtrace.wgsl b/renderer/shaders/pathtrace.wgslindex 197a0f0..e4cd59f 100644--- a/renderer/shaders/pathtrace.wgsl+++ b/renderer/shaders/pathtrace.wgsl@@ -641,7 +641,7 @@ fn tri_hit(ti: u32, o: vec3<f32>, d: vec3<f32>) -> Hit {if (mode == TRACE_SHADOW && (flags & TRI_NO_SHADOW) != 0u) { continue; }let r = intersect_tri(o, d, ti, hit.t);if (r.x < hit.t) {- if (has(F_MASKED) && (flags & TRI_MASKED) != 0u && !cutouts_opaque() && mode != TRACE_ANY_OPAQUE && !alpha_pass(ti, NO_INST, r.y, r.z, r.x)) { continue; }+ if (has(F_MASKED) && (flags & TRI_MASKED) != 0u && !cutouts_opaque() && mode != TRACE_ANY_OPAQUE && !alpha_pass(ti, NO_INST, r.y, r.z, r.x)) { shadow_hole = true; continue; }hit = Hit(r.x, ti, r.y, r.z, NO_INST);if (mode != TRACE_CLOSEST) { return hit; }}@@ -751,6 +751,7 @@ fn tri_hit(ti: u32, o: vec3<f32>, d: vec3<f32>) -> Hit {// Shadow rays of the analytic lights: objects that cast no shadow (castShadow = false, glass) let them through.fn occluded(o: vec3<f32>, d: vec3<f32>, tmax: f32, rd: u32) -> bool {shadow_tint = vec3<f32>(1.0);+ shadow_hole = false;if (trace(o, d, tmax, TRACE_SHADOW).tri != 0xffffffffu) { return true; }if (!approx(A_GLASS_SHADOWS) && has(F_GLASS) && glass_blocks(o, d, tmax, false, rd)) { return true; }// Through media: blocked with probability 1 - transmittance (an unbiased estimate of it, ratio tracking).@@ -813,7 +814,10 @@ fn gtint(v: vec3<f32>) -> vec3<f32> {// What reaches a lit point from a light along a shadow ray: 0 when occluded, else 1 or the tint of the glass it passed// (`gtint`). `shadows` false (a light that casts none): 1, no ray.fn light_vis(shadows: bool, o: vec3<f32>, d: vec3<f32>, tmax: f32, rd: u32) -> vec3<f32> {- if (!shadows) { return vec3<f32>(1.0); }+ if (!shadows) {+ shadow_hole = false;+ return vec3<f32>(1.0);+ }if (occluded(o, d, tmax, rd)) { return vec3<f32>(0.0); }return shadow_tint;}@@ -872,6 +876,7 @@ fn glass_at(h: Hit, d: vec3<f32>, t: f32) -> GlassAt {// path loop counts it in full past such glass), which find it where it is (Snell's window from under water).fn occluded_sky(o: vec3<f32>, d: vec3<f32>, rd: u32) -> bool {shadow_tint = vec3<f32>(1.0);+ shadow_hole = false;if (trace(o, d, INF, TRACE_SHADOW).tri != 0xffffffffu) { return true; }if (!approx(A_GLASS_SHADOWS) && has(F_GLASS) && glass_blocks(o, d, INF, true, rd)) { return true; }if (media_shadows()) { return rng1(rd) >= media_tr(o, d, INF, rd); }@@ -966,12 +971,14 @@ fn media_shadows() -> bool { return (!approx(A_MEDIA_SHADOWS) || media_voxels) &// Removing the bias without the cost (an opacity micromap, or these rays compacted into a pass of their own) is a// follow-up item (ROADMAP § 0). Offline rendering always tests.fn occluded_emitter(o: vec3<f32>, d: vec3<f32>, tmax: f32, rd: u32) -> bool {+ shadow_hole = false;if (trace(o, d, tmax, select(TRACE_ANY, TRACE_ANY_OPAQUE, approx(A_CUTOUT_EMITTERS))).tri != 0xffffffffu) { return true; }if (media_shadows()) { return rng1(rd) >= media_tr(o, d, tmax, rd); }return homogeneous_blocks(o, d, tmax, rd);}fn occluded_any(o: vec3<f32>, d: vec3<f32>, tmax: f32, rd: u32) -> bool {+ shadow_hole = false;if (trace(o, d, tmax, TRACE_ANY).tri != 0xffffffffu) { return true; }if (media_shadows()) { return rng1(rd) >= media_tr(o, d, tmax, rd); }return homogeneous_blocks(o, d, tmax, rd);@@ -1134,6 +1141,12 @@ fn cutouts_opaque() -> bool {return approx(A_CUTOUT_PATHS) && cur_depth >= 1u && !mirror_path;}+// A path stays mirror-like (`mirror_path`) through specular samples off surfaces smoother than this.+const MIRROR_ROUGHNESS: f32 = 0.3;++// Whether a cutout let the last shadow ray through (`occluded*` clear it, `trace` sets it).+var<private> shadow_hole: bool = false;+// Alpha-cutout test for candidate hits on masked triangles (mesh UVs, footprint of a camera ray at distance t). `inst`:// the model instance a pool triangle belongs to (its scale shifts the texel density, as in `inst_attr`), or NO_INST.fn alpha_pass(ti: u32, inst: u32, u: f32, v: f32, t: f32) -> bool {@@ -1449,6 +1462,32 @@ fn pdf_bsdf(s: Surface, lb: Lobes, v: vec3<f32>, l: vec3<f32>) -> f32 {return lb.p_coat * pc + (1.0 - lb.p_coat - lb.p_trans) * (lb.p_spec * ps + (1.0 - lb.p_spec) * pd);}+// MIS weight of a light sample (solid-angle pdf `pdf_l`) against BSDF sampling. A_CUTOUT_PATHS: light that reached+// the sample through a cutout (`shadow_hole`) is light this vertex's BSDF ray cannot bring, a cutout being opaque to+// it, unless it is a specular or coat sample that keeps the path mirror-like (`mirror_path`). The sample takes the+// BSDF ray's weight for the rest: 1 - (the share of pdf_bsdf that still tests cutouts) x (the BSDF ray's weight).+// (Until 2026-11-15 the weight was the plain heuristic: under a card cut out everywhere a uniform sky lit a Lambert+// plane with 18 % of its light.)+fn light_mis(pdf_l: f32, s: Surface, lb: Lobes, v: vec3<f32>, l: vec3<f32>) -> f32 {+ let pb = pdf_bsdf(s, lb, v, l);+ let w = power_heuristic(pdf_l, pb);+ if (!approx(A_CUTOUT_PATHS) || !has(F_MASKED) || !shadow_hole) { return w; }+ var see = 0.0;+ if ((cur_depth == 0u || mirror_path) && s.roughness < MIRROR_ROUGHNESS && pb > 0.0) {+ // pdf_bsdf's specular and coat terms.+ let nov = max(dot(s.n, v), 1e-4);+ let h = normalize(v + l);+ let noh = max(dot(s.n, h), 0.0);+ var p = (1.0 - lb.p_coat - lb.p_trans) * lb.p_spec * g1(lb.a2, nov) * d_ggx(lb.a2, noh) / (4.0 * nov);+ if (has(F_COAT) && lb.coat > 0.0) {+ let ca2 = lb.coat_alpha * lb.coat_alpha;+ p += lb.p_coat * g1(ca2, nov) * d_ggx(ca2, noh) / (4.0 * nov);+ }+ see = min(p / pb, 1.0);+ }+ return 1.0 - see * (1.0 - w);+}+fn basis(n: vec3<f32>) -> mat3x3<f32> {let sgn = select(-1.0, 1.0, n.z >= 0.0);let a = -1.0 / (sgn + n.z);@@ -2053,7 +2092,7 @@ fn hg_phase(g: f32, c: f32) -> f32 {let vis = light_vis(li.ext.w >= 0.5, o, l, INF, rng_sub(rng_dim(D_SUN_VIS), i));if (any(vis > vec3<f32>(0.0))) {let pdf_l = 1.0 / (2.0 * PI * (1.0 - cos_max));- sum += li.color.rgb * f * power_heuristic(pdf_l, pdf_bsdf(s, lb, v, l)) * gtint(vis);+ sum += li.color.rgb * f * light_mis(pdf_l, s, lb, v, l) * gtint(vis);}}}@@ -2083,7 +2122,7 @@ fn direct_light(s: Surface, lb: Lobes, v: vec3<f32>) -> vec3<f32> {if (any(vis > vec3<f32>(0.0))) {let pdf_l = 1.0 / (2.0 * PI * (1.0 - cos_max));let frac = split_frac;- let wgt = power_heuristic(pdf_l, pdf_bsdf(s, lb, v, l));+ let wgt = light_mis(pdf_l, s, lb, v, l);sum += li.color.rgb * f * wgt * gtint(vis);dl_spec += li.color.rgb * f * wgt * gtint(vis) * frac;}@@ -2129,7 +2168,7 @@ fn direct_light(s: Surface, lb: Lobes, v: vec3<f32>) -> vec3<f32> {let f = eval_bsdf(s, lb, v, es.l);if (any(f > vec3<f32>(0.0)) && !occluded_emitter(o, es.l, es.dist * 0.999, rng_dim(D_EMIT_VIS))) {let frac = split_frac;- let wgt = power_heuristic(es.pdf_w, pdf_bsdf(s, lb, v, es.l));+ let wgt = light_mis(es.pdf_w, s, lb, v, es.l);sum += es.le * f * wgt * es.w;dl_spec += es.le * f * wgt * es.w * frac;}@@ -2145,7 +2184,7 @@ fn direct_light(s: Surface, lb: Lobes, v: vec3<f32>) -> vec3<f32> {let pdf_e = env_pdf(l);if (pdf_e > 0.0 && !occluded_sky(o, l, rng_dim(D_ENV_VIS))) {let frac = split_frac;- let wgt = power_heuristic(pdf_e, pdf_bsdf(s, lb, v, l));+ let wgt = light_mis(pdf_e, s, lb, v, l);sum += env_radiance(l) * f * wgt / pdf_e * gtint(shadow_tint);dl_spec += env_radiance(l) * f * wgt / pdf_e * gtint(shadow_tint) * frac;}@@ -3567,7 +3606,7 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {straight = straight && through;cone_w = width;cone_a += 2.0 * s.roughness * s.roughness;- mirror_path = (depth == 0u || mirror_path) && s.roughness < 0.3 && (sampled_spec || through);+ mirror_path = (depth == 0u || mirror_path) && s.roughness < MIRROR_ROUGHNESS && (sampled_spec || through);if (depth >= P.counts.w) {let q = min(max(thr.x, max(thr.y, thr.z)), 0.95);if (rng1(rng_dim(D_RR)) > q) { break; }diff --git a/renderer/shaders/trace_hw.wgsl b/renderer/shaders/trace_hw.wgslindex 0925168..881d8bc 100644--- a/renderer/shaders/trace_hw.wgsl+++ b/renderer/shaders/trace_hw.wgsl@@ -37,7 +37,11 @@var t0 = 0.0;for (var k = 0u; k < steps; k++) {let h = trace_opaque(o + d * t0, d, tmax_in - t0, mask, false);- if (h.tri == 0xffffffffu) { return Hit(tmax_in, 0xffffffffu, 0.0, 0.0, NO_INST); }+ if (h.tri == 0xffffffffu) {+ // (A shadow ray comes here only past a cutout that let it through: `shadow_hole`.)+ if (mode != TRACE_CLOSEST) { shadow_hole = true; }+ return Hit(tmax_in, 0xffffffffu, 0.0, 0.0, NO_INST);+ }let t = t0 + h.t;if (is_proc(h) || !cutout(h.tri) || alpha_pass(h.tri, h.inst, h.u, h.v, t)) { return Hit(t, h.tri, h.u, h.v, h.inst); }t0 = t + 1e-4 * max(1.0, t);diff --git a/renderer/shaders/trace_units.wgsl b/renderer/shaders/trace_units.wgslindex be6af43..023e572 100644--- a/renderer/shaders/trace_units.wgsl+++ b/renderer/shaders/trace_units.wgsl@@ -98,7 +98,7 @@let x = intersect_tri(ro, rd, ti, hit.t);if (x.x < hit.t) {let flags = bitcast<u32>(tri_verts[ti * 3u].w);- if (has(F_MASKED) && (flags & TRI_MASKED) != 0u && !cutouts_opaque() && mode != TRACE_ANY_OPAQUE && !alpha_pass(ti, rec, x.y, x.z, x.x)) { continue; }+ if (has(F_MASKED) && (flags & TRI_MASKED) != 0u && !cutouts_opaque() && mode != TRACE_ANY_OPAQUE && !alpha_pass(ti, rec, x.y, x.z, x.x)) { shadow_hole = true; continue; }hit = Hit(x.x, ti, x.y, x.z, rec);if (mode != TRACE_CLOSEST) { return hit; }}diff --git a/tests/render/sky-cutout-bounce-live-exact.ts b/tests/render/sky-cutout-bounce-live-exact.tsnew file mode 100644index 0000000..998be46--- /dev/null+++ b/tests/render/sky-cutout-bounce-live-exact.ts@@ -0,0 +1,8 @@+// sky/cutout-bounce in the real-time renderer without `APPROX.cutoutPaths`: what the live view reads with no card+// at all, 0.21. (That is 16 % under the offline 0.25: the radiance cache's light off the floor, card or not.)+import def, { test as base } from "./sky-cutout-bounce.ts";+import { APPROX, type TestMeta } from "./lib.ts";++export const test: TestMeta = { ...base, id: "sky/cutout-bounce-live-exact", metric: { ...base.metric, expected: 0.21, tolerance: 0.01 }, temporal: true, live: 120, approx: APPROX.all & ~APPROX.cutoutPaths };++export default def;diff --git a/tests/render/sky-cutout-bounce-live.ts b/tests/render/sky-cutout-bounce-live.tsnew file mode 100644index 0000000..0c7fefa--- /dev/null+++ b/tests/render/sky-cutout-bounce-live.ts@@ -0,0 +1,8 @@+// sky/cutout-bounce in the real-time renderer as it ships: black. What is left of `APPROX.cutoutPaths`: the radiance+// cache's rays take the card as solid, their light samples too, so the floor it holds is unlit and sends nothing on.+import def, { test as base } from "./sky-cutout-bounce.ts";+import type { TestMeta } from "./lib.ts";++export const test: TestMeta = { ...base, id: "sky/cutout-bounce-live", metric: { ...base.metric, expected: 0 }, temporal: true, live: 120 };++export default def;diff --git a/tests/render/sky-cutout-bounce.ts b/tests/render/sky-cutout-bounce.tsnew file mode 100644index 0000000..b5c8631--- /dev/null+++ b/tests/render/sky-cutout-bounce.ts@@ -0,0 +1,25 @@+// Light that a cutout lets through and a surface sends on: a 100-nit sky over an albedo-0.5 Lambert floor (radiance+// 50), a card cut out everywhere at 1 m, and under it a 0.1 m albedo-0.5 slab at 0.5 m, seen from below. Its+// underside sees only the floor: 0.5 x 50 = 25, a quarter of the sky beside it (its own shadow on the floor takes+// under 1 % of that).+import { BoxGeometry, Mesh } from "@threert/scene";+import { clearCard } from "./shadow-cutout-emitter.ts";+import { camera, lambert, plane, result, sceneWithSky, type TestMeta } from "./lib.ts";++export const test: TestMeta = {+ id: "sky/cutout-bounce", features: ["sky", "alpha-test", "lambert", "gi"], reference: "analytic", temporal: false,+ metric: { kind: "ratio", a: [0.47, 0.47, 0.53, 0.53], b: [0.05, 0.05, 0.2, 0.2], expected: 0.25, tolerance: 0.01 },+ spp: 256,+ note: "the slab's underside against the sky seen past the card",+};++export default function () {+ const scene = sceneWithSky(100);+ const card = clearCard(400, 400);+ card.rotation.x = -Math.PI / 2;+ card.position.set(0, 1, 0);+ const slab = new Mesh(new BoxGeometry(0.1, 0.01, 0.1), lambert(0.5, "slab"));+ slab.name = "slab";+ slab.position.set(0, 0.505, 0);+ return result(scene, camera(scene, [0, 0.1, 0.001], [0, 0.5, 0], 40), [card, slab, plane(400, lambert(0.5))]);+}diff --git a/tests/render/sky-cutout-clear-live-exact.ts b/tests/render/sky-cutout-clear-live-exact.tsindex ed1c270..844984b 100644--- a/tests/render/sky-cutout-clear-live-exact.ts+++ b/tests/render/sky-cutout-clear-live-exact.ts@@ -1,5 +1,5 @@-// sky/cutout-clear in the real-time renderer without `APPROX.cutoutPaths` (as it ships, the rays a surface sends on-// take the card as opaque, and their share of the sky is lost): the offline value.+// sky/cutout-clear in the real-time renderer without `APPROX.cutoutPaths`: the rays the plane sends on pass the card+// too, and share the sky's light with its samples as they do offline.import def, { test as base } from "./sky-cutout-clear.ts";import { APPROX, type TestMeta } from "./lib.ts";diff --git a/tests/render/sky-cutout-clear-live.ts b/tests/render/sky-cutout-clear-live.tsnew file mode 100644index 0000000..5dde1a0--- /dev/null+++ b/tests/render/sky-cutout-clear-live.ts@@ -0,0 +1,9 @@+// sky/cutout-clear in the real-time renderer as it ships: the offline value. The sky's samples pass the cutout and+// keep the whole of its light there, because the rays the plane sends on take the card as solid+// (`APPROX.cutoutPaths`) and bring none of it (pathtrace.wgsl `light_mis`). Until 2026-11-15 this read 8.8.+import def, { test as base } from "./sky-cutout-clear.ts";+import type { TestMeta } from "./lib.ts";++export const test: TestMeta = { ...base, id: "sky/cutout-clear-live", metric: { ...base.metric, tolerance: 0.03 }, temporal: true, live: 120 };++export default def;