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.

feat(anim): a retargeted step travels as far as the rig would

esengine2e51b3892584
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0976c1c92..31e198b33 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -77,8 +77,14 @@ published separately; it ships inside the editor.
and restated from where this rig's joint rests. The editor can describe a rig
it already has: **Create Avatar from Rig** reads the pose the joints are
standing in, which is the bind pose, because nobody was going to type a
- quaternion per joint. What this does *not* do is scale — a clip authored on
- longer bones still asks for the same angles.
+ quaternion per joint.
+
+ **And a step travels the distance the rig it is on would travel.** An avatar
+ measures its rig — root to the joint furthest from it, one measure every avatar
+ uses, since two measured differently give a ratio that means nothing — and root
+ motion is scaled by the ratio. The turn beside it is not: a taller character
+ covers more ground per step and turns by the same angle. A rig that states no
+ size is not guessed at; the clip travels as authored.
- **One clip drives rigs that do not share its bone names.** A clip addresses
what it animates by the name whoever exported the model chose, so two
diff --git a/desktop b/desktop
index b9ab09c0d..5c47aeb09 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit b9ab09c0de891d11949529a108c7d15ac09ba304
+Subproject commit 5c47aeb09bf3f2e1050008e0c296656affc13494
diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.json
index 19b1037ef..0ab42ceca 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": 1879,
+ "experimental": 1880,
"deprecated": 3
},
- "total": 2094,
+ "total": 2095,
"subsystems": [
{
"id": "ecs",
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 0bf61720a..a80ecd0eb 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 · 1718 experimental · 3 deprecated
+Symbols: 114 public · 94 beta · 1719 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
@@ -411,6 +411,7 @@ ComponentDef<AnimatorData>
```
joints: Record<string, string>
rest: Record<string, Quat> | undefined
+scale: number | undefined
```
## AnimatorBlend1D — interface @experimental
@@ -13900,6 +13901,11 @@ TrailPlugin
(host: App | SceneManagerState, targetScene: string, config: TransitionConfig): Promise<void>
```
+## travelRatio — function @experimental
+```
+(source: AnimatorAvatar | null, target: AnimatorAvatar | null): number
+```
+
## trimTextureCache — function @experimental
```
(): number
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 94b514fd4..8c496e792 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 · 1737 experimental · 3 deprecated
+Symbols: 114 public · 99 beta · 1738 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
@@ -411,6 +411,7 @@ ComponentDef<AnimatorData>
```
joints: Record<string, string>
rest: Record<string, Quat> | undefined
+scale: number | undefined
```
## AnimatorBlend1D — interface @experimental
@@ -14143,6 +14144,11 @@ TrailPlugin
(host: App | SceneManagerState, targetScene: string, config: TransitionConfig): Promise<void>
```
+## travelRatio — function @experimental
+```
+(source: AnimatorAvatar | null, target: AnimatorAvatar | null): number
+```
+
## trimTextureCache — function @experimental
```
(): number
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 6aafb944e..d6076f554 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 · 1717 experimental · 3 deprecated
+Symbols: 114 public · 96 beta · 1718 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -406,6 +406,7 @@ ComponentDef<AnimatorData>
```
joints: Record<string, string>
rest: Record<string, Quat> | undefined
+scale: number | undefined
```
## AnimatorBlend1D — interface @experimental
@@ -13905,6 +13906,11 @@ TrailPlugin
(host: App | SceneManagerState, targetScene: string, config: TransitionConfig): Promise<void>
```
+## travelRatio — function @experimental
+```
+(source: AnimatorAvatar | null, target: AnimatorAvatar | null): number
+```
+
## trimTextureCache — function @experimental
```
(): number
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 3d9e7bb64..8d3889d85 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 · 1739 experimental · 3 deprecated
+Symbols: 114 public · 94 beta · 1740 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -406,6 +406,7 @@ ComponentDef<AnimatorData>
```
joints: Record<string, string>
rest: Record<string, Quat> | undefined
+scale: number | undefined
```
## AnimatorBlend1D — interface @experimental
@@ -14164,6 +14165,11 @@ TrailPlugin
(host: App | SceneManagerState, targetScene: string, config: TransitionConfig): Promise<void>
```
+## travelRatio — function @experimental
+```
+(source: AnimatorAvatar | null, target: AnimatorAvatar | null): number
+```
+
## trimTextureCache — function @experimental
```
(): number
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 7997b9d15..79a469830 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 · 1729 experimental · 3 deprecated
+Symbols: 114 public · 94 beta · 1730 experimental · 3 deprecated
## ANCHOR_AXES — const @experimental
```
@@ -406,6 +406,7 @@ ComponentDef<AnimatorData>
```
joints: Record<string, string>
rest: Record<string, Quat> | undefined
+scale: number | undefined
```
## AnimatorBlend1D — interface @experimental
@@ -13928,6 +13929,11 @@ TrailPlugin
(host: App | SceneManagerState, targetScene: string, config: TransitionConfig): Promise<void>
```
+## travelRatio — function @experimental
+```
+(source: AnimatorAvatar | null, target: AnimatorAvatar | null): number
+```
+
## trimTextureCache — function @experimental
```
(): number
diff --git a/sdk/src/animation/Animator.ts b/sdk/src/animation/Animator.ts
index dc8c30ffd..b6e4ad732 100644
--- a/sdk/src/animation/Animator.ts
+++ b/sdk/src/animation/Animator.ts
@@ -33,7 +33,7 @@ import { mixPoses, type WeightedPose } from './poseMix';
import { overlayPose, addPoseOver } from './layerStack';
import { MaskReach, type AnimatorMask } from './animatorMask';
import {
- avatarResolver, rebasePose, type AnimatorAvatar, type JointResolver,
+ avatarResolver, rebasePose, travelRatio, type AnimatorAvatar, type JointResolver,
} from './animatorAvatar';
import { solveAnimatorIK, type AnimatorIK } from './animatorIK';
import { AnimatorRootMotion, type AnimatorRootMotionData } from './animatorRootMotion';
@@ -767,6 +767,7 @@ export class AnimatorControllerAPI {
const rig = this.avatarOf(a.avatar);
const clips = this.avatarOf(def.avatar);
const joints = avatarResolver(world, rig);
+ const travel = travelRatio(clips, rig);
const ctx = this.motions_.context(world, entity, params, joints);
const rt = this.runtimeFor(entity, count);
rt.composed.reset();
@@ -774,7 +775,8 @@ export class AnimatorControllerAPI {
let wrote = false;
this.spentTriggers_.length = 0;
for (let index = 0; index < count; index++) {
- wrote = this.stepLayer(world, entity, a, def, index, ctx, rt, dt, events) || wrote;
+ wrote = this.stepLayer(world, entity, a, def, index, ctx, rt, dt, events, travel)
+ || wrote;
}
// Consumed after EVERY layer has been asked, not as each fires: one
// trigger is one event every layer is told about, so an attack both
@@ -807,7 +809,7 @@ export class AnimatorControllerAPI {
private stepLayer(
world: World, entity: Entity, a: AnimatorData, def: AnimatorControllerDef,
index: number, ctx: MotionContext, rt: AnimatorRuntime, dt: number,
- events: AnimatorEventSink | undefined,
+ events: AnimatorEventSink | undefined, travel: number,
): boolean {
const scope = animatorLayer(def, index);
if (!scope || scope.states.length === 0) return false;
@@ -875,7 +877,9 @@ export class AnimatorControllerAPI {
from: lrt.prevTime, to: lrt.time, inclusiveStart: lrt.entered,
};
if (motion && events) this.emitEvents(ctx, entity, motion, span, events);
- if (index === 0) this.publishRootMotion(world, entity, ctx, motion, span, drivesRoot);
+ if (index === 0) {
+ this.publishRootMotion(world, entity, ctx, motion, span, drivesRoot, travel);
+ }
return posed !== null;
}
@@ -935,7 +939,7 @@ export class AnimatorControllerAPI {
*/
private publishRootMotion(
world: World, entity: Entity, ctx: MotionContext,
- motion: AnimatorMotion | null, span: MotionSpan, drivesRoot: boolean,
+ motion: AnimatorMotion | null, span: MotionSpan, drivesRoot: boolean, travel: number,
): void {
if (!world.has(entity, AnimatorRootMotion)) return;
const out = this.rootScratch_;
@@ -953,9 +957,11 @@ export class AnimatorControllerAPI {
world.update(entity, AnimatorRootMotion, (data: AnimatorRootMotionData) => {
data.active = active;
- data.deltaPosition.x = moved.x;
- data.deltaPosition.y = moved.y;
- data.deltaPosition.z = moved.z;
+ // Scaled, not the turn beside it: a taller rig covers more ground per
+ // step and turns by the same angle.
+ data.deltaPosition.x = moved.x * travel;
+ data.deltaPosition.y = moved.y * travel;
+ data.deltaPosition.z = moved.z * travel;
data.deltaRotation.w = turn.w;
data.deltaRotation.x = turn.x;
data.deltaRotation.y = turn.y;
diff --git a/sdk/src/animation/animatorAvatar.ts b/sdk/src/animation/animatorAvatar.ts
index 596dff4d4..dc0c445ec 100644
--- a/sdk/src/animation/animatorAvatar.ts
+++ b/sdk/src/animation/animatorAvatar.ts
@@ -42,6 +42,13 @@ export interface AnimatorAvatar {
* the same way. Absent for a rig nothing is retargeted onto.
*/
rest?: Record<string, Quat>;
+ /**
+ * Root to the joint furthest from it, at rest — the ONE measure every avatar
+ * uses, since two measured differently give a ratio that means nothing. What
+ * it buys is displacement: a step authored on a smaller rig slides on a
+ * taller one unless it travels further.
+ */
+ scale?: number;
}
interface Quat { w: number; x: number; y: number; z: number }
@@ -81,8 +88,28 @@ export function parseAvatar(raw: unknown): AnimatorAvatar {
// An entry mapping a name to itself is what having no entry already means.
if (path && path !== name) out[name] = path;
}
+ const scale = obj['scale'];
+ if (scale !== undefined && (typeof scale !== 'number' || !(scale > 0))) {
+ throw new Error('Avatar "scale" must be a positive number');
+ }
const rest = obj['rest'] === undefined ? undefined : readRest(obj['rest']);
- return rest ? { joints: out, rest } : { joints: out };
+ const avatar: AnimatorAvatar = { joints: out };
+ if (rest) avatar.rest = rest;
+ if (scale !== undefined) avatar.scale = scale as number;
+ return avatar;
+}
+
+/**
+ * How far a clip authored against `source` should travel on `target`. One where
+ * either rig does not state its size — a ratio against a guess would move a
+ * character by an amount nobody chose.
+ */
+export function travelRatio(
+ source: AnimatorAvatar | null, target: AnimatorAvatar | null,
+): number {
+ const from = source?.scale;
+ const to = target?.scale;
+ return from && to ? to / from : 1;
}
/** The bind pose, one rotation per joint. A joint listed without a usable
diff --git a/sdk/src/animation/index.ts b/sdk/src/animation/index.ts
index f0a984d8b..41b0bce54 100644
--- a/sdk/src/animation/index.ts
+++ b/sdk/src/animation/index.ts
@@ -153,6 +153,7 @@ export {
parseAvatar,
emptyAvatar,
avatarResolver,
+ travelRatio,
AVATAR_FORMAT_VERSION,
type AnimatorAvatar,
type JointResolver,
diff --git a/sdk/src/core-content.ts b/sdk/src/core-content.ts
index b66106b99..f2f8b62f9 100644
--- a/sdk/src/core-content.ts
+++ b/sdk/src/core-content.ts
@@ -476,6 +476,7 @@ export {
parseAvatar,
emptyAvatar,
avatarResolver,
+ travelRatio,
AVATAR_FORMAT_VERSION,
overlayPose,
addPoseOver,
diff --git a/sdk/tests/animatorAvatar.test.ts b/sdk/tests/animatorAvatar.test.ts
index 2a91bb17d..36cbf7f4d 100644
--- a/sdk/tests/animatorAvatar.test.ts
+++ b/sdk/tests/animatorAvatar.test.ts
@@ -7,6 +7,7 @@
import { describe, it, expect } from 'vitest';
import {
Animator, AnimatorControllerAPI, parseAvatar, emptyAvatar, avatarResolver,
+ travelRatio, AnimatorRootMotion,
type AnimatorData, type AnimatorControllerDef, type AnimatorAvatar,
} from '../src/animation';
import { Parent, Children, Name, Transform } from '../src/ecs/component';
@@ -325,3 +326,89 @@ describe('one clip over rigs bound differently', () => {
expect(() => parseAvatar({ joints: {}, rest: [] })).toThrow(/rest/);
});
});
+
+// ---------------------------------------------------------------------------
+// Rigs of different sizes
+// ---------------------------------------------------------------------------
+
+describe('how far a retargeted clip travels', () => {
+ it('is the ratio of the two rigs, or one when either is silent', () => {
+ const small = parseAvatar({ joints: {}, scale: 100 });
+ const tall = parseAvatar({ joints: {}, scale: 250 });
+ const unsized = parseAvatar({ joints: {} });
+
+ expect(travelRatio(small, tall)).toBeCloseTo(2.5, 6);
+ expect(travelRatio(tall, small)).toBeCloseTo(0.4, 6);
+ // Not a guess: a ratio against an assumed size moves a character by an
+ // amount nobody chose.
+ expect(travelRatio(small, unsized)).toBe(1);
+ expect(travelRatio(null, tall)).toBe(1);
+ });
+
+ it('refuses a size that is not a positive number', () => {
+ expect(() => parseAvatar({ joints: {}, scale: 0 })).toThrow(/scale/);
+ expect(() => parseAvatar({ joints: {}, scale: -3 })).toThrow(/scale/);
+ expect(() => parseAvatar({ joints: {}, scale: 'tall' })).toThrow(/scale/);
+ });
+
+ it('walks the taller rig further, and turns it the same', () => {
+ // A clip stating one unit of travel and a quarter turn, on a rig twice
+ // the size of the one it was authored for: two units, same quarter turn.
+ const world = makeWorld();
+ rig(world, 1, 2, 'Arm');
+ world.insert(1, AnimatorRootMotion, {
+ enabled: true, active: false,
+ deltaPosition: { x: 0, y: 0, z: 0 },
+ deltaRotation: { w: 1, x: 0, y: 0, z: 0 }, deltaTime: 0,
+ });
+ world.insert(1, Transform, {
+ position: { x: 0, y: 0, z: 0 }, rotation: { w: 1, x: 0, y: 0, z: 0 },
+ scale: { x: 1, y: 1, z: 1 },
+ });
+
+ const avatars: Record<string, AnimatorAvatar> = {
+ 'assets/clips.esavatar': parseAvatar({ joints: {}, scale: 100 }),
+ 'assets/rig.esavatar': parseAvatar({ joints: {}, scale: 200 }),
+ };
+ const timeline = new TimelineAPI();
+ timeline.registerAsset('walk.estimeline', travels());
+ const ctrl = new AnimatorControllerAPI();
+ ctrl.registerMotionDriver(TIMELINE_MOTION, createTimelineMotionDriver(timeline));
+ ctrl.useAssetAvatars((ref) => avatars[ref]);
+ ctrl.registerController('rig', {
+ version: 2, parameters: [], initialState: 'Walk',
+ avatar: 'assets/clips.esavatar',
+ states: [{
+ name: 'Walk', transitions: [], rootMotion: true,
+ motion: { kind: TIMELINE_MOTION, clip: 'walk.estimeline', loop: true },
+ }],
+ } as AnimatorControllerDef);
+ attach(world, 1, 'assets/rig.esavatar');
+
+ ctrl.update(world, 0);
+ ctrl.update(world, 1);
+ const asked = world.get(1, AnimatorRootMotion) as {
+ deltaPosition: { z: number }; deltaRotation: { z: number };
+ };
+ expect(asked.deltaPosition.z).toBeCloseTo(200, 2);
+ expect(asked.deltaRotation.z).toBeCloseTo(Math.SQRT1_2, 3);
+ });
+});
+
+/** A clip walking 100 units along +Z and turning a quarter, over one second. */
+function travels(): TimelineAsset {
+ const key = (time: number, value: number) => ({
+ time, value, inTangent: 0, outTangent: 0, interpolation: InterpType.Linear,
+ });
+ return {
+ version: '1.2', type: 'timeline', duration: 1, wrapMode: WrapMode.Loop,
+ tracks: [{
+ type: TrackType.Property, component: 'Transform', childPath: '', name: 'root',
+ channels: [
+ { property: 'position.z', keyframes: [key(0, 0), key(1, 100)] },
+ { property: 'rotation.z', keyframes: [key(0, 0), key(1, Math.SQRT1_2)] },
+ { property: 'rotation.w', keyframes: [key(0, 1), key(1, Math.SQRT1_2)] },
+ ],
+ }],
+ } as TimelineAsset;
+}