Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(anim): a layer's clips ship, and reach the game that plays them

esengine366bd5f66b45
diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.json
index 6bf0050e0..c9536a233 100644
--- a/docs/astro/src/data/apiStability.generated.json
+++ b/docs/astro/src/data/apiStability.generated.json
@@ -3,10 +3,10 @@
"counts": {
"public": 114,
"beta": 101,
- "experimental": 1865,
+ "experimental": 1866,
"deprecated": 3
},
- "total": 2080,
+ "total": 2081,
"subsystems": [
{
"id": "ecs",
diff --git a/pipeline/src/assets/cookAssets.ts b/pipeline/src/assets/cookAssets.ts
index 5a5ee2698..35c17c297 100644
--- a/pipeline/src/assets/cookAssets.ts
+++ b/pipeline/src/assets/cookAssets.ts
@@ -265,15 +265,19 @@ function rewriteAnimatorRefs(
): Uint8Array {
const json = JSON.parse(Buffer.from(bytes).toString('utf8')) as {
states?: { motion?: unknown; stateMachine?: { states?: unknown[] } }[];
+ layers?: { states?: unknown[] }[];
};
// Only a motion whose clip is an ASSET: a sprite clip and a spine animation
// are names their own runtime holds, and rewriting those would break them.
const ASSET_KINDS = new Set(['timeline']);
const walk = (motion: unknown): void => {
if (!motion || typeof motion !== 'object') return;
- const m = motion as { kind?: string; clip?: string; thresholds?: { motion?: unknown }[] };
- if (m.kind === 'blend1d') {
- for (const stop of m.thresholds ?? []) walk(stop.motion);
+ const m = motion as {
+ kind?: string; clip?: string;
+ thresholds?: { motion?: unknown }[]; points?: { motion?: unknown }[];
+ };
+ if (m.kind === 'blend1d' || m.kind === 'blend2d') {
+ for (const child of m.thresholds ?? m.points ?? []) walk(child.motion);
return;
}
if (m.kind && ASSET_KINDS.has(m.kind) && typeof m.clip === 'string') {
@@ -287,7 +291,11 @@ function rewriteAnimatorRefs(
states(st.stateMachine?.states);
}
};
+ // Every machine, not just the base one: a clip only a layer plays ships with
+ // its authored ref intact, which resolves to nothing once staging has moved
+ // the file — the layer is then silent in the package and nowhere else.
states(json.states);
+ for (const layer of json.layers ?? []) states(layer.states);
return new TextEncoder().encode(JSON.stringify(json, null, 2) + '\n');
}
diff --git a/pipeline/tests/document-ref-cook-deps.test.ts b/pipeline/tests/document-ref-cook-deps.test.ts
index cd103a99d..a6b2a7f37 100644
--- a/pipeline/tests/document-ref-cook-deps.test.ts
+++ b/pipeline/tests/document-ref-cook-deps.test.ts
@@ -25,6 +25,7 @@ const SPECULAR = '11111111-1111-4111-8111-111111111111';
const ESENV = '22222222-2222-4222-8222-222222222222';
const CLIP = '33333333-3333-4333-8333-333333333333';
const CONTROLLER = '44444444-4444-4444-8444-444444444444';
+const LAYER_CLIP = '88888888-8888-4888-8888-888888888888';
const FRAME = '55555555-5555-4555-8555-555555555555';
const FNT = '66666666-6666-4666-8666-666666666666';
const FNT_PAGE = '77777777-7777-4777-8777-777777777777';
@@ -44,9 +45,25 @@ const esenv = JSON.stringify({
faceSize: 8, mipCount: 1, maxRange: 8,
});
+// A layer's clip is named NOWHERE else — not by the scene, not by the base
+// machine. A rewriter that walks `states` and calls that the controller stages
+// this one with its authored ref, and the layer is silent in the package only.
const controller = JSON.stringify({
- parameters: [], initialState: 'Idle',
+ version: 2, parameters: [], initialState: 'Idle',
states: [{ name: 'Idle', clip: 'assets/anim/idle.esanim', loop: true, transitions: [] }],
+ layers: [{
+ name: 'Upper', initialState: 'Wave',
+ states: [{
+ name: 'Wave', transitions: [],
+ // A SIBLING name, which is what the editor writes and what staging then
+ // moves out from under — the same shape the environment's atlas has.
+ motion: { kind: 'timeline', clip: 'wave.estimeline', loop: false },
+ }],
+ }],
+});
+
+const layerClip = JSON.stringify({
+ version: '1.2', type: 'timeline', duration: 1, wrapMode: 'once', tracks: [],
});
// A bitmap font is TEXT, and names its page image the way a spine atlas does:
@@ -90,6 +107,7 @@ beforeAll(() => {
writeAsset('assets/env/sky.esenv', 'environment', ESENV, esenv);
writeAsset('assets/anim/frame0.png', 'texture', FRAME, 'PNG');
writeAsset('assets/anim/idle.esanim', 'animclip', CLIP, clip);
+ writeAsset('assets/anim/wave.estimeline', 'animation', LAYER_CLIP, layerClip);
writeAsset('assets/anim/player.esanimator', 'animatorcontroller', CONTROLLER, controller);
writeAsset('assets/fonts/tiny.png', 'texture', FNT_PAGE, 'PNG');
// The type an IMPORT writes for a .fnt. It read `bitmapFont` here and in the
@@ -119,10 +137,15 @@ describe('assets named only inside another document are cooked', () => {
expect(index.deps[CONTROLLER]).toContain(CLIP);
});
+ it('links it to the clip only a LAYER plays', async () => {
+ const { index } = await scanAssetDatabase(root, { write: false });
+ expect(index.deps[CONTROLLER]).toContain(LAYER_CLIP);
+ });
+
it('ships both, and the clip\'s own frame through it', async () => {
const res = await cookAssets(root, { entryScenes: ['assets/scenes/main.esscene'], outDir: 'build' });
expect(res.ok).toBe(true);
- expect(res.included).toEqual(expect.arrayContaining([SCENE, ESENV, SPECULAR, CONTROLLER, CLIP, FRAME, FNT, FNT_PAGE]));
+ expect(res.included).toEqual(expect.arrayContaining([SCENE, ESENV, SPECULAR, CONTROLLER, CLIP, LAYER_CLIP, FRAME, FNT, FNT_PAGE]));
expect(res.unused).toEqual([]);
});
@@ -147,6 +170,17 @@ describe('assets named only inside another document are cooked', () => {
const stagedFnt = readFileSync(path.join(res.outDir, font.path), 'utf8');
expect(stagedFnt).toContain('file="assets/fonts/tiny.png"');
+ // A layer's clip is staged under a name the package has, same as the base
+ // machine's. Left as authored it resolves to nothing and the layer poses
+ // nothing — in the build only, the editor serving the whole project.
+ const ctrl = manifest.entries.find((e) => e.uuid === CONTROLLER)!;
+ const stagedCtrl = JSON.parse(readFileSync(path.join(res.outDir, ctrl.path), 'utf8')) as {
+ layers: { states: { motion: { clip: string } }[] }[];
+ };
+ const layerRef = stagedCtrl.layers[0]!.states[0]!.motion.clip;
+ expect(layerRef).toBe('assets/anim/wave.estimeline');
+ expect(manifest.entries.some((e) => e.sourcePath === layerRef)).toBe(true);
+
// And the package is self-consistent: nothing it staged names a path it lacks.
expect(res.warnings.filter((w) => /does not carry/.test(w))).toEqual([]);
});
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index d757557ff..3e7eea646 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 114 public · 94 beta · 1704 experimental · 3 deprecated
+Symbols: 114 public · 94 beta · 1705 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
@@ -10796,6 +10796,11 @@ AnimationPlugin
(def: AnimatorControllerDef): number
```
+## animatorScopes — function @experimental
+```
+(def: AnimatorControllerDef): AnimatorScope[]
+```
+
## applyAreasToGrid — function @experimental
```
(grid: NavGrid, areas: ReadonlyArray<NavAreaBox & { cost: number; }>): void
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 50d7cb4c9..6fc41793c 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index.native
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 114 public · 99 beta · 1723 experimental · 3 deprecated
+Symbols: 114 public · 99 beta · 1724 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
@@ -10989,6 +10989,11 @@ AnimationPlugin
(def: AnimatorControllerDef): number
```
+## animatorScopes — function @experimental
+```
+(def: AnimatorControllerDef): AnimatorScope[]
+```
+
## applyAreasToGrid — function @experimental
```
(grid: NavGrid, areas: ReadonlyArray<NavAreaBox & { cost: number; }>): void
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index c290db36d..145ce888c 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/index.node
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 114 public · 96 beta · 1703 experimental · 3 deprecated
+Symbols: 114 public · 96 beta · 1704 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -10791,6 +10791,11 @@ AnimationPlugin
(def: AnimatorControllerDef): number
```
+## animatorScopes — function @experimental
+```
+(def: AnimatorControllerDef): AnimatorScope[]
+```
+
## applyAreasToGrid — function @experimental
```
(grid: NavGrid, areas: ReadonlyArray<NavAreaBox & { cost: number; }>): void
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 4bd1da32f..d8d405a25 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/minigame
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 114 public · 94 beta · 1725 experimental · 3 deprecated
+Symbols: 114 public · 94 beta · 1726 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -11045,6 +11045,11 @@ AnimationPlugin
(def: AnimatorControllerDef): number
```
+## animatorScopes — function @experimental
+```
+(def: AnimatorControllerDef): AnimatorScope[]
+```
+
## applyAreasToGrid — function @experimental
```
(grid: NavGrid, areas: ReadonlyArray<NavAreaBox & { cost: number; }>): void
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index b6eda3475..6efa46bfc 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -1,7 +1,7 @@
# API surface — esengine/wechat
<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->
-Symbols: 114 public · 94 beta · 1715 experimental · 3 deprecated
+Symbols: 114 public · 94 beta · 1716 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -10819,6 +10819,11 @@ AnimationPlugin
(def: AnimatorControllerDef): number
```
+## animatorScopes — function @experimental
+```
+(def: AnimatorControllerDef): AnimatorScope[]
+```
+
## applyAreasToGrid — function @experimental
```
(grid: NavGrid, areas: ReadonlyArray<NavAreaBox & { cost: number; }>): void
diff --git a/sdk/src/animation/Animator.ts b/sdk/src/animation/Animator.ts
index d8a5a8609..667f339d2 100644
--- a/sdk/src/animation/Animator.ts
+++ b/sdk/src/animation/Animator.ts
@@ -226,6 +226,16 @@ export function animatorLayer(def: AnimatorControllerDef, index: number): Animat
return def.layers?.[index - 1] ?? null;
}
+/**
+ * Every machine a controller runs, base layer first. The ONE way to walk them: a
+ * reader that reaches for `def.states` sees the base layer and calls it the
+ * controller, which is how the loader shipped acquiring no clip any layer played
+ * — silent in a real project, green in every hand-built test.
+ */
+export function animatorScopes(def: AnimatorControllerDef): AnimatorScope[] {
+ return [def, ...(def.layers ?? [])];
+}
+
// =============================================================================
// Pure transition evaluator (no World, no side effects → unit-testable)
// =============================================================================
@@ -703,7 +713,7 @@ export class AnimatorControllerAPI {
if (!a.enabled) continue;
const def = this.getController(a.controller);
- if (!def || def.states.length === 0) continue;
+ if (!def || animatorScopes(def).every((s) => s.states.length === 0)) continue;
const count = animatorLayerCount(def);
const params = resolveParams(def, this.params.get(entity) ?? EMPTY_PARAMS);
diff --git a/sdk/src/animation/index.ts b/sdk/src/animation/index.ts
index 3da08b979..cc97f0005 100644
--- a/sdk/src/animation/index.ts
+++ b/sdk/src/animation/index.ts
@@ -53,6 +53,7 @@ export {
leafStateOf,
animatorLayerCount,
animatorLayer,
+ animatorScopes,
layerState,
setLayerState,
ANIMATOR_FORMAT_VERSION,
diff --git a/sdk/src/asset/loaders/AnimatorControllerAssetLoader.ts b/sdk/src/asset/loaders/AnimatorControllerAssetLoader.ts
index 6593fdc90..7e6f8755a 100644
--- a/sdk/src/asset/loaders/AnimatorControllerAssetLoader.ts
+++ b/sdk/src/asset/loaders/AnimatorControllerAssetLoader.ts
@@ -12,6 +12,7 @@ import type {
AssetLoader, LoadContext, AnimatorControllerResult, RegistryAssetLoader,
} from '../AssetLoader';
import type { RegistryEra } from '../registryAssets';
+import { animatorScopes } from '../../animation/Animator';
import type { AnimatorControllerDef, AnimatorState } from '../../animation/Animator';
import { isBlend1D, isBlend2D, type AnimatorMotion } from '../../animation/motion';
import { migrateAnimatorController } from '../../animation/animatorMigrate';
@@ -26,7 +27,7 @@ const MOTION_ASSET_TYPES: Readonly<Record<string, string>> = {
timeline: 'timeline',
};
-/** Every motion in the graph, descending through blends and sub-machines. */
+/** Every motion in the controller, across its layers, blends and sub-machines. */
function* motionsOf(states: readonly AnimatorState[]): Generator<AnimatorMotion> {
for (const state of states) {
if (state.motion) yield* flatten(state.motion);
@@ -34,6 +35,11 @@ function* motionsOf(states: readonly AnimatorState[]): Generator<AnimatorMotion>
}
}
+/** Across every layer: a clip only a layer plays is a clip a game plays. */
+function* motionsOfController(def: AnimatorControllerDef): Generator<AnimatorMotion> {
+ for (const scope of animatorScopes(def)) yield* motionsOf(scope.states);
+}
+
function* flatten(motion: AnimatorMotion): Generator<AnimatorMotion> {
yield motion;
if (isBlend1D(motion)) {
@@ -70,7 +76,7 @@ async function acquireMotionAssets(
def: AnimatorControllerDef, path: string, ctx: LoadContext,
): Promise<void> {
const wanted = new Map<string, string>();
- for (const motion of motionsOf(def.states)) {
+ for (const motion of motionsOfController(def)) {
if (isBlend1D(motion) || isBlend2D(motion)) continue;
const type = MOTION_ASSET_TYPES[motion.kind];
if (!type || !motion.clip) continue;
diff --git a/sdk/src/core-content.ts b/sdk/src/core-content.ts
index f90431708..a4f638d5d 100644
--- a/sdk/src/core-content.ts
+++ b/sdk/src/core-content.ts
@@ -465,6 +465,7 @@ export {
leafStateOf,
animatorLayerCount,
animatorLayer,
+ animatorScopes,
layerState,
setLayerState,
ANIMATOR_FORMAT_VERSION,
diff --git a/sdk/tests/animator.test.ts b/sdk/tests/animator.test.ts
index aa275a7d1..f464671b6 100644
--- a/sdk/tests/animator.test.ts
+++ b/sdk/tests/animator.test.ts
@@ -441,4 +441,31 @@ describe('AnimatorController runtime load-by-path', () => {
expect((era.published as { initialState: string }).initialState).toBe('idle');
expect(getRegisteredAnimatorController(PATH), 'prepare wrote a store').toBeUndefined();
});
+
+ it('acquires and resolves the clips a LAYER plays, not only the base one', async () => {
+ // Shipped once without this: the loader walked `def.states` and called it
+ // the controller, so no clip a layer played was fetched or resolved —
+ // visible in a real project, invisible to a hand-built one.
+ const withLayer = {
+ version: 2, parameters: [], initialState: 'Hold',
+ states: [{ name: 'Hold', transitions: [], motion: { kind: 'timeline', clip: 'base.estimeline' } }],
+ layers: [{
+ name: 'Upper', initialState: 'Hold',
+ states: [{ name: 'Hold', transitions: [], motion: { kind: 'timeline', clip: 'upper.estimeline' } }],
+ }],
+ };
+ const acquired: string[] = [];
+ const ctx = {
+ catalog: { getBuildPath: (p: string) => p },
+ loadText: async () => JSON.stringify(withLayer),
+ acquireAsset: async (_type: string, ref: string) => { acquired.push(ref); },
+ } as unknown as LoadContext;
+
+ const era = await new AnimatorControllerAssetLoader().registry.prepare('assets/rig.esanimator', ctx);
+ expect(acquired.sort()).toEqual(['assets/base.estimeline', 'assets/upper.estimeline']);
+ // Resolved IN PLACE, or the driver looks the clip up under a name nothing
+ // was registered as and the layer is silent in exactly the same way.
+ const def = era.published as { layers: { states: { motion: { clip: string } }[] }[] };
+ expect(def.layers[0]!.states[0]!.motion.clip).toBe('assets/upper.estimeline');
+ });
});