Skip to content

3D engine that AI agents drive

Merge origin/main into 2310-native-file-clip

Nguyen Xuan An43670a4eb07b
diff --cc src/file-scene-clip.js
index 1c3e8332,45c55a41..f1a5e47a
--- a/src/file-scene-clip.js
+++ b/src/file-scene-clip.js
@@@ -29,3 -24,14 +29,17 @@@ export function createFileSceneClipPlay
},
};
}
+
+ // 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.
++// time. `camera` is a camera sampler or null; without one the scene file's own eye and target are used. The returned function
++// carries its clip player as `clipPlayer` (null without a clip), so the native path tracer can pose the same clip.
+ export function createFileSceneRenderAt({ asset, posedParts, sceneParts, rendererParts, renderer, camera, description, frame }) {
+ const clipPlayer = createFileSceneClipPlayer({ asset, posedParts, sceneParts, rendererParts });
- return seconds => {
++ const renderAt = seconds => {
+ clipPlayer?.poseAt(renderer, seconds);
+ const pose = camera ? camera.sample(seconds) : { eye: description.eye, target: description.target };
+ renderer.render({ ...frame, ...pose });
+ };
++ renderAt.clipPlayer = clipPlayer;
++ return renderAt;
+ }
diff --cc src/file-scene.js
index 51d19fa2,ff89b8ba..e2614cfe
--- a/src/file-scene.js
+++ b/src/file-scene.js
@@@ -71,14 -71,9 +71,9 @@@ 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, poseClipAt: seconds => clipPlayer?.applyAt(seconds), pause() {}, parts: reflection.parts, environment: { ...(rendererOptions.environment ?? {}), map } };
- window.__scene = { renderer, duration: camera?.duration, renderAt, pause() {}, parts: reflection.parts, environment: { ...(rendererOptions.environment ?? {}), map } };
++ window.__scene = { renderer, duration: camera?.duration, renderAt, poseClipAt: seconds => renderAt.clipPlayer?.applyAt(seconds), pause() {}, parts: reflection.parts, environment: { ...(rendererOptions.environment ?? {}), map } };
}
start().catch(error => {
diff --cc tests/file-scene-clip.test.js
index 16f2daa0,c710120d..e12cc5b9
--- a/tests/file-scene-clip.test.js
+++ b/tests/file-scene-clip.test.js
@@@ -2,8 -2,9 +2,10 @@@ 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 { skinnedGeometry } from '../src/path-tracer-scene.js';
+ import { applyWaterReflection } from '../src/water-reflection.js';
async function loadRobot() {
const bytes = await readFile(new URL('fixtures/RobotExpressive.glb', import.meta.url));
@@@ -81,28 -82,52 +83,78 @@@ test('renderer parts that do not match
);
});
+// What the native tracer reads from a part: the skinned vertices at the part's current pose (src/path-tracer-scene.js).
+function tracedPositionsOf(parts) {
+ return parts.filter(part => part.skin).map(part => [...skinnedGeometry(part).positions]);
+}
+
+test('applyAt poses the parts handed to the native tracer: the traced vertices differ between t=0 and half the clip', async () => {
+ const { asset, player } = await playerForRobot();
+ player.applyAt(0);
+ const atStart = tracedPositionsOf(asset.parts);
+ player.applyAt(player.clip.duration / 2);
+ const atMiddle = tracedPositionsOf(asset.parts);
+ assert.ok(atStart.length > 0, 'the robot must have skinned parts, or this test does not cover the tracer input');
+ assert.notDeepEqual(atMiddle, atStart);
+ player.applyAt(player.clip.duration / 2 + 2 * player.clip.duration);
+ assert.deepEqual(tracedPositionsOf(asset.parts), atMiddle);
+});
+
+test('a scene without a clip has no player, so the traced parts stay as loaded', async () => {
+ const asset = await loadRobot();
+ asset.animations = [];
+ const before = tracedPositionsOf(asset.parts);
+ const player = createFileSceneClipPlayer({ asset, posedParts: asset.parts, sceneParts: asset.parts, rendererParts: rendererCopiesOf(asset.parts) });
+ assert.equal(player, null);
+ assert.deepEqual(tracedPositionsOf(asset.parts), before);
+});
++
+ // 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);
+ });