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

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 (133) hide show
  1. package/CHANGELOG.md +35 -1
  2. package/components.needle.json +1 -1
  3. package/dist/{needle-engine.bundle-qZfVf_v-.umd.cjs → needle-engine.bundle-C-ixARur.umd.cjs} +128 -127
  4. package/dist/{needle-engine.bundle-B-5Q2CpC.min.js → needle-engine.bundle-CHmXdnE1.min.js} +138 -137
  5. package/dist/{needle-engine.bundle-dit3f1l5.js → needle-engine.bundle-DF01sSGQ.js} +7815 -7602
  6. package/dist/needle-engine.d.ts +125 -27
  7. package/dist/needle-engine.js +524 -521
  8. package/dist/needle-engine.min.js +1 -1
  9. package/dist/needle-engine.umd.cjs +1 -1
  10. package/lib/engine/api.d.ts +2 -0
  11. package/lib/engine/api.js +2 -0
  12. package/lib/engine/api.js.map +1 -1
  13. package/lib/engine/debug/debug_spatial_console.d.ts +2 -0
  14. package/lib/engine/debug/debug_spatial_console.js +10 -7
  15. package/lib/engine/debug/debug_spatial_console.js.map +1 -1
  16. package/lib/engine/engine_addressables.d.ts +2 -0
  17. package/lib/engine/engine_addressables.js +6 -3
  18. package/lib/engine/engine_addressables.js.map +1 -1
  19. package/lib/engine/engine_audio.d.ts +68 -0
  20. package/lib/engine/engine_audio.js +172 -0
  21. package/lib/engine/engine_audio.js.map +1 -1
  22. package/lib/engine/engine_components.js +1 -1
  23. package/lib/engine/engine_components.js.map +1 -1
  24. package/lib/engine/engine_context.d.ts +1 -1
  25. package/lib/engine/engine_context.js +1 -1
  26. package/lib/engine/engine_context.js.map +1 -1
  27. package/lib/engine/engine_gameobject.js +2 -2
  28. package/lib/engine/engine_gameobject.js.map +1 -1
  29. package/lib/engine/engine_init.js +13 -3
  30. package/lib/engine/engine_init.js.map +1 -1
  31. package/lib/engine/engine_input.d.ts +1 -1
  32. package/lib/engine/engine_input.js +1 -1
  33. package/lib/engine/engine_input.js.map +1 -1
  34. package/lib/engine/engine_license.js +1 -1
  35. package/lib/engine/engine_license.js.map +1 -1
  36. package/lib/engine/engine_mainloop_utils.js +5 -2
  37. package/lib/engine/engine_mainloop_utils.js.map +1 -1
  38. package/lib/engine/engine_networking_blob.js +1 -1
  39. package/lib/engine/engine_networking_blob.js.map +1 -1
  40. package/lib/engine/engine_scenedata.d.ts +2 -0
  41. package/lib/engine/engine_scenedata.js +4 -2
  42. package/lib/engine/engine_scenedata.js.map +1 -1
  43. package/lib/engine/engine_serialization_builtin_serializer.d.ts +10 -16
  44. package/lib/engine/engine_serialization_builtin_serializer.js +55 -41
  45. package/lib/engine/engine_serialization_builtin_serializer.js.map +1 -1
  46. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +1 -1
  47. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js.map +1 -1
  48. package/lib/engine/webcomponents/needle menu/needle-menu.d.ts +1 -1
  49. package/lib/engine/webcomponents/needle menu/needle-menu.js +1 -1
  50. package/lib/engine/webcomponents/needle menu/needle-menu.js.map +1 -1
  51. package/lib/engine/webcomponents/needle-engine.d.ts +10 -4
  52. package/lib/engine/webcomponents/needle-engine.js +1 -1
  53. package/lib/engine/webcomponents/needle-engine.js.map +1 -1
  54. package/lib/engine/xr/NeedleXRSession.d.ts +3 -2
  55. package/lib/engine/xr/NeedleXRSession.js +50 -14
  56. package/lib/engine/xr/NeedleXRSession.js.map +1 -1
  57. package/lib/engine/xr/events.d.ts +1 -1
  58. package/lib/engine/xr/events.js.map +1 -1
  59. package/lib/engine-components/Animation.js +17 -16
  60. package/lib/engine-components/Animation.js.map +1 -1
  61. package/lib/engine-components/AnimatorController.d.ts +2 -0
  62. package/lib/engine-components/AnimatorController.js +4 -1
  63. package/lib/engine-components/AnimatorController.js.map +1 -1
  64. package/lib/engine-components/AudioSource.d.ts +19 -3
  65. package/lib/engine-components/AudioSource.js +121 -68
  66. package/lib/engine-components/AudioSource.js.map +1 -1
  67. package/lib/engine-components/DragControls.d.ts +7 -0
  68. package/lib/engine-components/DragControls.js +19 -0
  69. package/lib/engine-components/DragControls.js.map +1 -1
  70. package/lib/engine-components/Networking.d.ts +1 -1
  71. package/lib/engine-components/Networking.js +1 -1
  72. package/lib/engine-components/OrbitControls.js +16 -11
  73. package/lib/engine-components/OrbitControls.js.map +1 -1
  74. package/lib/engine-components/ReflectionProbe.js +2 -0
  75. package/lib/engine-components/ReflectionProbe.js.map +1 -1
  76. package/lib/engine-components/SeeThrough.js +2 -2
  77. package/lib/engine-components/SeeThrough.js.map +1 -1
  78. package/lib/engine-components/api.d.ts +1 -1
  79. package/lib/engine-components/api.js +1 -1
  80. package/lib/engine-components/api.js.map +1 -1
  81. package/lib/engine-components/postprocessing/Effects/Tonemapping.utils.d.ts +1 -1
  82. package/lib/engine-components/postprocessing/VolumeParameter.d.ts +2 -0
  83. package/lib/engine-components/postprocessing/VolumeParameter.js +4 -1
  84. package/lib/engine-components/postprocessing/VolumeParameter.js.map +1 -1
  85. package/lib/engine-components/ui/Canvas.d.ts +1 -1
  86. package/lib/engine-components/ui/Canvas.js +2 -8
  87. package/lib/engine-components/ui/Canvas.js.map +1 -1
  88. package/lib/engine-components/ui/Text.d.ts +1 -0
  89. package/lib/engine-components/ui/Text.js +10 -7
  90. package/lib/engine-components/ui/Text.js.map +1 -1
  91. package/lib/engine-components/web/CursorFollow.js +21 -12
  92. package/lib/engine-components/web/CursorFollow.js.map +1 -1
  93. package/lib/engine-components/webxr/WebXRImageTracking.js +4 -0
  94. package/lib/engine-components/webxr/WebXRImageTracking.js.map +1 -1
  95. package/package.json +1 -1
  96. package/plugins/vite/asap.js +17 -8
  97. package/plugins/vite/dependencies.js +29 -0
  98. package/plugins/vite/local-files-core.js +3 -3
  99. package/plugins/vite/local-files-utils.d.ts +3 -1
  100. package/plugins/vite/local-files-utils.js +29 -5
  101. package/src/engine/api.ts +3 -0
  102. package/src/engine/debug/debug_spatial_console.ts +10 -7
  103. package/src/engine/engine_addressables.ts +6 -3
  104. package/src/engine/engine_audio.ts +184 -0
  105. package/src/engine/engine_components.ts +1 -1
  106. package/src/engine/engine_context.ts +2 -2
  107. package/src/engine/engine_gameobject.ts +2 -2
  108. package/src/engine/engine_init.ts +13 -3
  109. package/src/engine/engine_input.ts +1 -1
  110. package/src/engine/engine_license.ts +1 -1
  111. package/src/engine/engine_mainloop_utils.ts +5 -2
  112. package/src/engine/engine_networking_blob.ts +1 -1
  113. package/src/engine/engine_scenedata.ts +5 -3
  114. package/src/engine/engine_serialization_builtin_serializer.ts +63 -46
  115. package/src/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +1 -1
  116. package/src/engine/webcomponents/needle menu/needle-menu.ts +1 -1
  117. package/src/engine/webcomponents/needle-engine.ts +10 -4
  118. package/src/engine/xr/NeedleXRSession.ts +48 -13
  119. package/src/engine/xr/events.ts +1 -1
  120. package/src/engine-components/Animation.ts +19 -16
  121. package/src/engine-components/AnimatorController.ts +4 -1
  122. package/src/engine-components/AudioSource.ts +130 -79
  123. package/src/engine-components/DragControls.ts +18 -2
  124. package/src/engine-components/Networking.ts +1 -1
  125. package/src/engine-components/OrbitControls.ts +18 -9
  126. package/src/engine-components/ReflectionProbe.ts +2 -0
  127. package/src/engine-components/SeeThrough.ts +2 -2
  128. package/src/engine-components/api.ts +1 -1
  129. package/src/engine-components/postprocessing/VolumeParameter.ts +4 -1
  130. package/src/engine-components/ui/Canvas.ts +2 -8
  131. package/src/engine-components/ui/Text.ts +12 -8
  132. package/src/engine-components/web/CursorFollow.ts +21 -13
  133. package/src/engine-components/webxr/WebXRImageTracking.ts +2 -0
