3D engine that AI agents drive
Stop the CPU, WebGL2 and WebGPU shadow rays leaking through cutouts (issue #626)
Nguyen Xuan Anceadfb8d3ff1
diff --git a/changes/626-cutouts.md b/changes/626-cutouts.mdindex 8172f3c7..464412d4 100644--- a/changes/626-cutouts.md+++ b/changes/626-cutouts.md@@ -1 +1,2 @@- The native tracer asks the hardware about alpha cutouts itself: cutout triangles are their own geometry and the kernel confirms a candidate hit when its colour map alpha reaches the cutoff, so rays no longer step through transparent texels (8 steps at most before).+- Shadow rays on the CPU, WebGL2 and WebGPU tracers no longer leak light through dense alpha cutouts. Their any-hit walk took the first hit traversal met as if it were the nearest, so a transparent texel far along the ray let it skip closer leaves. A solid any-hit still ends the walk; a transparent one now retraces for nearest hits. On 300 masked leaf cards the four backends agree within FLIP 0.0004, down from 0.043 between native and the rest.diff --git a/src/path-tracer-gpu-shader.js b/src/path-tracer-gpu-shader.jsindex 1f3a3aa2..15e25e1e 100644--- a/src/path-tracer-gpu-shader.js+++ b/src/path-tracer-gpu-shader.js@@ -523,13 +523,19 @@ float alphaAtNearestHit() {bool occluded(vec3 origin, vec3 direction, float limit) {vec3 start = origin;float remaining = limit;+ // A solid any-hit shadows the point wherever it lies; a transparent one hides what sits before it, so retrace for nearest hits.+ bool anyHit = true;for (int attempt = 0; attempt < ALPHA_STEP_LIMIT; attempt++) {- traceRay(start, direction, remaining, true);+ traceRay(start, direction, remaining, anyHit);if (nearestSlot < 0) return false;int material = int(fetchData(uTriangles, nearestSlot * TRIANGLE_TEXELS).w) * MATERIAL_TEXELS;float cutoff = fetchData(uMaterials, material + 3).z;if (cutoff == 0.0) return true;if (alphaAtNearestHit() >= cutoff) return true;+ if (anyHit) {+ anyHit = false;+ continue;+ }float advance = nearestDistance + uSurfaceOffset;remaining -= advance;if (remaining <= 0.0) return false;diff --git a/src/path-tracer-webgpu-kernel.js b/src/path-tracer-webgpu-kernel.jsindex 16aa8428..0e6bc4b8 100644--- a/src/path-tracer-webgpu-kernel.js+++ b/src/path-tracer-webgpu-kernel.js@@ -363,13 +363,19 @@ fn occluded(origin: vec3<f32>, direction: vec3<f32>, limit: f32) -> bool {var start = origin;var remaining = limit;let surfaceOffset = params.environmentRotationAndRays.z;+ // A solid any-hit shadows the point wherever it lies; a transparent one hides what sits before it, so retrace for nearest hits.+ var anyHit = true;for (var attempt = 0; attempt < ALPHA_STEP_LIMIT; attempt++) {- traceRay(start, direction, remaining, true);+ traceRay(start, direction, remaining, anyHit);if (nearestSlot < 0) { return false; }let material = i32(triangles[nearestSlot * TRIANGLE_TEXELS].w) * MATERIAL_TEXELS;let cutoff = materials[material + 3].z;if (cutoff == 0.0) { return true; }if (alphaAtNearestHit() >= cutoff) { return true; }+ if (anyHit) {+ anyHit = false;+ continue;+ }let advance = nearestDistance + surfaceOffset;remaining -= advance;if (remaining <= 0.0) { return false; }diff --git a/src/path-tracer.js b/src/path-tracer.jsindex 3ba53726..f5721f48 100644--- a/src/path-tracer.js+++ b/src/path-tracer.js@@ -407,13 +407,20 @@ function occluded(scene, originX, originY, originZ, directionX, directionY, direlet startY = originY;let startZ = originZ;let remaining = limit;+ // An any-hit trace returns the first hit traversal meets, not the nearest. A solid one shadows the point wherever it lies;+ // a transparent one says nothing about what sits before it, so the walk then retraces for nearest hits and steps through.+ let anyHit = true;for (let attempt = 0; attempt < ALPHA_STEP_LIMIT; attempt++) {- const found = scene.tracer.trace(startX, startY, startZ, directionX, directionY, directionZ, remaining, true);+ const found = scene.tracer.trace(startX, startY, startZ, directionX, directionY, directionZ, remaining, anyHit);if (found.nearestTriangle < 0) return false;// Only a cutout material needs its texture read to know whether the ray passes through: skip the interpolation for the rest.if (scene.materials[scene.triangleMaterial[found.nearestTriangle]].alphaCutoff === 0) return true;const alpha = fillHit(scene, found.nearestTriangle, startX, startY, startZ, directionX, directionY, directionZ, found.nearestDistance, hit);if (alpha >= hit.material.alphaCutoff) return true;+ if (anyHit) {+ anyHit = false;+ continue;+ }const advance = found.nearestDistance + scene.surfaceOffset;remaining -= advance;if (remaining <= 0) return false;