Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Path tracers: step through up to 64 transparent cutout texels, not 8 (issue #626)

Nguyen Xuan An72ca27c457be
diff --git a/changes/626-foliage-gap.md b/changes/626-foliage-gap.md
new file mode 100644
index 00000000..396c02d0
--- /dev/null
+++ b/changes/626-foliage-gap.md
@@ -0,0 +1 @@
+- Fixed: the CPU, WebGL2 and WebGPU path tracers gave up after 8 transparent cutout texels on one ray, so dense foliage showed holes and lost shadows; the limit is now 64, and the foliage reference scene matches the native tracer to FLIP 0.001
diff --git a/src/path-tracer-gpu-shader.js b/src/path-tracer-gpu-shader.js
index 15e25e1e..395afaec 100644
--- a/src/path-tracer-gpu-shader.js
+++ b/src/path-tracer-gpu-shader.js
@@ -151,7 +151,7 @@ const int LIGHT_TREE_FIRST_HIT_LIGHTS = ${LIGHT_TREE_FIRST_HIT_LIGHTS};
const int LIGHT_TREE_TEXELS = ${LIGHT_TREE_NODE_TEXELS};
// The real-time kernel picks one emitter at the first hit too (the denoiser averages the choices over frames) instead of tracing a shadow ray to every emitter.
const bool STOCHASTIC_FIRST_HIT = ${radianceCache};
-const int ALPHA_STEP_LIMIT = 8;
+const int ALPHA_STEP_LIMIT = 64;
const int RUSSIAN_ROULETTE_START = 3;
const float MINIMUM_ALPHA = 1e-3;
const float MIRROR_ALPHA = 2e-3;
diff --git a/src/path-tracer-webgpu-kernel.js b/src/path-tracer-webgpu-kernel.js
index 251c20a0..277bb8ca 100644
--- a/src/path-tracer-webgpu-kernel.js
+++ b/src/path-tracer-webgpu-kernel.js
@@ -21,7 +21,7 @@ const TRIANGLE_TEXELS: i32 = 9;
const MATERIAL_TEXELS: i32 = 10;
const LIGHT_TEXELS: i32 = 6;
const STACK_SIZE: i32 = 128;
-const ALPHA_STEP_LIMIT: i32 = 8;
+const ALPHA_STEP_LIMIT: i32 = 64;
const RUSSIAN_ROULETTE_START: i32 = 3;
const MINIMUM_ALPHA: f32 = 1e-3;
const MIRROR_ALPHA: f32 = 2e-3;
diff --git a/src/path-tracer.js b/src/path-tracer.js
index f5721f48..9a496906 100644
--- a/src/path-tracer.js
+++ b/src/path-tracer.js
@@ -30,7 +30,7 @@ export const PATH_TRACER_LIMITS = Object.freeze({
});
export const MAX_TRACE_PIXELS = 2_073_600;
export const MAX_TRACE_RAY_WORK = 4_000_000_000;
-const ALPHA_STEP_LIMIT = 8;
+const ALPHA_STEP_LIMIT = 64;
const WORKER_BAND_ROWS = 8;
// Held in a constant so the bundler leaves the URL alone: the worker is its own package entry, built beside this file.
const WORKER_FILE = './path-tracer-worker.js';
diff --git a/tests/path-tracer.test.js b/tests/path-tracer.test.js
index 43507ae1..a3e09706 100644
--- a/tests/path-tracer.test.js
+++ b/tests/path-tracer.test.js
@@ -322,3 +322,22 @@ test('adaptive bands split anywhere give the pixels of the whole image', () => {
test('the noise threshold is validated', () => {
for (const noiseThreshold of [-0.1, 1.5, Number.NaN, '0.1']) assert.throws(() => normalizePathTracerOptions({ noiseThreshold }), /noiseThreshold/);
});
+
+test('a camera ray and a shadow ray pass through a deep stack of transparent cutouts', async () => {
+ const transparentTexel = { image: { data: new Uint8Array([255, 255, 255, 0]), width: 1, height: 1 }, colorSpace: 'linear' };
+ const floor = { name: 'floor', geometry: floorQuad(), material: surface([0.5, 0.5, 0.5], { roughness: 1 }) };
+ const layerCount = 24;
+ const layers = Array.from({ length: layerCount }, (unused, layer) => ({
+ name: `layer-${layer}`,
+ geometry: floorQuad(),
+ matrix: placed([0, 0.1 + layer * 0.05, 0], [1, 1, 1]),
+ material: surface([1, 1, 1], { map: transparentTexel, alphaMode: 'mask', alphaCutoff: 0.5, doubleSided: true }),
+ }));
+ const options = { width: 16, height: 16, samples: 4, bounces: 1, denoise: false, threads: 1 };
+ const scene = parts => ({ parts, environment: OVERHEAD_SUN, camera: LOOKING_DOWN });
+ const plain = await renderPathTracedImage(scene([floor]), options);
+ const stacked = await renderPathTracedImage(scene([floor, ...layers]), options);
+ const centre = (8 * 16 + 8) * 3;
+ assert.ok(plain.linear[centre] > 0.1, 'the plain floor is lit');
+ assert.equal(stacked.linear[centre], plain.linear[centre], 'transparent layers change neither the hit nor the shadow');
+});