@needle-tools/engine 5.1.0-alpha.3 → 5.1.0-alpha.4

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 (178) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/SKILL.md +4 -1
  3. package/components.needle.json +1 -1
  4. package/dist/needle-engine.bundle-AjVIot3d.min.js +1733 -0
  5. package/dist/{needle-engine.bundle-DF01sSGQ.js → needle-engine.bundle-B7cqsI4c.js} +9149 -8867
  6. package/dist/{needle-engine.bundle-C-ixARur.umd.cjs → needle-engine.bundle-DQCuBTVp.umd.cjs} +154 -154
  7. package/dist/needle-engine.d.ts +754 -199
  8. package/dist/needle-engine.js +583 -584
  9. package/dist/needle-engine.min.js +1 -1
  10. package/dist/needle-engine.umd.cjs +1 -1
  11. package/lib/engine/api.d.ts +3 -0
  12. package/lib/engine/api.js +2 -0
  13. package/lib/engine/api.js.map +1 -1
  14. package/lib/engine/codegen/register_types.js +2 -10
  15. package/lib/engine/codegen/register_types.js.map +1 -1
  16. package/lib/engine/engine_context.js +2 -1
  17. package/lib/engine/engine_context.js.map +1 -1
  18. package/lib/engine/engine_disposable.d.ts +172 -0
  19. package/lib/engine/engine_disposable.js +136 -0
  20. package/lib/engine/engine_disposable.js.map +1 -0
  21. package/lib/engine/engine_gameobject.d.ts +1 -10
  22. package/lib/engine/engine_gameobject.js +20 -118
  23. package/lib/engine/engine_gameobject.js.map +1 -1
  24. package/lib/engine/engine_gltf_builtin_components.js +7 -69
  25. package/lib/engine/engine_gltf_builtin_components.js.map +1 -1
  26. package/lib/engine/engine_instantiate_resolve.d.ts +42 -0
  27. package/lib/engine/engine_instantiate_resolve.js +372 -0
  28. package/lib/engine/engine_instantiate_resolve.js.map +1 -0
  29. package/lib/engine/engine_mainloop_utils.js +2 -2
  30. package/lib/engine/engine_mainloop_utils.js.map +1 -1
  31. package/lib/engine/engine_networking.d.ts +51 -37
  32. package/lib/engine/engine_networking.js +132 -82
  33. package/lib/engine/engine_networking.js.map +1 -1
  34. package/lib/engine/engine_networking.transport.websocket.d.ts +15 -0
  35. package/lib/engine/engine_networking.transport.websocket.js +38 -0
  36. package/lib/engine/engine_networking.transport.websocket.js.map +1 -0
  37. package/lib/engine/engine_networking_instantiate.js +2 -2
  38. package/lib/engine/engine_networking_instantiate.js.map +1 -1
  39. package/lib/engine/engine_networking_types.d.ts +39 -1
  40. package/lib/engine/engine_networking_types.js +7 -0
  41. package/lib/engine/engine_networking_types.js.map +1 -1
  42. package/lib/engine/engine_physics_rapier.d.ts +11 -3
  43. package/lib/engine/engine_physics_rapier.js +88 -25
  44. package/lib/engine/engine_physics_rapier.js.map +1 -1
  45. package/lib/engine/engine_serialization_builtin_serializer.js +1 -5
  46. package/lib/engine/engine_serialization_builtin_serializer.js.map +1 -1
  47. package/lib/engine/engine_serialization_core.d.ts +1 -0
  48. package/lib/engine/engine_serialization_core.js +7 -0
  49. package/lib/engine/engine_serialization_core.js.map +1 -1
  50. package/lib/engine/engine_types.d.ts +19 -11
  51. package/lib/engine/engine_types.js +1 -1
  52. package/lib/engine/engine_types.js.map +1 -1
  53. package/lib/engine/engine_util_decorator.js +7 -2
  54. package/lib/engine/engine_util_decorator.js.map +1 -1
  55. package/lib/engine/engine_utils.d.ts +1 -1
  56. package/lib/engine/engine_utils.js +19 -5
  57. package/lib/engine/engine_utils.js.map +1 -1
  58. package/lib/engine-components/Animator.d.ts +6 -0
  59. package/lib/engine-components/Animator.js +17 -12
  60. package/lib/engine-components/Animator.js.map +1 -1
  61. package/lib/engine-components/AnimatorController.builder.d.ts +113 -0
  62. package/lib/engine-components/AnimatorController.builder.js +195 -0
  63. package/lib/engine-components/AnimatorController.builder.js.map +1 -0
  64. package/lib/engine-components/AnimatorController.d.ts +2 -119
  65. package/lib/engine-components/AnimatorController.js +31 -232
  66. package/lib/engine-components/AnimatorController.js.map +1 -1
  67. package/lib/engine-components/Collider.d.ts +18 -9
  68. package/lib/engine-components/Collider.js +61 -14
  69. package/lib/engine-components/Collider.js.map +1 -1
  70. package/lib/engine-components/Component.d.ts +72 -9
  71. package/lib/engine-components/Component.js +114 -10
  72. package/lib/engine-components/Component.js.map +1 -1
  73. package/lib/engine-components/DragControls.js +0 -7
  74. package/lib/engine-components/DragControls.js.map +1 -1
  75. package/lib/engine-components/EventList.d.ts +31 -9
  76. package/lib/engine-components/EventList.js +37 -76
  77. package/lib/engine-components/EventList.js.map +1 -1
  78. package/lib/engine-components/Joints.d.ts +4 -2
  79. package/lib/engine-components/Joints.js +19 -3
  80. package/lib/engine-components/Joints.js.map +1 -1
  81. package/lib/engine-components/Light.js +9 -1
  82. package/lib/engine-components/Light.js.map +1 -1
  83. package/lib/engine-components/RigidBody.d.ts +12 -4
  84. package/lib/engine-components/RigidBody.js +18 -4
  85. package/lib/engine-components/RigidBody.js.map +1 -1
  86. package/lib/engine-components/api.d.ts +1 -1
  87. package/lib/engine-components/api.js +1 -1
  88. package/lib/engine-components/api.js.map +1 -1
  89. package/lib/engine-components/codegen/components.d.ts +3 -9
  90. package/lib/engine-components/codegen/components.js +3 -9
  91. package/lib/engine-components/codegen/components.js.map +1 -1
  92. package/lib/engine-components/timeline/PlayableDirector.d.ts +16 -6
  93. package/lib/engine-components/timeline/PlayableDirector.js +70 -62
  94. package/lib/engine-components/timeline/PlayableDirector.js.map +1 -1
  95. package/lib/engine-components/timeline/SignalAsset.d.ts +3 -1
  96. package/lib/engine-components/timeline/SignalAsset.js +1 -0
  97. package/lib/engine-components/timeline/SignalAsset.js.map +1 -1
  98. package/lib/engine-components/timeline/TimelineBuilder.d.ts +247 -0
  99. package/lib/engine-components/timeline/TimelineBuilder.js +400 -0
  100. package/lib/engine-components/timeline/TimelineBuilder.js.map +1 -0
  101. package/lib/engine-components/timeline/TimelineModels.d.ts +2 -1
  102. package/lib/engine-components/timeline/TimelineModels.js +3 -0
  103. package/lib/engine-components/timeline/TimelineModels.js.map +1 -1
  104. package/lib/engine-components/timeline/TimelineTracks.d.ts +23 -0
  105. package/lib/engine-components/timeline/TimelineTracks.js +71 -13
  106. package/lib/engine-components/timeline/TimelineTracks.js.map +1 -1
  107. package/lib/engine-components/timeline/index.d.ts +2 -1
  108. package/lib/engine-components/timeline/index.js +2 -0
  109. package/lib/engine-components/timeline/index.js.map +1 -1
  110. package/package.json +2 -83
  111. package/plugins/common/license.js +5 -2
  112. package/plugins/common/worker.js +9 -4
  113. package/plugins/vite/dependencies.js +1 -11
  114. package/plugins/vite/dependency-watcher.js +2 -2
  115. package/plugins/vite/editor-connection.js +3 -3
  116. package/plugins/vite/reload.js +1 -1
  117. package/plugins/vite/server.js +2 -1
  118. package/src/engine/api.ts +4 -0
  119. package/src/engine/codegen/register_types.ts +2 -10
  120. package/src/engine/engine_context.ts +2 -1
  121. package/src/engine/engine_disposable.ts +214 -0
  122. package/src/engine/engine_gameobject.ts +52 -157
  123. package/src/engine/engine_gltf_builtin_components.ts +7 -76
  124. package/src/engine/engine_instantiate_resolve.ts +407 -0
  125. package/src/engine/engine_mainloop_utils.ts +2 -2
  126. package/src/engine/engine_networking.transport.websocket.ts +45 -0
  127. package/src/engine/engine_networking.ts +161 -137
  128. package/src/engine/engine_networking_instantiate.ts +2 -2
  129. package/src/engine/engine_networking_types.ts +41 -1
  130. package/src/engine/engine_physics_rapier.ts +82 -27
  131. package/src/engine/engine_serialization_builtin_serializer.ts +1 -6
  132. package/src/engine/engine_serialization_core.ts +9 -0
  133. package/src/engine/engine_types.ts +24 -15
  134. package/src/engine/engine_util_decorator.ts +7 -2
  135. package/src/engine/engine_utils.ts +16 -5
  136. package/src/engine-components/Animator.ts +18 -11
  137. package/src/engine-components/AnimatorController.builder.ts +261 -0
  138. package/src/engine-components/AnimatorController.ts +19 -291
  139. package/src/engine-components/Collider.ts +66 -18
  140. package/src/engine-components/Component.ts +118 -20
  141. package/src/engine-components/DragControls.ts +0 -9
  142. package/src/engine-components/EventList.ts +45 -83
  143. package/src/engine-components/Joints.ts +20 -4
  144. package/src/engine-components/Light.ts +10 -2
  145. package/src/engine-components/RigidBody.ts +18 -4
  146. package/src/engine-components/api.ts +1 -1
  147. package/src/engine-components/codegen/components.ts +3 -9
  148. package/src/engine-components/timeline/PlayableDirector.ts +67 -65
  149. package/src/engine-components/timeline/SignalAsset.ts +4 -1
  150. package/src/engine-components/timeline/TimelineBuilder.ts +564 -0
  151. package/src/engine-components/timeline/TimelineModels.ts +5 -1
  152. package/src/engine-components/timeline/TimelineTracks.ts +74 -13
  153. package/src/engine-components/timeline/index.ts +2 -1
  154. package/dist/needle-engine.bundle-CHmXdnE1.min.js +0 -1733
  155. package/lib/engine-components/AvatarLoader.d.ts +0 -80
  156. package/lib/engine-components/AvatarLoader.js +0 -232
  157. package/lib/engine-components/AvatarLoader.js.map +0 -1
  158. package/lib/engine-components/avatar/AvatarBlink_Simple.d.ts +0 -11
  159. package/lib/engine-components/avatar/AvatarBlink_Simple.js +0 -77
  160. package/lib/engine-components/avatar/AvatarBlink_Simple.js.map +0 -1
  161. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.d.ts +0 -14
  162. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js +0 -69
  163. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js.map +0 -1
  164. package/lib/engine-components/avatar/Avatar_Brain_LookAt.d.ts +0 -29
  165. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js +0 -122
  166. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js.map +0 -1
  167. package/lib/engine-components/avatar/Avatar_MouthShapes.d.ts +0 -15
  168. package/lib/engine-components/avatar/Avatar_MouthShapes.js +0 -80
  169. package/lib/engine-components/avatar/Avatar_MouthShapes.js.map +0 -1
  170. package/lib/engine-components/avatar/Avatar_MustacheShake.d.ts +0 -9
  171. package/lib/engine-components/avatar/Avatar_MustacheShake.js +0 -30
  172. package/lib/engine-components/avatar/Avatar_MustacheShake.js.map +0 -1
  173. package/src/engine-components/AvatarLoader.ts +0 -264
  174. package/src/engine-components/avatar/AvatarBlink_Simple.ts +0 -70
  175. package/src/engine-components/avatar/AvatarEyeLook_Rotation.ts +0 -64
  176. package/src/engine-components/avatar/Avatar_Brain_LookAt.ts +0 -140
  177. package/src/engine-components/avatar/Avatar_MouthShapes.ts +0 -84
  178. package/src/engine-components/avatar/Avatar_MustacheShake.ts +0 -32
