reze-engine 0.30.2 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -14,10 +14,11 @@ export { BODY_GRAPH } from "./graph/presets/body";
14
14
  export { STOCKINGS_GRAPH } from "./graph/presets/stockings";
15
15
  export { EYE_GRAPH } from "./graph/presets/eye";
16
16
  export { FACE_GRAPH } from "./graph/presets/face";
17
- export { Model } from "./model";
17
+ export { Model, type ClipEventInfo } from "./model";
18
18
  export { Vec3, Quat, Mat4, easeInOut, type EulerOrder } from "./math";
19
19
  export type { AnimationClip, AnimationPlayOptions, AnimationProgress, BlendEntry, BoneKeyframe, IkKeyframe, MorphKeyframe, BoneInterpolation, ControlPoint, } from "./animation";
20
- export { LocomotionController, type LocomotionClips, type LocomotionOptions, type LocomotionPose, type StrafeClipEntry, } from "./locomotion";
20
+ export { LocomotionController, type LocomotionClips, type LocomotionOptions, type LocomotionPose, type StrafeClipEntry, type TurnClipEntry, type RunTurnClipEntry, type StopClipEntry, } from "./locomotion";
21
+ export { AnimationStateMachine, type AnimStateDef, type AnimTransitionDef, type StateMachineOptions } from "./state-machine";
21
22
  export { FPS, bezierInterpolate, interpolateControlPoints, rawInterpolationToBoneInterpolation, } from "./animation";
22
23
  export { VMDLoader, type CameraKeyframe, type IkFrame } from "./vmd-loader";
23
24
  export { VMDWriter } from "./vmd-writer";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,EACrB,KAAK,mBAAmB,EACxB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,yBAAyB,EAC9B,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB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,UAAU,EACV,YAAY,EACZ,UAAU,EACV,aAAa,EACb,iBAAiB,EACjB,YAAY,GACb,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,oBAAoB,EACpB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,aAAa,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,qBAAqB,EAAE,KAAK,YAAY,EAAE,KAAK,iBAAiB,EAAE,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAC5H,OAAO,EACL,GAAG,EACH,iBAAiB,EACjB,wBAAwB,EACxB,mCAAmC,GACpC,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,SAAS,EAAE,KAAK,cAAc,EAAE,KAAK,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,eAAe,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAA;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA"}
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export { FACE_GRAPH } from "./graph/presets/face";
15
15
  export { Model } from "./model";
16
16
  export { Vec3, Quat, Mat4, easeInOut } from "./math";
17
17
  export { LocomotionController, } from "./locomotion";
18
+ export { AnimationStateMachine } from "./state-machine";
18
19
  export { FPS, bezierInterpolate, interpolateControlPoints, rawInterpolationToBoneInterpolation, } from "./animation";
19
20
  export { VMDLoader } from "./vmd-loader";
20
21
  export { VMDWriter } from "./vmd-writer";
@@ -1,4 +1,5 @@
1
1
  import { Model } from "./model";
2
+ import { type BlendEntry } from "./animation";
2
3
  import { Quat, Vec3 } from "./math";
