3D engine that AI agents drive
Dim a rough dielectric's diffuse layer by the specular albedo, not mirror Fresnel (part of #215)
Nguyen Xuan An0a1eda3833ce
diff --git a/bench/reference/path-tracer.json b/bench/reference/path-tracer.jsonindex 0a3068c3..ef064782 100644--- a/bench/reference/path-tracer.json+++ b/bench/reference/path-tracer.json@@ -2,43 +2,43 @@"metric": "FLIP (LDR), mean error against Cycles, lower is better","scores": {"brick-flat": {- "pathtracer-vs-cycles": 0.1152,- "seconds": 23,+ "pathtracer-vs-cycles": 0.0246,+ "seconds": 28.2,"samples": 64,"averageSamples": 64,"ignored": []},"brick-graph": {- "pathtracer-vs-cycles": 0.1476,- "seconds": 23.6,+ "pathtracer-vs-cycles": 0.0805,+ "seconds": 24.3,"samples": 64,"averageSamples": 64,"ignored": []},"brick-room": {- "pathtracer-vs-cycles": 0.2727,- "seconds": 95.7,+ "pathtracer-vs-cycles": 0.2587,+ "seconds": 98.1,"samples": 64,"averageSamples": 64,"ignored": []},"brushed-film": {- "pathtracer-vs-cycles": 0.1524,- "seconds": 35.1,+ "pathtracer-vs-cycles": 0.0599,+ "seconds": 36.1,"samples": 64,"averageSamples": 64,"ignored": []},"car-paint": {- "pathtracer-vs-cycles": 0.1343,- "seconds": 35,+ "pathtracer-vs-cycles": 0.0324,+ "seconds": 35.8,"samples": 64,"averageSamples": 64,"ignored": []},"character": {- "pathtracer-vs-cycles": 0.1289,- "seconds": 27.6,+ "pathtracer-vs-cycles": 0.0287,+ "seconds": 28.1,"samples": 64,"averageSamples": 64,"ignored": [@@ -46,8 +46,8 @@]},"fog": {- "pathtracer-vs-cycles": 0.5623,- "seconds": 24.5,+ "pathtracer-vs-cycles": 0.5333,+ "seconds": 24,"samples": 64,"averageSamples": 64,"ignored": [@@ -55,8 +55,8 @@]},"foliage": {- "pathtracer-vs-cycles": 0.1753,- "seconds": 60.9,+ "pathtracer-vs-cycles": 0.1001,+ "seconds": 61.5,"samples": 64,"averageSamples": 64,"ignored": [@@ -64,57 +64,57 @@]},"glass": {- "pathtracer-vs-cycles": 0.1406,- "seconds": 30.4,+ "pathtracer-vs-cycles": 0.0385,+ "seconds": 30.7,"samples": 64,"averageSamples": 64,"ignored": []},"light-linking": {- "pathtracer-vs-cycles": 0.6241,- "seconds": 34.6,+ "pathtracer-vs-cycles": 0.6518,+ "seconds": 35.2,"samples": 64,"averageSamples": 64,"ignored": []},"lit-interior": {- "pathtracer-vs-cycles": 0.2167,- "seconds": 75.3,+ "pathtracer-vs-cycles": 0.0769,+ "seconds": 78.5,"samples": 64,"averageSamples": 64,"ignored": []},"long-street": {- "pathtracer-vs-cycles": 0.1818,- "seconds": 42.1,+ "pathtracer-vs-cycles": 0.0291,+ "seconds": 40.9,"samples": 64,"averageSamples": 64,"ignored": []},"material-spheres": {- "pathtracer-vs-cycles": 0.1383,- "seconds": 43.9,+ "pathtracer-vs-cycles": 0.0449,+ "seconds": 45.1,"samples": 64,"averageSamples": 64,"ignored": []},"metal": {- "pathtracer-vs-cycles": 0.1492,- "seconds": 31.4,+ "pathtracer-vs-cycles": 0.0439,+ "seconds": 31.9,"samples": 64,"averageSamples": 64,"ignored": []},"outdoor-hdri": {- "pathtracer-vs-cycles": 0.1242,- "seconds": 18.9,+ "pathtracer-vs-cycles": 0.0226,+ "seconds": 19.5,"samples": 64,"averageSamples": 64,"ignored": []},"spot-area": {- "pathtracer-vs-cycles": 0.0658,- "seconds": 26,+ "pathtracer-vs-cycles": 0.0261,+ "seconds": 26.4,"samples": 64,"averageSamples": 64,"ignored": []diff --git a/changes/issue-215-path-tracer-sky.md b/changes/issue-215-path-tracer-sky.mdnew file mode 100644index 00000000..3ea970a8--- /dev/null+++ b/changes/issue-215-path-tracer-sky.md@@ -0,0 +1 @@+- Fix the path tracer drawing rough dielectric surfaces too dark at grazing views (part of #215): the diffuse layer was dimmed by the mirror Fresnel term of the view angle, so a rough floor lost most of its light toward the horizon (`outdoor-hdri` floor 0.21 against Cycles' 0.47 near the horizon, 0.42 against 0.47 in the front). The diffuse layer now loses only the specular layer's directional albedo for the surface roughness (Karis's fit of the split-sum BRDF), as Cycles' Principled BSDF does. The capped one-sun sky was not the cause: a floor lit from above measures 0.466 in the tracer, the closed form gives 0.458 and Cycles 0.468. FLIP against Cycles in `bench/reference/path-tracer.json` falls on 14 of 16 scenes (`outdoor-hdri` 0.1242 to 0.0226, `character` 0.1289 to 0.0287, `material-spheres` 0.1383 to 0.0449, `glass` 0.1406 to 0.0385, `foliage` 0.1753 to 0.1001, `lit-interior` 0.2167 to 0.0769, `long-street` 0.1818 to 0.0291). `light-linking` rises from 0.6241 to 0.6518 because the tracer still ignores `affects` and now lights every part more brightly.diff --git a/src/path-tracer-shading.js b/src/path-tracer-shading.jsindex 831eb289..1ea2942b 100644--- a/src/path-tracer-shading.js+++ b/src/path-tracer-shading.js@@ -203,6 +203,19 @@ export function createSurface() {};}+/**+ * The share of light a dielectric's GGX specular layer reflects from a view direction (Karis's analytic fit of the+ * split-sum environment BRDF). A rough layer reflects far less than the mirror Fresnel term at a grazing view, so the+ * diffuse layer below keeps more of its light, as Cycles' Principled BSDF does.+ */+function dielectricSpecularAlbedo(normalReflectance, roughness, viewCosine) {+ const smoothness = 1 - roughness;+ const slope = Math.min(smoothness * smoothness, 2 ** (-9.28 * viewCosine)) * smoothness + (0.0425 - 0.0275 * roughness);+ const scale = 1.04 - 0.572 * roughness - 1.04 * slope;+ const offset = 1.04 * slope + 0.022 * roughness - 0.04;+ return Math.min(1, Math.max(0, normalReflectance * scale + offset));+}+/*** Sets the lobes of a hit. `view` points from the surface toward the viewer and `normal` faces it. A dielectric keeps* the light its reflection did not take, so the diffuse colour shrinks with the Fresnel term, and a clear coat takes@@ -217,7 +230,7 @@ export function prepareSurface(surface, material, metallic, roughness, baseColorsurface.viewCosine = viewCosine;const ratio = (material.ior - 1) / (material.ior + 1);const dielectricNormalReflectance = ratio * ratio;- const dielectricFresnel = schlick(dielectricNormalReflectance, viewCosine);+ const dielectricFresnel = dielectricSpecularAlbedo(dielectricNormalReflectance, roughness, viewCosine);surface.coatWeight = material.clearcoat > 0 ? material.clearcoat * schlick(DIELECTRIC_COAT_REFLECTANCE, viewCosine) : 0;const underCoat = 1 - surface.coatWeight;const diffuseShare = (1 - metallic) * (1 - dielectricFresnel) * underCoat;diff --git a/tests/path-tracer.test.js b/tests/path-tracer.test.jsindex 2a5370a3..8d144973 100644--- a/tests/path-tracer.test.js+++ b/tests/path-tracer.test.js@@ -113,6 +113,16 @@ test('a floor lit by an overhead sun matches the diffuse closed form', async ()assert.ok(Math.abs(linear[centre] - expected) / expected < 0.05, `got ${linear[centre]}, expected near ${expected}`);});+test('a rough floor keeps its diffuse light when seen at a grazing angle', async () => {+ const GRAZING = { eye: [0, 0.3, 6], target: [0, 0, 0], up: [0, 1, 0], fov: 10 };+ const options = { width: 16, height: 16, samples: 32, bounces: 1, denoise: false, threads: 1 };+ const down = await renderPathTracedImage({ parts: FLOOR_ONLY, environment: OVERHEAD_SUN, camera: LOOKING_DOWN }, options);+ const grazing = await renderPathTracedImage({ parts: FLOOR_ONLY, environment: OVERHEAD_SUN, camera: GRAZING }, options);+ const centre = (8 * 16 + 8) * 3;+ // Cycles does not dim a rough dielectric's diffuse layer toward the horizon: its specular layer reflects little there.+ assert.ok(grazing.linear[centre] > 0.9 * down.linear[centre], `grazing ${grazing.linear[centre]} against ${down.linear[centre]} looking down`);+});+test('a block between the sun and the floor leaves a shadow', async () => {const blocker = { name: 'block', geometry: box(0, 1), matrix: placed([0, 1, 0], [2, 0.2, 2]), material: surface([0.5, 0.5, 0.5]) };// Sun from +x at 45 degrees: the block (x from -1 to 1, 1 m up) shades the floor from x = -2 to 0. Seen from above,