3D engine that AI agents drive
Render staged showcase scenes with --scene file (#1085)
Nguyen Xuan Anb54c433491ed
diff --git a/changes/1085.md b/changes/1085.mdnew file mode 100644index 00000000..cd4be253--- /dev/null+++ b/changes/1085.md@@ -0,0 +1 @@+- `--scene file` renders staged showcase scenes: each `glbRest` part loads with the shared textures and joins in the showcase page's rank order, so the airport and datacenter scenes film with every part (#1085)diff --git a/docs/learn/images-and-films.md b/docs/learn/images-and-films.mdindex 157070f4..6fe6aa71 100644--- a/docs/learn/images-and-films.md+++ b/docs/learn/images-and-films.md@@ -122,6 +122,8 @@ Install the checkout's dependencies and provide Chrome and FFmpeg. The exporter| `--assembly 1` | 0 to 1 | Renders an exploded object for inspection. || `--audio` | file | Selects original film audio. Other scenes default to silent. |+`--scene file --config scene.json [--root public] [--camera keys.json]` renders a showcase `scene.json`. A staged scene renders headless too: each `glbRest` path is routed like `glb`, its parts load with the first file's shared textures and join in the page's rank order, so a frame matches the page. `hdriPreview` and `glbFirstView` only speed up the page's first frame and are ignored. A `glbRest` that is not a list of paths fails with an error that names it.+The presets come from `RENDER_PRESETS`:| Preset | Size | Samples | fps | Budget |diff --git a/scripts/file-scene.mjs b/scripts/file-scene.mjsindex d8a4e8a0..5918df06 100644--- a/scripts/file-scene.mjs+++ b/scripts/file-scene.mjs@@ -64,5 +64,18 @@ export function prepareFileScene({ configPath, root }) {if (typeof description[field] !== 'string') throw new Error(`scene.json needs a "${field}" path.`);description[field] = route(description[field]);}+ routeStagedParts(description, route);return { description, assets };}++// A staged scene lists its other parts in "glbRest", routed like "glb". The first-view fields only speed up the page's first+// frame, so a headless render drops them.+function routeStagedParts(description, route) {+ if (description.glbRest !== undefined) {+ const isPathList = Array.isArray(description.glbRest) && description.glbRest.every(path => typeof path === 'string');+ if (!isPathList) throw new Error('scene.json "glbRest" must be a list of glb paths.');+ description.glbRest = description.glbRest.map(route);+ }+ delete description.hdriPreview;+ delete description.glbFirstView;+}diff --git a/src/file-scene.js b/src/file-scene.jsindex 7472542d..4b427c1c 100644--- a/src/file-scene.js+++ b/src/file-scene.js@@ -4,11 +4,28 @@ import { loadHdri } from './hdri.js';import { createCascadedShadows } from './cascaded-shadows.js';import { applyWaterReflection, attachWaterReflection } from './water-reflection.js';import { createCameraSampler } from './camera-track.js';+import { inWholeWorldOrder, readPartRanks } from './showcase-download.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// /__file/camera.json. The renderer options, sky, exposure and frame effects are read exactly as src/showcase.js reads them.+// Loads the "glbRest" files with the first file's shared textures and joins every file's parts in the order the showcase page+// uses, so a headless frame matches the page. A scene with no "glbRest" keeps the first file's parts as they are.+async function loadStagedParts(description, asset, model, sharedTextures) {+ const restPaths = description.glbRest ?? [];+ if (!restPaths.length) return asset.parts;+ const files = [{ parts: asset.parts, ranks: await readPartRanks(model) }];+ for (const restPath of restPaths) {+ const restModel = await fetch(restPath).then(response => response.blob());+ const baseUrl = new URL('.', new URL(restPath, location.href)).href;+ const rest = await loadGltf(restModel, { baseUrl, sharedTextures, ...(description.gltfOptions ?? {}) });+ files.push({ parts: rest.parts, ranks: await readPartRanks(restModel) });+ }+ await import('./replace-textured-parts.js');+ return inWholeWorldOrder(files);+}+async function readJson(url) {const response = await fetch(url);if (!response.ok) throw new Error(`${url} is not available (${response.status})`);@@ -19,12 +36,14 @@ async function start() {const description = await readJson('/__file/config.json');const cameraResponse = await fetch('/__file/camera.json');const camera = cameraResponse.ok ? createCameraSampler(await cameraResponse.json()) : null;- if (description.glbRest?.length || description.hdriPreview) throw new Error('Staged scene.json files (glbRest, hdriPreview) are not supported by --scene file yet.');const [model, map] = await Promise.all([fetch(description.glb).then(response => response.blob()),loadHdri(description.hdri, description.hdriOptions ?? {}),]);- const asset = await loadGltf(model, { baseUrl: new URL('.', new URL(description.glb, location.href)).href, sharedTextures: createSharedTextures(), ...(description.gltfOptions ?? {}) });+ const sharedTextures = createSharedTextures();+ 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 shadowCascades = rendererOptions.shadowCascades ? createCascadedShadows(rendererOptions.shadowCascades) : undefined;const reflection = description.waterReflection ? applyWaterReflection(asset.parts) : { parts: asset.parts, surface: null };diff --git a/src/showcase-download.js b/src/showcase-download.jsindex 08dd4e3b..de90f993 100644--- a/src/showcase-download.js+++ b/src/showcase-download.js@@ -77,3 +77,11 @@ export async function readPartRanks(blob) {if (!extras?.partRankRuns) return extras?.partRanks ?? null;return extras.partRankRuns.flatMap(([first, count]) => Array.from({ length: count }, (unused, offset) => first + offset));}++// Puts the parts of every file back in the order a one-piece load gives them, because the order sets how the renderer batches and+// draws them. Each file lists the rank of its parts in that order (the split script writes it); without ranks the files join as they are.+export function inWholeWorldOrder(files) {+ if (files.some(file => !file.ranks || file.ranks.length !== file.parts.length)) return files.flatMap(file => file.parts);+ const ranked = files.flatMap(file => file.parts.map((part, index) => ({ part, rank: file.ranks[index] })));+ return ranked.sort((first, second) => first.rank - second.rank).map(entry => entry.part);+}diff --git a/src/showcase.js b/src/showcase.jsindex 4964f8bb..07923280 100644--- a/src/showcase.js+++ b/src/showcase.js@@ -11,7 +11,7 @@ import { applyWaterReflection, attachWaterReflection } from './water-reflection.// The mirror redraws every second lit frame while the camera moves: about 1.3 ms off the orbiting port frame// (issue #504), and An saw no stutter in a side-by-side. A still camera keeps its picture either way.const WATER_REFLECTION_OPTIONS = { updateEvery: 2 };-import { createDownloadMeter, downloadBlob, readByteLength, readPartRanks } from './showcase-download.js';+import { createDownloadMeter, downloadBlob, inWholeWorldOrder, readByteLength, readPartRanks } from './showcase-download.js';// Each scene lives in public/showcase/<id>/scene.json: its GLB, sky, camera, renderer and frame options, and credits.// Optional fields: "bounds" ({ min, max } the camera target stays inside), "minDistance" and "maxDistance".@@ -236,14 +236,6 @@ function markPressedTab(id) {for (const button of tabs.querySelectorAll('button')) button.setAttribute('aria-pressed', String(button.dataset.scene === id));}-// Puts the parts of every file back in the order a one-piece load gives them, because the order sets how the renderer batches and-// draws them. Each file lists the rank of its parts in that order (the split script writes it); without ranks the files join as they are.-function inWholeWorldOrder(files) {- if (files.some(file => !file.ranks || file.ranks.length !== file.parts.length)) return files.flatMap(file => file.parts);- const ranked = files.flatMap(file => file.parts.map((part, index) => ({ part, rank: file.ranks[index] })));- return ranked.sort((first, second) => first.rank - second.rank).map(entry => entry.part);-}-// Adds the other parts of a staged world once they have downloaded, then swaps the first frame's small sky for the full one.async function finishWorld(world) {const { request, description, asset, sharedTextures, detachReflection, remainingDownloads, abort } = world;diff --git a/tests/file-scene-staged.test.js b/tests/file-scene-staged.test.jsnew file mode 100644index 00000000..e46bd3d1--- /dev/null+++ b/tests/file-scene-staged.test.js@@ -0,0 +1,54 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { mkdtemp, writeFile, mkdir } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { prepareFileScene } from '../scripts/file-scene.mjs';+import { inWholeWorldOrder, readPartRanks } from '../src/showcase-download.js';++async function writeStagedWorld(description) {+ const folder = await mkdtemp(join(tmpdir(), 'file-scene-staged-'));+ const worldFolder = join(folder, 'public/world');+ await mkdir(worldFolder, { recursive: true });+ for (const name of ['scene.glb', 'rest-1.glb', 'rest-2.glb', 'sky.hdr']) await writeFile(join(worldFolder, name), 'data');+ const config = join(folder, 'scene.json');+ await writeFile(config, JSON.stringify({ glb: '/world/scene.glb', hdri: '/world/sky.hdr', ...description }));+ return { root: join(folder, 'public'), config };+}++function glbWithRankRuns(partRankRuns) {+ const json = Buffer.from(JSON.stringify({ asset: { version: '2.0' }, scene: 0, scenes: [{ extras: { partRankRuns } }] }));+ const header = Buffer.alloc(20);+ header.writeUInt32LE(0x46546c67, 0);+ header.writeUInt32LE(2, 4);+ header.writeUInt32LE(20 + json.length, 8);+ header.writeUInt32LE(json.length, 12);+ header.writeUInt32LE(0x4e4f534a, 16);+ return new Blob([header, json]);+}++test('a staged scene routes every glbRest path like glb and drops the first-view fields', async () => {+ const { root, config } = await writeStagedWorld({+ glbRest: ['/world/rest-1.glb', '/world/rest-2.glb'],+ hdriPreview: '/world/sky-small.hdr',+ glbFirstView: [[0, 0, 0]],+ });+ const { description, assets } = prepareFileScene({ configPath: config, root });+ assert.deepEqual(description.glbRest, ['/__file/asset/2/rest-1.glb', '/__file/asset/3/rest-2.glb']);+ assert.equal(assets.length, 4);+ assert.equal('hdriPreview' in description, false);+ assert.equal('glbFirstView' in description, false);+});++test('a glbRest that is not a list of paths is refused by name', async () => {+ const { root, config } = await writeStagedWorld({ glbRest: '/world/rest-1.glb' });+ assert.throws(() => prepareFileScene({ configPath: config, root }), /"glbRest" must be a list/);+});++test('the parts of a two-file scene join in the rank order the files record', async () => {+ const firstFile = { parts: ['a0', 'a1', 'a2'], ranks: await readPartRanks(glbWithRankRuns([[0, 1], [3, 2]])) };+ const restFile = { parts: ['b0', 'b1'], ranks: await readPartRanks(glbWithRankRuns([[1, 2]])) };+ assert.deepEqual(firstFile.ranks, [0, 3, 4]);+ assert.deepEqual(inWholeWorldOrder([firstFile, restFile]), ['a0', 'b0', 'b1', 'a1', 'a2']);+ assert.deepEqual(inWholeWorldOrder([firstFile, { parts: ['b0'], ranks: null }]), ['a0', 'a1', 'a2', 'b0']);+});