3D engine that AI agents drive
Keep the plain environment table when compensation leaves only rounding residue (#897)
Nguyen Xuan Anf2b6350826fa
diff --git a/packages/render-native/pack-input-v2.mjs b/packages/render-native/pack-input-v2.mjsindex 44c60bd0..5b06c921 100644--- a/packages/render-native/pack-input-v2.mjs+++ b/packages/render-native/pack-input-v2.mjs@@ -1349,11 +1349,13 @@ export function resolveEnvCompensationFraction(envCompensation, callerName) {return envCompensation;}+const MINIMUM_COMPENSATED_TABLE_FRACTION = 1e-6;+/*** MIS compensation of the environment sampling table (Karlik et al. 2019; pbrt-v4 ImageInfiniteLight, Apache-2.0).* Subtracts `fraction` times the sphere-mean radiance from every texel's weight and clamps at zero, so light sampling* spends its samples where BSDF sampling does badly. The kernel reads the pdf from the same table, so the pdf used in- * MIS stays the pdf that was sampled. A map that would end up all zero keeps its original table.+ * MIS stays the pdf that was sampled. A map that would end up all zero, or nearly so, keeps its original table.* Rewrites `pixelWeights` and `rowTotals` in place and returns the new total.*/function compensateEnvironmentWeights(pixelWeights, rowTotals, width, height, fraction, originalTotal) {@@ -1381,7 +1383,9 @@ function compensateEnvironmentWeights(pixelWeights, rowTotals, width, height, frcompensatedRowTotals[y] = rowSum;compensatedTotal += rowSum;}- if (!(compensatedTotal > 0)) {+ // A constant map leaves only rounding residue (about 1e-16 of the total) after the subtraction, and a table built+ // from that residue would sample an arbitrary patch of the sky, so anything this small counts as all zero.+ if (!(compensatedTotal > originalTotal * MINIMUM_COMPENSATED_TABLE_FRACTION)) {return originalTotal;}pixelWeights.set(compensatedWeights);diff --git a/tests/native-pack-v2.test.js b/tests/native-pack-v2.test.jsindex d0762c87..ba9e592d 100644--- a/tests/native-pack-v2.test.js+++ b/tests/native-pack-v2.test.js@@ -842,6 +842,17 @@ test('envCompensation keeps the original table for a uniform map and rejects badconst plain = buildNativeInputV2(scene, camera, base);const compensated = buildNativeInputV2(scene, camera, { ...base, envCompensation: true });assert.deepEqual(compensated.environmentTexels, plain.environmentTexels, 'an all-zero table falls back to the plain table');+ for (const [sweepWidth, sweepHeight] of [[16, 8], [32, 16], [64, 32]]) {+ for (const radiance of [0.123, 0.2, 0.5, 1, 3.7]) {+ for (const intensity of [1, 2.5]) {+ const constantMap = createEnvironmentMap({ width: sweepWidth, height: sweepHeight, data: new Float32Array(sweepWidth * sweepHeight * 3).fill(radiance), intensity });+ const constantScene = buildTraceScene(parts, { map: constantMap });+ const plainInput = buildNativeInputV2(constantScene, camera, base);+ const compensatedInput = buildNativeInputV2(constantScene, camera, { ...base, envCompensation: true });+ assert.deepEqual(compensatedInput.environmentTexels, plainInput.environmentTexels, `constant map ${sweepWidth}x${sweepHeight}, radiance ${radiance}, intensity ${intensity}`);+ }+ }+ }for (const invalid of [-0.1, 1.5, NaN, '1', null]) {assert.throws(() => validatePackOptions({ ...base, envCompensation: invalid }, 'traceNative', true), /envCompensation/, `envCompensation ${JSON.stringify(invalid)}`);}