3
4
  export interface StrafeClipEntry {
4
5
  /** Clip name previously loaded on the model. */
@@ -8,6 +9,38 @@ export interface StrafeClipEntry {
8
9
  /** The clip's authored root speed in MMD units/s (post-conversion scale) — drives root motion. */
9
10
  speed: number;
10
11
  }
12
+ export interface TurnClipEntry {
13
+ /** Clip name previously loaded on the model. */
14
+ clip: string;
15
+ /** Signed yaw the clip turns through, radians (+ = the character's right). */
16
+ angle: number;
17
+ /** Clip-local seconds at which the yaw is complete (the settle tail is skipped). */
18
+ exitTime: number;
19
+ }
20
+ export interface RunTurnClipEntry {
21
+ /** Clip name previously loaded on the model. */
22
+ clip: string;
23
+ /** Signed total yaw, radians (+ = the character's right; reversals are ±PI). */
24
+ angle: number;
25
+ /** Clip-local seconds at which the yaw is complete (settle tail skipped). */
26
+ exitTime: number;
27
+ /** Uniform samples over [0, exitTime] of the clip's authored forward displacement
28
+ * (MMD units along the heading at trigger) — the overrun-plant-return curve. */
29
+ forward: number[];
30
+ gear: "run" | "sprint";
31
+ foot: "L" | "R";
32
+ }
33
+ export interface StopClipEntry {
34
+ /** Clip name previously loaded on the model. */
35
+ clip: string;
36
+ /** Clip-local seconds at which the deceleration settles (idle tail skipped). */
37
+ exitTime: number;
38
+ /** Uniform samples over [0, exitTime] of the authored forward displacement
39
+ * (MMD units along the heading at release) — the skid-to-plant curve. */
40
+ forward: number[];
41
+ gear: "run" | "sprint";
42
+ foot: "L" | "R";
43
+ }
11
44
  export interface LocomotionClips {
12
45
  /** Clip names previously loaded on the model (loadVmd/loadClip). */
13
46
  idle: string;
@@ -17,8 +50,25 @@ export interface LocomotionClips {
17
50
  * blends the two ring clips nearest the local move angle. */
18
51
  strafeRun?: StrafeClipEntry[];
19
52
  strafeSprint?: StrafeClipEntry[];
53
+ /** Authored turn-in-place clips: reversal-class direction changes from near-
54
+ * standstill play the nearest clip (yaw baked in the bones, root held) and
55
+ * transfer its angle to the root at exitTime — instead of the eased pivot. */
56
+ turnInPlace?: TurnClipEntry[];
57
+ /** Authored RUNNING reversals (plant-and-turn): while moving fast with a
58
+ * reversal-class heading error, the matching clip plays with the root driven
59
+ * along its measured forward profile; yaw transfers at exitTime and she runs
60
+ * out along the new heading. */
61
+ runTurn?: RunTurnClipEntry[];
62
+ /** Authored stops: releasing input at speed plays the gear/foot-matched stop with
63
+ * the root driven along its measured skid profile, instead of a blend to idle.
64
+ * Re-pressing input interrupts the stop and resumes locomotion. */
65
+ stop?: StopClipEntry[];
20
66
  }
21
67
  export interface LocomotionOptions {
68
+ /** When false, update() computes the pose but does NOT call setBlendPose —
69
+ * read it with getBlendEntries(). For embedding in an AnimationStateMachine
70
+ * delegate state, which owns the final blend. Default true. */
71
+ autoApply?: boolean;
22
72
  /** Ground speed in MMD units/second at full run (default 67 — measured from the Unity
23
73
  * locomotion pack's root motion before the in-place strip; ≈5.4 m/s at MMD scale).
24
74
  * Match this to the clips or the feet slide. */
@@ -32,6 +82,14 @@ export interface LocomotionOptions {
32
82
  /** Heading error (radians) beyond which the character pivots strictly in place —
33
83
  * no translation until the body is back within this cone (default PI/4). */
34
84
  turnInPlaceThreshold?: number;
85
+ /** Minimum heading error (radians) for an authored turn clip to fire (default
86
+ * ~100°: reversals only — smaller corrections keep the instant pivot). */
87
+ turnClipMinAngle?: number;
88
+ /** Playback rate for turn clips (default 1.4 — the authored turns are deliberate;
89
+ * game pacing wants them brisker). */
90
+ turnTimeScale?: number;
91
+ /** Playback rate for stop clips (default 1.25) — same reasoning. */
92
+ stopTimeScale?: number;
35
93
  /** Tank-mode steering rate, radians/second (default 2.5 ≈ 143°/s). */
36
94
  steerRate?: number;
37
95
  /** Backpedal speed as a fraction of run speed in tank mode (default 0.5). */
@@ -54,12 +112,24 @@ export interface LocomotionPose {
54
112
  }
55
113
  export declare class LocomotionController {
56
114
  private readonly model;
115
+ private readonly autoApply;
116
+ private lastEntries;
57
117
  private readonly clips;
58
118
  private readonly runSpeed;
59
119
  private readonly sprintSpeed;
60
120
  private readonly speedResponse;
61
121
  private readonly turnResponse;
62
122
  private readonly cosTurnThreshold;
123
+ private readonly turnClipMinAngle;
124
+ private readonly turnTimeScale;
125
+ private readonly stopTimeScale;
126
+ private turning;
127
+ private runTurning;
128
+ /** An interrupted authored clip fading out OVER resumed locomotion, so breaking
129
+ * out of a stop is instantly responsive without a pose pop. */
130
+ private exitGhost;
131
+ private stopping;
132
+ private readonly turnEntries;
63
133
  private readonly yawOffset;
64
134
  private inputX;
65
135
  private inputY;
@@ -70,6 +140,10 @@ export declare class LocomotionController {
70
140
  private readonly steerRate;
71
141
  private readonly backpedalScale;
72
142
  private speedLevel;
143
+ /** Peak-hold of speedLevel (decays ~1.5/s): reversal triggers read this, because
144
+ * keyboard direction flips pass through a dead moment (W+S cancel, key gap) that
145
+ * dips the instantaneous level right when the reversal input lands. */
146
+ private recentSpeed;
73
147
  private yaw;
74
148
  private dirX;
75
149
  private dirZ;
@@ -102,6 +176,10 @@ export declare class LocomotionController {
102
176
  getPosition(): Vec3;
103
177
  /** Stop driving the model's pose (the blend is cleared; the single-clip player resumes). */
104
178
  detach(): void;
179
+ /** The most recent update()'s blend entries (null before the first update or
180
+ * after detach). The array is reused between frames — read, don't hold. */
181
+ getBlendEntries(): BlendEntry[] | null;
182
+ private emit;
105
183
  private clipDuration;
106
184
  /** Advance one frame: integrates yaw + position, updates the clip clocks, hands the
107
185
  * weighted pose to the model, and returns the root transform to apply. */
@@ -113,5 +191,21 @@ export declare class LocomotionController {
113
191
  * nearest the local move angle, per gear, all sharing one gait phase. Root motion
114
192
  * follows the input direction at the pair's authored (angle-lerped) speed. */
115
193
  private updateStrafe;
194
+ /** Authored turn-in-place frame: the clip rotates the body through its bones while
195
+ * the root yaw stays frozen; at exitTime the measured angle transfers to the root
196
+ * in the same instant the pose hands back to idle — the same orientation expressed
197
+ * two ways, so the cut is seamless. No translation during the turn. */
198
+ private updateTurnClip;
199
+ /** Authored-stop frame: root follows the clip's measured skid profile along the
200
+ * release heading; input returning interrupts and resumes locomotion at a level
201
+ * proportional to how much of the stop remains. */
202
+ private updateStop;
203
+ /** Linear interpolation over a uniformly sampled profile at t in [0, 1]. */
204
+ private static profileAt;
205
+ /** Running-reversal frame: bones carry the turn while the root travels the clip's
206
+ * AUTHORED forward profile along the trigger heading (overrun, plant, return) —
207
+ * no fabricated motion, so the feet stay glued. At exitTime the yaw transfers
208
+ * and the normal path runs her out along the new heading. */
209
+ private updateRunTurn;
116
210
  }
117
211
  //# sourceMappingURL=locomotion.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"locomotion.d.ts","sourceRoot":"","sources":["../src/locomotion.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAEnC,MAAM,WAAW,eAAe;IAC9B,gDAAgD;IAChD,IAAI,EAAE,MAAM,CAAA;IACZ,kGAAkG;IAClG,KAAK,EAAE,MAAM,CAAA;IACb,kGAAkG;IAClG,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,eAAe;IAC9B,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;kEAC8D;IAC9D,SAAS,CAAC,EAAE,eAAe,EAAE,CAAA;IAC7B,YAAY,CAAC,EAAE,eAAe,EAAE,CAAA;CACjC;AAED,MAAM,WAAW,iBAAiB;IAChC;;qDAEiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iFAAiF;IACjF,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,sFAAsF;IACtF,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,uEAAuE;IACvE,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;iFAC6E;IAC7E,oBAAoB,CAAC,EAAE,MAAM,CAAA;IAC7B,sEAAsE;IACtE,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,6EAA6E;IAC7E,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;qEACiE;I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YAAY,CAA0B;IACvD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAc;gBAEhC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,iBAAiB;IAkC7E;;yDAEqD;IACrD,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,UAAQ,GAAG,IAAI;IAYnD;;;2EAGuE;IACvE,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,UAAQ,GAAG,IAAI;IAO9D,sDAAsD;IACtD,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,SAAI,GAAG,IAAI;IAKxD;;iEAE6D;IAC7D,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAInC,WAAW,IAAI,IAAI;IAInB,4FAA4F;IAC5F,MAAM,IAAI,IAAI;IAKd;gFAC4E;IAC5E,eAAe,IAAI,UAAU,EAAE,GAAG,IAAI;IAItC,OAAO,CAAC,IAAI;IAKZ,OAAO,CAAC,YAAY;IAKpB;+EAC2E;IAC3E,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,cAAc;IAmNlC;+EAC2E;IAC3E,OAAO,CAAC,QAAQ;IAYhB;;mFAE+E;IAC/E,OAAO,CAAC,YAAY;IA2FpB;;;4EAGwE;IACxE,OAAO,CAAC,cAAc;IA6CtB;;wDAEoD;IACpD,OAAO,CAAC,UAAU;IA4ElB,4EAA4E;IAC5E,OAAO,CAAC,MAAM,CAAC,SAAS;IAQxB;;;kEAG8D;IAC9D,OAAO,CAAC,aAAa;CAkDtB"}
@@ -15,6 +15,13 @@ function wrapAngle(a) {
15
15
  }
16
16
  export class LocomotionController {
17
17
  constructor(model, clips, options) {
18
+ this.lastEntries = null;
19
+ this.turning = null;
20
+ this.runTurning = null;
21
+ /** An interrupted authored clip fading out OVER resumed locomotion, so breaking
22
+ * out of a stop is instantly responsive without a pose pop. */
23
+ this.exitGhost = null;
24
+ this.stopping = null;
18
25
  this.inputX = 0;
19
26
  this.inputY = 0;
20
27
  this.inputSprint = false;
@@ -23,6 +30,10 @@ export class LocomotionController {
23
30
  this.inputForward = 0;
24
31
  this.inputSteer = 0;
25
32
  this.speedLevel = 0;
33
+ /** Peak-hold of speedLevel (decays ~1.5/s): reversal triggers read this, because
34
+ * keyboard direction flips pass through a dead moment (W+S cancel, key gap) that
35
+ * dips the instantaneous level right when the reversal input lands. */
36
+ this.recentSpeed = 0;
26
37
  this.yaw = 0;
27
38
  // Movement direction = the INPUT heading, not the body yaw. The body turns
28
39
  // cosmetically toward it; translating along the (sweeping) body yaw instead
@@ -40,8 +51,17 @@ export class LocomotionController {
40
51
  this.runSpeed = options?.runSpeed ?? 67;
41
52
  this.sprintSpeed = options?.sprintSpeed ?? 92;
42
53
  this.speedResponse = options?.speedResponse ?? 5;
54
+ this.autoApply = options?.autoApply ?? true;
43
55
  this.turnResponse = options?.turnResponse ?? 10;
44
56
  this.cosTurnThreshold = Math.cos(options?.turnInPlaceThreshold ?? Math.PI / 4);
57
+ this.turnClipMinAngle = options?.turnClipMinAngle ?? (100 * Math.PI) / 180;
58
+ this.turnTimeScale = options?.turnTimeScale ?? 1.4;
59
+ this.stopTimeScale = options?.stopTimeScale ?? 1.25;
60
+ this.turnEntries = [
61
+ { name: clips.idle, time: 0, weight: 0 },
62
+ { name: clips.idle, time: 0, weight: 1 },
63
+ { name: clips.idle, time: 0, weight: 0 },
64
+ ];
45
65
  this.steerRate = options?.steerRate ?? 2.5;
46
66
  this.backpedalScale = options?.backpedalScale ?? 0.5;
47
67
  this.yawOffset = options?.yawOffset ?? Math.PI;
@@ -49,6 +69,7 @@ export class LocomotionController {
49
69
  { name: clips.idle, time: 0, weight: 1 },
50
70
  { name: clips.run, time: 0, weight: 0 },
51
71
  { name: clips.sprint ?? clips.run, time: 0, weight: 0 },
72
+ { name: clips.idle, time: 0, weight: 0 }, // fading ghost of an interrupted clip
52
73
  ];
53
74
  const byAngle = (a, b) => a.angle - b.angle;
54
75
  this.strafeRun = clips.strafeRun ? [...clips.strafeRun].sort(byAngle) : null;
@@ -97,7 +118,19 @@ export class LocomotionController {
97
118
  }
98
119
  /** Stop driving the model's pose (the blend is cleared; the single-clip player resumes). */
99
120
  detach() {
100
- this.model.clearBlendPose();
121
+ this.lastEntries = null;
122
+ if (this.autoApply)
123
+ this.model.clearBlendPose();
124
+ }
125
+ /** The most recent update()'s blend entries (null before the first update or
126
+ * after detach). The array is reused between frames — read, don't hold. */
127
+ getBlendEntries() {
128
+ return this.lastEntries;
129
+ }
130
+ emit(entries) {
131
+ this.lastEntries = entries;
132
+ if (this.autoApply)
133
+ this.model.setBlendPose(entries);
101
134
  }
102
135
  clipDuration(name) {
103
136
  const frames = this.model.getClip(name)?.frameCount ?? 0;
@@ -111,6 +144,15 @@ export class LocomotionController {
111
144
  if (this.facingYaw !== null && this.strafeRun !== null && !this.tankMode) {
112
145
  return this.updateStrafe(dt);
113
146
  }
147
+ if (this.turning !== null) {
148
+ return this.updateTurnClip(dt);
149
+ }
150
+ if (this.runTurning !== null) {
151
+ return this.updateRunTurn(dt);
152
+ }
153
+ if (this.stopping !== null) {
154
+ return this.updateStop(dt);
155
+ }
114
156
  const hasSprint = this.clips.sprint !== undefined;
115
157
  let moving;
116
158
  let align = 1;
@@ -131,12 +173,99 @@ export class LocomotionController {
131
173
  else {
132
174
  const m = Math.hypot(this.inputX, this.inputY);
133
175
  moving = m > 0.05;
176
+ this.recentSpeed = Math.max(this.speedLevel, this.recentSpeed - dt * 1.5);
177
+ // Release at speed: play the authored stop instead of blending to idle.
178
+ const stops = this.clips.stop;
179
+ // Gate on the peak-hold speed: the release decay (throttle/analog) drains the
180
+ // instantaneous level below any threshold before `moving` goes false. The
181
+ // small floor on the actual level keeps standstill taps from faking a skid.
182
+ if (!moving && stops && stops.length > 0 && this.recentSpeed >= 0.7 && this.speedLevel >= 0.35) {
183
+ const gear = this.recentSpeed > 1.4 ? "sprint" : "run";
184
+ const foot = this.gaitPhase < 0.5 ? "L" : "R";
185
+ // Gear must match exactly: supplying stops for only one gear means the
186
+ // other gear keeps the default quick blend-to-idle.
187
+ let best = null;
188
+ let bestScore = -1;
189
+ for (const e of stops) {
190
+ if (e.gear !== gear)
191
+ continue;
192
+ const score = e.foot === foot ? 1 : 0;
193
+ if (score > bestScore) {
194
+ best = e;
195
+ bestScore = score;
196
+ }
197
+ }
198
+ if (best) {
199
+ const fromClip = gear === "sprint" && this.clips.sprint ? this.clips.sprint : this.clips.run;
200
+ this.stopping = {
201
+ entry: best,
202
+ time: 0,
203
+ startX: this.position.x,
204
+ startZ: this.position.z,
205
+ dirX: this.dirX,
206
+ dirZ: this.dirZ,
207
+ startLevel: Math.min(this.recentSpeed, 2),
208
+ fromIdleW: Math.max(0, 1 - this.speedLevel),
209
+ fromClip,
210
+ fromTime: this.gaitPhase * this.clipDuration(fromClip),
211
+ };
212
+ return this.updateStop(dt);
213
+ }
214
+ }
134
215
  // Yaw eases toward the input heading only while there is one. Turns are strictly
135
216
  // in place: zero translation outside the threshold cone, ramping smoothly to full
136
217
  // speed as the body aligns — so direction reversals (L-R-L) cannot drift.
137
218
  if (moving) {
138
219
  const desired = Math.atan2(this.inputX, this.inputY);
139
220
  const err = wrapAngle(desired - this.yaw);
221
+ // Running reversal: while moving fast with a reversal-class error, play the
222
+ // authored plant-and-turn for the current gear and gait foot.
223
+ const runTurns = this.clips.runTurn;
224
+ if (runTurns && runTurns.length > 0 && this.recentSpeed >= 0.6 && Math.abs(err) >= (130 * Math.PI) / 180) {
225
+ const gear = this.recentSpeed > 1.4 ? "sprint" : "run";
226
+ const foot = this.gaitPhase < 0.5 ? "L" : "R";
227
+ const side = err < 0 ? -1 : 1;
228
+ let best = null;
229
+ let bestScore = -1;
230
+ for (const e of runTurns) {
231
+ if (Math.sign(e.angle) !== side)
232
+ continue;
233
+ const score = (e.gear === gear ? 2 : 0) + (e.foot === foot ? 1 : 0);
234
+ if (score > bestScore) {
235
+ best = e;
236
+ bestScore = score;
237
+ }
238
+ }
239
+ if (best) {
240
+ // Restore the pre-dip speed so she runs OUT of the turn at pace.
241
+ this.speedLevel = Math.max(this.speedLevel, Math.min(this.recentSpeed, 2));
242
+ this.runTurning = {
243
+ entry: best,
244
+ time: 0,
245
+ startX: this.position.x,
246
+ startZ: this.position.z,
247
+ dirX: Math.sin(this.yaw),
248
+ dirZ: Math.cos(this.yaw),
249
+ };
250
+ return this.updateRunTurn(dt);
251
+ }
252
+ }
253
+ // Reversal-class turn from near-standstill: play the authored turn clip
254
+ // whose angle is nearest the error instead of the eased pivot.
255
+ const turnClips = this.clips.turnInPlace;
256
+ if (turnClips && turnClips.length > 0 && this.speedLevel < 0.3 && Math.abs(err) >= this.turnClipMinAngle) {
257
+ let best = turnClips[0];
258
+ let bestD = Math.abs(wrapAngle(err - best.angle));
259
+ for (const e of turnClips) {
260
+ const d = Math.abs(wrapAngle(err - e.angle));
261
+ if (d < bestD) {
262
+ best = e;
263
+ bestD = d;
264
+ }
265
+ }
266
+ this.turning = { entry: best, time: 0 };
267
+ return this.updateTurnClip(dt);
268
+ }
140
269
  this.yaw = wrapAngle(this.yaw + err * Math.min(1, this.turnResponse * dt));
141
270
  align = Math.max(0, (Math.cos(err) - this.cosTurnThreshold) / (1 - this.cosTurnThreshold));
142
271
  this.dirX = this.inputX / m;
@@ -186,7 +315,28 @@ export class LocomotionController {
186
315
  this.entries[1].weight = wRun;
187
316
  this.entries[2].time = this.gaitPhase * sprintDur;
188
317
  this.entries[2].weight = wSprint;
189
- this.model.setBlendPose(this.entries);
318
+ const GHOST_FADE = 0.25;
319
+ if (this.exitGhost) {
320
+ const g = this.exitGhost;
321
+ g.elapsed += dt;
322
+ const wGhost = Math.max(0, 1 - g.elapsed / GHOST_FADE);
323
+ if (wGhost <= 0) {
324
+ this.exitGhost = null;
325
+ this.entries[3].weight = 0;
326
+ }
327
+ else {
328
+ this.entries[0].weight *= 1 - wGhost;
329
+ this.entries[1].weight *= 1 - wGhost;
330
+ this.entries[2].weight *= 1 - wGhost;
331
+ this.entries[3].name = g.clip;
332
+ this.entries[3].time = g.clipTime + g.elapsed; // the clip's real tail keeps playing
333
+ this.entries[3].weight = wGhost;
334
+ }
335
+ }
336
+ else {
337
+ this.entries[3].weight = 0;
338
+ }
339
+ this.emit(this.entries);
190
340
  const ry = this.yaw + this.yawOffset;
191
341
  const half = ry * 0.5;
192
342
  this.rotation.setXYZW(0, Math.sin(half), 0, Math.cos(half));
@@ -286,7 +436,7 @@ export class LocomotionController {
286
436
  e[4].name = sprintB.clip;
287
437
  e[4].time = this.gaitPhase * sprintDur;
288
438
  e[4].weight = wSprint * sprintT;
289
- this.model.setBlendPose(e);
439
+ this.emit(e);
290
440
  const ry = this.yaw + this.yawOffset;
291
441
  const half = ry * 0.5;
292
442
  this.rotation.setXYZW(0, Math.sin(half), 0, Math.cos(half));
@@ -294,4 +444,183 @@ export class LocomotionController {
294
444
  this.pose.speedLevel = level;
295
445
  return this.pose;
296
446
  }
447
+ /** Authored turn-in-place frame: the clip rotates the body through its bones while
448
+ * the root yaw stays frozen; at exitTime the measured angle transfers to the root
449
+ * in the same instant the pose hands back to idle — the same orientation expressed
450
+ * two ways, so the cut is seamless. No translation during the turn. */
451
+ updateTurnClip(dt) {
452
+ const t = this.turning;
453
+ t.time += dt * this.turnTimeScale;
454
+ // Speed level settles toward 0 while turning (we were near-standstill already).
455
+ const maxStep = this.speedResponse * dt;
456
+ this.speedLevel += Math.abs(-this.speedLevel) <= maxStep ? -this.speedLevel : -Math.sign(this.speedLevel) * maxStep;
457
+ const idleDur = this.clipDuration(this.clips.idle);
458
+ this.idleTime = (this.idleTime + dt) % idleDur;
459
+ if (t.time >= t.entry.exitTime) {
460
+ // Transfer the angle to the root and hand the pose to idle IN THE SAME frame —
461
+ // leaving the previous blend up would double the rotation for one frame.
462
+ this.yaw = wrapAngle(this.yaw + t.entry.angle);
463
+ this.turning = null;
464
+ const e = this.turnEntries;
465
+ e[0].weight = 0;
466
+ e[1].name = this.clips.idle;
467
+ e[1].time = this.idleTime;
468
+ e[1].weight = 1;
469
+ e[2].weight = 0;
470
+ this.emit(e);
471
+ }
472
+ else {
473
+ // Fade the turn clip over idle at the edges so entry doesn't pop.
474
+ const w = Math.min(1, t.time / 0.12);
475
+ const e = this.turnEntries;
476
+ e[0].name = t.entry.clip;
477
+ e[0].time = Math.min(t.time, t.entry.exitTime);
478
+ e[0].weight = w;
479
+ e[1].name = this.clips.idle;
480
+ e[1].time = this.idleTime;
481
+ e[1].weight = 1 - w;
482
+ e[2].weight = 0;
483
+ this.emit(e);
484
+ }
485
+ const ry = this.yaw + this.yawOffset;
486
+ const half = ry * 0.5;
487
+ this.rotation.setXYZW(0, Math.sin(half), 0, Math.cos(half));
488
+ this.pose.yaw = this.yaw;
489
+ this.pose.speedLevel = this.speedLevel;
490
+ return this.pose;
491
+ }
492
+ /** Authored-stop frame: root follows the clip's measured skid profile along the
493
+ * release heading; input returning interrupts and resumes locomotion at a level
494
+ * proportional to how much of the stop remains. */
495
+ updateStop(dt) {
496
+ const t = this.stopping;
497
+ const entry = t.entry;
498
+ // Interrupt: input came back — locomotion resumes instantly from a LOW level
499
+ // (a stop is committed; breaking out is a fresh walk-up, never a drift), while
500
+ // the stop pose fades out as a ghost instead of hard-cutting.
501
+ if (Math.hypot(this.inputX, this.inputY) > 0.05) {
502
+ this.speedLevel = Math.min(t.startLevel * (1 - Math.min(1, t.time / entry.exitTime)), 0.3);
503
+ this.recentSpeed = this.speedLevel;
504
+ this.exitGhost = { clip: entry.clip, clipTime: Math.min(t.time, entry.exitTime), elapsed: 0 };
505
+ this.stopping = null;
506
+ return this.update(dt);
507
+ }
508
+ t.time += dt * this.stopTimeScale;
509
+ const clipT = Math.min(t.time, entry.exitTime);
510
+ const fwd = LocomotionController.profileAt(entry.forward, clipT / entry.exitTime);
511
+ this.position.x = t.startX + t.dirX * fwd;
512
+ this.position.z = t.startZ + t.dirZ * fwd;
513
+ this.speedLevel = t.startLevel * Math.max(0, 1 - t.time / entry.exitTime);
514
+ const idleDur = this.clipDuration(this.clips.idle);
515
+ this.idleTime = (this.idleTime + dt) % idleDur;
516
+ const FADE_OUT = 0.35;
517
+ if (t.time >= entry.exitTime + FADE_OUT) {
518
+ this.stopping = null;
519
+ this.speedLevel = 0;
520
+ this.gaitPhase = 0;
521
+ const e = this.turnEntries;
522
+ e[0].weight = 0;
523
+ e[1].name = this.clips.idle;
524
+ e[1].time = this.idleTime;
525
+ e[1].weight = 1;
526
+ e[2].weight = 0;
527
+ this.emit(e);
528
+ }
529
+ else if (t.time >= entry.exitTime) {
530
+ // Settle tail: the root is already still, so keep playing the clip's own
531
+ // recovery (it exists past exitTime) while fading to idle — no hard cut.
532
+ const wOut = (t.time - entry.exitTime) / FADE_OUT;
533
+ const e = this.turnEntries;
534
+ e[0].name = entry.clip;
535
+ e[0].time = t.time; // real tail frames beyond exitTime
536
+ e[0].weight = 1 - wOut;
537
+ e[1].name = this.clips.idle;
538
+ e[1].time = this.idleTime;
539
+ e[1].weight = wOut;
540
+ e[2].weight = 0;
541
+ this.emit(e);
542
+ }
543
+ else {
544
+ const fromDur = this.clipDuration(t.fromClip);
545
+ t.fromTime = (t.fromTime + dt) % fromDur;
546
+ const w = Math.min(1, t.time / 0.25);
547
+ const e = this.turnEntries;
548
+ e[0].name = entry.clip;
549
+ e[0].time = clipT;
550
+ e[0].weight = w;
551
+ e[1].name = t.fromClip;
552
+ e[1].time = t.fromTime;
553
+ e[1].weight = (1 - w) * (1 - t.fromIdleW);
554
+ e[2].name = this.clips.idle;
555
+ e[2].time = this.idleTime;
556
+ e[2].weight = (1 - w) * t.fromIdleW;
557
+ this.emit(e);
558
+ }
559
+ const ry = this.yaw + this.yawOffset;
560
+ const half = ry * 0.5;
561
+ this.rotation.setXYZW(0, Math.sin(half), 0, Math.cos(half));
562
+ this.pose.yaw = this.yaw;
563
+ this.pose.speedLevel = this.speedLevel;
564
+ return this.pose;
565
+ }
566
+ /** Linear interpolation over a uniformly sampled profile at t in [0, 1]. */
567
+ static profileAt(samples, t) {
568
+ const n = samples.length - 1;
569
+ if (n <= 0)
570
+ return 0;
571
+ const x = Math.min(Math.max(t, 0), 1) * n;
572
+ const i = Math.min(n - 1, Math.floor(x));
573
+ return samples[i] + (samples[i + 1] - samples[i]) * (x - i);
574
+ }
575
+ /** Running-reversal frame: bones carry the turn while the root travels the clip's
576
+ * AUTHORED forward profile along the trigger heading (overrun, plant, return) —
577
+ * no fabricated motion, so the feet stay glued. At exitTime the yaw transfers
578
+ * and the normal path runs her out along the new heading. */
579
+ updateRunTurn(dt) {
580
+ const t = this.runTurning;
581
+ t.time += dt;
582
+ const entry = t.entry;
583
+ const clipT = Math.min(t.time, entry.exitTime);
584
+ const fwd = LocomotionController.profileAt(entry.forward, clipT / entry.exitTime);
585
+ this.position.x = t.startX + t.dirX * fwd;
586
+ this.position.z = t.startZ + t.dirZ * fwd;
587
+ const idleDur = this.clipDuration(this.clips.idle);
588
+ this.idleTime = (this.idleTime + dt) % idleDur;
589
+ if (t.time >= entry.exitTime) {
590
+ this.yaw = wrapAngle(this.yaw + entry.angle);
591
+ this.runTurning = null;
592
+ // She exits mid-stride: keep the speed level, restart the gait cleanly, and
593
+ // point the travel direction along the new heading so the next frame runs out.
594
+ this.gaitPhase = 0;
595
+ this.dirX = Math.sin(this.yaw);
596
+ this.dirZ = Math.cos(this.yaw);
597
+ const e = this.turnEntries;
598
+ e[0].name = this.clips.run;
599
+ e[0].time = 0;
600
+ e[0].weight = Math.min(1, this.speedLevel);
601
+ e[1].name = this.clips.idle;
602
+ e[1].time = this.idleTime;
603
+ e[1].weight = 1 - Math.min(1, this.speedLevel);
604
+ e[2].weight = 0;
605
+ this.emit(e);
606
+ }
607
+ else {
608
+ const w = Math.min(1, t.time / 0.1);
609
+ const e = this.turnEntries;
610
+ e[0].name = entry.clip;
611
+ e[0].time = clipT;
612
+ e[0].weight = w;
613
+ e[1].name = this.clips.run;
614
+ e[1].time = this.gaitPhase * this.clipDuration(this.clips.run);
615
+ e[1].weight = 1 - w;
616
+ e[2].weight = 0;
617
+ this.emit(e);
618
+ }
619
+ const ry = this.yaw + this.yawOffset;
620
+ const half = ry * 0.5;
621
+ this.rotation.setXYZW(0, Math.sin(half), 0, Math.cos(half));
622
+ this.pose.yaw = this.yaw;
623
+ this.pose.speedLevel = this.speedLevel;
624
+ return this.pose;
625
+ }
297
626
  }
package/dist/model.d.ts CHANGED
@@ -2,6 +2,13 @@ import { Mat4, Quat, Vec3 } from "./math";
2
2
  import { type AssetReader } from "./asset-reader";
3
3
  import { Rigidbody, Joint } from "./physics";
4
4
  import { AnimationClip, AnimationPlayOptions, AnimationProgress, BlendEntry } from "./animation";
5
+ export interface ClipEventInfo {
6
+ clip: string;
7
+ /** The registered event time, seconds. */
8
+ time: number;
9
+ /** The clip's blend weight at the moment of firing. */
10
+ weight: number;
11
+ }
5
12
  export interface Texture {
6
13
  path: string;
7
14
  name: string;
@@ -157,6 +164,10 @@ export declare class Model {
157
164
  private lastAppliedClip;
158
165
  private oneShot;
159
166
  private readonly oneShotEntries;
167
+ /** Time-triggered clip callbacks (footsteps, skill timing). Fired by every
168
+ * playback path when a clip's cursor crosses the event time with weight. */
169
+ private readonly clipEvents;
170
+ private readonly entryEventPrev;
160
171
  private blendEntries;
161
172
  private crossfade;
162
173
  private readonly blendBoneCursors;
@@ -253,6 +264,18 @@ export declare class Model {
253
264
  cancelOneShot(fadeOut?: number): void;
254
265
  /** Name of the active one-shot, or null. */
255
266
  getOneShot(): string | null;
267
+ /** Fire `callback` whenever `clip`'s playback crosses `time` (seconds) with at
268
+ * least `minWeight` influence (default 0.1) — on any path: blend entries,
269
+ * one-shots, crossfades, or plain play. Loop wraps fire correctly; hard cursor
270
+ * jumps may occasionally fire or skip (events are for sfx-grade timing, not
271
+ * logic). Returns an unsubscribe function. */
272
+ addClipEvent(clip: string, time: number, callback: (e: ClipEventInfo) => void, options?: {
273
+ minWeight?: number;
274
+ }): () => void;
275
+ private fireClipEvents;
276
+ /** Per-entry cursor memory for event crossing detection; keyed on entry object
277
+ * identity (controllers reuse their arrays, so this stays warm). */
278
+ private trackEntryEvents;
256
279
  /** Fade from the currently playing clip (or from the rest pose when nothing plays)
257
280
  * into `name` over `seconds`. The target starts at frame 0 and becomes the current
258
281
  * clip immediately — progress, looping and the camera clock report the target for