@vgai/engine 0.2.0 → 0.4.0-canary.20260715.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.
Files changed (123) hide show
  1. package/README.md +3 -1
  2. package/package.json +24 -4
  3. package/schemas/engine-api.json +124 -0
  4. package/schemas/engine-api.md +53 -0
  5. package/schemas/engine-capabilities.json +124 -0
  6. package/schemas/inputmap.schema.json +314 -0
  7. package/schemas/mat.schema.json +286 -0
  8. package/schemas/prefab.schema.json +10148 -0
  9. package/schemas/scn2d.schema.json +475 -0
  10. package/schemas/vgai-game.schema.json +383 -0
  11. package/schemas/vscn.schema.json +11007 -0
  12. package/src/adapter/{world-kind.ts → adapter-surface.ts} +6 -6
  13. package/src/adapter/authoring.ts +77 -0
  14. package/src/adapter/first-party-systems.ts +23 -34
  15. package/src/adapter/game-adapter.ts +8 -8
  16. package/src/adapter/host-context.ts +2 -4
  17. package/src/adapter/index.ts +4 -4
  18. package/src/adapter/system-adapter.ts +88 -22
  19. package/src/adapter/vgai-scene-game-adapter.ts +244 -194
  20. package/src/animation/anim-graph-types.ts +12 -43
  21. package/src/animation/animation-clock.ts +479 -0
  22. package/src/animation/camera-ownership.ts +467 -0
  23. package/src/animation/cinematic-cues.ts +451 -0
  24. package/src/animation/clip-map.ts +41 -0
  25. package/src/animation/gsap-registration.ts +184 -0
  26. package/src/animation/theatre-clock-binding.ts +111 -0
  27. package/src/animation/theatre-director.ts +347 -0
  28. package/src/animation/theatre-object-binding.ts +661 -0
  29. package/src/animation/xstate-animation-binding.ts +436 -0
  30. package/src/animation/xstate-animation-meta.ts +319 -0
  31. package/src/audio/index.ts +39 -7
  32. package/src/audio/tone-clock-binding.ts +98 -0
  33. package/src/audio/tone-context.ts +129 -0
  34. package/src/audio/tone-offline-render.ts +167 -0
  35. package/src/audio/wav-encode.ts +119 -0
  36. package/src/character/cloth-sim.ts +533 -0
  37. package/src/character/spring-chain.ts +307 -0
  38. package/src/core/game-loop.ts +57 -2
  39. package/src/core/seeded-random.ts +161 -0
  40. package/src/core/system-runner.ts +20 -3
  41. package/src/core/types.ts +50 -0
  42. package/src/data/data-asset.ts +167 -0
  43. package/src/data/data-check-core.ts +242 -0
  44. package/src/data/data-ref.ts +145 -0
  45. package/src/data/vite-plugin-data.ts +290 -0
  46. package/src/dev/performance-profiler.ts +213 -0
  47. package/src/dev/webgl-gpu-timer.ts +53 -0
  48. package/src/ecs/component-manager.ts +45 -12
  49. package/src/ecs/game-component.ts +95 -11
  50. package/src/humanoid/bake.operation.ts +326 -0
  51. package/src/humanoid/body.ts +663 -0
  52. package/src/humanoid/clips.ts +149 -0
  53. package/src/humanoid/compose.ts +209 -0
  54. package/src/humanoid/generate.ts +189 -0
  55. package/src/humanoid/index.ts +36 -0
  56. package/src/humanoid/schema.ts +108 -0
  57. package/src/humanoid/skeleton.ts +345 -0
  58. package/src/index.ts +48 -0
  59. package/src/input/input-manager.ts +1886 -33
  60. package/src/input/input-types.ts +158 -3
  61. package/src/input/prompt-labels.ts +122 -0
  62. package/src/input/rebind-controller.ts +105 -0
  63. package/src/input/schema.ts +206 -52
  64. package/src/manifest/index.ts +5 -5
  65. package/src/manifest/load.ts +125 -72
  66. package/src/manifest/schema.ts +362 -255
  67. package/src/react/game-state.tsx +135 -32
  68. package/src/react/root-adapter.tsx +49 -0
  69. package/src/react/unmanaged-root-detector.ts +66 -0
  70. package/src/react/use-data.ts +124 -0
  71. package/src/react/use-selection.tsx +135 -0
  72. package/src/runtime/create-runtime.ts +112 -273
  73. package/src/runtime/debug-bridge.ts +483 -0
  74. package/src/runtime/debug-registry.ts +856 -0
  75. package/src/runtime/game.ts +342 -93
  76. package/src/runtime/gameplay-rng-trap.ts +134 -0
  77. package/src/runtime/input-router.ts +7 -7
  78. package/src/runtime/mount-game.ts +40 -38
  79. package/src/runtime/mount-manifest.ts +169 -37
  80. package/src/runtime/render-audio-control.ts +168 -0
  81. package/src/runtime/render-control.ts +522 -0
  82. package/src/runtime/render-seed.ts +79 -0
  83. package/src/runtime/state-bridge.ts +24 -10
  84. package/src/runtime/types.ts +110 -33
  85. package/src/scene/asset-loaders.ts +10 -36
  86. package/src/scene/asset-paths.ts +0 -2
  87. package/src/scene/asset-ref-check.ts +248 -0
  88. package/src/scene/asset-registry.ts +22 -0
  89. package/src/scene/component-registry.ts +14 -3
  90. package/src/scene/defaults.ts +1 -0
  91. package/src/scene/light-camera-factory.ts +11 -3
  92. package/src/scene/parse.ts +133 -0
  93. package/src/scene/scene-apply.ts +55 -4
  94. package/src/scene/scene-loader.ts +91 -123
  95. package/src/scene/scene-types.ts +0 -1
  96. package/src/scene/schema/animation.ts +30 -79
  97. package/src/scene/schema/entity.ts +20 -0
  98. package/src/scene/schema/index.ts +2 -46
  99. package/src/scene/schema/light.ts +16 -1
  100. package/src/scene/schema/material.ts +96 -91
  101. package/src/scene/schema/scene-file.ts +1 -7
  102. package/src/scene/user-data.ts +22 -10
  103. package/src/setup/setup-renderer.ts +10 -3
  104. package/src/tools/define-tool.ts +191 -0
  105. package/src/world2d/authoring-2d.ts +17 -1
  106. package/src/world2d/collision-2d.ts +1 -1
  107. package/src/world2d/pixi-game-adapter.ts +19 -17
  108. package/src/world2d/scene2d-loader.ts +1 -0
  109. package/src/world2d/types.ts +8 -2
  110. package/src/animation/anim-graph.ts +0 -406
  111. package/src/animation/anim-system.ts +0 -28
  112. package/src/animation/property-track.ts +0 -178
  113. package/src/animation/schema.ts +0 -204
  114. package/src/audio/ambient.ts +0 -300
  115. package/src/audio/impacts.ts +0 -212
  116. package/src/audio/movement.ts +0 -140
  117. package/src/audio/musical.ts +0 -200
  118. package/src/audio/ui-sounds.ts +0 -171
  119. package/src/audio/vehicle.ts +0 -235
  120. package/src/audio/weapons.ts +0 -152
  121. package/src/runtime/scene-ui-bridge.ts +0 -86
  122. package/src/runtime/scene-ui-data.ts +0 -119
  123. package/src/scene/schema/ui.ts +0 -602
