Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Let a game recipe animate a placed, retargeted character (part of #913)

Nguyen Xuan Ance4bee18c444
diff --git a/changes/913-asset-character-animation.md b/changes/913-asset-character-animation.md
new file mode 100644
index 00000000..adfe0096
--- /dev/null
+++ b/changes/913-asset-character-animation.md
@@ -0,0 +1 @@
+- A game recipe animates a placed, retargeted character: `player.animation` and `npcs[].animation` take clips of asset files (`from: { asset, clip }`), the graph blends idle, walk and run by speed with planted feet holding to under 1e-4 m, and the web export writes the placed assets to `assets/<id>.glb` for the page to load (Part of #913)
diff --git a/changes/913-asset-character-animation.size.md b/changes/913-asset-character-animation.size.md
new file mode 100644
index 00000000..099ffb25
--- /dev/null
+++ b/changes/913-asset-character-animation.size.md
@@ -0,0 +1,2 @@
+- entry:game+lazy: the asset loader (src/game-assets.js, gltf decode of the exported files) sits in its own on-demand chunk, loaded only when the page has assetUrls; the game entry itself gains only the option plumbing and the half-turn for glTF facing
+- scene:agent-scene+lazy: `setAssetPose` and the posed-placement branch in the placed-asset module are a few hundred bytes; the recipe validator for asset clips is plain checks
diff --git a/docs/game.md b/docs/game.md
index 9f307f1e..b30f9291 100644
--- a/docs/game.md
+++ b/docs/game.md
@@ -53,7 +53,7 @@ A rules part named like a scene node is hidden and shown with it, which is how a
## The web build
-`scene_export_web` writes one folder: `index.html`, `game.json` (the recipe) and `engine/` (the built 67js modules, found by following the imports from `build/game.js`). It runs from any static host with no server code, and the folder is bounded: the engine stays under 12 MiB, the recipe under 1 MiB, and the newest four builds are kept. Procedural scenes only; placed catalog assets and vegetation are refused with the fix. The page shows the variables and the verdict in a corner label and exposes the running game as `window.__game` (`rules.state`, `controller.state`, `scene`, `runtime`).
+`scene_export_web` writes one folder: `index.html`, `game.json` (the recipe) and `engine/` (the built 67js modules, found by following the imports from `build/game.js`). It runs from any static host with no server code, and the folder is bounded: the engine stays under 12 MiB, the recipe under 1 MiB, and the newest four builds are kept. Placed catalog assets (and the assets whose clips a character retargets) are copied to `assets/<id>.glb`, up to 64 MiB together; grown vegetation is refused with the fix. The page shows the variables and the verdict in a corner label and exposes the running game as `window.__game` (`rules.state`, `controller.state`, `scene`, `runtime`).
`index.html?replay=name.json` plays a recording (`recordInput`, see [input](input.md#custom-and-scripted-input)) from the same folder instead of the keyboard. `scripts/smoke-game-web.mjs` builds a creature that collects 3 coins and reaches a goal through the MCP tools, exports it, and wins it from a replay in headless Chrome, with a head-bob animation graph that the page must play. `node scripts/smoke-game-web.mjs bin/67js-mcp.mjs drive` does the same for a car that passes 3 gates and reaches a finish line under a `chase` camera.
@@ -95,4 +95,8 @@ game.dispose();
- An NPC needs `rootMotion` and a number parameter `speed`. Its path follower sets `speed`, so idle, walk and run blend; it moves by the root motion, turns at corners and stops at the last of up to 256 `[x, z]` waypoints, where `speed` returns to 0. `cruiseSpeed` is 2.2 by default; `yaw` and `footOffset` work as for the player. Each NPC node is separate from the player and the other NPCs.
- Root travel along +Z works as written; for clips whose root travels -Z set `rootMotion.facing` to `"-z"`.
-A wrong field is refused with its path and the values to use, for example `player.animation.clips.walk.tracks[0].target names "Ghost", which is not a child node of the character`. `window.__game.animation` and `window.__game.npcs` expose the graphs. Clips of placed catalog assets and retargeting are not part of the recipe yet, because the web build takes procedural scenes only.
+A wrong field is refused with its path and the values to use, for example `player.animation.clips.walk.tracks[0].target names "Ghost", which is not a child node of the character`. `window.__game.animation` and `window.__game.npcs` expose the graphs.
+
+### A placed character
+
+When the player or an NPC node is a placed catalog asset (`asset: { id }`), `animation.clips` names clips of asset files instead of keyed tracks: `{ "walk": { "from": { "asset": "<catalog id>", "clip": 1 }, "loop": true, "scale": 1 } }`. Leave out `from.asset` for a clip of the character's own file; a clip of another humanoid is retargeted onto the character's skeleton by the humanoid bone map, as in an asset recipe's `motion.layers`. `rootMotion.bone`, `boneGroups` and mask joints name joints of the character's skeleton, such as `Hips`. The game turns the node half a circle because a glTF model faces +Z while yaw 0 faces -Z. The bone map, clip indexes and graph are checked when the game starts, and the error names the field. Pass `assets` (a Map keyed by `assetCopyKey(id, copy)`) to `startGame`, or `assetUrls` (asset id to GLB URL), which the web export fills in. A character animates through `scene.setAssetPose`, so it takes its own decoded copy of the asset and counts toward the 16 animated placements.
diff --git a/scripts/agent-runtime.mjs b/scripts/agent-runtime.mjs
index be1ae2d2..fba9daa4 100644
--- a/scripts/agent-runtime.mjs
+++ b/scripts/agent-runtime.mjs
@@ -24,6 +24,7 @@ import { sequenceFrameIds } from '../src/agent-texture-sequence.js';
import { catalogLookReferences, BUNDLED_ENVIRONMENT_NAMES, BUNDLED_CUBE_LOOK_NAMES } from '../src/look-catalog.js';
import { TEXTURE_SEQUENCE_CAPABILITIES } from '../src/texture-sequence-limits.js';
import { writeWebBuild } from './web-export.mjs';
+import { gameAssetIds } from '../src/game-assets.js';
import { createAgentRenderJobs, EXPORT_DIRECTORY_NAME, HIGH_PRECISION_FRAME_FACTOR } from './agent-render-jobs.mjs';
import { encodeRenderOutput, encodeMultilayerExr } from './render-output-encoders.mjs';
import { createFrameEncoder } from './frame-encoder.mjs';
@@ -1064,7 +1065,9 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', brow
const outputDirectory = join(directory, `web-${randomUUID()}`);
let summary;
try {
- summary = await writeWebBuild({ recipe, buildDirectory: fileURLToPath(new URL('../', previewUrl)), outputDirectory, title });
+ const assets = new Map();
+ for (const id of gameAssetIds(recipe)) assets.set(id, await (await catalog()).read(id));
+ summary = await writeWebBuild({ recipe, buildDirectory: fileURLToPath(new URL('../', previewUrl)), outputDirectory, title, assets });
} catch (error) {
// A refused or half-written build never uses up a retention slot or evicts a good one.
await rm(outputDirectory, { recursive: true, force: true });
diff --git a/scripts/web-export.mjs b/scripts/web-export.mjs
index f5d8adb0..8966bbe7 100644
--- a/scripts/web-export.mjs
+++ b/scripts/web-export.mjs
@@ -5,7 +5,8 @@ import { dirname, join } from 'node:path';
// section) and engine/ (the built 67js modules that index.html imports, found by following the import statements from
// build/game.js). There is no server code. The folder is bounded, so an export never fills the disk.
-export const WEB_EXPORT_LIMITS = Object.freeze({ maxEngineBytes: 12 * 1024 * 1024, maxEngineFiles: 400, maxTitleLength: 64, maxRecipeBytes: 1024 * 1024 });
+export const WEB_EXPORT_LIMITS = Object.freeze({ maxEngineBytes: 12 * 1024 * 1024, maxEngineFiles: 400, maxTitleLength: 64, maxRecipeBytes: 1024 * 1024, maxAssetBytes: 64 * 1024 * 1024 });
+const ASSET_ID = /^[a-f0-9]{64}$/;
const ENTRY_MODULE = 'game.js';
const IMPORT_PATTERN = /(?:from\s*|import\s*\(\s*|import\s*)["'](\.\/[A-Za-z0-9_.-]+\.js)["']/g;
@@ -42,7 +43,7 @@ function pageTitle(title) {
}
/** The page. `?replay=name.json` plays a recording from the same folder instead of the keyboard, for tests and demos. */
-export function indexHtml(title) {
+export function indexHtml(title, assetUrls = {}) {
return `<!doctype html>
<html lang="en">
<head>
@@ -60,7 +61,8 @@ const canvas = document.getElementById('game');
const recipe = await (await fetch('./game.json')).json();
const replayName = new URLSearchParams(location.search).get('replay');
const replay = /^[\\w.-]+\\.json$/.test(replayName ?? '') ? await (await fetch('./' + replayName)).json() : undefined;
-window.__game = await startGame(canvas, recipe, { replay });
+const assetUrls = ${JSON.stringify(assetUrls)};
+window.__game = await startGame(canvas, recipe, { replay, assetUrls });
</script>
</body>
</html>
@@ -71,10 +73,12 @@ window.__game = await startGame(canvas, recipe, { replay });
* Write the web build of a recipe into `outputDirectory` (created, must not hold a build already) and return its summary.
* `buildDirectory` is the package's build/ folder.
*/
-export async function writeWebBuild({ recipe, buildDirectory, outputDirectory, title }) {
+export async function writeWebBuild({ recipe, buildDirectory, outputDirectory, title, assets = new Map() }) {
if (!recipe?.game) throw new Error('This scene has no game. Add one with scene_apply set-game (fields: scene_capabilities topic game), then export again.');
if (recipe.game.physics) throw new Error('game.physics is true, and the web export does not bundle the physics engine yet. Set game.physics to false with set-game.');
- if (recipe.nodes?.some(node => node.asset || node.vegetation)) throw new Error('The web export holds procedural scenes only. Remove the placed catalog assets and vegetation, or build them from geometry.');
+ if (recipe.nodes?.some(node => node.vegetation)) throw new Error('The web export does not carry grown vegetation yet. Remove it, or build the plants from geometry.');
+ const missing = (recipe.nodes ?? []).filter(node => node.asset && !assets.has(node.asset.id)).map(node => node.id);
+ if (missing.length) throw new Error(`The web export needs the bytes of the catalog assets placed by ${missing.join(', ')}. Pass them as assets (asset id to Buffer).`);
if (recipe.format !== '67js.scene') throw new Error('The web export needs a 67js.scene recipe. Create the scene with scene_create.');
const recipeText = JSON.stringify(recipe);
if (Buffer.byteLength(recipeText) > WEB_EXPORT_LIMITS.maxRecipeBytes) throw new RangeError(`The recipe is larger than ${WEB_EXPORT_LIMITS.maxRecipeBytes} bytes. Simplify the scene.`);
@@ -87,8 +91,18 @@ export async function writeWebBuild({ recipe, buildDirectory, outputDirectory, t
await mkdir(join(outputDirectory, 'engine'), { recursive: true });
for (const name of names) await copyFile(join(buildDirectory, name), join(outputDirectory, 'engine', name));
await writeFile(join(outputDirectory, 'game.json'), recipeText);
- const html = indexHtml(title);
+ const assetUrls = {};
+ let assetBytes = 0;
+ if (assets.size) await mkdir(join(outputDirectory, 'assets'), { recursive: true });
+ for (const [id, bytes] of assets) {
+ if (!ASSET_ID.test(id)) throw new RangeError(`Asset id ${JSON.stringify(id)} is not a catalog id of 64 hexadecimal characters.`);
+ assetBytes += bytes.byteLength;
+ if (assetBytes > WEB_EXPORT_LIMITS.maxAssetBytes) throw new RangeError(`The placed assets are over ${WEB_EXPORT_LIMITS.maxAssetBytes} bytes together. Use fewer or smaller assets.`);
+ await writeFile(join(outputDirectory, 'assets', `${id}.glb`), bytes);
+ assetUrls[id] = `./assets/${id}.glb`;
+ }
+ const html = indexHtml(title, assetUrls);
await writeFile(join(outputDirectory, 'index.html'), html);
- const totalBytes = engineBytes + Buffer.byteLength(recipeText) + Buffer.byteLength(html);
- return { directory: dirname(join(outputDirectory, 'index.html')), files: names.length + 2, engineModules: names.length, bytes: totalBytes };
+ const totalBytes = engineBytes + assetBytes + Buffer.byteLength(recipeText) + Buffer.byteLength(html);
+ return { directory: dirname(join(outputDirectory, 'index.html')), files: names.length + 2 + assets.size, engineModules: names.length, bytes: totalBytes };
}
diff --git a/src/agent-scene-assets.js b/src/agent-scene-assets.js
index 87c5fa65..17c40a61 100644
--- a/src/agent-scene-assets.js
+++ b/src/agent-scene-assets.js
@@ -123,6 +123,14 @@ export function normalizeAssetPlacement(value) {
return result;
}
+/** The nodes a game recipe animates itself: a placed asset there is posed by the game's animation graph, frame by frame. */
+export function gamePosedNodeIds(game) {
+ const ids = new Set();
+ if (game?.player?.animation && game.player.node) ids.add(game.player.node);
+ for (const npc of game?.npcs ?? []) if (npc?.animation && npc.node) ids.add(npc.node);
+ return ids;
+}
+
function collectPlacements(value, found, depth = 0) {
if (!value || typeof value !== 'object' || depth > 8) return;
if (Array.isArray(value)) {
@@ -146,10 +154,12 @@ export function recipeAssetNeeds(value) {
const placements = [];
collectPlacements(value, placements);
const needs = new Map();
+ const posedNodes = gamePosedNodeIds(value?.game);
+ const posedPlacements = (value?.nodes ?? []).filter(node => node.asset && posedNodes.has(node.id)).map(node => node.asset);
for (const placement of placements) {
const id = placement.id.toLowerCase();
const need = needs.get(id) ?? { id, copies: 1 };
- if (placement.clip || placement.drive || placement.crowd) need.copies++;
+ if (placement.clip || placement.drive || placement.crowd || posedPlacements.includes(placement)) need.copies++;
needs.set(id, need);
}
return [...needs.values()];
@@ -203,7 +213,9 @@ export function expandAssetPlacements(recipe, assets, limits, outputCache = new
throw new Error('This recipe places catalog assets, which a host loads and decodes first. Pass them as the assets option of createAgentScene; MCP does this for scene_create.');
}
const distinct = new Set(placed.map(node => node.asset.id));
- const animated = placed.filter(node => node.asset.clip || node.asset.drive || node.asset.crowd);
+ const posedNodes = gamePosedNodeIds(recipe.game);
+ const isAnimated = node => Boolean(node.asset.clip || node.asset.drive || node.asset.crowd || posedNodes.has(node.id));
+ const animated = placed.filter(isAnimated);
if (placed.length > ASSET_PLACEMENT_LIMITS.maxPlacements || distinct.size > ASSET_PLACEMENT_LIMITS.maxAssets ||
animated.length > ASSET_PLACEMENT_LIMITS.maxAnimatedPlacements) {
throw new RangeError(`A scene places at most ${ASSET_PLACEMENT_LIMITS.maxPlacements} assets, ${ASSET_PLACEMENT_LIMITS.maxAssets} different ones, ${ASSET_PLACEMENT_LIMITS.maxAnimatedPlacements} of them playing a clip; this recipe places ${placed.length}, ${distinct.size} and ${animated.length}.`);
@@ -214,8 +226,9 @@ export function expandAssetPlacements(recipe, assets, limits, outputCache = new
const generatedNodes = [];
for (const node of placed) {
const { id: assetId, materials: overrides = [], clip, drive, crowd } = node.asset;
+ const posed = posedNodes.has(node.id);
let copy = 0;
- if (clip || drive || crowd) {
+ if (clip || drive || crowd || posed) {
copy = (copyCounter.get(assetId) ?? 0) + 1;
copyCounter.set(assetId, copy);
}
@@ -273,9 +286,9 @@ export function expandAssetPlacements(recipe, assets, limits, outputCache = new
...(part.castShadow !== undefined ? { castShadow: part.castShadow } : {}),
});
});
- const record = { node: node.id, assetId, copy, asset, partNodeIds, clip: clip ?? null, drive: drive ? createDrive(node.id, `nodes[${recipe.nodes.indexOf(node)}].asset.drive`, createAssetDrive, asset, drive) : null, drivePose: null };
+ const record = { node: node.id, assetId, copy, asset, partNodeIds, clip: clip ?? null, pose: posed ? { nodeOverrides: [], morphWeights: undefined } : null, drive: drive ? createDrive(node.id, `nodes[${recipe.nodes.indexOf(node)}].asset.drive`, createAssetDrive, asset, drive) : null, drivePose: null };
runtime.placements.push(record);
- if (clip || drive) runtime.animators.push(record);
+ if (clip || drive || posed) runtime.animators.push(record);
}
if (recipe.nodes.length + generatedNodes.length > limits.maxNodes) {
throw new RangeError(`Placed assets expand to ${generatedNodes.length} part nodes, which with the ${recipe.nodes.length} authored nodes exceeds maxNodes ${limits.maxNodes}. Place fewer copies or simpler assets.`);
@@ -315,7 +328,9 @@ export function stageAssetClips(runtime, graph, time) {
transactions.push(record.crowd.stage(time ?? 0));
continue;
}
- if (record.drive) {
+ if (record.pose) {
+ transaction = record.asset.stagePose({ animation: null, nodeOverrides: record.pose.nodeOverrides, morphWeights: record.pose.morphWeights });
+ } else if (record.drive) {
const driven = record.drive.sample(time ?? 0);
record.drivePose = driven.pose;
transaction = record.asset.stagePose({ nodeOverrides: driven.nodeOverrides });
diff --git a/src/agent-scene.js b/src/agent-scene.js
index c9ca6298..5ebef196 100644
--- a/src/agent-scene.js
+++ b/src/agent-scene.js
@@ -1788,6 +1788,17 @@ export function createAgentScene(input, inputLimits = {}, options = {}) {
}
return state.motionTime;
},
+ /**
+ * Set the pose a game's animation graph computed for a placed asset: `nodeOverrides` (and optional `morphWeights`) as
+ * `asset.stagePose` takes them. The next `sampleMotion` draws it. Only the player and NPC nodes of the game section are posable.
+ */
+ setAssetPose(nodeId, { nodeOverrides = [], morphWeights } = {}) {
+ const record = state.assetRuntime.animators.find(candidate => candidate.node === nodeId && candidate.pose);
+ if (!record) {
+ throw invalidField('nodeId', nodeId, 'a placed asset node that game.player.animation or game.npcs[].animation animates; other placed assets play their asset.clip or asset.drive');
+ }
+ record.pose = { nodeOverrides, morphWeights };
+ },
// After sampleMotion, the lights, sun and sky the motion tracks set at that time; the recipe keeps its authored values.
get environment() {
const environment = structuredClone(state.recipe.environment);
diff --git a/src/game-animation.js b/src/game-animation.js
index 91e87bb6..100ef3cc 100644
--- a/src/game-animation.js
+++ b/src/game-animation.js
@@ -1,6 +1,8 @@
import { createAnimationClip } from './animation.js';
import { createAnimationGraph } from './animation-graph.js';
-import { createSkeletonPose } from './skeleton-pose.js';
+import { assetCopyKey } from './agent-scene-assets.js';
+import { retargetAssetClip } from './agent-retarget.js';
+import { clipFromGltfAnimation, createAssetLayerEvaluator, createSkeletonPose } from './skeleton-pose.js';
// The `animation` section of a game character in a scene recipe: clips keyed on the character's child nodes, an animation
// graph (states, 1D and 2D blend spaces, layers with masks) and how the game drives it. It is plain data. The same section
@@ -18,6 +20,9 @@ export const GAME_ANIMATION_DRIVES = Object.freeze(['speed', 'intentSpeed', 'air
const ANIMATION_FIELDS = ['clips', 'parameters', 'boneGroups', 'layers', 'rootMotion', 'drive'];
const CLIP_FIELDS = ['duration', 'loop', 'tracks'];
const TRACK_PROPERTIES = ['translation', 'rotation'];
+// A character that is a placed catalog asset takes its clips from asset files instead of keyed tracks.
+const ASSET_CLIP_FIELDS = ['from', 'loop', 'scale'];
+const ASSET_ID = /^[a-f0-9]{64}$/;
function fail(path, problem) {
throw new RangeError(`Game setup ${path} ${problem}`);
@@ -91,6 +96,79 @@ function checkClips(clips, descendantIds, path) {
return normalized;
}
+function checkAssetClips(clips, path) {
+ requireRecord(clips, `${path}.clips`, '{ "walk": { "from": { "asset": "<64 hex catalog asset id>", "clip": 0 } } }');
+ const names = Object.keys(clips);
+ if (names.length === 0 || names.length > GAME_ANIMATION_LIMITS.maxClips) fail(`${path}.clips`, `needs 1 to ${GAME_ANIMATION_LIMITS.maxClips} clips, got ${names.length}.`);
+ const normalized = {};
+ for (const name of names) {
+ const clipPath = `${path}.clips.${name}`;
+ const clip = clips[name];
+ requireRecord(clip, clipPath, '{ "from": { "asset": "<64 hex catalog asset id>", "clip": 0 }, "loop": true }');
+ rejectUnknownFields(clip, ASSET_CLIP_FIELDS, clipPath);
+ requireRecord(clip.from, `${clipPath}.from`, '{ "clip": 0 } for a clip of the character\'s own asset, or { "asset": "<64 hex catalog asset id>", "clip": 0 } for another humanoid');
+ rejectUnknownFields(clip.from, ['asset', 'clip'], `${clipPath}.from`);
+ if (clip.from.asset !== undefined && !(typeof clip.from.asset === 'string' && ASSET_ID.test(clip.from.asset.toLowerCase()))) {
+ fail(`${clipPath}.from.asset`, `is ${JSON.stringify(clip.from.asset)}. Pass a catalog asset id of 64 hexadecimal characters, or leave it out for the character's own asset.`);
+ }
+ if (!Number.isInteger(clip.from.clip) || clip.from.clip < 0) fail(`${clipPath}.from.clip`, `must be the index of a clip in that asset, a whole number of 0 or more, got ${JSON.stringify(clip.from.clip)}.`);
+ if (clip.loop !== undefined && typeof clip.loop !== 'boolean') fail(`${clipPath}.loop`, `must be true or false, got ${JSON.stringify(clip.loop)}.`);
+ if (clip.scale !== undefined && !(Number.isFinite(clip.scale) && clip.scale > 0 && clip.scale <= 100)) fail(`${clipPath}.scale`, `must be a number above 0 and at most 100 (the hips travel ratio between the two rigs), got ${JSON.stringify(clip.scale)}.`);
+ normalized[name] = {
+ from: { ...(clip.from.asset ? { asset: clip.from.asset.toLowerCase() } : {}), clip: clip.from.clip },
+ loop: clip.loop ?? true,
+ ...(clip.scale === undefined ? {} : { scale: clip.scale }),
+ };
+ }
+ return normalized;
+}
+
+function decodedAsset(assets, assetId, label) {
+ const asset = assets?.get(assetCopyKey(assetId, 0));
+ if (!asset) {
+ throw new RangeError(`${label} needs catalog asset ${assetId.slice(0, 12)}, which the host did not load. Pass it in the assets option of startGame, keyed by assetCopyKey(id, 0).`);
+ }
+ return asset;
+}
+
+// The clips of an asset character, moved onto its own skeleton: a clip of another asset is retargeted by the humanoid bone map.
+function assetCharacterClips(animation, ownId, assets, path) {
+ const own = decodedAsset(assets, ownId, `${path}.clips`);
+ const clips = {};
+ for (const [name, definition] of Object.entries(animation.clips)) {
+ const sourceId = definition.from.asset ?? ownId;
+ const source = decodedAsset(assets, sourceId, `${path}.clips.${name}.from.asset`);
+ const gltfAnimation = source.animations[definition.from.clip];
+ if (!gltfAnimation) {
+ fail(`${path}.clips.${name}.from.clip`, `is ${definition.from.clip}, but asset ${sourceId.slice(0, 12)} has ${source.animations.length} clips. Use an index from 0 to ${source.animations.length - 1}.`);
+ }
+ if (sourceId === ownId) {
+ clips[name] = clipFromGltfAnimation(gltfAnimation, { loop: definition.loop });
+ continue;
+ }
+ // Retargeted from a clip that does not wrap: a looping source would sample its last frame as its first and lose the travel of the cycle.
+ const retargeted = retargetAssetClip(source.nodes, own.nodes, clipFromGltfAnimation(gltfAnimation, { loop: false }), { scale: definition.scale });
+ clips[name] = createAnimationClip({ duration: retargeted.duration, loop: definition.loop, tracks: retargeted.tracks });
+ }
+ return { own, clips };
+}
+
+function createAssetCharacterAnimation(animation, characterNode, assets) {
+ const { clips: _clips, drive: _drive, ...graphDefinition } = animation;
+ const { own, clips } = assetCharacterClips(animation, characterNode.asset.id, assets, `animation of ${characterNode.id}`);
+ const graph = createAnimationGraph({ ...graphDefinition, clips });
+ const evaluator = createAssetLayerEvaluator(own, graph.poseLayers());
+ return {
+ graph,
+ drive: animation.drive,
+ hasRootMotion: Boolean(animation.rootMotion),
+ assetNode: characterNode.id,
+ operations: () => [],
+ /** The pose of the asset at the graph's time, for `scene.setAssetPose`. */
+ assetPose: () => evaluator.evaluate(graph.time),
+ };
+}
+
function jointsOf(descendants, characterId) {
const index = new Map(descendants.map((node, position) => [node.id, position]));
return descendants.map(node => ({
@@ -107,7 +185,9 @@ function jointsOf(descendants, characterId) {
* `characterId` the node the controller or follower moves. `graph` is a normal animation graph; `operations()` poses the nodes
* below the character at the graph's time and returns the `set-node` operations that write them.
*/
-export function createGameAnimation(animation, nodes, characterId) {
+export function createGameAnimation(animation, nodes, characterId, { assets } = {}) {
+ const characterNode = nodes.find(node => node.id === characterId);
+ if (characterNode?.asset) return createAssetCharacterAnimation(animation, characterNode, assets);
const descendants = characterDescendants(nodes, characterId);
const clips = Object.fromEntries(Object.entries(animation.clips).map(([name, clip]) => [name, createAnimationClip(clip)]));
const { clips: _clips, drive: _drive, ...graphDefinition } = animation;
@@ -118,6 +198,8 @@ export function createGameAnimation(animation, nodes, characterId) {
graph,
drive: animation.drive,
hasRootMotion: Boolean(animation.rootMotion),
+ assetNode: null,
+ assetPose: () => null,
operations() {
pose.apply(graph.time);
return written.map(({ id, index, translation }) => ({
@@ -157,6 +239,7 @@ function checkDrive(drive, animation, path) {
export function normalizeGameAnimation(animation, nodes, characterId, { path = 'animation', driven = true } = {}) {
requireRecord(animation, path, '{ "clips": {...}, "layers": [...] }');
rejectUnknownFields(animation, ANIMATION_FIELDS, path);
+ if (nodes.find(node => node.id === characterId)?.asset) return normalizeAssetCharacterAnimation(animation, path, driven);
const descendants = characterDescendants(nodes, characterId);
if (descendants.length === 0) fail(path, `has nothing to move: "${characterId}" has no child nodes. Give the character child nodes (a body, limbs) and point clip tracks at them.`);
if (descendants.length > GAME_ANIMATION_LIMITS.maxCharacterNodes) fail(path, `would pose ${descendants.length} nodes; the limit is ${GAME_ANIMATION_LIMITS.maxCharacterNodes}.`);
@@ -172,3 +255,13 @@ export function normalizeGameAnimation(animation, nodes, characterId, { path = '
}
return normalized;
}
+
+// An asset character cannot be built here: its clips and skeleton live in files the host loads when the game starts. The
+// shape is checked now; clip indexes, bone names and the graph itself are checked by createGameAnimation with the files.
+function normalizeAssetCharacterAnimation(animation, path, driven) {
+ if (!Array.isArray(animation.layers)) fail(`${path}.layers`, 'must be a list of animation graph layers, such as [{ "name": "base", "states": [{ "name": "idle", "clip": "idle" }] }].');
+ const normalized = { ...structuredClone(animation), clips: checkAssetClips(animation.clips, path) };
+ if (driven) normalized.drive = checkDrive(animation.drive, normalized, path);
+ else if (animation.drive !== undefined) fail(`${path}.drive`, 'is not allowed on an NPC: its path follower sets the "speed" parameter. Remove drive.');
+ return normalized;
+}
diff --git a/src/game-assets.js b/src/game-assets.js
new file mode 100644
index 00000000..59f2cfed
--- /dev/null
+++ b/src/game-assets.js
@@ -0,0 +1,53 @@
+import { parseGltf } from './gltf.js';
+import { agentAssetDecodeOptions } from './agent-asset-limits.js';
+import { assetCopyKey, recipeAssetNeeds } from './agent-scene-assets.js';
+
+// The catalog assets a game recipe places or retargets clips from, decoded for startGame. The web export writes each asset
+// as assets/<id>.glb and tells the page its URL; this file fetches and decodes them, one decoded copy per copy the scene needs.
+
+export const GAME_ASSET_LIMITS = Object.freeze({ maxAssets: 16, maxCopiesPerAsset: 18 });
+const ASSET_ID = /^[a-f0-9]{64}$/;
+
+/** The ids of every catalog asset the game needs: the placed ones and the sources of its animation clips. */
+export function gameAssetIds(recipe) {
+ const ids = new Set(recipeAssetNeeds(recipe).map(need => need.id));
+ const animations = [recipe.game?.player?.animation, ...(recipe.game?.npcs ?? []).map(npc => npc.animation)];
+ for (const animation of animations) {
+ for (const clip of Object.values(animation?.clips ?? {})) {
+ const id = clip?.from?.asset;
+ if (typeof id === 'string' && ASSET_ID.test(id)) ids.add(id);
+ }
+ }
+ return [...ids];
+}
+
+/**
+ * Fetch and decode the assets of a recipe. `assetUrls` maps an asset id to the URL of its GLB; `fetchBytes(url)` returns an
+ * ArrayBuffer (the global fetch by default). Returns the Map that createAgentScene takes, keyed by assetCopyKey(id, copy).
+ */
+export async function loadGameAssets(recipe, assetUrls, fetchBytes = defaultFetchBytes) {
+ const ids = gameAssetIds(recipe);
+ if (ids.length > GAME_ASSET_LIMITS.maxAssets) {
+ throw new RangeError(`This game uses ${ids.length} catalog assets; the limit is ${GAME_ASSET_LIMITS.maxAssets}.`);
+ }
+ const copiesById = new Map(recipeAssetNeeds(recipe).map(need => [need.id, need.copies]));
+ const assets = new Map();
+ for (const id of ids) {
+ const url = assetUrls?.[id];
+ if (typeof url !== 'string') {
+ throw new RangeError(`The game places or retargets from asset ${id.slice(0, 12)}, but assetUrls has no URL for it. Export the game again with the web export, which writes assets/<id>.glb.`);
+ }
+ const bytes = await fetchBytes(url);
+ const copies = Math.min(copiesById.get(id) ?? 1, GAME_ASSET_LIMITS.maxCopiesPerAsset);
+ for (let copy = 0; copy < copies; copy++) {
+ assets.set(assetCopyKey(id, copy), await parseGltf(bytes.slice(0), agentAssetDecodeOptions()));
+ }
+ }
+ return assets;
+}
+
+async function defaultFetchBytes(url) {
+ const response = await fetch(url);
+ if (!response.ok) throw new Error(`Could not load ${url}: HTTP ${response.status}.`);
+ return response.arrayBuffer();
+}
diff --git a/src/game.js b/src/game.js
index 8e8b26c1..452fceec 100644
--- a/src/game.js
+++ b/src/game.js
@@ -65,7 +65,11 @@ function nodeTranslation(nodes, id) {
}
// The node a controller or follower moves: its feet position and the heading about the vertical axis.
-function characterOperation(id, position, yaw, footOffset) {
+// A placed glTF character faces +Z, while yaw 0 faces -Z, so its node is turned half a circle.
+const GLTF_FACING_YAW = Math.PI;
+
+function characterOperation(id, position, yaw, footOffset, modelYaw = 0) {
+ yaw += modelYaw;
return { type: 'set-node', id, translation: [position[0], position[1] + footOffset, position[2]], rotation: [0, Math.sin(yaw / 2), 0, Math.cos(yaw / 2)] };
}
@@ -91,6 +95,10 @@ function createStatusLine(canvas) {
return element;
}
+function modelYawOf(animation) {
+ return animation?.assetNode ? GLTF_FACING_YAW : 0;
+}
+
function describeState(state) {
const variables = Object.entries(state.variables).map(([name, value]) => `${name} ${value}`).join(' ');
const verdict = state.status === 'won' ? ' YOU WIN' : state.status === 'lost' ? ' GAME OVER' : '';
@@ -104,7 +112,7 @@ async function loadAnimationRuntime() {
/**
* Start the game a recipe describes on a canvas and return its parts: `scene`, `runtime`, `rules`, `input`, `controller`, `renderer`
- * and `dispose()`. Options: `replay` (a recording from recordInput; the keyboard is not attached) `showStatus` (default true) and `createRenderer` (tests pass a stand-in for the WebGL renderer).
+ * and `dispose()`. Options: `replay` (a recording from recordInput; the keyboard is not attached) `showStatus` (default true) `assets` (decoded catalog assets by assetCopyKey) or `assetUrls` (asset id to GLB URL, which startGame fetches and decodes) and `createRenderer` (tests pass a stand-in for the WebGL renderer).
*/
export async function startGame(canvas, recipe, options = {}) {
const cleanups = [];
@@ -130,11 +138,13 @@ function runCleanups(cleanups) {
while (cleanups.length > 0) cleanups.pop()();
}
-async function buildGame(canvas, recipe, { replay, showStatus = true, createRenderer = (...rendererArguments) => new Renderer67(...rendererArguments) }, cleanups) {
+async function buildGame(canvas, recipe, { replay, showStatus = true, assets: providedAssets, assetUrls, createRenderer = (...rendererArguments) => new Renderer67(...rendererArguments) }, cleanups) {
const setup = recipe.game;
if (!setup) throw new Error('startGame needs a recipe with a game section. Add one with the set-game operation.');
await loadAgentSceneModules(recipe);
- const scene = createAgentScene(recipe);
+ // The exported page gives URLs; the assets load only for a recipe that places or retargets a catalog asset.
+ const assets = providedAssets ?? (assetUrls ? await (await import('./game-assets.js')).loadGameAssets(recipe, assetUrls) : undefined);
+ const scene = createAgentScene(recipe, {}, { assets });
const { game } = scene.exportRecipe();
const placement = agentGroundPlacement(scene.parts, scene.environment);
const environment = litEnvironment(scene, Boolean(placement));
@@ -157,7 +167,7 @@ async function buildGame(canvas, recipe, { replay, showStatus = true, createRend
// The animation graph runtime loads only for a recipe that declares an animation or an NPC, so other games do not carry it.
const hasAnimation = Boolean(game.player.animation) || (game.npcs?.length ?? 0) > 0;
const { createGameAnimation, createPathFollower } = hasAnimation ? await loadAnimationRuntime() : {};
- const playerAnimation = game.player.animation ? createGameAnimation(game.player.animation, recipe.nodes, game.player.node) : null;
+ const playerAnimation = game.player.animation ? createGameAnimation(game.player.animation, recipe.nodes, game.player.node, { assets }) : null;
const startPosition = [start[0], start[1] - footOffset, start[2]];
const controller = game.player.controller === 'vehicle'
? createVehicleController({ input, position: startPosition, ground: game.ground, maximumSpeed: game.player.maximumSpeed, node: body })
@@ -185,14 +195,15 @@ async function buildGame(canvas, recipe, { replay, showStatus = true, createRend
animationSystems.push(parameterSystem(playerAnimation, controller.state), playerAnimation.graph.system());
}
const npcs = (game.npcs ?? []).map(npc => {
- const animation = createGameAnimation(npc.animation, recipe.nodes, npc.node);
+ const animation = createGameAnimation(npc.animation, recipe.nodes, npc.node, { assets });
const start = nodeTranslation(recipe.nodes, npc.node);
const follower = createPathFollower({ graph: animation.graph, position: [start[0], start[1] - npc.footOffset, start[2]], yaw: npc.yaw, cruiseSpeed: npc.cruiseSpeed });
follower.follow(npc.path);
animatedCharacters.push(animation);
animationSystems.push(follower.system(), animation.graph.system());
- return { node: npc.node, footOffset: npc.footOffset, follower, graph: animation.graph };
+ return { node: npc.node, footOffset: npc.footOffset, follower, graph: animation.graph, modelYaw: modelYawOf(animation) };
});
+ const hasPosedAssets = animatedCharacters.some(animation => animation.assetNode);
let drawnTopology = scene.topologyRevision;
const status = showStatus && rules ? createStatusLine(canvas) : null;
if (status) cleanups.push(() => status.remove());
@@ -201,8 +212,8 @@ async function buildGame(canvas, recipe, { replay, showStatus = true, createRend
name: 'scene',
update(deltaSeconds, context) {
const position = controller.positionAt(context.alpha);
- const operations = [characterOperation(game.player.node, position, body.rotationY, footOffset)];
- for (const npc of npcs) operations.push(characterOperation(npc.node, npc.follower.state.position, npc.follower.state.yaw, npc.footOffset));
+ const operations = [characterOperation(game.player.node, position, body.rotationY, footOffset, modelYawOf(playerAnimation))];
+ for (const npc of npcs) operations.push(characterOperation(npc.node, npc.follower.state.position, npc.follower.state.yaw, npc.footOffset, npc.modelYaw));
for (const animation of animatedCharacters) operations.push(...animation.operations());
for (const part of rules?.state.parts ?? []) {
if (!nodeIds.has(part.id) || part.id === game.player.node || shownNodes.get(part.id) === part.visible) continue;
@@ -210,6 +221,10 @@ async function buildGame(canvas, recipe, { replay, showStatus = true, createRend
operations.push({ type: 'set-node', id: part.id, visible: part.visible });
}
scene.apply(operations, { expectedRevision: scene.revision });
+ if (hasPosedAssets) {
+ for (const animation of animatedCharacters) if (animation.assetNode) scene.setAssetPose(animation.assetNode, animation.assetPose());
+ scene.sampleMotion(context.elapsedSeconds % 3600);
+ }
if (scene.topologyRevision === drawnTopology) {
renderer.updateParts(scene.parts);
} else {
diff --git a/tests/agent-runtime.test.js b/tests/agent-runtime.test.js
index fb6b7333..9093810c 100644
--- a/tests/agent-runtime.test.js
+++ b/tests/agent-runtime.test.js
@@ -19,6 +19,7 @@ import { MESH_BLEND_CAPABILITIES } from '../src/mesh-blend.js';
import { MATERIAL_SNIPPET_CAPABILITIES } from '../src/material-snippet.js';
import { AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, RENDER_PASS_NAMES, RENDER_PASS_DEPTH_RANGE, VIDEO_ENCODERS, MAXIMUM_VIDEO_SECONDS, MAXIMUM_JOB_SIDE, MAXIMUM_JOB_PIXELS, MAXIMUM_MEMORY_MIB, BAKE_WORKER_MAXIMUM_SECONDS } from '../src/agent-scene-capabilities.js';
import { recipeAssetNeeds } from '../src/agent-scene-assets.js';
+import { gameAssetIds } from '../src/game-assets.js';
import { sequenceFrameIds } from '../src/agent-texture-sequence.js';
import { catalogLookReferences, BUNDLED_ENVIRONMENT_NAMES, BUNDLED_CUBE_LOOK_NAMES } from '../src/look-catalog.js';
import { TEXTURE_SEQUENCE_CAPABILITIES } from '../src/texture-sequence-limits.js';
@@ -120,7 +121,7 @@ async function reviewRuntimeHarness({ failShot, blockShot = false, oversize = fa
readFile: async () => '', packageMetadata: { version: 'test' },
createRequire: () => ({ resolve: () => { throw new Error('no peer in tests'); } }),
VEGETATION_CAPABILITY_NOTE, CROWD_CAPABILITY_NOTE, GAME_CAPABILITIES, USER_PASS_CAPABILITIES, MESH_BLEND_CAPABILITIES, MATERIAL_SNIPPET_CAPABILITIES, AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, RENDER_PASS_NAMES, RENDER_PASS_DEPTH_RANGE, VIDEO_ENCODERS, MAXIMUM_VIDEO_SECONDS, MAXIMUM_JOB_SIDE, MAXIMUM_JOB_PIXELS, MAXIMUM_MEMORY_MIB, BAKE_WORKER_MAXIMUM_SECONDS, PATH_TRACER_CAPABILITIES,
- recipeAssetNeeds, sequenceFrameIds, catalogLookReferences, BUNDLED_ENVIRONMENT_NAMES, BUNDLED_CUBE_LOOK_NAMES, TEXTURE_SEQUENCE_CAPABILITIES, AGENT_ASSET_LIMITS, assertCombinedAssetBytes,
+ recipeAssetNeeds, gameAssetIds, sequenceFrameIds, catalogLookReferences, BUNDLED_ENVIRONMENT_NAMES, BUNDLED_CUBE_LOOK_NAMES, TEXTURE_SEQUENCE_CAPABILITIES, AGENT_ASSET_LIMITS, assertCombinedAssetBytes,
audioInputArguments, createAudioLibrary, measureStreamSeconds, filmTimelineDuration, filmTimelineShotStarts, sampleFilmTimeline, normalizeReview, REVIEW_LIMITS, isPreviewHost, BRANCH_LIMITS, validateBranchName, sessionProblem, SCENE_STORE_LIMITS,
chromium: { launch: async () => browser }, URL, Buffer, process, performance, clearTimeout,
runtimeOptions, mkdir, rm, join, isAbsolute, fileURLToPath, writeWebBuild, EXPORT_DIRECTORY_NAME, createAgentRenderJobs: async () => ({ list: () => [], close: async () => {} }), ...contextOverrides,
diff --git a/tests/game-animation-asset.test.js b/tests/game-animation-asset.test.js
new file mode 100644
index 00000000..8b039e65
--- /dev/null
+++ b/tests/game-animation-asset.test.js
@@ -0,0 +1,182 @@
+import './support/load-agent-scene-modules.mjs';
+import test from 'node:test';
+import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import assert from 'node:assert/strict';
+import { createAgentScene } from '../src/agent-scene.js';
+import { assetCopyKey } from '../src/agent-scene-assets.js';
+import { parseGltf } from '../src/gltf.js';
+import { startGame } from '../src/game.js';
+import { gameAssetIds, loadGameAssets } from '../src/game-assets.js';
+import { writeWebBuild } from '../scripts/web-export.mjs';
+import { MIXAMO_LIKE, UNREAL_LIKE, humanoidGlb, humanoidJoints, walkTracks } from './fixtures/humanoid-glb.js';
+
+// A game recipe whose player is a placed catalog humanoid: the walk, run and idle clips come from another humanoid file and are
+// retargeted to the player's skeleton, and the animation graph blends them by speed while the controller moves the node.
+
+const SOURCE_ID = 'ab'.repeat(32);
+const HERO_ID = 'cd'.repeat(32);
+const WALK_SPEED = 1.2;
+const RUN_SPEED = 2.4;
+const FRAME_MILLISECONDS = 1000 / 60;
+const FOOT_DRIFT_BOUND = 1e-4;
+const CONTACT_HEIGHT = 1e-3;
+const SETTLE_FRAMES = 30;
+
+const SOURCE_JOINTS = humanoidJoints();
+const HERO_JOINTS = humanoidJoints({ armTiltDegrees: 30 });
+const parse = bytes => parseGltf(bytes, { maxVertices: 500000 });
+
+function locomotionClips() {
+ const clip = (name, duration, speed) => ({ name, duration, tracks: walkTracks(SOURCE_JOINTS, { speed, duration }) });
+ return [clip('Idle', 1, 0), clip('Walk', 1, WALK_SPEED), clip('Run', 0.5, RUN_SPEED)];
+}
+
+async function loadAssets() {
+ const assets = new Map();
+ assets.set(assetCopyKey(SOURCE_ID, 0), await parse(humanoidGlb({ names: MIXAMO_LIKE, joints: SOURCE_JOINTS, clips: locomotionClips() })));
+ assets.set(assetCopyKey(HERO_ID, 0), await parse(humanoidGlb({ names: UNREAL_LIKE, joints: HERO_JOINTS })));
+ assets.set(assetCopyKey(HERO_ID, 1), await parse(humanoidGlb({ names: UNREAL_LIKE, joints: HERO_JOINTS })));
+ return assets;
+}
+
+function heroAnimation(overrides = {}) {
+ return {
+ clips: {
+ idle: { from: { asset: SOURCE_ID, clip: 0 } },
+ walk: { from: { asset: SOURCE_ID, clip: 1 } },
+ run: { from: { asset: SOURCE_ID, clip: 2 } },
+ },
+ parameters: { speed: { type: 'number' } },
+ rootMotion: { bone: UNREAL_LIKE.hips },
+ layers: [{
+ name: 'base',
+ states: [{
+ name: 'move',
+ blend: { parameter: 'speed', motions: [{ clip: 'idle', threshold: 0 }, { clip: 'walk', threshold: WALK_SPEED }, { clip: 'run', threshold: RUN_SPEED }] },
+ }],
+ }],
+ ...overrides,
+ };
+}
+
+function heroScene(assets) {
+ return createAgentScene({
+ format: '67js.scene', schemaVersion: 1,
+ geometries: [], materials: [],
+ nodes: [{ id: 'Hero', asset: { id: HERO_ID } }],
+ }, {}, { assets });
+}
+
+function setGame(scene, game) {
+ scene.apply([{ type: 'set-game', game }], { expectedRevision: scene.revision });
+ return scene.exportRecipe();
+}
+
+const standInRenderer = () => ({ render() {}, resize() {}, updateParts() {}, replaceParts() {}, dispose() {} });
+
+async function startRecipe(recipe, assets, replay) {
+ const previous = { request: globalThis.requestAnimationFrame, cancel: globalThis.cancelAnimationFrame };
+ globalThis.requestAnimationFrame = () => 1;
+ globalThis.cancelAnimationFrame = () => {};
+ const game = await startGame({}, recipe, { replay, assets, showStatus: false, createRenderer: standInRenderer });
+ return {
+ game,
+ finish() {
+ game.dispose();
+ globalThis.requestAnimationFrame = previous.request;
+ globalThis.cancelAnimationFrame = previous.cancel;
+ },
+ };
+}
+
+function soleParts(game, asset) {
+ return ['sole_left', 'sole_right'].map(name => {
+ const index = asset.parts.findIndex(part => part.name === name);
+ assert.ok(index >= 0, `no part ${name}`);
+ return game.scene.parts.find(part => part.name === `Hero/${index}`);
+ });
+}
+
+test('a recipe validates an asset character animation at the boundary and names the fix', async () => {
+ const assets = await loadAssets();
+ const scene = heroScene(assets);
+ const attempt = animation => () => scene.apply([{ type: 'set-game', game: { player: { node: 'Hero', animation } } }], { expectedRevision: scene.revision });
+ assert.throws(attempt(heroAnimation({ clips: { walk: { from: { asset: 'nope', clip: 0 } } } })), /clips\.walk\.from\.asset is "nope"\. Pass a catalog asset id of 64 hexadecimal/);
+ assert.throws(attempt(heroAnimation({ clips: { walk: { from: { clip: -1 } } } })), /clips\.walk\.from\.clip must be the index of a clip/);
+ assert.throws(attempt(heroAnimation({ clips: { walk: { tracks: [] } } })), /clips\.walk\.tracks is not a field\. Use one of: from, loop, scale/);
+ const recipe = setGame(scene, { player: { node: 'Hero', walkSpeed: WALK_SPEED, runSpeed: RUN_SPEED, animation: heroAnimation() } });
+ assert.deepEqual(recipe.game.player.animation.drive, { speed: 'intentSpeed' });
+ assert.deepEqual(recipe.game.player.animation.clips.walk, { from: { asset: SOURCE_ID, clip: 1 }, loop: true });
+});
+
+test('starting the game says which asset or clip is missing', async () => {
+ const assets = await loadAssets();
+ const recipe = setGame(heroScene(assets), { player: { node: 'Hero', animation: heroAnimation({ clips: { idle: { from: { asset: SOURCE_ID, clip: 7 } } }, layers: [{ name: 'base', states: [{ name: 'idle', clip: 'idle' }] }] }) } });
+ await assert.rejects(startRecipe(recipe, assets, { version: 1, samples: 1, events: [] }), /clips\.idle\.from\.clip is 7, but asset abababababab has 3 clips\. Use an index from 0 to 2/);
+ const withoutSource = new Map(assets);
+ withoutSource.delete(assetCopyKey(SOURCE_ID, 0));
+ const walkRecipe = setGame(heroScene(assets), { player: { node: 'Hero', animation: heroAnimation() } });
+ await assert.rejects(startRecipe(walkRecipe, withoutSource, { version: 1, samples: 1, events: [] }), /needs catalog asset abababababab, which the host did not load/);
+});
+
+test('a retargeted character blends idle, walk and run by speed and a planted foot stays put', async () => {
+ const assets = await loadAssets();
+ const recipe = setGame(heroScene(assets), { player: { node: 'Hero', walkSpeed: WALK_SPEED, runSpeed: RUN_SPEED, animation: heroAnimation() } });
+ const replay = { version: 1, samples: 600, events: [[1, 'keyboard/KeyW', 1]] };
+ const { game, finish } = await startRecipe(recipe, assets, replay);
+ try {
+ const soles = soleParts(game, assets.get(assetCopyKey(HERO_ID, 0)));
+ const contacts = soles.map(() => null);
+ const drifts = [];
+ for (let frame = 0; frame < 480; frame++) {
+ game.runtime.step(FRAME_MILLISECONDS);
+ if (frame < SETTLE_FRAMES) continue;
+ soles.forEach((sole, index) => {
+ const world = [sole.matrix[12], sole.matrix[13], sole.matrix[14]];
+ if (world[1] > CONTACT_HEIGHT) {
+ contacts[index] = null;
+ return;
+ }
+ contacts[index] ??= { anchor: world, drift: 0 };
+ contacts[index].drift = Math.max(contacts[index].drift, Math.hypot(world[0] - contacts[index].anchor[0], world[2] - contacts[index].anchor[2]));
+ drifts.push(contacts[index].drift);
+ });
+ }
+ assert.ok(game.controller.state.position[2] < -5, `the player walked forward, z ${game.controller.state.position[2]}`);
+ assert.ok(drifts.length > 100, `feet were planted for ${drifts.length} samples`);
+ assert.ok(Math.max(0, ...drifts) < FOOT_DRIFT_BOUND, `worst planted foot drift ${Math.max(0, ...drifts)} m`);
+ } finally {
+ finish();
+ }
+});
+
+test('the web export carries the placed assets and the page loads them by URL', async () => {
+ const sourceBytes = humanoidGlb({ names: MIXAMO_LIKE, joints: SOURCE_JOINTS, clips: locomotionClips() });
+ const heroBytes = humanoidGlb({ names: UNREAL_LIKE, joints: HERO_JOINTS });
+ const recipe = setGame(heroScene(await loadAssets()), { player: { node: 'Hero', walkSpeed: WALK_SPEED, runSpeed: RUN_SPEED, animation: heroAnimation() } });
+ assert.deepEqual(gameAssetIds(recipe).sort(), [HERO_ID, SOURCE_ID].sort());
+
+ const directory = await mkdtemp(join(tmpdir(), '67js-asset-export-'));
+ const buildDirectory = join(directory, 'build');
+ await mkdir(buildDirectory);
+ await writeFile(join(buildDirectory, 'game.js'), 'export const startGame = () => {};');
+ try {
+ await assert.rejects(writeWebBuild({ recipe, buildDirectory, outputDirectory: join(directory, 'refused') }), /needs the bytes of the catalog assets placed by Hero/);
+ const assetBytes = new Map([[HERO_ID, Buffer.from(heroBytes)], [SOURCE_ID, Buffer.from(sourceBytes)]]);
+ const summary = await writeWebBuild({ recipe, buildDirectory, outputDirectory: join(directory, 'built'), assets: assetBytes });
+ const page = await readFile(join(directory, 'built', 'index.html'), 'utf8');
+ assert.ok(page.includes(`"${HERO_ID}":"./assets/${HERO_ID}.glb"`));
+ assert.equal((await readFile(join(directory, 'built', 'assets', `${SOURCE_ID}.glb`))).byteLength, sourceBytes.byteLength);
+ assert.ok(summary.bytes > sourceBytes.byteLength + heroBytes.byteLength);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+
+ const urls = { [HERO_ID]: 'hero.glb', [SOURCE_ID]: 'source.glb' };
+ const files = { 'hero.glb': heroBytes, 'source.glb': sourceBytes };
+ const loaded = await loadGameAssets(recipe, urls, async url => files[url]);
+ assert.deepEqual([...loaded.keys()].sort(), [assetCopyKey(HERO_ID, 0), assetCopyKey(HERO_ID, 1), assetCopyKey(SOURCE_ID, 0)].sort());
+ await assert.rejects(loadGameAssets(recipe, { [HERO_ID]: 'hero.glb' }, async () => files['hero.glb']), /assetUrls has no URL for it/);
+});
diff --git a/tests/game-setup.test.js b/tests/game-setup.test.js
index 9f613b21..452fa941 100644
--- a/tests/game-setup.test.js
+++ b/tests/game-setup.test.js
@@ -127,7 +127,8 @@ test('the web export refuses what it cannot play and says how to fix it', async
await assert.rejects(async () => writeWebBuild({ recipe: recipeWith({ ...game, physics: true }), buildDirectory: build, outputDirectory: await out() }), /Set game\.physics to false/);
const placed = recipeWith(game);
placed.nodes.push({ id: 'tree', asset: { id: 'a'.repeat(64) } });
- await assert.rejects(async () => writeWebBuild({ recipe: placed, buildDirectory: build, outputDirectory: await out() }), /procedural scenes only/);
+ await assert.rejects(async () => writeWebBuild({ recipe: placed, buildDirectory: build, outputDirectory: await out() }), /needs the bytes of the catalog assets placed by tree/);
+ await assert.rejects(async () => writeWebBuild({ recipe: { ...placed, nodes: [{ id: 'grove', vegetation: {} }] }, buildDirectory: build, outputDirectory: await out() }), /does not carry grown vegetation/);
await assert.rejects(async () => writeWebBuild({ recipe: recipeWith(game), buildDirectory: await mkdtemp(join(tmpdir(), '67js-empty-build-')), outputDirectory: await out() }), /game\.js is missing.*build:lib/);
const big = await fakeBuild();
await writeFile(join(big, 'chunk-a.js'), `${'/'.repeat(WEB_EXPORT_LIMITS.maxEngineBytes)}\nexport {};`);