@@ -0,0 +1,564 @@
1
+ import { AnimationClip, Object3D } from "three";
2
+
3
+ import type { Animator } from "../Animator.js";
4
+ import type { AudioSource } from "../AudioSource.js";
5
+ import { GameObject } from "../Component.js";
6
+ import { InstantiateIdProvider } from "../../engine/engine_networking_instantiate.js";
7
+ import { EventList } from "../EventList.js";
8
+ import { SignalAsset, SignalReceiver, SignalReceiverEvent } from "./SignalAsset.js";
9
+ import type { PlayableDirector } from "./PlayableDirector.js";
10
+ import { ClipExtrapolation, TrackType } from "./TimelineModels.js";
11
+ import type { TimelineAssetModel, TrackModel, ClipModel, AnimationClipModel, AudioClipModel, ControlClipModel, MarkerModel, SignalMarkerModel, TrackOffset } from "./TimelineModels.js";
12
+ import { MarkerType } from "./TimelineModels.js";
13
+
14
+ /**
15
+ * Options for an animation clip in the timeline builder
16
+ */
17
+ export declare type AnimationClipOptions = {
18
+ /** Start time of the clip in seconds. If omitted, placed after the previous clip on this track. */
19
+ start?: number;
20
+ /** Duration of the clip in seconds. Defaults to the animation clip duration. */
21
+ duration?: number;
22
+ /** Playback speed multiplier (default: 1) */
23
+ speed?: number;
24
+ /** Whether the animation should loop within the clip (default: false) */
25
+ loop?: boolean;
26
+ /** Ease-in duration in seconds (default: 0) */
27
+ easeIn?: number;
28
+ /** Ease-out duration in seconds (default: 0) */
29
+ easeOut?: number;
30
+ /** Offset into the source animation clip in seconds (default: 0) */
31
+ clipIn?: number;
32
+ /** Whether to remove the start offset of the animation (default: false) */
33
+ removeStartOffset?: boolean;
34
+ /** Pre-extrapolation mode (default: None) */
35
+ preExtrapolation?: ClipExtrapolation;
36
+ /** Post-extrapolation mode (default: None) */
37
+ postExtrapolation?: ClipExtrapolation;
38
+ /** Play the clip in reverse */
39
+ reversed?: boolean;
40
+ }
41
+
42
+ /**
43
+ * Options for an audio clip in the timeline builder
44
+ */
45
+ export declare type AudioClipOptions = {
46
+ /** Start time of the clip in seconds. If omitted, placed after the previous clip on this track. */
47
+ start?: number;
48
+ /** Duration of the clip in seconds (required for audio since we can't infer it) */
49
+ duration: number;
50
+ /** Playback speed multiplier (default: 1) */
51
+ speed?: number;
52
+ /** Volume multiplier for this clip (default: 1) */
53
+ volume?: number;
54
+ /** Whether the audio should loop within the clip (default: false) */
55
+ loop?: boolean;
56
+ /** Ease-in duration in seconds (default: 0) */
57
+ easeIn?: number;
58
+ /** Ease-out duration in seconds (default: 0) */
59
+ easeOut?: number;
60
+ }
61
+
62
+ /**
63
+ * Options for an activation clip in the timeline builder
64
+ */
65
+ export declare type ActivationClipOptions = {
66
+ /** Start time of the clip in seconds. If omitted, placed after the previous clip on this track. */
67
+ start?: number;
68
+ /** Duration of the clip in seconds (required) */
69
+ duration: number;
70
+ /** Ease-in duration in seconds (default: 0) */
71
+ easeIn?: number;
72
+ /** Ease-out duration in seconds (default: 0) */
73
+ easeOut?: number;
74
+ }
75
+
76
+ /**
77
+ * Options for a control clip in the timeline builder
78
+ */
79
+ export declare type ControlClipOptions = {
80
+ /** Start time of the clip in seconds. If omitted, placed after the previous clip on this track. */
81
+ start?: number;
82
+ /** Duration of the clip in seconds (required) */
83
+ duration: number;
84
+ /** Whether to control the activation of the source object (default: true) */
85
+ controlActivation?: boolean;
86
+ /** Whether to update a nested PlayableDirector on the source object (default: true) */
87
+ updateDirector?: boolean;
88
+ }
89
+
90
+ /**
91
+ * Options for a signal marker in the timeline builder
92
+ */
93
+ export declare type SignalMarkerOptions = {
94
+ /** Whether the signal should fire if the playback starts past its time (default: false) */
95
+ retroActive?: boolean;
96
+ /** Whether the signal should only fire once (default: false) */
97
+ emitOnce?: boolean;
98
+ }
99
+
100
+ // Internal types for track building
101
+ type BuilderTrack = {
102
+ name: string;
103
+ type: TrackType;
104
+ muted: boolean;
105
+ outputs: Array<null | object>;
106
+ clips: ClipModel[];
107
+ markers: MarkerModel[];
108
+ volume?: number;
109
+ trackOffset?: TrackOffset;
110
+ cursor: number; // current time position for auto-advancing
111
+ };
112
+
113
+ type PendingSignal = {
114
+ trackIndex: number;
115
+ guid: string;
116
+ callback: Function;
117
+ };
118
+
119
+ /**
120
+ * A fluent builder for creating timeline assets ({@link TimelineAssetModel}) from code.
121
+ *
122
+ * Use {@link TimelineBuilder.create} to start building a timeline.
123
+ *
124
+ * @example Using build() for timelines without signal callbacks
125
+ * ```ts
126
+ * const timeline = TimelineBuilder.create("MySequence")
127
+ * .animationTrack("Character", animator)
128
+ * .clip(walkClip, { duration: 2, easeIn: 0.3 })
129
+ * .clip(runClip, { duration: 3, easeIn: 0.5, easeOut: 0.5 })
130
+ * .activationTrack("FX", particleObject)
131
+ * .clip({ start: 1, duration: 2 })
132
+ * .audioTrack("Music", audioSource)
133
+ * .clip("music.mp3", { start: 0, duration: 5, volume: 0.8 })
134
+ * .build();
135
+ *
136
+ * director.playableAsset = timeline;
137
+ * director.play();
138
+ * ```
139
+ *
140
+ * @example Using install() with signal callbacks
141
+ * ```ts
142
+ * TimelineBuilder.create("WithSignals")
143
+ * .animationTrack("Character", animator)
144
+ * .clip(walkClip, { duration: 2 })
145
+ * .signalTrack("Events")
146
+ * .signal(1.0, () => console.log("1 second!"))
147
+ * .signal(2.0, () => spawnParticles())
148
+ * .install(director);
149
+ *
150
+ * director.play();
151
+ * ```
152
+ *
153
+ * @category Animation and Sequencing
154
+ * @group Utilities
155
+ */
156
+ export class TimelineBuilder {
157
+ private _name: string;
158
+ private _tracks: BuilderTrack[] = [];
159
+ private _currentTrack: BuilderTrack | null = null;
160
+ private _pendingSignals: PendingSignal[] = [];
161
+ private _idProvider: InstantiateIdProvider;
162
+
163
+ private constructor(name: string, seed?: number) {
164
+ this._name = name;
165
+ this._idProvider = new InstantiateIdProvider(seed ?? Date.now());
166
+ }
167
+
168
+ /**
169
+ * Creates a new TimelineBuilder instance.
170
+ * @param name - Name for the timeline asset
171
+ * @param seed - Optional numeric seed for deterministic guid generation. Defaults to `Date.now()`.
172
+ */
173
+ static create(name?: string, seed?: number): TimelineBuilder {
174
+ return new TimelineBuilder(name ?? "Timeline", seed);
175
+ }
176
+
177
+ // #region Track creation
178
+
179
+ /**
180
+ * Adds an animation track. Subsequent `.clip()` calls add animation clips to this track.
181
+ * @param name - Display name for the track
182
+ * @param binding - The Animator or Object3D to animate
183
+ */
184
+ animationTrack(name: string, binding?: Animator | Object3D | null): this {
185
+ this._currentTrack = this.pushTrack(name, TrackType.Animation, binding ?? null);
186
+ return this;
187
+ }
188
+
189
+ /**
190
+ * Adds an audio track. Subsequent `.clip()` calls add audio clips to this track.
191
+ * @param name - Display name for the track
192
+ * @param binding - The AudioSource to play audio on (optional)
193
+ * @param volume - Track volume multiplier (default: 1)
194
+ */
195
+ audioTrack(name: string, binding?: AudioSource | Object3D | null, volume?: number): this {
196
+ this._currentTrack = this.pushTrack(name, TrackType.Audio, binding ?? null);
197
+ this._currentTrack.volume = volume;
198
+ return this;
199
+ }
200
+
201
+ /**
202
+ * Adds an activation track. Subsequent `.clip()` calls define when the bound object is active.
203
+ * @param name - Display name for the track
204
+ * @param binding - The Object3D to show/hide
205
+ */
206
+ activationTrack(name: string, binding?: Object3D | null): this {
207
+ this._currentTrack = this.pushTrack(name, TrackType.Activation, binding ?? null);
208
+ return this;
209
+ }
210
+
211
+ /**
212
+ * Adds a control track. Subsequent `.clip()` calls control nested timelines or objects.
213
+ * @param name - Display name for the track
214
+ */
215
+ controlTrack(name: string): this {
216
+ this._currentTrack = this.pushTrack(name, TrackType.Control, null);
217
+ return this;
218
+ }
219
+
220
+ /**
221
+ * Adds a signal track. Use `.signal()` or `.marker()` to add signal markers.
222
+ * @param name - Display name for the track
223
+ * @param binding - The SignalReceiver component (optional — if using `.signal()` with callbacks, one is created automatically by {@link install})
224
+ */
225
+ signalTrack(name: string, binding?: SignalReceiver | Object3D | null): this {
226
+ this._currentTrack = this.pushTrack(name, TrackType.Signal, binding ?? null);
227
+ return this;
228
+ }
229
+
230
+ /**
231
+ * Adds a marker track. Use `.marker()` to add markers.
232
+ * @param name - Display name for the track
233
+ */
234
+ markerTrack(name: string): this {
235
+ this._currentTrack = this.pushTrack(name, TrackType.Marker, null);
236
+ return this;
237
+ }
238
+
239
+ // #endregion
240
+
241
+ // #region Clip and marker methods
242
+
243
+ /**
244
+ * Adds a clip to the current track. The clip type must match the track type.
245
+ *
246
+ * - On an **animation track**: pass an `AnimationClip` and optional {@link AnimationClipOptions}
247
+ * - On an **audio track**: pass a clip URL (string) and {@link AudioClipOptions}
248
+ * - On an **activation track**: pass {@link ActivationClipOptions}
249
+ * - On a **control track**: pass an Object3D and {@link ControlClipOptions}
250
+ */
251
+ clip(asset: AnimationClip, options?: AnimationClipOptions): this;
252
+ clip(url: string, options: AudioClipOptions): this;
253
+ clip(options: ActivationClipOptions): this;
254
+ clip(sourceObject: Object3D, options: ControlClipOptions): this;
255
+ clip(assetOrOptions: AnimationClip | string | Object3D | ActivationClipOptions, options?: AnimationClipOptions | AudioClipOptions | ControlClipOptions): this {
256
+ if (!this._currentTrack) throw new Error("TimelineBuilder: .clip() must be called after a track method (e.g. .animationTrack())");
257
+
258
+ const track = this._currentTrack;
259
+
260
+ switch (track.type) {
261
+ case TrackType.Animation: {
262
+ const animClip = assetOrOptions as AnimationClip;
263
+ const opts = (options ?? {}) as AnimationClipOptions;
264
+ const duration = opts.duration ?? animClip.duration;
265
+ const start = opts.start ?? track.cursor;
266
+ const end = start + duration;
267
+
268
+ const clipModel: ClipModel = {
269
+ start,
270
+ end,
271
+ duration,
272
+ timeScale: opts.speed ?? 1,
273
+ clipIn: opts.clipIn ?? 0,
274
+ easeInDuration: opts.easeIn ?? 0,
275
+ easeOutDuration: opts.easeOut ?? 0,
276
+ preExtrapolationMode: opts.preExtrapolation ?? ClipExtrapolation.None,
277
+ postExtrapolationMode: opts.postExtrapolation ?? ClipExtrapolation.None,
278
+ reversed: opts.reversed,
279
+ asset: {
280
+ clip: animClip,
281
+ loop: opts.loop ?? false,
282
+ duration: animClip.duration,
283
+ removeStartOffset: opts.removeStartOffset ?? false,
284
+ } satisfies AnimationClipModel,
285
+ };
286
+ track.clips.push(clipModel);
287
+ track.cursor = end;
288
+ break;
289
+ }
290
+
291
+ case TrackType.Audio: {
292
+ const url = assetOrOptions as string;
293
+ const opts = (options ?? {}) as AudioClipOptions;
294
+ const duration = opts.duration;
295
+ const start = opts.start ?? track.cursor;
296
+ const end = start + duration;
297
+
298
+ const clipModel: ClipModel = {
299
+ start,
300
+ end,
301
+ duration,
302
+ timeScale: opts.speed ?? 1,
303
+ clipIn: 0,
304
+ easeInDuration: opts.easeIn ?? 0,
305
+ easeOutDuration: opts.easeOut ?? 0,
306
+ preExtrapolationMode: ClipExtrapolation.None,
307
+ postExtrapolationMode: ClipExtrapolation.None,
308
+ asset: {
309
+ clip: url,
310
+ loop: opts.loop ?? false,
311
+ volume: opts.volume ?? 1,
312
+ } satisfies AudioClipModel,
313
+ };
314
+ track.clips.push(clipModel);
315
+ track.cursor = end;
316
+ break;
317
+ }
318
+
319
+ case TrackType.Activation: {
320
+ const opts = assetOrOptions as ActivationClipOptions;
321
+ const start = opts.start ?? track.cursor;
322
+ const end = start + opts.duration;
323
+
324
+ const clipModel: ClipModel = {
325
+ start,
326
+ end,
327
+ duration: opts.duration,
328
+ timeScale: 1,
329
+ clipIn: 0,
330
+ easeInDuration: opts.easeIn ?? 0,
331
+ easeOutDuration: opts.easeOut ?? 0,
332
+ preExtrapolationMode: ClipExtrapolation.None,
333
+ postExtrapolationMode: ClipExtrapolation.None,
334
+ asset: {},
335
+ };
336
+ track.clips.push(clipModel);
337
+ track.cursor = end;
338
+ break;
339
+ }
340
+
341
+ case TrackType.Control: {
342
+ const sourceObject = assetOrOptions as Object3D;
343
+ const opts = (options ?? {}) as ControlClipOptions;
344
+ const start = opts.start ?? track.cursor;
345
+ const duration = opts.duration;
346
+ const end = start + duration;
347
+
348
+ const clipModel: ClipModel = {
349
+ start,
350
+ end,
351
+ duration,
352
+ timeScale: 1,
353
+ clipIn: 0,
354
+ easeInDuration: 0,
355
+ easeOutDuration: 0,
356
+ preExtrapolationMode: ClipExtrapolation.None,
357
+ postExtrapolationMode: ClipExtrapolation.None,
358
+ asset: {
359
+ sourceObject,
360
+ controlActivation: opts.controlActivation ?? true,
361
+ updateDirector: opts.updateDirector ?? true,
362
+ } satisfies ControlClipModel,
363
+ };
364
+ track.clips.push(clipModel);
365
+ track.cursor = end;
366
+ break;
367
+ }
368
+
369
+ default:
370
+ throw new Error(`TimelineBuilder: .clip() is not supported on track type "${track.type}"`);
371
+ }
372
+
373
+ return this;
374
+ }
375
+
376
+ /**
377
+ * Adds a signal marker to the current signal or marker track.
378
+ * @param time - Time in seconds when the signal fires
379
+ * @param asset - The signal asset identifier (guid string)
380
+ * @param options - Optional marker configuration
381
+ */
382
+ marker(time: number, asset: string, options?: SignalMarkerOptions): this {
383
+ if (!this._currentTrack) throw new Error("TimelineBuilder: .marker() must be called after a track method");
384
+ if (this._currentTrack.type !== TrackType.Signal && this._currentTrack.type !== TrackType.Marker) {
385
+ throw new Error(`TimelineBuilder: .marker() is only supported on signal and marker tracks, not "${this._currentTrack.type}"`);
386
+ }
387
+
388
+ const marker: SignalMarkerModel = {
389
+ type: MarkerType.Signal,
390
+ time,
391
+ retroActive: options?.retroActive ?? false,
392
+ emitOnce: options?.emitOnce ?? false,
393
+ asset,
394
+ };
395
+ this._currentTrack.markers.push(marker);
396
+
397
+ // Update cursor past the marker
398
+ if (time > this._currentTrack.cursor) {
399
+ this._currentTrack.cursor = time;
400
+ }
401
+
402
+ return this;
403
+ }
404
+
405
+ /**
406
+ * Adds a signal with a callback to the current signal track.
407
+ * This is a convenience method that automatically generates a signal asset guid,
408
+ * adds the marker, and registers the callback so that {@link install} can wire up
409
+ * the `SignalReceiver` on the director's GameObject.
410
+ *
411
+ * @param time - Time in seconds when the signal fires
412
+ * @param callback - The function to invoke when the signal fires
413
+ * @param options - Optional marker configuration
414
+ *
415
+ * @example
416
+ * ```ts
417
+ * const timeline = TimelineBuilder.create("Sequence")
418
+ * .signalTrack("Events")
419
+ * .signal(1.0, () => console.log("1 second reached!"))
420
+ * .signal(3.5, () => console.log("halfway!"), { emitOnce: true })
421
+ * .install(director);
422
+ * ```
423
+ */
424
+ signal(time: number, callback: Function, options?: SignalMarkerOptions): this {
425
+ if (!this._currentTrack) throw new Error("TimelineBuilder: .signal() must be called after a track method");
426
+ if (this._currentTrack.type !== TrackType.Signal && this._currentTrack.type !== TrackType.Marker) {
427
+ throw new Error(`TimelineBuilder: .signal() is only supported on signal and marker tracks, not "${this._currentTrack.type}"`);
428
+ }
429
+
430
+ const guid = this._idProvider.generateUUID();
431
+ const trackIndex = this._tracks.indexOf(this._currentTrack);
432
+
433
+ // Add the marker with the generated guid
434
+ const marker: SignalMarkerModel = {
435
+ type: MarkerType.Signal,
436
+ time,
437
+ retroActive: options?.retroActive ?? false,
438
+ emitOnce: options?.emitOnce ?? false,
439
+ asset: guid,
440
+ };
441
+ this._currentTrack.markers.push(marker);
442
+
443
+ // Store the pending signal for wiring during install()
444
+ this._pendingSignals.push({ trackIndex, guid, callback });
445
+
446
+ if (time > this._currentTrack.cursor) {
447
+ this._currentTrack.cursor = time;
448
+ }
449
+
450
+ return this;
451
+ }
452
+
453
+ /**
454
+ * Mutes the current track so it is skipped during playback.
455
+ */
456
+ muted(muted: boolean = true): this {
457
+ if (!this._currentTrack) throw new Error("TimelineBuilder: .muted() must be called after a track method");
458
+ this._currentTrack.muted = muted;
459
+ return this;
460
+ }
461
+
462
+ // #endregion
463
+
464
+ /**
465
+ * Builds and returns the {@link TimelineAssetModel}.
466
+ * Assign the result to `PlayableDirector.playableAsset` to play it.
467
+ *
468
+ * If you used `.signal()` with callbacks, use {@link install} instead — it calls `build()`
469
+ * internally and also wires up the SignalReceiver on the director's GameObject.
470
+ */
471
+ build(): TimelineAssetModel {
472
+ const tracks: TrackModel[] = this._tracks.map(t => {
473
+ const track: TrackModel = {
474
+ name: t.name,
475
+ type: t.type,
476
+ muted: t.muted,
477
+ outputs: t.outputs,
478
+ };
479
+ if (t.clips.length > 0) track.clips = t.clips;
480
+ if (t.markers.length > 0) track.markers = t.markers;
481
+ if (t.volume !== undefined) track.volume = t.volume;
482
+ if (t.trackOffset !== undefined) track.trackOffset = t.trackOffset;
483
+ return track;
484
+ });
485
+
486
+ return {
487
+ name: this._name,
488
+ tracks,
489
+ };
490
+ }
491
+
492
+ /**
493
+ * Builds the timeline asset, assigns it to the director, and wires up any
494
+ * `.signal()` callbacks by creating/configuring a {@link SignalReceiver} on the
495
+ * director's GameObject.
496
+ *
497
+ * @param director - The PlayableDirector to install the timeline on
498
+ * @returns The built TimelineAssetModel (also assigned to `director.playableAsset`)
499
+ *
500
+ * @example
501
+ * ```ts
502
+ * TimelineBuilder.create("MyTimeline")
503
+ * .animationTrack("Anim", animator)
504
+ * .clip(walkClip, { duration: 2 })
505
+ * .signalTrack("Events")
506
+ * .signal(1.0, () => console.log("signal fired!"))
507
+ * .install(director);
508
+ *
509
+ * director.play();
510
+ * ```
511
+ */
512
+ install(director: PlayableDirector): TimelineAssetModel {
513
+ const asset = this.build();
514
+
515
+ // Wire up signal callbacks
516
+ if (this._pendingSignals.length > 0) {
517
+ const obj = director.gameObject;
518
+ let receiver = GameObject.getComponent(obj, SignalReceiver);
519
+ if (!receiver) {
520
+ receiver = GameObject.addComponent(obj, SignalReceiver);
521
+ }
522
+ if (!receiver.events) {
523
+ receiver.events = [];
524
+ }
525
+
526
+ for (const pending of this._pendingSignals) {
527
+ const signalAsset = new SignalAsset();
528
+ signalAsset.guid = pending.guid;
529
+
530
+ const evt = new SignalReceiverEvent();
531
+ evt.signal = signalAsset;
532
+ evt.reaction = new EventList([pending.callback]);
533
+ receiver.events.push(evt);
534
+
535
+ // Wire the receiver as the output binding for the signal track
536
+ const track = asset.tracks[pending.trackIndex];
537
+ if (track && !track.outputs.includes(receiver)) {
538
+ track.outputs.push(receiver);
539
+ }
540
+ }
541
+ }
542
+
543
+ director.playableAsset = asset;
544
+ return asset;
545
+ }
546
+
547
+ // #region Private helpers
548
+
549
+ private pushTrack(name: string, type: TrackType, binding: object | null): BuilderTrack {
550
+ const track: BuilderTrack = {
551
+ name,
552
+ type,
553
+ muted: false,
554
+ outputs: binding ? [binding] : [],
555
+ clips: [],
556
+ markers: [],
557
+ cursor: 0,
558
+ };
559
+ this._tracks.push(track);
560
+ return track;
561
+ }
562
+
563
+ // #endregion
564
+ }
@@ -42,7 +42,7 @@ export enum ClipExtrapolation {
42
42
  * @category Animation and Sequencing
43
43
  * @see {@link PlayableDirector} for the main component to control timelines in Needle Engine.
44
44
  */
45
- export declare type TrackModel = {
45
+ export type TrackModel = {
46
46
  name: string;
47
47
  type: TrackType;
48
48
  muted: boolean;
@@ -53,6 +53,10 @@ export declare type TrackModel = {
53
53
  volume?: number;
54
54
  }
55
55
 
56
+ export function isTrackModel(obj:unknown): obj is TrackModel {
57
+ return typeof obj === "object" && obj !== null && "name" in obj && "type" in obj && "outputs" in obj && "muted" in obj;
58
+ }
59
+
56
60
  declare type Vec3 = { x: number, y: number, z: number };
57
61
  declare type Quat = { x: number, y: number, z: number, w: number };
58
62