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

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 (96) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/components.needle.json +1 -1
  3. package/dist/{needle-engine.bundle-B7cqsI4c.js → needle-engine.bundle-C-LG00ZZ.js} +6570 -6271
  4. package/dist/{needle-engine.bundle-AjVIot3d.min.js → needle-engine.bundle-D7tzaiYE.min.js} +157 -157
  5. package/dist/{needle-engine.bundle-DQCuBTVp.umd.cjs → needle-engine.bundle-OPkPmdUM.umd.cjs} +140 -140
  6. package/dist/needle-engine.d.ts +668 -191
  7. package/dist/needle-engine.js +597 -595
  8. package/dist/needle-engine.min.js +1 -1
  9. package/dist/needle-engine.umd.cjs +1 -1
  10. package/dist/three.js +1 -0
  11. package/dist/three.min.js +21 -21
  12. package/dist/three.umd.cjs +16 -16
  13. package/lib/engine/api.d.ts +2 -0
  14. package/lib/engine/api.js +2 -0
  15. package/lib/engine/api.js.map +1 -1
  16. package/lib/engine/codegen/register_types.js +10 -10
  17. package/lib/engine/codegen/register_types.js.map +1 -1
  18. package/lib/engine/engine_camera.fit.js +16 -4
  19. package/lib/engine/engine_camera.fit.js.map +1 -1
  20. package/lib/engine/engine_context.d.ts +20 -7
  21. package/lib/engine/engine_context.js +29 -14
  22. package/lib/engine/engine_context.js.map +1 -1
  23. package/lib/engine/engine_context_eventbus.d.ts +47 -0
  24. package/lib/engine/engine_context_eventbus.js +47 -0
  25. package/lib/engine/engine_context_eventbus.js.map +1 -0
  26. package/lib/engine/engine_input.d.ts +23 -4
  27. package/lib/engine/engine_input.js +2 -1
  28. package/lib/engine/engine_input.js.map +1 -1
  29. package/lib/engine/engine_physics_rapier.d.ts +10 -0
  30. package/lib/engine/engine_physics_rapier.js +6 -0
  31. package/lib/engine/engine_physics_rapier.js.map +1 -1
  32. package/lib/engine/engine_types.d.ts +10 -0
  33. package/lib/engine-components/AnimationBuilder.d.ts +158 -0
  34. package/lib/engine-components/AnimationBuilder.js +305 -0
  35. package/lib/engine-components/AnimationBuilder.js.map +1 -0
  36. package/lib/engine-components/Animator.js +6 -1
  37. package/lib/engine-components/Animator.js.map +1 -1
  38. package/lib/engine-components/AnimatorController.builder.d.ts +101 -23
  39. package/lib/engine-components/AnimatorController.builder.js +88 -20
  40. package/lib/engine-components/AnimatorController.builder.js.map +1 -1
  41. package/lib/engine-components/AnimatorController.js +2 -0
  42. package/lib/engine-components/AnimatorController.js.map +1 -1
  43. package/lib/engine-components/ContactShadows.d.ts +1 -0
  44. package/lib/engine-components/ContactShadows.js +14 -1
  45. package/lib/engine-components/ContactShadows.js.map +1 -1
  46. package/lib/engine-components/DropListener.js +3 -0
  47. package/lib/engine-components/DropListener.js.map +1 -1
  48. package/lib/engine-components/OrbitControls.d.ts +0 -2
  49. package/lib/engine-components/OrbitControls.js +14 -1
  50. package/lib/engine-components/OrbitControls.js.map +1 -1
  51. package/lib/engine-components/SceneSwitcher.js +3 -0
  52. package/lib/engine-components/SceneSwitcher.js.map +1 -1
  53. package/lib/engine-components/api.d.ts +1 -0
  54. package/lib/engine-components/api.js +1 -0
  55. package/lib/engine-components/api.js.map +1 -1
  56. package/lib/engine-components/codegen/components.d.ts +6 -6
  57. package/lib/engine-components/codegen/components.js +6 -6
  58. package/lib/engine-components/codegen/components.js.map +1 -1
  59. package/lib/engine-components/postprocessing/Effects/Tonemapping.utils.d.ts +1 -1
  60. package/lib/engine-components/timeline/PlayableDirector.d.ts +7 -7
  61. package/lib/engine-components/timeline/PlayableDirector.js +6 -6
  62. package/lib/engine-components/timeline/PlayableDirector.js.map +1 -1
  63. package/lib/engine-components/timeline/TimelineBuilder.d.ts +175 -9
  64. package/lib/engine-components/timeline/TimelineBuilder.js +108 -2
  65. package/lib/engine-components/timeline/TimelineBuilder.js.map +1 -1
  66. package/lib/engine-components/timeline/TimelineTracks.d.ts +15 -7
  67. package/lib/engine-components/timeline/TimelineTracks.js +22 -14
  68. package/lib/engine-components/timeline/TimelineTracks.js.map +1 -1
  69. package/lib/engine-components/web/CursorFollow.d.ts +0 -1
  70. package/lib/engine-components/web/CursorFollow.js +0 -1
  71. package/lib/engine-components/web/CursorFollow.js.map +1 -1
  72. package/package.json +1 -1
  73. package/plugins/common/cloud.js +6 -1
  74. package/plugins/vite/license.js +19 -1
  75. package/src/engine/api.ts +3 -0
  76. package/src/engine/codegen/register_types.ts +10 -10
  77. package/src/engine/engine_camera.fit.ts +15 -4
  78. package/src/engine/engine_context.ts +30 -15
  79. package/src/engine/engine_context_eventbus.ts +73 -0
  80. package/src/engine/engine_input.ts +27 -6
  81. package/src/engine/engine_physics_rapier.ts +20 -6
  82. package/src/engine/engine_types.ts +22 -12
  83. package/src/engine-components/AnimationBuilder.ts +472 -0
  84. package/src/engine-components/Animator.ts +6 -1
  85. package/src/engine-components/AnimatorController.builder.ts +163 -37
  86. package/src/engine-components/AnimatorController.ts +1 -0
  87. package/src/engine-components/ContactShadows.ts +15 -1
  88. package/src/engine-components/DropListener.ts +3 -0
  89. package/src/engine-components/OrbitControls.ts +16 -5
  90. package/src/engine-components/SceneSwitcher.ts +3 -0
  91. package/src/engine-components/api.ts +1 -0
  92. package/src/engine-components/codegen/components.ts +6 -6
  93. package/src/engine-components/timeline/PlayableDirector.ts +20 -20
  94. package/src/engine-components/timeline/TimelineBuilder.ts +277 -17
  95. package/src/engine-components/timeline/TimelineTracks.ts +24 -16
  96. package/src/engine-components/web/CursorFollow.ts +0 -1
