Skip to content

3D engine that AI agents drive

Pick levels at 0.5 pixels of error by default in scene files (#2144)

Nguyen Xuan An93c477d81bba
diff --git a/bench/glaze-passes/harness.js b/bench/glaze-passes/harness.js
index b0ee4b9f..c9d0a67b 100644
--- a/bench/glaze-passes/harness.js
+++ b/bench/glaze-passes/harness.js
@@ -2,7 +2,7 @@
import { CrucibleRenderer } from '../../src/renderer.js';
import { createSharedTextures, loadGltf } from '../../src/gltf.js';
import { loadHdri } from '../../src/hdri.js';
-import { addLodsToDenseParts } from '../../src/dense-lods.js';
+import { addLodsToDenseParts, withSceneFileRendererDefaults } from '../../src/dense-lods.js';
import { createCascadedShadows } from '../../src/cascaded-shadows.js';
const WARMUP_FRAMES = 20;
@@ -123,7 +123,7 @@ window.measureScene = async function measureScene({ description, glbUrl, hdriUrl
loadHdri(hdriUrl, description.hdriOptions ?? {}),
]);
const asset = await loadGltf(model, { baseUrl: new URL('.', new URL(glbUrl, location.href)).href, sharedTextures: createSharedTextures(), ...(description.gltfOptions ?? {}) });
- const rendererOptions = description.renderer ?? {};
+ const rendererOptions = withSceneFileRendererDefaults(description.renderer);
const shadowCascades = rendererOptions.shadowCascades ? createCascadedShadows(rendererOptions.shadowCascades) : undefined;
const lodStarted = performance.now();
const renderedParts = denseLods ? addLodsToDenseParts(asset.parts, { pixels: width * height, minimumTriangles: denseLodsMinimumTriangles }) : asset.parts;
diff --git a/changes/2144.md b/changes/2144.md
index caef251a..8e1c11ba 100644
--- a/changes/2144.md
+++ b/changes/2144.md
@@ -1 +1,2 @@
- A scene of many parts with a few thousand triangles each, such as Lone Monk, now gets levels of detail by default: `addLodsToDenseParts` starts at 4000 triangles instead of 20000, which drew 1.06 instead of 1.80 million triangles and took a courtyard view from 4.36 to 3.38 ms on the GPU (a slight change in far detail; #2144). `bench/glaze-passes` can write a camera move and measure the shimmer and level pops of it (#2157).
+- A scene file now picks levels of detail at `lodPixelError` 0.5 instead of 1 (about 21 percent more triangles than at 1 on Lone Monk, less shimmer in a camera move); `"renderer": { "lodPixelError": 1 }` restores the old choice (#2157).
diff --git a/docs/culling-lod.md b/docs/culling-lod.md
index a7e548c8..77018296 100644
--- a/docs/culling-lod.md
+++ b/docs/culling-lod.md
@@ -60,7 +60,7 @@ A scan with about one triangle per pixel or more is shaded once per sample at ne
`addLodsToDenseParts(parts, { pixels })` from `src/dense-lods.js` gives levels to the parts of a scene that has more than one triangle per pixel (`pixels` is the canvas width times its height times the pixel ratio squared). A part gets levels when it has at least `minimumTriangles` (default 4000, so a scene of many meshes with a few thousand triangles each gets them too, #2144, #2157) triangles and can take them; parts with the same geometry share one set. It returns `parts` itself for a scene under the density. It is not applied by itself, and it takes seconds on a million triangles, so call it once while loading. On the Monster scene (1.6 million triangles, 1280 by 720, 4 samples) called with the canvas size, the frame went from 5.8 to 1.9 ms, and Lone Monk (5.6 million) from 15.4 to 9.5 ms (measured before the error metric was changed to a distance); the picture changes slightly (root mean square 2.0 of 255), because far parts draw from coarser levels (#1163).
-A scene file (`scene.json`, rendered by `scripts/render-media.mjs --scene file`) gets the same levels by default; `"renderer": { "denseLods": false }` keeps every triangle of a dense scan. With the option on, `partsForRendererOptions` in `src/dense-lods.js` calls `addLodsToDenseParts` at the canvas size and pixel ratio before the renderer is built (#1163).
+A scene file (`scene.json`, rendered by `scripts/render-media.mjs --scene file`) gets the same levels by default; `"renderer": { "denseLods": false }` keeps every triangle of a dense scan. A scene file also picks levels at `lodPixelError` 0.5, not the renderer's 1, so far detail shimmers less; `"renderer": { "lodPixelError": 1 }` restores the old choice (#2157). With the option on, `partsForRendererOptions` in `src/dense-lods.js` calls `addLodsToDenseParts` at the canvas size and pixel ratio before the renderer is built (#1163).
On Lone Monk seen from the courtyard (a camera that shows the scene, 1280 by 720, 4 samples), drawing from the levels of parts of 4000 triangles and up, not 20000 and up, sent 1.06 million triangles instead of 1.80 million and cut the GPU frame from 4.36 to 3.38 ms (1500 and up: 3.51 to 3.62 ms, 8000 and up: 3.30 ms) (no levels at all: 3.26 million triangles, 7.12 ms). The picture differs from the 20000 floor by a root mean square of 1.74 of 255 (2.51 from no levels). `node bench/glaze-passes/run.mjs <out.json> <scene directories> whole '{"denseLods":true,"denseLodsMinimumTriangles":4000}'` measures it; `'{"engine":"three"}'` draws the same file with three.js, and `bench/glaze-passes/still-pair.mjs` writes the picture pair and the RMSE (#2144).
diff --git a/src/dense-lods.js b/src/dense-lods.js
index 4bf978ed..3005f2a3 100644
--- a/src/dense-lods.js
+++ b/src/dense-lods.js
@@ -9,6 +9,14 @@ import { DENSE_TRIANGLES_PER_PIXEL, partTakesLods } from './renderer.js';
// Meshes of a few thousand triangles, many of them, are as dense as a few huge scans, so the floor sits low (#2144). Lone Monk draws as fast from 4000 as from 1500 and shimmers no more (#2157).
export const DEFAULT_MINIMUM_TRIANGLES = 4000;
+/** A scene file picks a level at this many pixels of error (CrucibleRenderer's own default is 1); `renderer.lodPixelError` overrides it (#2157). */
+export const SCENE_FILE_LOD_PIXEL_ERROR = 0.5;
+
+/** The renderer options of a scene file with the scene-file defaults under them. */
+export function withSceneFileRendererDefaults(rendererOptions = {}) {
+ return { lodPixelError: SCENE_FILE_LOD_PIXEL_ERROR, ...rendererOptions };
+}
+
/** Triangles over pixels for these parts on a canvas of `pixels` pixels (the canvas size times the pixel ratio squared). */
export function trianglesPerPixel(parts, pixels) {
if (!(pixels > 0)) throw new RangeError('pixels must be the canvas width times its height times the pixel ratio squared, a positive number.');
diff --git a/src/file-scene.js b/src/file-scene.js
index edafb312..161ad5b9 100644
--- a/src/file-scene.js
+++ b/src/file-scene.js
@@ -6,7 +6,7 @@ import { applyWaterReflection, attachWaterReflection } from './water-reflection.
import { createCameraSampler } from './camera-track.js';
import { inWholeWorldOrder, readPartRanks } from './showcase-download.js';
import { withinDrawnLightLimits } from './drawn-light-lists.js';
-import { partsForRendererOptions } from './dense-lods.js';
+import { partsForRendererOptions, withSceneFileRendererDefaults } from './dense-lods.js';
// Headless twin of the showcase page for scripts/render-media.mjs (--scene file). The export script serves the scene.json
// at /__file/config.json with every asset path already pointing at a file it can read, and an optional camera track at
@@ -55,7 +55,7 @@ async function start() {
const gltfOptions = description.gltfOptions ?? {};
const asset = await loadGltf(model, { baseUrl: new URL('.', new URL(description.glb, location.href)).href, sharedTextures, ...gltfOptions });
asset.parts = await loadStagedParts(description, asset, model, sharedTextures);
- const rendererOptions = description.renderer ?? {};
+ const rendererOptions = withSceneFileRendererDefaults(description.renderer);
const shadowCascades = rendererOptions.shadowCascades ? createCascadedShadows(rendererOptions.shadowCascades) : undefined;
const reflection = description.waterReflection ? applyWaterReflection(asset.parts) : { parts: asset.parts, surface: null };
const viewport = document.querySelector('#file-viewport');
diff --git a/tests/dense-lods-option.test.js b/tests/dense-lods-option.test.js
index aea1fa69..95eee6e5 100644
--- a/tests/dense-lods-option.test.js
+++ b/tests/dense-lods-option.test.js
@@ -2,7 +2,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { sphere } from '../src/geometry.js';
-import { partsForRendererOptions } from '../src/dense-lods.js';
+import { partsForRendererOptions, SCENE_FILE_LOD_PIXEL_ERROR, withSceneFileRendererDefaults } from '../src/dense-lods.js';
function scanPart() {
return { name: 'scan', geometry: sphere(200, 100), matrix: new Float32Array(16), material: { tag: 'surface', color: [0.5, 0.5, 0.5], metallic: 0, roughness: 0.5 } };
@@ -41,3 +41,11 @@ test('the pixel ratio counts: the same scan is sparse on a big canvas', () => {
test('denseLods must be a boolean, and the error says so', () => {
assert.throws(() => partsForRendererOptions([scanPart()], SMALL_CANVAS, { denseLods: 'yes' }), /renderer\.denseLods must be true or false/);
});
+
+test('a scene file picks levels at 0.5 pixels of error unless its renderer says otherwise (#2157)', () => {
+ assert.equal(SCENE_FILE_LOD_PIXEL_ERROR, 0.5);
+ assert.equal(withSceneFileRendererDefaults().lodPixelError, 0.5);
+ assert.equal(withSceneFileRendererDefaults({ samples: 2 }).lodPixelError, 0.5);
+ assert.equal(withSceneFileRendererDefaults({ lodPixelError: 2 }).lodPixelError, 2);
+ assert.equal(withSceneFileRendererDefaults({ samples: 2 }).samples, 2);
+});