@@ -4,6 +4,7 @@ import { isDevEnvironment, showBalloonMessage, showBalloonWarning } from "../eng
4
4
  import { Behaviour, Component, GameObject } from "../engine-components/Component.js";
5
5
  import { CallInfo, EventList } from "../engine-components/EventList.js";
6
6
  import { AssetReference } from "./engine_addressables.js";
7
+ import { AudioClip } from "./engine_audio.js";
7
8
  import { debugExtension } from "./engine_default_parameters.js";
8
9
  import { SerializationContext, TypeSerializer } from "./engine_serialization_core.js";
9
10
  import { RenderTexture } from "./engine_texture.js";
@@ -11,25 +12,7 @@ import { IComponent } from "./engine_types.js";
11
12
  import { resolveUrl } from "./engine_utils.js";
12
13
  import { RGBAColor } from "./js-extensions/index.js";
13
14
 
14
- // export class SourcePath {
15
- // src?:string
16
- // };
17
-
18
- // class SourcePathSerializer extends TypeSerializer{
19
- // constructor(){
20
- // super(SourcePath);
21
- // }
22
- // onDeserialize(data: any, _context: SerializationContext) {
23
- // if(data.src && typeof data.src === "string"){
24
- // return data.src;
25
- // }
26
- // }
27
- // onSerialize(_data: any, _context: SerializationContext) {
28
-
29
- // }
30
- // }
31
- // new SourcePathSerializer();
32
-
15
+ // #region Color
33
16
  class ColorSerializer extends TypeSerializer {
34
17
  constructor() {
35
18
  super([Color, RGBAColor], "ColorSerializer")
@@ -52,7 +35,8 @@ class ColorSerializer extends TypeSerializer {
52
35
  return { r: data.r, g: data.g, b: data.b }
53
36
  }
54
37
  }
55
- export const colorSerializer = new ColorSerializer();
38
+
39
+ // #region Euler
56
40
 
57
41
  class EulerSerializer extends TypeSerializer {
58
42
  constructor() {
@@ -72,12 +56,13 @@ class EulerSerializer extends TypeSerializer {
72
56
  return { x: data.x, y: data.y, z: data.z, order: data.order };
73
57
  }
74
58
  }
75
- export const euler = new EulerSerializer();
76
59
 
77
60
  declare type ObjectData = {
78
61
  node?: number;
79
62
  guid?: string;
80
63
  }
64
+
65
+ // #region ObjectSerializer
81
66
  class ObjectSerializer extends TypeSerializer {
82
67
  constructor() {
83
68
  super(Object3D, "ObjectSerializer");
@@ -107,16 +92,6 @@ class ObjectSerializer extends TypeSerializer {
107
92
  console.warn(`Wrong usage of @serializable detected in your script \"${scriptname}\"\n\nIt looks like you used @serializable(Object3D) or @serializable(GameObject) for a prefab or scene reference which is exported to a separate glTF file.\n\nTo fix this please change your code to:\n\n@serializable(AssetReference)\n${context.path}! : AssetReference;\n\0`);
108
93
  }
109
94
  // ACTUALLY: this is already handled by the extension_utils where we resolve json pointers recursively
110
- // if(data.startsWith("/nodes/")){
111
- // const node = parseInt(data.substring("/nodes/".length));
112
- // if (context.nodeToObject) {
113
- // const res = context.nodeToObject[node];
114
- // if (debugExtension)
115
- // console.log("Deserialized object reference?", data, res, context?.nodeToObject);
116
- // if (!res) console.warn("Did not find node: " + data, context.nodeToObject, context.object);
117
- // return res;
118
- // }
119
- // }
120
95
  return undefined;
121
96
  }
122
97
 
@@ -166,9 +141,8 @@ class ObjectSerializer extends TypeSerializer {
166
141
  return undefined;
167
142
  }
168
143
  }
169
- export const objectSerializer = new ObjectSerializer();
170
-
171
144
 
145
+ // #region ComponentSerializer
172
146
  class ComponentSerializer extends TypeSerializer {
173
147
 
174
148
  constructor() {
@@ -241,7 +215,6 @@ class ComponentSerializer extends TypeSerializer {
241
215
  }
242
216
  }
243
217
  }
244
- export const componentSerializer = new ComponentSerializer();
245
218
 
246
219
 
247
220
  declare class EventListData {
@@ -258,6 +231,7 @@ declare type EventListCall = {
258
231
 
259
232
  const $eventListDebugInfo = Symbol("eventListDebugInfo");
260
233
 
234
+ // #region EventListSerializer
261
235
  class EventListSerializer extends TypeSerializer {
262
236
  constructor() {
263
237
  super([EventList]);
@@ -396,22 +370,14 @@ class EventListSerializer extends TypeSerializer {
396
370
  // };
397
371
  // }
398
372
  }
399
- export const eventListSerializer = new EventListSerializer();
400
373
 
401
374
 
402
375
  /** Map<Clone, Original> texture. This is used for compressed textures (or when the GLTFLoader is cloning RenderTextures)
403
376
  * It's a weak map so we don't have to worry about memory leaks
404
377
  */
405
378
  const cloneOriginalMap = new WeakMap<Texture, Texture>();
406
- const textureClone = Texture.prototype.clone;
407
- Texture.prototype.clone = function () {
408
- const clone = textureClone.call(this);
409
- if (!cloneOriginalMap.has(clone)) {
410
- cloneOriginalMap.set(clone, this);
411
- }
412
- return clone;
413
- }
414
379
 
380
+ // #region RenderTextureSerializer
415
381
  export class RenderTextureSerializer extends TypeSerializer {
416
382
  constructor() {
417
383
  super([RenderTexture, WebGLRenderTarget]);
@@ -452,9 +418,8 @@ export class RenderTextureSerializer extends TypeSerializer {
452
418
  return undefined;
453
419
  }
454
420
  }
455
- new RenderTextureSerializer();
456
-
457
421
 
422
+ // #region UriSerializer
458
423
  export class UriSerializer extends TypeSerializer {
459
424
  constructor() {
460
425
  super([URL]);
@@ -471,4 +436,56 @@ export class UriSerializer extends TypeSerializer {
471
436
  return undefined;
472
437
  }
473
438
  }
474
- new UriSerializer();
439
+
440
+ // #region AudioClipSerializer
441
+ class AudioClipSerializer extends TypeSerializer {
442
+ constructor() {
443
+ super([AudioClip]);
444
+ }
445
+
446
+ onSerialize(_data: AudioClip, _context: SerializationContext) {
447
+ return null;
448
+ }
449
+
450
+ onDeserialize(data: string, context: SerializationContext) {
451
+ if (typeof data === "string" && data.length > 0) {
452
+ const url = resolveUrl(context.gltfId, data);
453
+ if (url) return new AudioClip(url);
454
+ }
455
+ return undefined;
456
+ }
457
+ }
458
+
459
+
460
+ // #region Init serializer
461
+ // Module-level references used by EventListSerializer internally
462
+ export let colorSerializer: ColorSerializer;
463
+ export let objectSerializer: ObjectSerializer;
464
+ export let componentSerializer: ComponentSerializer;
465
+ export let eventListSerializer: EventListSerializer;
466
+
467
+ /** Register all builtin serializers and prototype patches.
468
+ * Must be called from {@link initEngine} so the registrations survive tree-shaking
469
+ * when the package declares `sideEffects: false`.
470
+ */
471
+ export function initBuiltinSerializers() {
472
+ // Prototype patches
473
+ const textureClone = Texture.prototype.clone;
474
+ Texture.prototype.clone = function () {
475
+ const clone = textureClone.call(this);
476
+ if (!cloneOriginalMap.has(clone)) {
477
+ cloneOriginalMap.set(clone, this);
478
+ }
479
+ return clone;
480
+ }
481
+
482
+ // Register all serializers
483
+ colorSerializer = new ColorSerializer();
484
+ new EulerSerializer();
485
+ objectSerializer = new ObjectSerializer();
486
+ componentSerializer = new ComponentSerializer();
487
+ eventListSerializer = new EventListSerializer();
488
+ new RenderTextureSerializer();
489
+ new UriSerializer();
490
+ new AudioClipSerializer();
491
+ }
@@ -35,7 +35,7 @@ export class GenerateMeshBVHWorker extends WorkerBase {
35
35
 
36
36
  worker.onerror = e => {
37
37
 
38
- reject(new Error(`[GenerateMeshBVHWorker] ${e.message || "Unknown error. Please check the server console. If you're using vite try adding 'three-mesh-bvh' to 'optimizeDeps.exclude' in your vite.config.js"}`));
38
+ reject(new Error(`[GenerateMeshBVHWorker] ${e.message || "Could not load worker."}`));
39
39
 
40
40
  };
41
41
 
@@ -1,8 +1,8 @@
1
1
  import { showBalloonMessage } from "../../debug/debug.js";
2
- import { HTMLElementBase } from "../../engine_ssr.js";
3
2
  import type { Context } from "../../engine_context.js";
4
3
  import { hasCommercialLicense, onLicenseCheckResultChanged, Telemetry } from "../../engine_license.js";
5
4
  import { isLocalNetwork } from "../../engine_networking_utils.js";
5
+ import { HTMLElementBase } from "../../engine_ssr.js";
6
6
  import { DeviceUtilities, getParam } from "../../engine_utils.js";
7
7
  import { onXRSessionStart, XRSessionEventArgs } from "../../xr/events.js";
8
8
  import { ButtonsFactory } from "../buttons.js";
@@ -1,11 +1,11 @@
1
1
  import { isDevEnvironment, showBalloonWarning } from "../debug/index.js";
2
- import { HTMLElementBase } from "../engine_ssr.js";
3
2
  import { PUBLIC_KEY, VERSION } from "../engine_constants.js";
4
3
  import { ContextEvent, ContextRegistry } from "../engine_context_registry.js";
5
4
  import { hasCommercialLicense } from "../engine_license.js";
6
5
  import { onStart } from "../engine_lifecycle_api.js";
7
6
  import { setDracoDecoderPath, setDracoDecoderType, setKtx2TranscoderPath } from "../engine_loaders.gltf.js";
8
7
  import { Context, ContextCreateArgs } from "../engine_setup.js";
8
+ import { HTMLElementBase } from "../engine_ssr.js";
9
9
  import { nameToThreeTonemapping } from "../engine_tonemapping.js";
10
10
  import { type INeedleEngineComponent, type LoadedModel } from "../engine_types.js";
11
11
  import type { addAttributeChangeCallback } from "../engine_utils.js";
@@ -42,11 +42,17 @@ export interface NeedleEngineAttributes {
42
42
  'hash': string;
43
43
  /** Set to automatically add OrbitControls to the loaded scene. */
44
44
  'camera-controls': string;
45
- /** Override the default draco decoder path location. */
45
+ /** Override the default Draco decoder/decompressor path. Can be a URL or a local path to a directory containing the Draco decoder files.
46
+ * @default "https://www.gstatic.com/draco/versioned/decoders/1.5.7/"
47
+ * @example <needle-engine dracoDecoderPath="./decoders/draco/"></needle-engine>
48
+ */
46
49
  'dracoDecoderPath': string;
47
- /** Override the default draco library type. */
50
+ /** Override the default Draco decoder type. */
48
51
  'dracoDecoderType': 'wasm' | 'js';
49
- /** Override the default KTX2 transcoder/decoder path. */
52
+ /** Override the default KTX2 transcoder/decoder path. Can be a URL or a local path to a directory containing the KTX2 transcoder files.
53
+ * @default "https://cdn.needle.tools/static/three/0.179.1/basis2/"
54
+ * @example <needle-engine ktx2DecoderPath="./decoders/ktx2/"></needle-engine>
55
+ */
50
56
  'ktx2DecoderPath': string;
51
57
  /** Prevent context from being disposed when element is removed from DOM. */
52
58
  'keep-alive': 'true' | 'false';
@@ -840,15 +840,16 @@ export class NeedleXRSession implements INeedleXRSession {
840
840
  get viewerPose(): XRViewerPose | undefined { return this._viewerPose; }
841
841
 
842
842
 
843
- /** @returns `true` if any image is currently being tracked */
844
- /** returns true if images are currently being tracked */
843
+ /** @returns `true` if any image is currently being tracked or emulated */
845
844
  get isTrackingImages() {
846
845
  if (this.frame && "getImageTrackingResults" in this.frame && typeof this.frame.getImageTrackingResults === "function") {
847
846
  try {
848
847
  const trackingResult = this.frame.getImageTrackingResults();
849
848
  for (const result of trackingResult) {
850
849
  const state = result.trackingState;
851
- if (state === "tracked") return true;
850
+ if (state === "tracked" || state === "emulated") {
851
+ return true;
852
+ }
852
853
  }
853
854
  }
854
855
  catch {
@@ -1053,6 +1054,8 @@ export class NeedleXRSession implements INeedleXRSession {
1053
1054
  private readonly _xr_update_scripts: INeedleXRSessionEventReceiver[] = [];
1054
1055
  /** scripts that are in the scene but inactive (e.g. disabled parent gameObject) */
1055
1056
  private readonly _inactive_scripts: INeedleXRSessionEventReceiver[] = [];
1057
+ /** tracks scripts that have received onEnterXR — prevents spurious onLeaveXR calls */
1058
+ private readonly _scripts_in_xr = new Set<INeedleXRSessionEventReceiver>();
1056
1059
  private readonly _controllerAdded: ControllerChangedEvt[];
1057
1060
  private readonly _controllerRemoved: ControllerChangedEvt[];
1058
1061
  private readonly _originalCameraWorldPosition?: Vector3 | null;
@@ -1163,7 +1166,18 @@ export class NeedleXRSession implements INeedleXRSession {
1163
1166
  // we set the session on the webxr manager at the end because we want to receive inputsource events first
1164
1167
  // e.g. in case there's a bug in the threejs codebase
1165
1168
  this.context.xr = this;
1166
- this.context.renderer.xr.setSession(this.session).then(this.onRendererSessionSet);
1169
+ this.context.renderer.xr.setSession(this.session).then(this.onRendererSessionSet)
1170
+ .catch(err => {
1171
+ // Workaround for WebXR emulators on localhost: the polyfilled XRSession fails the
1172
+ // native XRWebGLBinding constructor's instanceof check. Retry with layers disabled.
1173
+ // See https://github.com/meta-quest/immersive-web-emulator/issues/80
1174
+ if (isDevEnvironment() && err instanceof TypeError && typeof XRWebGLBinding !== "undefined") {
1175
+ console.error(`XRWebGLBinding failed (${err.message}).\nSee https://github.com/meta-quest/immersive-web-emulator/issues/80`);
1176
+ }
1177
+ else {
1178
+ console.error("renderer.xr.setSession failed:", err);
1179
+ }
1180
+ });
1167
1181
  // disable three.js renderer controller autoUpdate (added in ac67b31e3548386f8a93e23a4176554c92bbd0d9)
1168
1182
  if ("controllerAutoUpdate" in this.context.renderer.xr) {
1169
1183
  console.debug("Disabling three.js controllerAutoUpdate");
@@ -1331,8 +1345,10 @@ export class NeedleXRSession implements INeedleXRSession {
1331
1345
  // even if they might already be destroyed e.g. by the WebXR component (it destroys the default controller scripts)
1332
1346
  // they should still receive this callback to be properly cleaned up
1333
1347
  for (const listener of this._xr_scripts) {
1348
+ this._scripts_in_xr.delete(listener);
1334
1349
  listener?.onLeaveXR?.({ xr: this });
1335
1350
  }
1351
+ this._scripts_in_xr.clear();
1336
1352
 
1337
1353
  this.sync?.onExitXR(this);
1338
1354
 
@@ -1399,10 +1415,17 @@ export class NeedleXRSession implements INeedleXRSession {
1399
1415
  }
1400
1416
 
1401
1417
  // make sure the camera is parented to the active rig
1402
- if (this.rig && this._mainCamera?.gameObject) {
1403
- const currentParent = this._mainCamera?.gameObject?.parent;
1404
- if (currentParent !== this.rig.gameObject) {
1405
- this.rig.gameObject.add(this._mainCamera?.gameObject);
1418
+ // Note: applyCustomForward() inserts _cameraRenderParent between rig and camera,
1419
+ // so the hierarchy is: rig → _cameraRenderParent → camera. We check the effective parent.
1420
+ if (this.rig) {
1421
+ const cameraObject = this._mainCamera?.gameObject ?? this.context.mainCamera;
1422
+ if (cameraObject) {
1423
+ const effectiveParent = cameraObject.parent === this._cameraRenderParent
1424
+ ? this._cameraRenderParent.parent
1425
+ : cameraObject.parent;
1426
+ if (effectiveParent !== this.rig.gameObject) {
1427
+ this.rig.gameObject.add(cameraObject);
1428
+ }
1406
1429
  }
1407
1430
  }
1408
1431
 
@@ -1534,8 +1557,12 @@ export class NeedleXRSession implements INeedleXRSession {
1534
1557
  }
1535
1558
 
1536
1559
  //performance.mark('NeedleXRSession update scripts start');
1537
- // invoke update on all scripts
1538
- for (const script of this._xr_update_scripts) {
1560
+ // check all XR scripts for destroyed or inactive state
1561
+ // this must cover _xr_scripts (not just _xr_update_scripts) so that scripts
1562
+ // without onUpdateXR are also detected as inactive when removed from the scene
1563
+ // iterate backwards since markInactive/removeScript modifies the array
1564
+ for (let i = this._xr_scripts.length - 1; i >= 0; i--) {
1565
+ const script = this._xr_scripts[i];
1539
1566
  if (script.destroyed === true) {
1540
1567
  this._script_to_remove.push(script);
1541
1568
  continue;
@@ -1544,6 +1571,10 @@ export class NeedleXRSession implements INeedleXRSession {
1544
1571
  this.markInactive(script);
1545
1572
  continue;
1546
1573
  }
1574
+ }
1575
+ // invoke update on scripts that have onUpdateXR
1576
+ for (const script of this._xr_update_scripts) {
1577
+ if (script.destroyed || script.activeAndEnabled === false) continue;
1547
1578
  if (script.onUpdateXR) script.onUpdateXR(args);
1548
1579
  }
1549
1580
  //performance.mark('NeedleXRSession update scripts end');
@@ -1666,9 +1697,11 @@ export class NeedleXRSession implements INeedleXRSession {
1666
1697
  const script = this._inactive_scripts[i];
1667
1698
  if (script.activeAndEnabled) {
1668
1699
  this._inactive_scripts.splice(i, 1);
1669
- this.addScript(script);
1670
- this.invokeCallback_EnterXR(script);
1671
- for (const ctrl of this.controllers) this.invokeCallback_ControllerAdded(script, ctrl);
1700
+ // addScript returns false if already re-added (e.g. via new_scripts_xr processing)
1701
+ if (this.addScript(script)) {
1702
+ this.invokeCallback_EnterXR(script);
1703
+ for (const ctrl of this.controllers) this.invokeCallback_ControllerAdded(script, ctrl);
1704
+ }
1672
1705
  }
1673
1706
  }
1674
1707
  }
@@ -1689,6 +1722,7 @@ export class NeedleXRSession implements INeedleXRSession {
1689
1722
  }
1690
1723
 
1691
1724
  private invokeCallback_EnterXR(script: INeedleXRSessionEventReceiver) {
1725
+ this._scripts_in_xr.add(script);
1692
1726
  if (script.onEnterXR) {
1693
1727
  script.onEnterXR({ xr: this });
1694
1728
  }
@@ -1704,6 +1738,7 @@ export class NeedleXRSession implements INeedleXRSession {
1704
1738
  }
1705
1739
  }
1706
1740
  private invokeCallback_LeaveXR(script: INeedleXRSessionEventReceiver) {
1741
+ if (!this._scripts_in_xr.delete(script)) return;
1707
1742
  if (script.onLeaveXR && !script.destroyed) {
1708
1743
  script.onLeaveXR({ xr: this });
1709
1744
  }
@@ -1,5 +1,5 @@
1
- import { NeedleXRSession } from "./NeedleXRSession.js";
2
1
  import type { Context } from "../engine_context.js";
2
+ import { NeedleXRSession } from "./NeedleXRSession.js";
3
3
 
4
4
  export declare type XRSessionEventArgs = { session: NeedleXRSession };
5
5
 
@@ -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
  });
@@ -1586,4 +1586,7 @@ class AnimatorControllerSerializator extends TypeSerializer {
1586
1586
  return undefined;
1587
1587
  }
1588
1588
  }
1589
- new AnimatorControllerSerializator(AnimatorController);
1589
+ /** @internal */
1590
+ export function initAnimatorControllerSerializer() {
1591
+ new AnimatorControllerSerializator(AnimatorController);
1592
+ }