@@ -0,0 +1,319 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * E1 — Zod/TypeScript metadata attached to XState state `meta.animation`
5
+ * (spec §11 E1, §5.6, §3.3).
6
+ *
7
+ * This module defines ONLY animation data: which clip(s) a state plays, how
8
+ * they loop/blend/crossfade, and (for locomotion-style states) how a 1D/2D/
9
+ * direct blend tree distributes weight across several clips from a live
10
+ * parameter. It intentionally contains **no transition vocabulary** — no
11
+ * `on`/`target`/`guard`/`always`/`after`/`entry`/`exit`. XState alone owns
12
+ * states, transitions, events, and guards (§3.3); this metadata is read-only
13
+ * data a binding (`xstate-animation-binding.ts`, E2) consumes to drive a
14
+ * `THREE.AnimationMixer` when XState enters/exits a state. Every schema below
15
+ * is `.strict()` specifically so that smuggling a transition-shaped key into
16
+ * `meta.animation` is a loud Zod "unrecognized key" error, not a silently
17
+ * ignored field (E1 AC: "Metadata contains no transition language").
18
+ *
19
+ * ## Native layered composition
20
+ *
21
+ * Parallel XState regions may drive independent named layers. Each layer can
22
+ * filter a clip to selected bone subtrees, scale its weight, and use normal
23
+ * override or Three's native additive blend mode. XState still owns all
24
+ * state/transition semantics; these fields only describe how the active
25
+ * state's native `AnimationAction`s are composed by the binding.
26
+ *
27
+ * ## Blend tree shape
28
+ *
29
+ * Deliberately structurally IDENTICAL to `anim-graph-types.ts`'s
30
+ * `BlendTreeDef` (same `type`/`parameter`/`parameterY`/`children[].clip`/
31
+ * `.threshold`/`.thresholdY`/`.weight` fields) so the binding can hand a
32
+ * parsed `AnimationBlendTree` straight to the existing, already-tested
33
+ * `evaluateBlendTree` (`blend-node.ts`) with zero conversion — E2 reuses that
34
+ * function verbatim rather than re-implementing 1D/2D/direct blend math.
35
+ */
36
+
37
+ // ---------------------------------------------------------------------------
38
+ // Blend tree children
39
+ // ---------------------------------------------------------------------------
40
+
41
+ const AnimationBlendChildSchema = z
42
+ .object({
43
+ clip: z.string().min(1).describe('Animation clip name this child plays'),
44
+ threshold: z.number().describe('1D/2D blend position on the primary parameter axis'),
45
+ thresholdY: z
46
+ .number()
47
+ .optional()
48
+ .describe('2D blend position on the secondary parameter axis (2D blend trees only)'),
49
+ weight: z
50
+ .number()
51
+ .min(0)
52
+ .optional()
53
+ .describe('Explicit weight for "direct" blend trees (ignored by 1D/2D)'),
54
+ })
55
+ .strict();
56
+
57
+ export type AnimationBlendChild = z.infer<typeof AnimationBlendChildSchema>;
58
+
59
+ const AnimationBlendTree1DSchema = z
60
+ .object({
61
+ type: z.literal('1D').describe('Interpolates linearly between sorted children by threshold'),
62
+ parameter: z.string().min(1).describe('Context/selector value driving the blend'),
63
+ children: z
64
+ .array(AnimationBlendChildSchema)
65
+ .min(2, '1D blend tree needs at least 2 children to interpolate between'),
66
+ })
67
+ .strict();
68
+
69
+ const AnimationBlendTree2DSchema = z
70
+ .object({
71
+ type: z
72
+ .literal('2D')
73
+ .describe('Inverse-distance-weights children by (parameter, parameterY) position'),
74
+ parameter: z.string().min(1).describe('Primary axis context/selector value'),
75
+ parameterY: z.string().min(1).describe('Secondary axis context/selector value'),
76
+ children: z
77
+ .array(AnimationBlendChildSchema.extend({ thresholdY: z.number() }))
78
+ .min(1, '2D blend tree needs at least 1 child'),
79
+ })
80
+ .strict();
81
+
82
+ const AnimationBlendTreeDirectSchema = z
83
+ .object({
84
+ type: z.literal('direct').describe('Each child has an explicit weight, normalized to sum to 1'),
85
+ // `parameter` is unused by direct blending (evaluateBlendTree never reads
86
+ // it for `type: 'direct'`) but kept required+defaulted so the parsed
87
+ // value stays structurally assignable to `BlendTreeDef`
88
+ // (`anim-graph-types.ts`), which declares `parameter: string` for every
89
+ // blend mode — the same convention that type already uses.
90
+ parameter: z.string().default(''),
91
+ children: z
92
+ .array(AnimationBlendChildSchema.extend({ weight: z.number().min(0) }))
93
+ .min(1, 'direct blend tree needs at least 1 child'),
94
+ })
95
+ .strict();
96
+
97
+ export const AnimationBlendTreeSchema = z.discriminatedUnion('type', [
98
+ AnimationBlendTree1DSchema,
99
+ AnimationBlendTree2DSchema,
100
+ AnimationBlendTreeDirectSchema,
101
+ ]);
102
+
103
+ export type AnimationBlendTree = z.infer<typeof AnimationBlendTreeSchema>;
104
+
105
+ // ---------------------------------------------------------------------------
106
+ // Crossfade / blend-in-out metadata (shared by clip and blend-tree states)
107
+ // ---------------------------------------------------------------------------
108
+
109
+ const AnimationCrossfadeMetaSchema = z
110
+ .object({
111
+ duration: z
112
+ .number()
113
+ .min(0)
114
+ .default(0)
115
+ .describe(
116
+ 'Seconds to blend from the previously active clip into this state on entry, applied via ' +
117
+ 'native `AnimationAction.crossFadeTo`/`fadeIn` (E2). 0 = instant activation, no fade.',
118
+ ),
119
+ warp: z
120
+ .boolean()
121
+ .default(false)
122
+ .describe(
123
+ "Passed through to AnimationAction.crossFadeTo's `warp` param — linearly warps timeScale " +
124
+ 'across the two clips during the blend so differing clip lengths line up. Default false.',
125
+ ),
126
+ })
127
+ .strict()
128
+ .describe('Blend-in/out/crossfade timing for entering this state');
129
+
130
+ export type AnimationCrossfadeMeta = z.infer<typeof AnimationCrossfadeMetaSchema>;
131
+
132
+ const AnimationBoneMaskSchema = z
133
+ .object({
134
+ include: z
135
+ .array(z.string().min(1))
136
+ .min(1)
137
+ .optional()
138
+ .describe('Bone/object names whose complete descendant subtrees are retained'),
139
+ exclude: z
140
+ .array(z.string().min(1))
141
+ .min(1)
142
+ .optional()
143
+ .describe('Bone/object names whose complete descendant subtrees are removed'),
144
+ })
145
+ .strict()
146
+ .refine((mask) => Boolean(mask.include?.length || mask.exclude?.length), {
147
+ message: 'boneMask needs at least one include or exclude bone name',
148
+ })
149
+ .describe('Filters animation tracks by named Object3D/Bone subtrees');
150
+
151
+ export type AnimationBoneMask = z.infer<typeof AnimationBoneMaskSchema>;
152
+
153
+ const AnimationLayerFields = {
154
+ layer: z
155
+ .string()
156
+ .min(1)
157
+ .optional()
158
+ .describe('Composition layer name; omitted means the single base layer'),
159
+ weight: z
160
+ .number()
161
+ .min(0)
162
+ .max(1)
163
+ .optional()
164
+ .describe('Layer weight multiplier from 0 to 1; defaults to 1'),
165
+ blendMode: z
166
+ .enum(['override', 'additive'])
167
+ .optional()
168
+ .describe('Native Three normal/override or additive action blending; defaults to override'),
169
+ boneMask: AnimationBoneMaskSchema.optional().describe(
170
+ 'Optional include/exclude filter applied to every clip track in this state',
171
+ ),
172
+ };
173
+
174
+ // ---------------------------------------------------------------------------
175
+ // Per-state animation metadata: single clip OR blend tree, plus crossfade
176
+ // ---------------------------------------------------------------------------
177
+
178
+ const ClipAnimationMetaSchema = z
179
+ .object({
180
+ clip: z.string().min(1).describe('Single clip to play while this XState state is active'),
181
+ loop: z
182
+ .boolean()
183
+ .default(true)
184
+ .describe(
185
+ 'Loop the clip (THREE.LoopRepeat) vs play once (THREE.LoopOnce, clamped on last frame)',
186
+ ),
187
+ speed: z
188
+ .number()
189
+ .positive()
190
+ .default(1)
191
+ .describe('Playback speed multiplier (AnimationAction.timeScale)'),
192
+ crossfade: AnimationCrossfadeMetaSchema.optional(),
193
+ ...AnimationLayerFields,
194
+ })
195
+ .strict();
196
+
197
+ export type ClipAnimationMeta = z.infer<typeof ClipAnimationMetaSchema>;
198
+
199
+ const BlendTreeAnimationMetaSchema = z
200
+ .object({
201
+ blendTree: AnimationBlendTreeSchema.describe(
202
+ 'Multiple clips blended by a live context/selector-driven parameter',
203
+ ),
204
+ crossfade: AnimationCrossfadeMetaSchema.optional(),
205
+ ...AnimationLayerFields,
206
+ })
207
+ .strict();
208
+
209
+ export type BlendTreeAnimationMeta = z.infer<typeof BlendTreeAnimationMetaSchema>;
210
+
211
+ /**
212
+ * The full shape of `state.meta.animation` for one XState state (spec §5.6):
213
+ * exactly one of a single clip or a blend tree, plus optional crossfade
214
+ * timing. `.strict()` on both branches is what makes authoring a transition
215
+ * keyword (`on`, `target`, `guard`, …) inside `meta.animation` a validation
216
+ * error instead of a silently-accepted second transition dialect.
217
+ */
218
+ export const StateAnimationMetaSchema = z.union([
219
+ ClipAnimationMetaSchema,
220
+ BlendTreeAnimationMetaSchema,
221
+ ]);
222
+
223
+ export type StateAnimationMeta = z.infer<typeof StateAnimationMetaSchema>;
224
+
225
+ /** True iff a validated meta declares a single clip rather than a blend tree. */
226
+ export function isClipAnimationMeta(meta: StateAnimationMeta): meta is ClipAnimationMeta {
227
+ return 'clip' in meta;
228
+ }
229
+
230
+ /** True iff a validated meta declares a blend tree rather than a single clip. */
231
+ export function isBlendTreeAnimationMeta(meta: StateAnimationMeta): meta is BlendTreeAnimationMeta {
232
+ return 'blendTree' in meta;
233
+ }
234
+
235
+ // ---------------------------------------------------------------------------
236
+ // Validation entry point
237
+ // ---------------------------------------------------------------------------
238
+
239
+ /** Thrown by `parseStateAnimationMeta` — always names the offending state id. */
240
+ export class AnimationMetaValidationError extends Error {
241
+ constructor(
242
+ message: string,
243
+ readonly stateId: string,
244
+ readonly issues: z.ZodIssue[],
245
+ ) {
246
+ super(message);
247
+ this.name = 'AnimationMetaValidationError';
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Validate one state's `meta.animation` value. Called by the E2 binding for
253
+ * every state a machine declares (both at bind time, up front, and — for
254
+ * belt-and-suspenders — again whenever a state is entered).
255
+ *
256
+ * @throws {AnimationMetaValidationError} naming the state id and every Zod
257
+ * issue (path + message) — e.g. an unrecognized `on`/`target` key, a
258
+ * missing `clip`, or a 1D blend tree with only one child.
259
+ */
260
+ export function parseStateAnimationMeta(stateId: string, raw: unknown): StateAnimationMeta {
261
+ const result = StateAnimationMetaSchema.safeParse(raw);
262
+ if (!result.success) {
263
+ const detail = result.error.issues
264
+ .map((issue) => `${issue.path.length ? issue.path.join('.') : '(root)'}: ${issue.message}`)
265
+ .join('; ');
266
+ throw new AnimationMetaValidationError(
267
+ `[xstate-animation] invalid meta.animation for state "${stateId}": ${detail}`,
268
+ stateId,
269
+ result.error.issues,
270
+ );
271
+ }
272
+ return result.data;
273
+ }
274
+
275
+ // ---------------------------------------------------------------------------
276
+ // Machine-wide collection (used by E2 to validate + resolve clips up front)
277
+ // ---------------------------------------------------------------------------
278
+
279
+ /** The minimal shape of an XState `StateNode` this module needs to walk. */
280
+ export interface AnimationMetaStateNodeLike {
281
+ id: string;
282
+ meta?: { animation?: unknown } & Record<string, unknown>;
283
+ states?: Record<string, AnimationMetaStateNodeLike>;
284
+ }
285
+
286
+ export interface MachineAnimationMetaEntry {
287
+ stateId: string;
288
+ meta: StateAnimationMeta;
289
+ }
290
+
291
+ /**
292
+ * Walk every state node in a machine (from its root `StateNode`, e.g.
293
+ * `actor.logic.root`) and validate every `meta.animation` value found.
294
+ * States with no `meta.animation` are skipped (not every XState state needs
295
+ * to drive animation). Returns one entry per animated state, in document
296
+ * order.
297
+ *
298
+ * @throws {AnimationMetaValidationError} on the FIRST invalid meta found —
299
+ * naming the specific state id, so an agent authoring a machine gets an
300
+ * actionable pointer straight to the bad state instead of a generic error.
301
+ */
302
+ export function collectMachineAnimationMeta(
303
+ root: AnimationMetaStateNodeLike,
304
+ ): MachineAnimationMetaEntry[] {
305
+ const out: MachineAnimationMetaEntry[] = [];
306
+ const visit = (node: AnimationMetaStateNodeLike): void => {
307
+ if (node.meta && 'animation' in node.meta && node.meta.animation !== undefined) {
308
+ out.push({ stateId: node.id, meta: parseStateAnimationMeta(node.id, node.meta.animation) });
309
+ }
310
+ for (const child of Object.values(node.states ?? {})) visit(child);
311
+ };
312
+ visit(root);
313
+ return out;
314
+ }
315
+
316
+ /** Every clip name referenced anywhere in a validated meta (1 for clip states, N for blend trees). */
317
+ export function clipNamesOf(meta: StateAnimationMeta): string[] {
318
+ return isClipAnimationMeta(meta) ? [meta.clip] : meta.blendTree.children.map((c) => c.clip);
319
+ }
@@ -1,7 +1,39 @@
1
- export { hum, nature, rain, wind } from './ambient';
2
- export { bounce, crash, explosion, hit } from './impacts';
3
- export { footstep, jump, whoosh } from './movement';
4
- export { arpeggio, bassPulse, stinger } from './musical';
5
- export { uiClick, uiError, uiHover, uiSuccess } from './ui-sounds';
6
- export { brakeSqueal, engineDrone, tireSkid } from './vehicle';
7
- export { chargeUp, gunshot, laser } from './weapons';
1
+ // Engine-core audio surface (spec §13 workstream G).
2
+ //
3
+ // G3 ("Replace or Relocate Procedural Audio Helpers") removed the former
4
+ // `musical.ts`/`weapons.ts`/`impacts.ts`/`vehicle.ts`/`ambient.ts`/
5
+ // `movement.ts`/`ui-sounds.ts` — raw-`AudioContext`/`Math.random`-driven
6
+ // oscillator and noise-buffer SFX generators that were engine-owned but
7
+ // non-spatial (they never touched `THREE.PositionalAudio`, so the "spatial
8
+ // one-shot helper" exception in the G3 acceptance criteria does not apply to
9
+ // them). Each was either relocated verbatim into the example project that
10
+ // used it (`examples/{third-person,fps,third-person-arena,rts,vehicle}/src/
11
+ // procedural-audio.ts`) or deleted as dead code with no shipped consumer
12
+ // (`bassPulse`/`arpeggio`/`gunshot`/`laser`/`bounce`/`crash`/`explosion`/
13
+ // `tireSkid`/`wind`/`rain`/`hum`/`nature`/`uiHover`/`uiError`). The canonical
14
+ // replacements — a procedural-music example built directly on Tone's own API
15
+ // and a spatial-audio example built directly on `THREE.PositionalAudio` — are
16
+ // `examples/audio-showcase`.
17
+ //
18
+ // What remains here is the genuine engine-core audio capability: the G1/G2
19
+ // Tone↔Three context bridge and offline render (`wav-encode.ts`, the G4
20
+ // PCM->WAV encoder, is consumed by its own module path, not this barrel — see
21
+ // `packages/engine/src/runtime/render-audio-control.ts`).
22
+ export {
23
+ bindClockToTone,
24
+ type ToneClockBinding,
25
+ type ToneClockBindingOptions,
26
+ type ToneTransportLike,
27
+ } from './tone-clock-binding';
28
+ export {
29
+ bridgeToneToThreeAudio,
30
+ connectToneBusToMasterGain,
31
+ resumeSharedAudio,
32
+ type ToneAudioBridge,
33
+ } from './tone-context';
34
+ export {
35
+ renderToneOffline,
36
+ type ToneOfflineComposeFn,
37
+ type ToneOfflineRenderRequest,
38
+ type ToneOfflineRenderResult,
39
+ } from './tone-offline-render';
@@ -0,0 +1,98 @@
1
+ import type { AnimationClock, ClockEvaluatorHandle } from '../animation/animation-clock';
2
+ import type { SystemPhaseName } from '../core/types';
3
+
4
+ /**
5
+ * G2 — Integrate Tone with the Canonical Clock, preview/real-time half
6
+ * (spec §13 G2, §10 D1). Mirrors `animation/theatre-clock-binding.ts`'s D2
7
+ * pattern exactly, on purpose: the canonical `AnimationClock` is the single
8
+ * source of truth for "what time is it", and every participating system
9
+ * (Theatre's sequence position, a registered GSAP timeline, and — here — a
10
+ * Tone transport) is seeked to match it on every clock crossing. Tone must
11
+ * never be left to free-run its own real-time clock while participating in
12
+ * a cinematic, for the exact same reason Theatre isn't: deterministic
13
+ * export, offline scrubbing, and no wall-clock dependency
14
+ * (`animation-clock.ts`'s module doc explains why in full).
15
+ *
16
+ * ## Why setting `transport.seconds` is enough
17
+ *
18
+ * Setting `Tone.getTransport().seconds` repositions the transport's
19
+ * playhead synchronously; it does not itself start real-time ticking (that
20
+ * still requires an explicit `transport.start()`, which this binding
21
+ * deliberately never calls — see the module doc on `animation-clock.ts` for
22
+ * why nothing in this codebase is allowed to drive its own
23
+ * `requestAnimationFrame`/wall-clock loop). A clock-seeked Tone transport is
24
+ * a pure function of `clock.time`, exactly like a Theatre sequence's
25
+ * `position` setter.
26
+ *
27
+ * ## Structural (not nominal) transport typing
28
+ *
29
+ * `ToneTransportLike` intentionally types only the ONE property this
30
+ * binding touches (`seconds`), the same narrowing discipline
31
+ * `runtime/render-control.ts`'s `SeekableClock` uses for the canonical
32
+ * clock. `Tone.getTransport()`'s real `TransportClass` satisfies this
33
+ * structurally with no adapter — pass it directly at the real call site.
34
+ * The narrow type is also what makes this binding's own unit tests real
35
+ * (not mocked-away): `packages/engine/test/audio/tone-clock-binding.test.ts`
36
+ * exercises the FULL seek/pause/loop/time-scale matrix against a real
37
+ * `AnimationClock` and a plain `{ seconds: number }` object — no Tone
38
+ * import, no `AudioContext`, and it still proves the exact behavior a real
39
+ * `Tone.Transport` will see, because `TransportClass` IS a
40
+ * `ToneTransportLike` at the type level.
41
+ */
42
+
43
+ export interface ToneTransportLike {
44
+ seconds: number;
45
+ }
46
+
47
+ export interface ToneClockBindingOptions {
48
+ /**
49
+ * Which `AnimationClock` phase to seek the transport in. Defaults to
50
+ * `'animation'` — the same phase `theatre-clock-binding.ts` uses by
51
+ * default, so a Theatre sequence and a Tone transport bound to the same
52
+ * clock stay in lockstep with each other (`PHASE_ORDER`-then-registration-
53
+ * order — see `animation-clock.ts`'s `subscribe` doc comment).
54
+ */
55
+ phase?: SystemPhaseName;
56
+ }
57
+
58
+ export interface ToneClockBinding {
59
+ /** Removes the clock evaluator. No further `transport.seconds` writes
60
+ * occur after this — the transport is left at whatever position it was
61
+ * last seeked to (matches `TheatreClockBinding.dispose`'s contract). */
62
+ dispose(): void;
63
+ }
64
+
65
+ /**
66
+ * Subscribe `transport.seconds = clock.time` to every crossing of `clock`
67
+ * in the given phase (default `'animation'`). Returns a disposer; call it
68
+ * to stop driving the transport (e.g. on `GameCleanup.dispose()`, or before
69
+ * rebinding on hot reload — this is what keeps HMR from accumulating
70
+ * duplicate listeners).
71
+ *
72
+ * Only call this for a Tone composition that PARTICIPATES in a cinematic
73
+ * (spec G2 AC). Ordinary, non-cinematic game music should drive Tone's own
74
+ * real-time transport directly (`transport.start()`, `Tone.Transport.bpm`,
75
+ * etc. — see `tone-context.ts`'s "escape hatch" section) and must NEVER be
76
+ * bound here, since a bound transport's position is entirely clock-driven
77
+ * and `start()`/`stop()` calls on it would fight this binding's writes.
78
+ */
79
+ export function bindClockToTone(
80
+ clock: AnimationClock,
81
+ transport: ToneTransportLike,
82
+ options: ToneClockBindingOptions = {},
83
+ ): ToneClockBinding {
84
+ const phase = options.phase ?? 'animation';
85
+
86
+ const handle: ClockEvaluatorHandle = clock.subscribe(phase, (crossing) => {
87
+ transport.seconds = crossing.currentTime;
88
+ });
89
+
90
+ let disposed = false;
91
+ return {
92
+ dispose(): void {
93
+ if (disposed) return;
94
+ disposed = true;
95
+ handle.dispose();
96
+ },
97
+ };
98
+ }
@@ -0,0 +1,129 @@
1
+ import type { ToneAudioNode } from 'tone';
2
+ import * as Tone from 'tone';
3
+
4
+ /**
5
+ * G1 — Establish Audio Ownership (spec §13 G1, §3.6, §4).
6
+ *
7
+ * ## The boundary
8
+ *
9
+ * Two independent, native-vocabulary audio systems coexist in every VGAI
10
+ * game, each with a genuinely different job. This module does not wrap
11
+ * either one — it only decides who owns which native `AudioContext` and how
12
+ * the two graphs meet:
13
+ *
14
+ * - **Three/Web Audio** (`setup/setup-audio.ts`) remains authoritative for
15
+ * SPATIAL audio: `THREE.AudioListener` (attached to the active camera)
16
+ * and `THREE.Audio`/`THREE.PositionalAudio` sources. (G3, a later unit,
17
+ * has since removed the non-spatial engine-owned one-shot procedural
18
+ * helpers this comment used to name here — `audio/impacts.ts`,
19
+ * `audio/weapons.ts`, `audio/ambient.ts`, and their siblings — relocating
20
+ * each into the example project that used it, or deleting it as dead code;
21
+ * see `packages/engine/src/audio/index.ts`'s header comment and
22
+ * `examples/audio-showcase` for the canonical native-Tone/native-Three
23
+ * replacements. This module's own boundary/bridge is unchanged by that
24
+ * unit.)
25
+ * - **Tone.js** owns musical transport, tempo, scheduling, synthesis, and
26
+ * procedural composition — imported and used with its OWN native API
27
+ * (`new Tone.Synth()`, `Tone.getTransport()`, `Tone.Offline(...)`,
28
+ * `Tone.getContext()`), never through a VGAI-renamed wrapper (spec §1's
29
+ * native-library rule). See `packages/engine/test/audio/tone-context.test.ts`
30
+ * for a direct, unwrapped usage example.
31
+ *
32
+ * ## Why they must share one native `AudioContext`
33
+ *
34
+ * Both systems ultimately talk to exactly one hardware output. If Tone ran
35
+ * its own independently-created `AudioContext` alongside
36
+ * `THREE.AudioListener`'s, they would: fight over autoplay-policy resume
37
+ * state (two contexts, two `state`s, two places a "click to start audio"
38
+ * gesture would need to unlock); produce TWO separate graphs that a single
39
+ * mute/volume control cannot coherently silence; and — fatally for G4/I6 —
40
+ * make deterministic offline capture impossible to align with the video's
41
+ * frame-addressed render range, since there would be no single graph whose
42
+ * lifecycle (suspend/resume/close) the render/capture harness can reason
43
+ * about.
44
+ *
45
+ * `bridgeToneToThreeAudio` fixes this: it points Tone's global context
46
+ * (`Tone.setContext`) at the SAME native `AudioContext` instance
47
+ * `THREE.AudioListener` already created (`setupAudio(camera).listener.context`,
48
+ * `setup/setup-audio.ts`). After bridging,
49
+ * `Tone.getContext().rawContext === threeListener.context` — there is
50
+ * exactly one native context. One `.resume()`/`.suspend()` call (or
51
+ * `Tone.start()`, which does both) affects both systems, and routing a Tone
52
+ * bus into `setupAudio()`'s `masterGain` (see `connectToneBusToMasterGain`
53
+ * below) puts Tone's output under the SAME mute/master-volume control as
54
+ * every `THREE.Audio` source.
55
+ *
56
+ * ## What this module does NOT do
57
+ *
58
+ * It does not create an `AudioContext` (that remains `setupAudio`'s job —
59
+ * call `bridgeToneToThreeAudio` AFTER `setupAudio(camera)`, before
60
+ * constructing any Tone node: Tone nodes built against the PREVIOUS global
61
+ * context are not retroactively migrated — Tone's own `setContext` contract,
62
+ * not a VGAI limitation). It does not wrap any Tone class, and it does not
63
+ * touch THREE's audio path at all. G3 (a later unit) is where engine-owned
64
+ * procedural helpers get rewritten directly on Tone or relocated to game
65
+ * code — this module only owns the context boundary + bridge.
66
+ *
67
+ * ## Escape hatch — ordinary (non-cinematic) game music
68
+ *
69
+ * None of the above requires participating in a cinematic. Once bridged,
70
+ * ordinary game music can drive Tone's REAL real-time transport exactly as
71
+ * Tone's own docs describe — `Tone.getTransport().bpm.value = 128`,
72
+ * `Tone.getTransport().start()`, `new Tone.Sequence(...).start(0)`, etc. —
73
+ * with no VGAI involvement at all. `tone-clock-binding.ts` (G2) is opt-in,
74
+ * only for the case where a Tone composition must stay in lockstep with the
75
+ * canonical `AnimationClock` because it participates in a cinematic.
76
+ */
77
+
78
+ /** Result of {@link bridgeToneToThreeAudio} — the shared native context plus
79
+ * a re-checkable assertion, so a caller/test doesn't need to re-derive the
80
+ * bridging logic to confirm it still holds later. */
81
+ export interface ToneAudioBridge {
82
+ /** The native `AudioContext` Tone and `THREE.AudioListener` now share. */
83
+ readonly context: AudioContext;
84
+ /** True iff `Tone.getContext().rawContext` is still exactly `context`
85
+ * (identity, not equality — proves ONE shared native context, not two
86
+ * compatible-looking ones). */
87
+ isBridged(): boolean;
88
+ }
89
+
90
+ /**
91
+ * Point Tone's global context at `context` (typically
92
+ * `setupAudio(camera).listener.context` from `setup/setup-audio.ts`). Call
93
+ * this once per game boot, after `setupAudio(camera)`, before constructing
94
+ * any Tone node.
95
+ */
96
+ export function bridgeToneToThreeAudio(context: AudioContext): ToneAudioBridge {
97
+ Tone.setContext(context);
98
+ return {
99
+ context,
100
+ isBridged: () => Tone.getContext().rawContext === context,
101
+ };
102
+ }
103
+
104
+ /**
105
+ * Route a Tone output node (a bus, `Tone.getDestination()`, or any
106
+ * `Tone.Gain` used as a submix) into the SAME `masterGain`
107
+ * `setupAudio()` created, so Tone output is silenced/attenuated by the
108
+ * identical mute/volume control as every `THREE.Audio`/`THREE.PositionalAudio`
109
+ * source. `toneNode` is Tone's own `ToneAudioNode` type (no VGAI wrapper);
110
+ * `masterGain` is the plain native `GainNode` `setupAudio()` already
111
+ * returns — Tone's `.connect()` natively accepts a raw `AudioNode` as its
112
+ * destination, so no adapter node is needed on either side.
113
+ */
114
+ export function connectToneBusToMasterGain(toneNode: ToneAudioNode, masterGain: GainNode): void {
115
+ toneNode.connect(masterGain);
116
+ }
117
+
118
+ /**
119
+ * Resume the shared context after a user gesture. Because
120
+ * {@link bridgeToneToThreeAudio} guarantees Tone and THREE share the
121
+ * identical native context, calling `Tone.start()` — which both resumes the
122
+ * context AND runs Tone's own autoplay-unlock bookkeeping (a silent-buffer
123
+ * trick required on some browsers) — is sufficient to unstick BOTH systems;
124
+ * a separate `context.resume()` call on the THREE side is redundant once
125
+ * bridged, not required.
126
+ */
127
+ export async function resumeSharedAudio(): Promise<void> {
128
+ await Tone.start();
129
+ }