@needle-tools/engine 5.0.4 → 5.0.6

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 (91) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/components.needle.json +1 -1
  3. package/dist/{needle-engine.bundle-CX-SJZzp.min.js → needle-engine.bundle-BNqZHrmH.min.js} +142 -141
  4. package/dist/{needle-engine.bundle-CYrPktak.umd.cjs → needle-engine.bundle-BY1mZ1X_.umd.cjs} +140 -139
  5. package/dist/{needle-engine.bundle-B3Km2VZ4.js → needle-engine.bundle-BsFWwS-j.js} +8183 -7957
  6. package/dist/needle-engine.d.ts +139 -14
  7. package/dist/needle-engine.js +560 -557
  8. package/dist/needle-engine.min.js +1 -1
  9. package/dist/needle-engine.umd.cjs +1 -1
  10. package/dist/{vendor-vHLk8sXu.js → vendor-CAcsI0eU.js} +116 -115
  11. package/dist/{vendor-CntUvmJu.umd.cjs → vendor-CEM38hLE.umd.cjs} +2 -2
  12. package/dist/{vendor-DPbfJJ4d.min.js → vendor-HRlxIBga.min.js} +2 -2
  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/debug/debug_spatial_console.d.ts +2 -0
  17. package/lib/engine/debug/debug_spatial_console.js +10 -7
  18. package/lib/engine/debug/debug_spatial_console.js.map +1 -1
  19. package/lib/engine/engine_addressables.d.ts +2 -0
  20. package/lib/engine/engine_addressables.js +6 -3
  21. package/lib/engine/engine_addressables.js.map +1 -1
  22. package/lib/engine/engine_audio.d.ts +68 -0
  23. package/lib/engine/engine_audio.js +172 -0
  24. package/lib/engine/engine_audio.js.map +1 -1
  25. package/lib/engine/engine_gameobject.js +2 -2
  26. package/lib/engine/engine_gameobject.js.map +1 -1
  27. package/lib/engine/engine_init.js +8 -0
  28. package/lib/engine/engine_init.js.map +1 -1
  29. package/lib/engine/engine_mainloop_utils.js +5 -2
  30. package/lib/engine/engine_mainloop_utils.js.map +1 -1
  31. package/lib/engine/engine_serialization_builtin_serializer.js +27 -0
  32. package/lib/engine/engine_serialization_builtin_serializer.js.map +1 -1
  33. package/lib/engine/webcomponents/needle-engine.d.ts +9 -3
  34. package/lib/engine/webcomponents/needle-engine.js.map +1 -1
  35. package/lib/engine/xr/NeedleXRSession.d.ts +3 -2
  36. package/lib/engine/xr/NeedleXRSession.js +50 -14
  37. package/lib/engine/xr/NeedleXRSession.js.map +1 -1
  38. package/lib/engine-components/Animation.js +17 -16
  39. package/lib/engine-components/Animation.js.map +1 -1
  40. package/lib/engine-components/AnimatorController.d.ts +2 -0
  41. package/lib/engine-components/AnimatorController.js +4 -1
  42. package/lib/engine-components/AnimatorController.js.map +1 -1
  43. package/lib/engine-components/AudioSource.d.ts +19 -3
  44. package/lib/engine-components/AudioSource.js +121 -68
  45. package/lib/engine-components/AudioSource.js.map +1 -1
  46. package/lib/engine-components/DragControls.d.ts +7 -0
  47. package/lib/engine-components/DragControls.js +19 -0
  48. package/lib/engine-components/DragControls.js.map +1 -1
  49. package/lib/engine-components/NestedGltf.d.ts +19 -3
  50. package/lib/engine-components/NestedGltf.js +19 -3
  51. package/lib/engine-components/NestedGltf.js.map +1 -1
  52. package/lib/engine-components/Networking.d.ts +1 -1
  53. package/lib/engine-components/Networking.js +1 -1
  54. package/lib/engine-components/OrbitControls.js +16 -11
  55. package/lib/engine-components/OrbitControls.js.map +1 -1
  56. package/lib/engine-components/postprocessing/VolumeParameter.d.ts +2 -0
  57. package/lib/engine-components/postprocessing/VolumeParameter.js +4 -1
  58. package/lib/engine-components/postprocessing/VolumeParameter.js.map +1 -1
  59. package/lib/engine-components/ui/Canvas.d.ts +1 -1
  60. package/lib/engine-components/ui/Canvas.js +2 -8
  61. package/lib/engine-components/ui/Canvas.js.map +1 -1
  62. package/lib/engine-components/ui/Text.d.ts +8 -1
  63. package/lib/engine-components/ui/Text.js +29 -14
  64. package/lib/engine-components/ui/Text.js.map +1 -1
  65. package/lib/engine-components/web/CursorFollow.js +21 -12
  66. package/lib/engine-components/web/CursorFollow.js.map +1 -1
  67. package/lib/engine-components/webxr/WebXRImageTracking.js +4 -0
  68. package/lib/engine-components/webxr/WebXRImageTracking.js.map +1 -1
  69. package/package.json +1 -1
  70. package/src/engine/api.ts +3 -0
  71. package/src/engine/debug/debug_spatial_console.ts +10 -7
  72. package/src/engine/engine_addressables.ts +6 -3
  73. package/src/engine/engine_audio.ts +184 -0
  74. package/src/engine/engine_gameobject.ts +2 -2
  75. package/src/engine/engine_init.ts +8 -0
  76. package/src/engine/engine_mainloop_utils.ts +5 -2
  77. package/src/engine/engine_serialization_builtin_serializer.ts +31 -3
  78. package/src/engine/webcomponents/needle-engine.ts +9 -3
  79. package/src/engine/xr/NeedleXRSession.ts +48 -13
  80. package/src/engine-components/Animation.ts +19 -16
  81. package/src/engine-components/AnimatorController.ts +4 -1
  82. package/src/engine-components/AudioSource.ts +130 -79
  83. package/src/engine-components/DragControls.ts +18 -2
  84. package/src/engine-components/NestedGltf.ts +20 -4
  85. package/src/engine-components/Networking.ts +1 -1
  86. package/src/engine-components/OrbitControls.ts +18 -9
  87. package/src/engine-components/postprocessing/VolumeParameter.ts +4 -1
  88. package/src/engine-components/ui/Canvas.ts +2 -8
  89. package/src/engine-components/ui/Text.ts +43 -18
  90. package/src/engine-components/web/CursorFollow.ts +21 -13
  91. package/src/engine-components/webxr/WebXRImageTracking.ts +2 -0
