3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Pin the scene-file clip wiring and water scenes with tests (#2308)
Nguyen Xuan And3e74441ced7
diff --git a/src/file-scene-clip.js b/src/file-scene-clip.jsindex 0c94c29c..45c55a41 100644--- a/src/file-scene-clip.js+++ b/src/file-scene-clip.js@@ -24,3 +24,14 @@ export function createFileSceneClipPlayer({ asset, posedParts, sceneParts, rende},};}++// The frame function of a scene-file page: it poses the clip (when the GLB has one) and then renders at the camera's pose for the+// time. `camera` is a camera sampler or null; without one the scene file's own eye and target are used.+export function createFileSceneRenderAt({ asset, posedParts, sceneParts, rendererParts, renderer, camera, description, frame }) {+ const clipPlayer = createFileSceneClipPlayer({ asset, posedParts, sceneParts, rendererParts });+ return seconds => {+ clipPlayer?.poseAt(renderer, seconds);+ const pose = camera ? camera.sample(seconds) : { eye: description.eye, target: description.target };+ renderer.render({ ...frame, ...pose });+ };+}diff --git a/src/file-scene.js b/src/file-scene.jsindex 13968932..ff89b8ba 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 { createFileSceneClipPlayer } from './file-scene-clip.js';+import { createFileSceneRenderAt } from './file-scene-clip.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@@ -71,12 +71,7 @@ async function start() {attachWaterReflection(renderer, reflection.surface, { updateEvery: 1 });const frame = { exposure: description.exposure ?? 1, ...(description.frame ?? {}) };// A GLB with an animation clip plays its first clip on a loop, as the showcase page does; one without a clip is drawn as before.- const clipPlayer = createFileSceneClipPlayer({ asset, posedParts: assetOwnParts, sceneParts: asset.parts, rendererParts });- const renderAt = seconds => {- clipPlayer?.poseAt(renderer, seconds);- const pose = camera ? camera.sample(seconds) : { eye: description.eye, target: description.target };- renderer.render({ ...frame, ...pose });- };+ const renderAt = createFileSceneRenderAt({ asset, posedParts: assetOwnParts, sceneParts: asset.parts, rendererParts, renderer, camera, description, frame });// `parts` and `environment` let scripts/render-file-native.mjs pack the same scene for the native path tracer.window.__scene = { renderer, duration: camera?.duration, renderAt, pause() {}, parts: reflection.parts, environment: { ...(rendererOptions.environment ?? {}), map } };}diff --git a/tests/file-scene-clip.test.js b/tests/file-scene-clip.test.jsindex d641b46c..c710120d 100644--- a/tests/file-scene-clip.test.js+++ b/tests/file-scene-clip.test.js@@ -2,7 +2,9 @@ import test from 'node:test';import assert from 'node:assert/strict';import { readFile } from 'node:fs/promises';import { parseGltf } from '../src/gltf.js';-import { createFileSceneClipPlayer } from '../src/file-scene-clip.js';+import { createFileSceneClipPlayer, createFileSceneRenderAt } from '../src/file-scene-clip.js';+import { box } from '../src/geometry.js';+import { applyWaterReflection } from '../src/water-reflection.js';async function loadRobot() {const bytes = await readFile(new URL('fixtures/RobotExpressive.glb', import.meta.url));@@ -79,3 +81,53 @@ test('renderer parts that do not match the scene parts one for one are refused b/must match the scene parts/,);});++// A renderer that holds the parts it was built from and refuses any other identity, as CrucibleRenderer.updateParts does.+function strictRenderer(builtParts) {+ const known = new Set(builtParts);+ const frames = [];+ return {+ frames,+ updateParts(parts) {+ for (const part of parts) {+ if (!known.has(part)) throw new Error(`Unknown part identity: ${part.name}`);+ }+ },+ render(options) {+ frames.push({ options, pose: poseOf(builtParts) });+ },+ };+}++const DESCRIPTION = { eye: [0, 1, 5], target: [0, 1, 0] };++test('the scene-file frame function poses the clip before it renders: the pose handed to the renderer changes with time', async () => {+ const asset = await loadRobot();+ const rendererParts = rendererCopiesOf(asset.parts);+ const renderer = strictRenderer(rendererParts);+ const renderAt = createFileSceneRenderAt({+ asset, posedParts: asset.parts, sceneParts: asset.parts, rendererParts, renderer, camera: null, description: DESCRIPTION, frame: { exposure: 1 },+ });+ renderAt(0);+ renderAt(asset.animations[0].duration / 2);+ assert.equal(renderer.frames.length, 2);+ assert.notDeepEqual(renderer.frames[1].pose, renderer.frames[0].pose);+ assert.deepEqual(renderer.frames[0].options, { exposure: 1, eye: DESCRIPTION.eye, target: DESCRIPTION.target });+});++test('a scene with waterReflection on and a clip poses without "Unknown part identity"', async () => {+ const asset = await loadRobot();+ const lakeMatrix = new Float32Array([4000, 0, 0, 0, 0, 1, 0, 0, 0, 0, 4000, 0, 0, -50, 0, 1]);+ const lake = { name: 'lake', geometry: box(0, 1), matrix: lakeMatrix, material: { tag: 'surface', color: [0.02, 0.05, 0.08], metallic: 0, roughness: 0.04, emission: 0 } };+ const posedParts = [lake, ...asset.parts];+ const reflection = applyWaterReflection(posedParts);+ assert.ok(reflection.surface, 'the lake must qualify as water, or this test does not cover the reflection');+ assert.notEqual(reflection.parts[0], lake, 'the reflected lake is a copy the renderer knows instead of the original');+ const rendererParts = reflection.parts;+ const renderer = strictRenderer(rendererParts);+ const renderAt = createFileSceneRenderAt({+ asset, posedParts, sceneParts: posedParts, rendererParts, renderer, camera: null, description: DESCRIPTION, frame: {},+ });+ assert.doesNotThrow(() => renderAt(0.3));+ assert.equal(renderer.frames.length, 1);+});