@@ -0,0 +1,472 @@
1
+ import { AnimationClip, BooleanKeyframeTrack, Color, ColorKeyframeTrack, Euler,
2
+ InterpolateDiscrete, InterpolateLinear, InterpolateSmooth, KeyframeTrack,
3
+ NumberKeyframeTrack, Object3D, Quaternion, QuaternionKeyframeTrack,
4
+ Vector3, VectorKeyframeTrack } from "three";
5
+ import type { Camera, InterpolationModes, Light, Material, PerspectiveCamera } from "three";
6
+
7
+ import { isDevEnvironment } from "../engine/debug/index.js";
8
+
9
+
10
+ // ============================================================
11
+ // Value types (R3F-style array support)
12
+ // ============================================================
13
+
14
+ /** A Vector3 value, either as a Three.js Vector3 or as a `[x, y, z]` tuple */
15
+ export type Vec3Value = Vector3 | [number, number, number];
16
+ /** A Quaternion value, either as a Three.js Quaternion or as a `[x, y, z, w]` tuple */
17
+ export type QuatValue = Quaternion | [number, number, number, number];
18
+ /** A Color value, either as a Three.js Color or as an `[r, g, b]` tuple (0–1) */
19
+ export type ColorValue = Color | [number, number, number];
20
+ /** An Euler value, either as a Three.js Euler or as a `[x, y, z]` tuple (radians) */
21
+ export type EulerValue = Euler | [number, number, number];
22
+
23
+ // ============================================================
24
+ // Interpolation
25
+ // ============================================================
26
+
27
+ /** User-friendly interpolation mode names */
28
+ export type AnimationInterpolation = "linear" | "smooth" | "step";
29
+
30
+ // ============================================================
31
+ // Keyframe & Tween
32
+ // ============================================================
33
+
34
+ /** A single keyframe: a time and a value */
35
+ export type AnimationKeyframe<V> = {
36
+ /** Time in seconds */
37
+ time: number;
38
+ /** The value at this time */
39
+ value: V;
40
+ /** Interpolation mode for this track (default: `"linear"`). Note: Three.js applies one mode per track; the first keyframe's mode is used. */
41
+ interpolation?: AnimationInterpolation;
42
+ };
43
+
44
+ /** Shorthand for a simple two-keyframe animation (start → end) */
45
+ export type Tween<V> = {
46
+ /** Start value (at time 0) */
47
+ from: V;
48
+ /** End value (at time = duration) */
49
+ to: V;
50
+ /** Duration in seconds (default: 1) */
51
+ duration?: number;
52
+ /** Interpolation mode (default: `"linear"`) */
53
+ interpolation?: AnimationInterpolation;
54
+ };
55
+
56
+ /** Keyframe array or tween shorthand */
57
+ type KF<V> = AnimationKeyframe<V>[] | Tween<V>;
58
+
59
+ // ============================================================
60
+ // TrackDescriptor
61
+ // ============================================================
62
+
63
+ /**
64
+ * An opaque descriptor for a single animation track.
65
+ * Created by {@link track} and resolved into a Three.js KeyframeTrack
66
+ * when passed to {@link createAnimation}, or inline to
67
+ * {@link AnimatorControllerBuilder.state} / {@link TimelineBuilder.clip}.
68
+ *
69
+ * @category Animation and Sequencing
70
+ */
71
+ export type TrackDescriptor = {
72
+ readonly __isTrackDescriptor: true;
73
+ /** @internal */ readonly _target: object;
74
+ /** @internal */ readonly _property: string;
75
+ /** @internal */ readonly _keyframes: Array<{ time: number; value: any; interpolation?: AnimationInterpolation }>;
76
+ /** @internal */ readonly _root?: Object3D;
77
+ };
78
+
79
+ // ============================================================
80
+ // TrackOptions / CreateAnimationOptions
81
+ // ============================================================
82
+
83
+ /** Options for a single track */
84
+ export type TrackOptions = {
85
+ /**
86
+ * Root object for resolving the track path.
87
+ * - If `root === target` → self-targeting (`.property`)
88
+ * - If `root !== target` → named targeting (`"targetName.property"` using `target.name`)
89
+ * - If omitted → self-targeting by default
90
+ */
91
+ root?: Object3D;
92
+ };
93
+
94
+ /** Options for {@link createAnimation} */
95
+ export type CreateAnimationOptions = {
96
+ /** Default root for all tracks that don't specify their own */
97
+ root?: Object3D;
98
+ /** Clip name (auto-generated if omitted) */
99
+ name?: string;
100
+ };
101
+
102
+ // ============================================================
103
+ // track() — type-safe overloads
104
+ // ============================================================
105
+
106
+ // --- Object3D ---
107
+ /** Create an animation track for an Object3D's position or scale */
108
+ export function track(target: Object3D, property: "position" | "scale", keyframes: KF<Vec3Value>, options?: TrackOptions): TrackDescriptor;
109
+ /** Create an animation track for an Object3D's quaternion */
110
+ export function track(target: Object3D, property: "quaternion", keyframes: KF<QuatValue>, options?: TrackOptions): TrackDescriptor;
111
+ /** Create an animation track for an Object3D's rotation (Euler angles, converted to quaternion internally) */
112
+ export function track(target: Object3D, property: "rotation", keyframes: KF<EulerValue>, options?: TrackOptions): TrackDescriptor;
113
+ /** Create an animation track for an Object3D's visibility */
114
+ export function track(target: Object3D, property: "visible", keyframes: KF<boolean>, options?: TrackOptions): TrackDescriptor;
115
+
116
+ // --- Material ---
117
+ /** Create an animation track for a material's numeric property */
118
+ export function track(target: Material, property: "opacity" | "roughness" | "metalness" | "alphaTest" | "emissiveIntensity" | "envMapIntensity" | "bumpScale" | "displacementScale" | "displacementBias", keyframes: KF<number>, options?: TrackOptions): TrackDescriptor;
119
+ /** Create an animation track for a material's color property */
120
+ export function track(target: Material, property: "color" | "emissive", keyframes: KF<ColorValue>, options?: TrackOptions): TrackDescriptor;
121
+
122
+ // --- Light ---
123
+ /** Create an animation track for a light's numeric property */
124
+ export function track(target: Light, property: "intensity" | "distance" | "angle" | "penumbra" | "decay", keyframes: KF<number>, options?: TrackOptions): TrackDescriptor;
125
+ /** Create an animation track for a light's color */
126
+ export function track(target: Light, property: "color", keyframes: KF<ColorValue>, options?: TrackOptions): TrackDescriptor;
127
+
128
+ // --- Camera ---
129
+ /** Create an animation track for a camera's numeric property */
130
+ export function track(target: PerspectiveCamera, property: "fov" | "near" | "far" | "zoom", keyframes: KF<number>, options?: TrackOptions): TrackDescriptor;
131
+
132
+ // --- Implementation ---
133
+ /**
134
+ * Creates an animation track descriptor targeting a property on the given object.
135
+ *
136
+ * The `target` is used for **TypeScript type inference** — it determines which property names
137
+ * are offered and what value types the keyframes accept. By default, the resulting track
138
+ * targets "self" (the mixer root). Pass `{ root }` to target a named child instead.
139
+ *
140
+ * @param target - The object whose type determines valid properties and value types
141
+ * @param property - The property to animate (e.g. `"position"`, `"opacity"`, `"intensity"`)
142
+ * @param keyframes - An array of {@link AnimationKeyframe} objects, or a {@link Tween} shorthand
143
+ * @param options - Optional {@link TrackOptions} with a `root` for named targeting
144
+ * @returns A {@link TrackDescriptor} that can be passed to {@link createAnimation}, or inline
145
+ * to `AnimatorControllerBuilder.state()` or `TimelineBuilder.clip()`
146
+ *
147
+ * @example Keyframe array
148
+ * ```ts
149
+ * track(door, "position", [
150
+ * { time: 0, value: [0, 0, 0] },
151
+ * { time: 1, value: [2, 0, 0] },
152
+ * ])
153
+ * ```
154
+ *
155
+ * @example Tween shorthand
156
+ * ```ts
157
+ * track(door, "position", { from: [0, 0, 0], to: [2, 0, 0], duration: 1 })
158
+ * ```
159
+ *
160
+ * @example Named targeting (track targets a child of root)
161
+ * ```ts
162
+ * track(door, "position", keyframes, { root: room })
163
+ * ```
164
+ *
165
+ * @category Animation and Sequencing
166
+ * @group Utilities
167
+ */
168
+ export function track(target: object, property: string, keyframes: KF<any>, options?: TrackOptions): TrackDescriptor {
169
+ const kf = isTween(keyframes) ? tweenToKeyframes(keyframes) : keyframes;
170
+ return {
171
+ __isTrackDescriptor: true as const,
172
+ _target: target,
173
+ _property: property,
174
+ _keyframes: kf.map(k => ({ ...k, value: snapshotValue(k.value) })),
175
+ _root: options?.root,
176
+ };
177
+ }
178
+
179
+
180
+ /** @internal alias so the AnimationBuilder class method can call the standalone function */
181
+ const trackFn = track;
182
+
183
+ // ============================================================
184
+ // AnimationBuilder
185
+ // ============================================================
186
+
187
+ /**
188
+ * A fluent builder for creating `AnimationClip` instances from code.
189
+ *
190
+ * Use {@link AnimationBuilder.create} to start a new builder, chain `.track()` calls
191
+ * to add animation tracks, and call `.build()` to produce the clip.
192
+ *
193
+ * @example Single track
194
+ * ```ts
195
+ * const clip = AnimationBuilder.create()
196
+ * .track(door, "position", { from: [0,0,0], to: [2,0,0], duration: 1 })
197
+ * .build();
198
+ * ```
199
+ *
200
+ * @example Multiple tracks
201
+ * ```ts
202
+ * const clip = AnimationBuilder.create("DoorOpen")
203
+ * .track(door, "position", { from: [0,0,0], to: [2,0,0], duration: 1 })
204
+ * .track(light, "intensity", { from: 0, to: 5, duration: 1 })
205
+ * .build(room);
206
+ * ```
207
+ *
208
+ * @category Animation and Sequencing
209
+ * @group Utilities
210
+ */
211
+ export class AnimationBuilder {
212
+ private _name?: string;
213
+ private _tracks: TrackDescriptor[] = [];
214
+
215
+ /** Creates a new AnimationBuilder instance */
216
+ static create(name?: string): AnimationBuilder {
217
+ return new AnimationBuilder(name);
218
+ }
219
+
220
+ constructor(name?: string) {
221
+ this._name = name;
222
+ }
223
+
224
+ // --- Object3D ---
225
+ /** Adds an animation track for an Object3D's position or scale */
226
+ track(target: Object3D, property: "position" | "scale", keyframes: KF<Vec3Value>, options?: TrackOptions): this;
227
+ /** Adds an animation track for an Object3D's quaternion */
228
+ track(target: Object3D, property: "quaternion", keyframes: KF<QuatValue>, options?: TrackOptions): this;
229
+ /** Adds an animation track for an Object3D's rotation (Euler, converted to quaternion) */
230
+ track(target: Object3D, property: "rotation", keyframes: KF<EulerValue>, options?: TrackOptions): this;
231
+ /** Adds an animation track for an Object3D's visibility */
232
+ track(target: Object3D, property: "visible", keyframes: KF<boolean>, options?: TrackOptions): this;
233
+ // --- Material ---
234
+ /** Adds an animation track for a material's numeric property */
235
+ track(target: Material, property: "opacity" | "roughness" | "metalness" | "alphaTest" | "emissiveIntensity" | "envMapIntensity" | "bumpScale" | "displacementScale" | "displacementBias", keyframes: KF<number>, options?: TrackOptions): this;
236
+ /** Adds an animation track for a material's color property */
237
+ track(target: Material, property: "color" | "emissive", keyframes: KF<ColorValue>, options?: TrackOptions): this;
238
+ // --- Light ---
239
+ /** Adds an animation track for a light's numeric property */
240
+ track(target: Light, property: "intensity" | "distance" | "angle" | "penumbra" | "decay", keyframes: KF<number>, options?: TrackOptions): this;
241
+ /** Adds an animation track for a light's color */
242
+ track(target: Light, property: "color", keyframes: KF<ColorValue>, options?: TrackOptions): this;
243
+ // --- Camera ---
244
+ /** Adds an animation track for a camera's numeric property */
245
+ track(target: PerspectiveCamera, property: "fov" | "near" | "far" | "zoom", keyframes: KF<number>, options?: TrackOptions): this;
246
+ track(target: object, property: string, keyframes: KF<any>, options?: TrackOptions): this {
247
+ this._tracks.push(trackFn(target as Object3D, property as "position", keyframes, options));
248
+ return this;
249
+ }
250
+
251
+ /**
252
+ * Builds and returns the `AnimationClip`.
253
+ * @param root - Optional root Object3D for resolving track paths.
254
+ * When provided, tracks targeting a different object use `target.name` for named resolution.
255
+ */
256
+ build(root?: Object3D): AnimationClip {
257
+ return resolveToClip(this._tracks, root, this._name);
258
+ }
259
+ }
260
+
261
+ // Keep createAnimation as internal alias for backwards compatibility
262
+ /** @internal @deprecated Use {@link AnimationBuilder.create} instead */
263
+ export function createAnimation(options: CreateAnimationOptions, ...tracks: TrackDescriptor[]): AnimationClip;
264
+ /** @internal @deprecated Use {@link AnimationBuilder.create} instead */
265
+ export function createAnimation(...tracks: TrackDescriptor[]): AnimationClip;
266
+ export function createAnimation(...args: (CreateAnimationOptions | TrackDescriptor)[]): AnimationClip {
267
+ let options: CreateAnimationOptions | undefined;
268
+ let descriptors: TrackDescriptor[];
269
+
270
+ if (args.length > 0 && !isTrackDescriptor(args[0])) {
271
+ options = args[0] as CreateAnimationOptions;
272
+ descriptors = args.slice(1) as TrackDescriptor[];
273
+ }
274
+ else {
275
+ descriptors = args as TrackDescriptor[];
276
+ }
277
+
278
+ return resolveToClip(descriptors, options?.root, options?.name);
279
+ }
280
+
281
+
282
+ // ============================================================
283
+ // Resolution helpers (exported for use by AnimatorControllerBuilder / TimelineBuilder)
284
+ // ============================================================
285
+
286
+ /**
287
+ * Resolves a clip source (AnimationClip, TrackDescriptor, or TrackDescriptor[]) into an AnimationClip.
288
+ * Used internally by AnimatorControllerBuilder and TimelineBuilder.
289
+ * @internal
290
+ */
291
+ export function resolveClipSource(clip: AnimationClip | TrackDescriptor | TrackDescriptor[], root?: Object3D): AnimationClip {
292
+ if (clip instanceof AnimationClip) return clip;
293
+ if (Array.isArray(clip)) return resolveToClip(clip, root);
294
+ if (isTrackDescriptor(clip)) return resolveToClip([clip], root);
295
+ return clip as AnimationClip; // should not reach
296
+ }
297
+
298
+ /** Type guard for {@link TrackDescriptor} */
299
+ export function isTrackDescriptor(obj: unknown): obj is TrackDescriptor {
300
+ return obj != null && typeof obj === "object" && (obj as any).__isTrackDescriptor === true;
301
+ }
302
+
303
+ /** Resolves an array of TrackDescriptors into an AnimationClip. @internal */
304
+ export function resolveToClip(
305
+ descriptors: TrackDescriptor[],
306
+ buildRoot?: Object3D,
307
+ name?: string,
308
+ ): AnimationClip {
309
+ const keyframeTracks: KeyframeTrack[] = [];
310
+ for (const desc of descriptors) {
311
+ keyframeTracks.push(buildKeyframeTrack(desc, buildRoot));
312
+ }
313
+ let duration = 0;
314
+ for (const t of keyframeTracks) {
315
+ const last = t.times[t.times.length - 1];
316
+ if (last !== undefined && last > duration) duration = last;
317
+ }
318
+ return new AnimationClip(name ?? `clip_${_clipCounter++}`, duration, keyframeTracks);
319
+ }
320
+
321
+
322
+ // ============================================================
323
+ // Internal helpers
324
+ // ============================================================
325
+
326
+ let _clipCounter = 0;
327
+
328
+ function buildKeyframeTrack(desc: TrackDescriptor, buildRoot?: Object3D): KeyframeTrack {
329
+ const property = resolvePropertyName(desc._property);
330
+ const trackName = resolveTrackName(desc, buildRoot, property);
331
+
332
+ const times: number[] = [];
333
+ const values: number[] = [];
334
+ for (const kf of desc._keyframes) {
335
+ times.push(kf.time);
336
+ const flat = flattenValue(kf.value, desc._property);
337
+ for (let i = 0; i < flat.length; i++) values.push(flat[i]);
338
+ }
339
+
340
+ const interpolation = resolveInterpolation(desc._keyframes[0]?.interpolation);
341
+ const TrackClass = resolveTrackClass(desc._property, desc._keyframes[0]?.value);
342
+ return new TrackClass(trackName, times, values, interpolation);
343
+ }
344
+
345
+ function resolveTrackName(desc: TrackDescriptor, buildRoot?: Object3D, resolvedProperty?: string): string {
346
+ const root = desc._root ?? buildRoot;
347
+ const property = resolvedProperty ?? resolvePropertyName(desc._property);
348
+
349
+ // Material target → always self-targeting with .material. prefix
350
+ if (isMaterial(desc._target)) {
351
+ return `.material.${property}`;
352
+ }
353
+
354
+ // No root → self-targeting
355
+ if (!root) return `.${property}`;
356
+ // Root === target → self-targeting
357
+ if (root === desc._target) return `.${property}`;
358
+
359
+ // Root !== target → named targeting
360
+ const target = desc._target as Object3D;
361
+ const nodeName = target.name;
362
+ if (!nodeName) {
363
+ if (isDevEnvironment()) {
364
+ console.warn(`AnimationBuilder: target has no name, falling back to self-targeting. Set target.name for named targeting.`);
365
+ }
366
+ return `.${property}`;
367
+ }
368
+
369
+ // Dev mode: validate that target is actually a descendant of root
370
+ if (isDevEnvironment() && root instanceof Object3D && target instanceof Object3D) {
371
+ let found = false;
372
+ root.traverse(child => {
373
+ if (child === target) found = true;
374
+ });
375
+ if (!found) {
376
+ console.warn(`AnimationBuilder: target "${nodeName}" is not a descendant of the provided root "${root.name}". The track may not resolve at play time.`);
377
+ }
378
+ }
379
+
380
+ return `${nodeName}.${property}`;
381
+ }
382
+
383
+ function resolvePropertyName(property: string): string {
384
+ if (property === "rotation") return "quaternion";
385
+ return property;
386
+ }
387
+
388
+ function resolveTrackClass(property: string, sampleValue: any): new (name: string, times: ArrayLike<number>, values: ArrayLike<number>, interpolation?: InterpolationModes) => KeyframeTrack {
389
+ // Check property name first (most reliable)
390
+ if (property === "quaternion" || property === "rotation") return QuaternionKeyframeTrack;
391
+ if (property === "visible") return BooleanKeyframeTrack;
392
+ if (property === "position" || property === "scale") return VectorKeyframeTrack;
393
+ if (property === "color" || property === "emissive") return ColorKeyframeTrack;
394
+
395
+ // Check value type
396
+ if (sampleValue instanceof Vector3) return VectorKeyframeTrack;
397
+ if (sampleValue instanceof Quaternion) return QuaternionKeyframeTrack;
398
+ if (sampleValue instanceof Color) return ColorKeyframeTrack;
399
+ if (sampleValue instanceof Euler) return QuaternionKeyframeTrack;
400
+ if (typeof sampleValue === "boolean") return BooleanKeyframeTrack;
401
+ if (typeof sampleValue === "number") return NumberKeyframeTrack;
402
+
403
+ // Array → infer from length + property context
404
+ if (Array.isArray(sampleValue)) {
405
+ if (sampleValue.length === 4) return QuaternionKeyframeTrack;
406
+ if (sampleValue.length === 3) return VectorKeyframeTrack;
407
+ if (sampleValue.length === 2) return VectorKeyframeTrack;
408
+ return NumberKeyframeTrack;
409
+ }
410
+
411
+ return NumberKeyframeTrack;
412
+ }
413
+
414
+ function flattenValue(value: any, property: string): number[] {
415
+ // Tuple arrays — already flat
416
+ if (Array.isArray(value)) {
417
+ // Special case: Euler array [x,y,z] for "rotation" → convert to quaternion
418
+ if (property === "rotation" && value.length === 3) {
419
+ const q = new Quaternion().setFromEuler(new Euler(value[0], value[1], value[2]));
420
+ return [q.x, q.y, q.z, q.w];
421
+ }
422
+ return value;
423
+ }
424
+
425
+ if (typeof value === "number") return [value];
426
+ if (typeof value === "boolean") return [value ? 1 : 0];
427
+ if (value instanceof Vector3) return [value.x, value.y, value.z];
428
+ if (value instanceof Quaternion) return [value.x, value.y, value.z, value.w];
429
+ if (value instanceof Color) return [value.r, value.g, value.b];
430
+ if (value instanceof Euler) {
431
+ const q = new Quaternion().setFromEuler(value);
432
+ return [q.x, q.y, q.z, q.w];
433
+ }
434
+ // duck-type Vector2/Vector3-like
435
+ if (typeof value === "object" && value !== null && "x" in value && "y" in value) {
436
+ if ("w" in value) return [value.x, value.y, value.z, value.w];
437
+ if ("z" in value) return [value.x, value.y, value.z];
438
+ return [value.x, value.y];
439
+ }
440
+ return [Number(value)];
441
+ }
442
+
443
+ function resolveInterpolation(mode?: AnimationInterpolation): InterpolationModes {
444
+ switch (mode) {
445
+ case "smooth": return InterpolateSmooth;
446
+ case "step": return InterpolateDiscrete;
447
+ default: return InterpolateLinear;
448
+ }
449
+ }
450
+
451
+ function isTween<V>(kf: KF<V>): kf is Tween<V> {
452
+ return kf != null && !Array.isArray(kf) && typeof kf === "object" && "from" in kf && "to" in kf;
453
+ }
454
+
455
+ function tweenToKeyframes<V>(tween: Tween<V>): AnimationKeyframe<V>[] {
456
+ return [
457
+ { time: 0, value: tween.from, interpolation: tween.interpolation },
458
+ { time: tween.duration ?? 1, value: tween.to, interpolation: tween.interpolation },
459
+ ];
460
+ }
461
+
462
+ /** Snapshot a keyframe value so live references (e.g. `obj.position`) are captured at definition time */
463
+ function snapshotValue(value: any): any {
464
+ if (value == null || typeof value !== "object") return value; // primitives are fine
465
+ if (typeof value.clone === "function") return value.clone(); // Vector3, Quaternion, Color, Euler, etc.
466
+ if (Array.isArray(value)) return value.slice(); // tuple arrays
467
+ return value;
468
+ }
469
+
470
+ function isMaterial(obj: any): obj is Material {
471
+ return obj != null && typeof obj === "object" && obj.isMaterial === true;
472
+ }
@@ -128,6 +128,10 @@ export class Animator extends Behaviour implements IAnimationComponent {
128
128
  if (this._animatorController && this._animatorController.model === val) {
129
129
  return;
130
130
  }
131
+ // Dispose the previous controller to stop its mixer and unregister it
132
+ if (this._animatorController) {
133
+ this._animatorController.dispose();
134
+ }
131
135
  if (val) {
132
136
  if (!(val instanceof AnimatorController)) {
133
137
  if (debug) console.log("Assign animator controller", val, this);
@@ -144,7 +148,8 @@ export class Animator extends Behaviour implements IAnimationComponent {
144
148
  val = new AnimatorController(val.model);
145
149
  }
146
150
  this._animatorController = val;
147
- this._animatorController.bind(this);
151
+ if (this.__didAwake)
152
+ this._animatorController.bind(this);
148
153
  }
149
154
  }
150
155
  else this._animatorController = null;