@@ -805,15 +805,16 @@ export class NeedleXRSession implements INeedleXRSession {
805
805
  get viewerPose(): XRViewerPose | undefined { return this._viewerPose; }
806
806
 
807
807
 
808
- /** @returns `true` if any image is currently being tracked */
809
- /** returns true if images are currently being tracked */
808
+ /** @returns `true` if any image is currently being tracked or emulated */
810
809
  get isTrackingImages() {
811
810
  if (this.frame && "getImageTrackingResults" in this.frame && typeof this.frame.getImageTrackingResults === "function") {
812
811
  try {
813
812
  const trackingResult = this.frame.getImageTrackingResults();
814
813
  for (const result of trackingResult) {
815
814
  const state = result.trackingState;
816
- if (state === "tracked") return true;
815
+ if (state === "tracked" || state === "emulated") {
816
+ return true;
817
+ }
817
818
  }
818
819
  }
819
820
  catch {
@@ -1018,6 +1019,8 @@ export class NeedleXRSession implements INeedleXRSession {
1018
1019
  private readonly _xr_update_scripts: INeedleXRSessionEventReceiver[] = [];
1019
1020
  /** scripts that are in the scene but inactive (e.g. disabled parent gameObject) */
1020
1021
  private readonly _inactive_scripts: INeedleXRSessionEventReceiver[] = [];
1022
+ /** tracks scripts that have received onEnterXR — prevents spurious onLeaveXR calls */
1023
+ private readonly _scripts_in_xr = new Set<INeedleXRSessionEventReceiver>();
1021
1024
  private readonly _controllerAdded: ControllerChangedEvt[];
1022
1025
  private readonly _controllerRemoved: ControllerChangedEvt[];
1023
1026
  private readonly _originalCameraWorldPosition?: Vector3 | null;
@@ -1128,7 +1131,18 @@ export class NeedleXRSession implements INeedleXRSession {
1128
1131
  // we set the session on the webxr manager at the end because we want to receive inputsource events first
1129
1132
  // e.g. in case there's a bug in the threejs codebase
1130
1133
  this.context.xr = this;
1131
- this.context.renderer.xr.setSession(this.session).then(this.onRendererSessionSet);
1134
+ this.context.renderer.xr.setSession(this.session).then(this.onRendererSessionSet)
1135
+ .catch(err => {
1136
+ // Workaround for WebXR emulators on localhost: the polyfilled XRSession fails the
1137
+ // native XRWebGLBinding constructor's instanceof check. Retry with layers disabled.
1138
+ // See https://github.com/meta-quest/immersive-web-emulator/issues/80
1139
+ if (isDevEnvironment() && err instanceof TypeError && typeof XRWebGLBinding !== "undefined") {
1140
+ console.error(`XRWebGLBinding failed (${err.message}).\nSee https://github.com/meta-quest/immersive-web-emulator/issues/80`);
1141
+ }
1142
+ else {
1143
+ console.error("renderer.xr.setSession failed:", err);
1144
+ }
1145
+ });
1132
1146
  // disable three.js renderer controller autoUpdate (added in ac67b31e3548386f8a93e23a4176554c92bbd0d9)
1133
1147
  if ("controllerAutoUpdate" in this.context.renderer.xr) {
1134
1148
  console.debug("Disabling three.js controllerAutoUpdate");
@@ -1296,8 +1310,10 @@ export class NeedleXRSession implements INeedleXRSession {
1296
1310
  // even if they might already be destroyed e.g. by the WebXR component (it destroys the default controller scripts)
1297
1311
  // they should still receive this callback to be properly cleaned up
1298
1312
  for (const listener of this._xr_scripts) {
1313
+ this._scripts_in_xr.delete(listener);
1299
1314
  listener?.onLeaveXR?.({ xr: this });
1300
1315
  }
1316
+ this._scripts_in_xr.clear();
1301
1317
 
1302
1318
  this.sync?.onExitXR(this);
1303
1319
 
@@ -1364,10 +1380,17 @@ export class NeedleXRSession implements INeedleXRSession {
1364
1380
  }
1365
1381
 
1366
1382
  // make sure the camera is parented to the active rig
1367
- if (this.rig && this._mainCamera?.gameObject) {
1368
- const currentParent = this._mainCamera?.gameObject?.parent;
1369
- if (currentParent !== this.rig.gameObject) {
1370
- this.rig.gameObject.add(this._mainCamera?.gameObject);
1383
+ // Note: applyCustomForward() inserts _cameraRenderParent between rig and camera,
1384
+ // so the hierarchy is: rig → _cameraRenderParent → camera. We check the effective parent.
1385
+ if (this.rig) {
1386
+ const cameraObject = this._mainCamera?.gameObject ?? this.context.mainCamera;
1387
+ if (cameraObject) {
1388
+ const effectiveParent = cameraObject.parent === this._cameraRenderParent
1389
+ ? this._cameraRenderParent.parent
1390
+ : cameraObject.parent;
1391
+ if (effectiveParent !== this.rig.gameObject) {
1392
+ this.rig.gameObject.add(cameraObject);
1393
+ }
1371
1394
  }
1372
1395
  }
1373
1396
 
@@ -1499,8 +1522,12 @@ export class NeedleXRSession implements INeedleXRSession {
1499
1522
  }
1500
1523
 
1501
1524
  //performance.mark('NeedleXRSession update scripts start');
1502
- // invoke update on all scripts
1503
- for (const script of this._xr_update_scripts) {
1525
+ // check all XR scripts for destroyed or inactive state
1526
+ // this must cover _xr_scripts (not just _xr_update_scripts) so that scripts
1527
+ // without onUpdateXR are also detected as inactive when removed from the scene
1528
+ // iterate backwards since markInactive/removeScript modifies the array
1529
+ for (let i = this._xr_scripts.length - 1; i >= 0; i--) {
1530
+ const script = this._xr_scripts[i];
1504
1531
  if (script.destroyed === true) {
1505
1532
  this._script_to_remove.push(script);
1506
1533
  continue;
@@ -1509,6 +1536,10 @@ export class NeedleXRSession implements INeedleXRSession {
1509
1536
  this.markInactive(script);
1510
1537
  continue;
1511
1538
  }
1539
+ }
1540
+ // invoke update on scripts that have onUpdateXR
1541
+ for (const script of this._xr_update_scripts) {
1542
+ if (script.destroyed || script.activeAndEnabled === false) continue;
1512
1543
  if (script.onUpdateXR) script.onUpdateXR(args);
1513
1544
  }
1514
1545
  //performance.mark('NeedleXRSession update scripts end');
@@ -1631,9 +1662,11 @@ export class NeedleXRSession implements INeedleXRSession {
1631
1662
  const script = this._inactive_scripts[i];
1632
1663
  if (script.activeAndEnabled) {
1633
1664
  this._inactive_scripts.splice(i, 1);
1634
- this.addScript(script);
1635
- this.invokeCallback_EnterXR(script);
1636
- for (const ctrl of this.controllers) this.invokeCallback_ControllerAdded(script, ctrl);
1665
+ // addScript returns false if already re-added (e.g. via new_scripts_xr processing)
1666
+ if (this.addScript(script)) {
1667
+ this.invokeCallback_EnterXR(script);
1668
+ for (const ctrl of this.controllers) this.invokeCallback_ControllerAdded(script, ctrl);
1669
+ }
1637
1670
  }
1638
1671
  }
1639
1672
  }
@@ -1654,6 +1687,7 @@ export class NeedleXRSession implements INeedleXRSession {
1654
1687
  }
1655
1688
 
1656
1689
  private invokeCallback_EnterXR(script: INeedleXRSessionEventReceiver) {
1690
+ this._scripts_in_xr.add(script);
1657
1691
  if (script.onEnterXR) {
1658
1692
  script.onEnterXR({ xr: this });
1659
1693
  }
@@ -1669,6 +1703,7 @@ export class NeedleXRSession implements INeedleXRSession {
1669
1703
  }
1670
1704
  }
1671
1705
  private invokeCallback_LeaveXR(script: INeedleXRSessionEventReceiver) {
1706
+ if (!this._scripts_in_xr.delete(script)) return;
1672
1707
  if (script.onLeaveXR && !script.destroyed) {
1673
1708
  script.onLeaveXR({ xr: this });
1674
1709
  }
@@ -447,25 +447,13 @@ export class Animation extends Behaviour implements IAnimationComponent {
447
447
 
448
448
  private internalOnPlay(action: AnimationAction, options: PlayOptions): Promise<AnimationAction> {
449
449
  var existing = this.actions.find(a => a === action);
450
- if (existing === action && existing.isRunning() && existing.time < existing.getClip().duration) {
451
- const handle = this.tryFindHandle(action);
452
- if (existing.paused) {
453
- existing.paused = false;
454
- }
455
- if (handle) return handle.waitForFinish();
456
- }
457
450
 
458
- // Assign defaults
459
- if (options.loop === undefined) options.loop = this.loop;
460
- if (options.clampWhenFinished === undefined) options.clampWhenFinished = this.clampWhenFinished;
461
- if (options.minMaxOffsetNormalized === undefined && this.randomStartTime) options.minMaxOffsetNormalized = this.minMaxOffsetNormalized;
462
- if (options.minMaxSpeed === undefined) options.minMaxSpeed = this.minMaxSpeed;
463
-
464
- // Reset currently running animations
451
+ // Stop other animations before the early-return check so exclusive always applies,
452
+ // even when the target animation is already running.
465
453
  const stopOther = options?.exclusive ?? true;
466
454
  if (stopOther) {
467
455
  for (const act of this.actions) {
468
- if (act != existing) {
456
+ if (act !== action) {
469
457
  if (options.fadeDuration) {
470
458
  act.fadeOut(options.fadeDuration);
471
459
  }
@@ -475,6 +463,20 @@ export class Animation extends Behaviour implements IAnimationComponent {
475
463
  }
476
464
  }
477
465
  }
466
+
467
+ if (existing === action && existing.isRunning() && existing.time < existing.getClip().duration) {
468
+ const handle = this.tryFindHandle(action);
469
+ if (existing.paused) {
470
+ existing.paused = false;
471
+ }
472
+ if (handle) return handle.waitForFinish();
473
+ }
474
+
475
+ // Assign defaults
476
+ if (options.loop === undefined) options.loop = this.loop;
477
+ if (options.clampWhenFinished === undefined) options.clampWhenFinished = this.clampWhenFinished;
478
+ if (options.minMaxOffsetNormalized === undefined && this.randomStartTime) options.minMaxOffsetNormalized = this.minMaxOffsetNormalized;
479
+ if (options.minMaxSpeed === undefined) options.minMaxSpeed = this.minMaxSpeed;
478
480
  if (options?.fadeDuration) {
479
481
  action.fadeIn(options.fadeDuration);
480
482
  }
@@ -515,10 +517,11 @@ export class Animation extends Behaviour implements IAnimationComponent {
515
517
 
516
518
  action.paused = false;
517
519
  action.play();
520
+ if (debug) console.log("[Animation] Now Playing", action.getClip().name, action)
521
+
518
522
 
519
523
  window.requestAnimationFrame(() => AnimationUtils.testIfRootCanAnimate(action));
520
524
 
521
- if (debug) console.log("PLAY", action.getClip().name, action)
522
525
  const handle = new AnimationHandle(action, this.mixer!, options, _ => {
523
526
  this._handles.splice(this._handles.indexOf(handle), 1);
524
527
  });
@@ -1316,4 +1316,7 @@ class AnimatorControllerSerializator extends TypeSerializer {
1316
1316
  return undefined;
1317
1317
  }
1318
1318
  }
1319
- new AnimatorControllerSerializator(AnimatorController);
1319
+ /** @internal */
1320
+ export function initAnimatorControllerSerializer() {
1321
+ new AnimatorControllerSerializator(AnimatorController);
1322
+ }
@@ -4,7 +4,6 @@ import { PositionalAudioHelper } from 'three/examples/jsm/helpers/PositionalAudi
4
4
  import { isDevEnvironment } from "../engine/debug/index.js";
5
5
  import { Application, ApplicationEvents } from "../engine/engine_application.js";
6
6
  import { findObjectOfType } from "../engine/engine_components.js";
7
- import { Mathf } from "../engine/engine_math.js";
8
7
  import { serializable } from "../engine/engine_serialization_decorator.js";
9
8
  import { DeviceUtilities, getParam } from "../engine/engine_utils.js";
10
9
  import { AudioListener } from "./AudioListener.js";
@@ -140,12 +139,23 @@ export class AudioSource extends Behaviour {
140
139
  get isPlaying(): boolean { return this.sound?.isPlaying ?? false; }
141
140
 
142
141
  /**
143
- * The total duration of the current audio clip in seconds.
142
+ * The total duration of the currently loaded audio clip in seconds.
144
143
  *
145
144
  * @returns Duration in seconds or undefined if no clip is loaded
145
+ * @remarks For MediaStream clips, duration is not directly available and will return undefined. If the audio clip has not started loading or is still loading, duration may also be undefined until the audio buffer is ready.
146
146
  */
147
147
  get duration() {
148
- return this.sound?.buffer?.duration;
148
+ if (this.sound?.buffer?.duration) {
149
+ return this.sound.buffer.duration;
150
+ }
151
+ else if (this._audioElement?.duration) {
152
+ return this._audioElement.duration;
153
+ }
154
+ else if (this._loadedClip instanceof MediaStream) {
155
+ // MediaStream duration is not directly available; return undefined or estimate if possible
156
+ return undefined;
157
+ }
158
+ return undefined;
149
159
  }
150
160
 
151
161
  /**
@@ -200,7 +210,8 @@ export class AudioSource extends Behaviour {
200
210
  /**
201
211
  * Controls how the audio is positioned in space.
202
212
  * Values range from 0 (2D, non-positional) to 1 (fully 3D positioned).
203
- * Note: 2D playback is not fully supported in the current implementation.
213
+ * Internally uses a dual-path audio graph to crossfade between a spatialized (PannerNode)
214
+ * and a non-spatialized (direct) signal path.
204
215
  */
205
216
  @serializable()
206
217
  get spatialBlend(): number {
@@ -292,12 +303,18 @@ export class AudioSource extends Behaviour {
292
303
  // set this from audio context time, used to set clip offset when setting "time" property
293
304
  // there is maybe a better way to set a audio clip current time?!
294
305
  private _lastClipStartedLoading: string | MediaStream | null = null;
306
+ private _loadedClip: string | MediaStream | null = null;
295
307
  private _audioElement: HTMLAudioElement | null = null;
296
308
 
309
+ // Spatial blend dual-path audio nodes
310
+ private _entryNode: GainNode | null = null;
311
+ private _spatialGain: GainNode | null = null;
312
+ private _bypassGain: GainNode | null = null;
313
+
297
314
  /**
298
315
  * Returns the underlying {@link PositionalAudio} object, creating it if necessary.
299
316
  * The audio source needs a user interaction to be initialized due to browser autoplay policies.
300
- *
317
+ *
301
318
  * @returns The three.js PositionalAudio object or null if unavailable
302
319
  */
303
320
  public get Sound(): PositionalAudio | null {
@@ -312,45 +329,12 @@ export class AudioSource extends Behaviour {
312
329
  if (listener?.listener) {
313
330
  this.sound = new PositionalAudio(listener.listener);
314
331
  this.gameObject?.add(this.sound);
315
-
316
- // this._listener = listener;
317
- // this._originalSoundMatrixWorldFunction = this.sound.updateMatrixWorld;
318
- // this.sound.updateMatrixWorld = this._onSoundMatrixWorld;
319
332
  }
320
333
  else if (debug) console.warn("No audio listener found in scene - can not play audio");
321
334
  }
322
335
  return this.sound;
323
336
  }
324
337
 
325
- // This is a hacky workaround to get the PositionalAudio behave like a 2D audio source
326
- // private _listener: AudioListener | null = null;
327
- // private _originalSoundMatrixWorldFunction: Function | null = null;
328
- // private _onSoundMatrixWorld = (force: boolean) => {
329
- // if (this._spatialBlend > .05) {
330
- // if (this._originalSoundMatrixWorldFunction) {
331
- // this._originalSoundMatrixWorldFunction.call(this.sound, force);
332
- // }
333
- // }
334
- // else {
335
- // // we use another object's matrix world function (but bound to the positional audio)
336
- // // this is just a little trick to prevent calling the PositionalAudio's updateMatrixWorld function
337
- // this.gameObject.updateMatrixWorld?.call(this.sound, force);
338
- // if (this.sound && this._listener) {
339
- // this.sound.gain.connect(this._listener.listener.getInput());
340
- // // const pos = getTempVector().setFromMatrixPosition(this._listener.gameObject.matrixWorld);
341
- // // const ctx = this.sound.context;
342
- // // const delay = this._listener.listener.timeDelta;
343
- // // const time = ctx.currentTime ;
344
- // // this.sound.panner.positionX.setValueAtTime(pos.x, time);
345
- // // this.sound.panner.positionY.setValueAtTime(pos.y, time);
346
- // // this.sound.panner.positionZ.setValueAtTime(pos.z, time);
347
- // // this.sound.panner.orientationX.setValueAtTime(0, time);
348
- // // this.sound.panner.orientationY.setValueAtTime(0, time);
349
- // // this.sound.panner.orientationZ.setValueAtTime(-1, time);
350
- // }
351
- // }
352
- // }
353
-
354
338
  /**
355
339
  * Indicates whether the audio source is queued to play when possible.
356
340
  * This may be true before user interaction has been registered.
@@ -376,8 +360,10 @@ export class AudioSource extends Behaviour {
376
360
  if (this.playOnAwake) this.shouldPlay = true;
377
361
 
378
362
  if (this.preload) {
379
- if (typeof this.clip === "string") {
380
- this.audioLoader.load(this.clip, this.createAudio, () => { }, console.error);
363
+ if (typeof this.clip === "string" && this.clip.length > 0) {
364
+ this.audioLoader.load(this.clip, this.createAudio, () => { }, e => {
365
+ console.error(`[AudioSource] Error preloading audio clip "${this.clip}":`, e);
366
+ });
381
367
  }
382
368
  }
383
369
  }
@@ -433,9 +419,9 @@ export class AudioSource extends Behaviour {
433
419
  this.sound?.setVolume(this.volume);
434
420
  }
435
421
 
436
- private createAudio = (buffer?: AudioBuffer) => {
437
- if(this.destroyed) {
438
- if(debug) console.warn("AudioSource destroyed, not creating audio", this.name);
422
+ private createAudio = (buffer?: AudioBuffer) => {
423
+ if (this.destroyed) {
424
+ if (debug) console.warn("AudioSource destroyed, not creating audio", this.name);
439
425
  return;
440
426
  }
441
427
 
@@ -451,26 +437,17 @@ export class AudioSource extends Behaviour {
451
437
  sound.stop();
452
438
 
453
439
  if (buffer) sound.setBuffer(buffer);
440
+ this._loadedClip = this.clip;
454
441
  sound.loop = this._loop;
455
442
  if (this.context.application.muted) sound.setVolume(0);
456
443
  else sound.setVolume(this.volume);
457
444
  sound.autoplay = this.shouldPlay && AudioSource.userInteractionRegistered;
445
+ this.setupSpatialBlendNodes();
458
446
  this.applySpatialDistanceSettings();
459
447
 
460
448
  if (sound.isPlaying)
461
449
  sound.stop();
462
450
 
463
- // const panner = sound.panner;
464
- // panner.coneOuterGain = 1;
465
- // sound.setDirectionalCone(360, 360, 1);
466
- // const src = sound.context.createBufferSource();
467
- // src.buffer = sound.buffer;
468
- // src.connect(sound.panner);
469
- // src.start(this.audioContext?.currentTime);
470
- // const gain = sound.context.createGain();
471
- // gain.gain.value = 1 - this.spatialBlend;
472
- // src.connect(gain);
473
-
474
451
  // make sure we only play the sound if the user has interacted with the page
475
452
  AudioSource.registerWaitForAllowAudio(this.__onAllowAudioCallback);
476
453
  }
@@ -479,30 +456,97 @@ export class AudioSource extends Behaviour {
479
456
  this.play();
480
457
  }
481
458
 
459
+ /**
460
+ * Sets up the dual-path audio graph for spatial blend crossfading.
461
+ * Creates two parallel signal paths from source to output:
462
+ * - 3D path: entry → panner → spatialGain → gain (spatialized)
463
+ * - 2D path: entry → bypassGain → gain (non-spatialized)
464
+ */
465
+ private setupSpatialBlendNodes() {
466
+ const sound = this.sound;
467
+ if (!sound || this._entryNode) return;
468
+
469
+ const ctx = sound.context;
470
+ this._entryNode = ctx.createGain();
471
+ this._spatialGain = ctx.createGain();
472
+ this._bypassGain = ctx.createGain();
473
+
474
+ // Disconnect the default panner → gain connection set up by three.js
475
+ try { sound.panner.disconnect(sound.gain); } catch { /* not connected */ }
476
+
477
+ // 3D path: entry → panner → spatialGain → gain
478
+ this._entryNode.connect(sound.panner);
479
+ sound.panner.connect(this._spatialGain);
480
+ this._spatialGain.connect(sound.gain);
481
+
482
+ // 2D path: entry → bypassGain → gain
483
+ this._entryNode.connect(this._bypassGain);
484
+ this._bypassGain.connect(sound.gain);
485
+
486
+ // Override getOutput so three.js connects source → entryNode instead of panner
487
+ sound.getOutput = () => this._entryNode! as unknown as PannerNode;
488
+
489
+ // Wrap connect() to undo PositionalAudio's redundant panner → gain reconnection
490
+ const origConnect = sound.connect;
491
+ sound.connect = function () {
492
+ origConnect.call(sound);
493
+ try { sound.panner.disconnect(sound.gain); } catch { /* expected */ }
494
+ return sound;
495
+ };
496
+
497
+ // Wrap disconnect() to prevent error when panner → gain doesn't exist
498
+ const origDisconnect = sound.disconnect;
499
+ sound.disconnect = function () {
500
+ try { origDisconnect.call(sound); } catch { /* panner → gain already removed */ }
501
+ return sound;
502
+ };
503
+
504
+ this.updateSpatialBlendGains();
505
+ }
506
+
507
+ /** Updates the spatial/bypass gain values based on the current spatialBlend. */
508
+ private updateSpatialBlendGains() {
509
+ if (!this._spatialGain || !this._bypassGain || !this.sound) return;
510
+ const t = this.sound.context.currentTime;
511
+ // Use setTargetAtTime for smooth transitions (avoids clicks/pops)
512
+ this._spatialGain.gain.setTargetAtTime(this._spatialBlend, t, 0.01);
513
+ this._bypassGain.gain.setTargetAtTime(1 - this._spatialBlend, t, 0.01);
514
+ }
515
+
482
516
  private applySpatialDistanceSettings() {
483
517
  const sound = this.sound;
484
518
  if (!sound) return;
485
519
  this._needUpdateSpatialDistanceSettings = false;
486
- const dist = Mathf.lerp(10 * this._maxDistance / Math.max(0.0001, this.spatialBlend), this._minDistance, this.spatialBlend);
487
- if (debug) console.log(this.name, this._minDistance, this._maxDistance, this.spatialBlend, "Ref distance=" + dist);
488
- sound.setRefDistance(dist);
489
- sound.setMaxDistance(Math.max(0.01, this._maxDistance));
490
- // sound.setRolloffFactor(this.spatialBlend);
491
- // sound.panner.positionZ.automationRate
520
+
521
+ const ref = Math.max(0.0001, this._minDistance);
522
+ const max = Math.max(0.01, this._maxDistance);
523
+ if (debug) console.log(this.name, ref, max, this.spatialBlend);
524
+ sound.setRefDistance(ref);
525
+ sound.setMaxDistance(max);
492
526
 
493
527
  // https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/distanceModel
494
528
  switch (this.rollOffMode) {
495
529
  case AudioRolloffMode.Logarithmic:
496
- sound.setDistanceModel('exponential');
530
+ // Unity's logarithmic rolloff matches Web Audio's 'inverse' model:
531
+ // gain = refDistance / (refDistance + rolloffFactor * (distance - refDistance))
532
+ // We compute rolloffFactor so gain reaches ~0.01 (-40dB) at maxDistance,
533
+ // matching Unity's behavior of effective silence at maxDistance.
534
+ sound.setDistanceModel('inverse');
535
+ sound.setRolloffFactor(99 * ref / Math.max(0.001, max - ref));
497
536
  break;
498
537
  case AudioRolloffMode.Linear:
538
+ // gain = 1 - rolloffFactor * (distance - refDistance) / (maxDistance - refDistance)
539
+ // With rolloffFactor=1 this reaches exactly 0 at maxDistance.
499
540
  sound.setDistanceModel('linear');
541
+ sound.setRolloffFactor(1);
500
542
  break;
501
543
  case AudioRolloffMode.Custom:
502
544
  console.warn("Custom rolloff for AudioSource is not supported: " + this.name);
503
545
  break;
504
546
  }
505
547
 
548
+ this.updateSpatialBlendGains();
549
+
506
550
  if (this.spatialBlend > 0) {
507
551
  if (debug && !this.helper) {
508
552
  this.helper = new PositionalAudioHelper(sound, sound.getRefDistance());
@@ -519,7 +563,7 @@ export class AudioSource extends Behaviour {
519
563
  if (typeof clip === "string") {
520
564
  if (debug)
521
565
  console.log(clip);
522
- if (clip.endsWith(".mp3") || clip.endsWith(".wav")) {
566
+ if (clip.endsWith(".mp3") || clip.endsWith(".wav") || clip.endsWith(".ogg") || clip.endsWith(".flac") || clip.endsWith(".aac") || clip.endsWith(".webm")) {
523
567
  if (!this.audioLoader)
524
568
  this.audioLoader = new AudioLoader();
525
569
  this.shouldPlay = true;
@@ -530,9 +574,12 @@ export class AudioSource extends Behaviour {
530
574
  this._lastClipStartedLoading = clip;
531
575
  if (debug)
532
576
  console.log("load audio", clip);
533
- const buffer = await this.audioLoader.loadAsync(clip).catch(console.error);
534
- if(this.destroyed) return;
535
- if(this._lastClipStartedLoading === clip) this._lastClipStartedLoading = null;
577
+ const buffer = await this.audioLoader.loadAsync(clip).catch(e => {
578
+ console.error(`[AudioSource] Error loading audio clip "${clip}":`, e);
579
+ return null;
580
+ });
581
+ if (this.destroyed) return;
582
+ if (this._lastClipStartedLoading === clip) this._lastClipStartedLoading = null;
536
583
  if (buffer) this.createAudio(buffer);
537
584
  }
538
585
  else console.warn("Unsupported audio clip type", clip)
@@ -548,8 +595,9 @@ export class AudioSource extends Behaviour {
548
595
  * If no argument is provided, plays the currently assigned clip.
549
596
  *
550
597
  * @param clip - Optional audio clip or {@link MediaStream} to play
598
+ * @returns A promise that resolves when playback starts successfully, or rejects on error
551
599
  */
552
- play(clip: string | MediaStream | undefined = undefined) {
600
+ async play(clip: string | MediaStream | undefined = undefined): Promise<boolean> {
553
601
  // use audio source's clip when no clip is passed in
554
602
  if (!clip && this.clip)
555
603
  clip = this.clip;
@@ -557,27 +605,27 @@ export class AudioSource extends Behaviour {
557
605
  // We only support strings and media stream
558
606
  // TODO: maybe we should return here if an invalid value is passed in
559
607
  if (clip !== undefined && typeof clip !== "string" && !(clip instanceof MediaStream)) {
560
- if (isDevEnvironment())
561
- console.warn("Called play on AudioSource with unknown argument type:", clip + "\nUsing the assigned clip instead:", this.clip)
562
608
  // e.g. when a AudioSource.Play is called from SpatialTrigger onEnter this event is called with the TriggerReceiver... to still make this work we *re-use* our already assigned clip. Because otherwise calling `play` would not play the clip...
563
609
  clip = this.clip;
610
+ if (isDevEnvironment()) console.warn("[AudioSource] Called play on AudioSource with unknown argument type:", clip + "\nUsing the assigned clip instead:", this.clip)
564
611
  }
565
612
 
566
- // Check if we need to call load first
567
- let needsLoading = !this.sound || (clip && clip !== this.clip);
613
+ // Load if the sound hasn't been created yet or if the clip changed since last load
614
+ let needsLoading = !this.sound || (clip && clip !== this._loadedClip);
568
615
  if (typeof clip === "string" && !this.audioLoader) needsLoading = true;
569
616
  if (clip instanceof MediaStream || typeof clip === "string")
570
617
  this.clip = clip;
571
618
  if (needsLoading) {
572
619
  this.shouldPlay = true;
573
- this.onNewClip(clip);
574
- return;
620
+ return this.onNewClip(clip).then(() => true).catch(() => false);
575
621
  }
576
622
 
577
623
  this.shouldPlay = true;
578
624
  this._hasEnded = false;
579
- if (debug)
580
- console.log("play", this.sound?.getVolume(), this.sound);
625
+
626
+ if (debug) console.log("[AudioSource] play", this.sound?.getVolume(), this.sound);
627
+ // If a different clip was passed in, needsLoading above would be true and onNewClip handles it.
628
+ // This guard prevents double-playing the same already-playing sound (which would throw in three.js).
581
629
  if (this.sound && !this.sound.isPlaying) {
582
630
  const muted = this.context.application.muted;
583
631
  if (muted) this.sound.setVolume(0);
@@ -597,13 +645,17 @@ export class AudioSource extends Behaviour {
597
645
  this.context.domElement.shadowRoot?.append(this._audioElement);
598
646
  this._audioElement.srcObject = this.clip;
599
647
  this._audioElement.autoplay = false;
648
+ return true;
600
649
 
601
650
  }
602
651
  else {
603
652
  if (this._audioElement) this._audioElement.remove();
604
653
  this.sound.play(muted ? .1 : 0);
654
+ return true;
605
655
  }
606
656
  }
657
+
658
+ return false;
607
659
  }
608
660
 
609
661
  /**
@@ -611,7 +663,7 @@ export class AudioSource extends Behaviour {
611
663
  * Use play() to resume from the paused position.
612
664
  */
613
665
  pause() {
614
- if (debug) console.log("Pause", this);
666
+ if (debug) console.log("[AudioSource] pause", this);
615
667
  this._hasEnded = true;
616
668
  this.shouldPlay = false;
617
669
  if (this.sound && this.sound.isPlaying && this.sound.source) {
@@ -626,13 +678,13 @@ export class AudioSource extends Behaviour {
626
678
  * Unlike pause(), calling play() after stop() will start from the beginning.
627
679
  */
628
680
  stop() {
629
- if (debug) console.log("Pause", this);
681
+ if (debug) console.log("[AudioSource] stop", this);
630
682
  this._hasEnded = true;
631
683
  this.shouldPlay = false;
632
684
  if (this.sound && this.sound.source) {
633
685
  this._lastContextTime = this.sound?.context.currentTime;
634
686
  if (debug)
635
- console.log(this._lastContextTime)
687
+ console.log("[AudioSource] lastContextTime", this._lastContextTime);
636
688
  this.sound.stop();
637
689
  }
638
690
  this._audioElement?.remove();
@@ -657,8 +709,7 @@ export class AudioSource extends Behaviour {
657
709
 
658
710
  if (this.sound && !this.sound.isPlaying && this.shouldPlay && !this._hasEnded) {
659
711
  this._hasEnded = true;
660
- if (debug)
661
- console.log("Audio clip ended", this.clip);
712
+ if (debug) console.log("[AudioSource] Audio clip ended", this.clip);
662
713
  this.dispatchEvent(new CustomEvent("ended", { detail: this }));
663
714
  }
664
715
 
@@ -12,6 +12,7 @@ import { getParam } from "../engine/engine_utils.js";
12
12
  import { NeedleXRSession } from "../engine/engine_xr.js";
13
13
  import { Avatar_POI } from "./avatar/Avatar_Brain_LookAt.js";
14
14
  import { Behaviour, GameObject } from "./Component.js";
15
+ import { EventList } from "./EventList.js";
15
16
  import { UsageMarker } from "./Interactable.js";
16
17
  import { Rigidbody } from "./RigidBody.js";
17
18
  import { SyncedTransform } from "./SyncedTransform.js";
@@ -155,7 +156,19 @@ export class DragControls extends Behaviour implements IPointerEventHandler {
155
156
  @serializable()
156
157
  public showGizmo: boolean = false;
157
158
 
158
- /**
159
+ /** Invoked once when a drag begins (after the minimum drag distance threshold is met). */
160
+ @serializable(EventList)
161
+ dragStarted: EventList = new EventList();
162
+
163
+ /** Invoked every frame while the object is being dragged. */
164
+ @serializable(EventList)
165
+ dragUpdated: EventList = new EventList();
166
+
167
+ /** Invoked once when the last pointer is released and the drag ends. */
168
+ @serializable(EventList)
169
+ dragEnded: EventList = new EventList();
170
+
171
+ /**
159
172
  * Returns the object currently being dragged by this DragControls component, if any.
160
173
  * @returns The object being dragged or null if no object is currently dragged
161
174
  */
@@ -457,6 +470,7 @@ export class DragControls extends Behaviour implements IPointerEventHandler {
457
470
  if (!object) return;
458
471
 
459
472
  this._isDragging = true;
473
+ this.dragStarted?.invoke();
460
474
 
461
475
  const sync = GameObject.getComponentInChildren(object, SyncedTransform);
462
476
  if (debug) console.log("DRAG START", sync, object);
@@ -497,9 +511,10 @@ export class DragControls extends Behaviour implements IPointerEventHandler {
497
511
  const object = this._targetObject || this.gameObject;
498
512
 
499
513
  InstancingUtil.markDirty(object);
514
+ this.dragUpdated?.invoke();
500
515
  }
501
516
 
502
- /**
517
+ /**
503
518
  * Called when the last pointer has been removed from this object.
504
519
  * Cleans up drag state and applies final velocities to rigidbodies.
505
520
  * @param evt Pointer event data for the last pointer that was lifted
@@ -507,6 +522,7 @@ export class DragControls extends Behaviour implements IPointerEventHandler {
507
522
  private onLastDragEnd(evt: PointerEventData | null) {
508
523
  if (!this || !this._isDragging) return;
509
524
  this._isDragging = false;
525
+ this.dragEnded?.invoke();
510
526
  for (const rb of this._draggingRigidbodies) {
511
527
  rb.setVelocity(rb.smoothedVelocity.multiplyScalar(this.context.time.deltaTime));
512
528
  }