Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Keep the pixel where every wavelet weight is zero (#969)

Nguyen Xuan An94214d6ae0a9
diff --git a/changes/969-wavelet-nan.md b/changes/969-wavelet-nan.md
new file mode 100644
index 00000000..44223856
--- /dev/null
+++ b/changes/969-wavelet-nan.md
@@ -0,0 +1 @@
+- The wavelet denoiser keeps the input pixel where every neighbour weight is zero (background pixels with a zero normal), so the Cook cycles kernel no longer returns NaN there (#969).
diff --git a/src/path-tracer-denoise.js b/src/path-tracer-denoise.js
index eaa19b59..0d4d67eb 100644
--- a/src/path-tracer-denoise.js
+++ b/src/path-tracer-denoise.js
@@ -90,6 +90,14 @@ function filterPass({ width, height, light, variance, normal, depth }, step, out
blue += weight * light[neighbor * 3 + 2];
}
}
+ // Background pixels carry a zero normal, so every weight is zero: keep the pixel as it is instead of dividing 0 by 0.
+ if (!(weightSum > 0)) {
+ outputLight[center * 3] = light[center * 3];
+ outputLight[center * 3 + 1] = light[center * 3 + 1];
+ outputLight[center * 3 + 2] = light[center * 3 + 2];
+ outputVariance[center] = variance[center];
+ continue;
+ }
outputLight[center * 3] = red / weightSum;
outputLight[center * 3 + 1] = green / weightSum;
outputLight[center * 3 + 2] = blue / weightSum;
diff --git a/tests/path-tracer-denoise.test.js b/tests/path-tracer-denoise.test.js
new file mode 100644
index 00000000..b9c81375
--- /dev/null
+++ b/tests/path-tracer-denoise.test.js
@@ -0,0 +1,38 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { denoiseRadiance } from '../src/path-tracer-denoise.js';
+
+function buildBuffers(width, height, backgroundRows) {
+ const pixelCount = width * height;
+ const color = new Float32Array(pixelCount * 3);
+ const albedo = new Float32Array(pixelCount * 3).fill(0.5);
+ const normal = new Float32Array(pixelCount * 3);
+ const depth = new Float32Array(pixelCount);
+ const variance = new Float32Array(pixelCount).fill(0.01);
+ for (let pixel = 0; pixel < pixelCount; pixel++) {
+ const isBackground = Math.floor(pixel / width) < backgroundRows;
+ const value = 0.2 + 0.1 * ((pixel * 7) % 5);
+ color.fill(value, pixel * 3, pixel * 3 + 3);
+ if (isBackground) continue;
+ normal[pixel * 3 + 1] = 1;
+ depth[pixel] = 5;
+ }
+ return { width, height, color, albedo, normal, depth, variance };
+}
+
+test('wavelet filter keeps every pixel finite when background pixels have zero normal and depth', () => {
+ const buffers = buildBuffers(16, 16, 6);
+ const result = denoiseRadiance(buffers);
+ assert.equal(result.length, buffers.color.length);
+ for (let index = 0; index < result.length; index++) {
+ assert.ok(Number.isFinite(result[index]), `output ${index} is ${result[index]}`);
+ }
+});
+
+test('wavelet filter leaves a background pixel at its input colour', () => {
+ const buffers = buildBuffers(16, 16, 6);
+ const result = denoiseRadiance(buffers);
+ for (let channel = 0; channel < 3; channel++) {
+ assert.ok(Math.abs(result[channel] - buffers.color[channel]) < 1e-5);
+ }
+});