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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. package/CHANGELOG.md +74 -1
  2. package/SKILL.md +4 -1
  3. package/components.needle.json +1 -1
  4. package/dist/needle-engine.bundle-AjVIot3d.min.js +1733 -0
  5. package/dist/{needle-engine.bundle-dit3f1l5.js → needle-engine.bundle-B7cqsI4c.js} +12199 -11704
  6. package/dist/needle-engine.bundle-DQCuBTVp.umd.cjs +1733 -0
  7. package/dist/needle-engine.d.ts +879 -226
  8. package/dist/needle-engine.js +584 -582
  9. package/dist/needle-engine.min.js +1 -1
  10. package/dist/needle-engine.umd.cjs +1 -1
  11. package/lib/engine/api.d.ts +5 -0
  12. package/lib/engine/api.js +4 -0
  13. package/lib/engine/api.js.map +1 -1
  14. package/lib/engine/codegen/register_types.js +2 -10
  15. package/lib/engine/codegen/register_types.js.map +1 -1
  16. package/lib/engine/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_components.js +1 -1
  26. package/lib/engine/engine_components.js.map +1 -1
  27. package/lib/engine/engine_context.d.ts +1 -1
  28. package/lib/engine/engine_context.js +3 -2
  29. package/lib/engine/engine_context.js.map +1 -1
  30. package/lib/engine/engine_disposable.d.ts +172 -0
  31. package/lib/engine/engine_disposable.js +136 -0
  32. package/lib/engine/engine_disposable.js.map +1 -0
  33. package/lib/engine/engine_gameobject.d.ts +1 -10
  34. package/lib/engine/engine_gameobject.js +22 -120
  35. package/lib/engine/engine_gameobject.js.map +1 -1
  36. package/lib/engine/engine_gltf_builtin_components.js +7 -69
  37. package/lib/engine/engine_gltf_builtin_components.js.map +1 -1
  38. package/lib/engine/engine_init.js +13 -3
  39. package/lib/engine/engine_init.js.map +1 -1
  40. package/lib/engine/engine_input.d.ts +1 -1
  41. package/lib/engine/engine_input.js +1 -1
  42. package/lib/engine/engine_input.js.map +1 -1
  43. package/lib/engine/engine_instantiate_resolve.d.ts +42 -0
  44. package/lib/engine/engine_instantiate_resolve.js +372 -0
  45. package/lib/engine/engine_instantiate_resolve.js.map +1 -0
  46. package/lib/engine/engine_license.js +1 -1
  47. package/lib/engine/engine_license.js.map +1 -1
  48. package/lib/engine/engine_mainloop_utils.js +7 -4
  49. package/lib/engine/engine_mainloop_utils.js.map +1 -1
  50. package/lib/engine/engine_networking.d.ts +51 -37
  51. package/lib/engine/engine_networking.js +132 -82
  52. package/lib/engine/engine_networking.js.map +1 -1
  53. package/lib/engine/engine_networking.transport.websocket.d.ts +15 -0
  54. package/lib/engine/engine_networking.transport.websocket.js +38 -0
  55. package/lib/engine/engine_networking.transport.websocket.js.map +1 -0
  56. package/lib/engine/engine_networking_blob.js +1 -1
  57. package/lib/engine/engine_networking_blob.js.map +1 -1
  58. package/lib/engine/engine_networking_instantiate.js +2 -2
  59. package/lib/engine/engine_networking_instantiate.js.map +1 -1
  60. package/lib/engine/engine_networking_types.d.ts +39 -1
  61. package/lib/engine/engine_networking_types.js +7 -0
  62. package/lib/engine/engine_networking_types.js.map +1 -1
  63. package/lib/engine/engine_physics_rapier.d.ts +11 -3
  64. package/lib/engine/engine_physics_rapier.js +88 -25
  65. package/lib/engine/engine_physics_rapier.js.map +1 -1
  66. package/lib/engine/engine_scenedata.d.ts +2 -0
  67. package/lib/engine/engine_scenedata.js +4 -2
  68. package/lib/engine/engine_scenedata.js.map +1 -1
  69. package/lib/engine/engine_serialization_builtin_serializer.d.ts +10 -16
  70. package/lib/engine/engine_serialization_builtin_serializer.js +56 -46
  71. package/lib/engine/engine_serialization_builtin_serializer.js.map +1 -1
  72. package/lib/engine/engine_serialization_core.d.ts +1 -0
  73. package/lib/engine/engine_serialization_core.js +7 -0
  74. package/lib/engine/engine_serialization_core.js.map +1 -1
  75. package/lib/engine/engine_types.d.ts +19 -11
  76. package/lib/engine/engine_types.js +1 -1
  77. package/lib/engine/engine_types.js.map +1 -1
  78. package/lib/engine/engine_util_decorator.js +7 -2
  79. package/lib/engine/engine_util_decorator.js.map +1 -1
  80. package/lib/engine/engine_utils.d.ts +1 -1
  81. package/lib/engine/engine_utils.js +19 -5
  82. package/lib/engine/engine_utils.js.map +1 -1
  83. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +1 -1
  84. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js.map +1 -1
  85. package/lib/engine/webcomponents/needle menu/needle-menu.d.ts +1 -1
  86. package/lib/engine/webcomponents/needle menu/needle-menu.js +1 -1
  87. package/lib/engine/webcomponents/needle menu/needle-menu.js.map +1 -1
  88. package/lib/engine/webcomponents/needle-engine.d.ts +10 -4
  89. package/lib/engine/webcomponents/needle-engine.js +1 -1
  90. package/lib/engine/webcomponents/needle-engine.js.map +1 -1
  91. package/lib/engine/xr/NeedleXRSession.d.ts +3 -2
  92. package/lib/engine/xr/NeedleXRSession.js +50 -14
  93. package/lib/engine/xr/NeedleXRSession.js.map +1 -1
  94. package/lib/engine/xr/events.d.ts +1 -1
  95. package/lib/engine/xr/events.js.map +1 -1
  96. package/lib/engine-components/Animation.js +17 -16
  97. package/lib/engine-components/Animation.js.map +1 -1
  98. package/lib/engine-components/Animator.d.ts +6 -0
  99. package/lib/engine-components/Animator.js +17 -12
  100. package/lib/engine-components/Animator.js.map +1 -1
  101. package/lib/engine-components/AnimatorController.builder.d.ts +113 -0
  102. package/lib/engine-components/AnimatorController.builder.js +195 -0
  103. package/lib/engine-components/AnimatorController.builder.js.map +1 -0
  104. package/lib/engine-components/AnimatorController.d.ts +4 -119
  105. package/lib/engine-components/AnimatorController.js +35 -233
  106. package/lib/engine-components/AnimatorController.js.map +1 -1
  107. package/lib/engine-components/AudioSource.d.ts +19 -3
  108. package/lib/engine-components/AudioSource.js +121 -68
  109. package/lib/engine-components/AudioSource.js.map +1 -1
  110. package/lib/engine-components/Collider.d.ts +18 -9
  111. package/lib/engine-components/Collider.js +61 -14
  112. package/lib/engine-components/Collider.js.map +1 -1
  113. package/lib/engine-components/Component.d.ts +72 -9
  114. package/lib/engine-components/Component.js +114 -10
  115. package/lib/engine-components/Component.js.map +1 -1
  116. package/lib/engine-components/DragControls.d.ts +7 -0
  117. package/lib/engine-components/DragControls.js +19 -7
  118. package/lib/engine-components/DragControls.js.map +1 -1
  119. package/lib/engine-components/EventList.d.ts +31 -9
  120. package/lib/engine-components/EventList.js +37 -76
  121. package/lib/engine-components/EventList.js.map +1 -1
  122. package/lib/engine-components/Joints.d.ts +4 -2
  123. package/lib/engine-components/Joints.js +19 -3
  124. package/lib/engine-components/Joints.js.map +1 -1
  125. package/lib/engine-components/Light.js +9 -1
  126. package/lib/engine-components/Light.js.map +1 -1
  127. package/lib/engine-components/Networking.d.ts +1 -1
  128. package/lib/engine-components/Networking.js +1 -1
  129. package/lib/engine-components/OrbitControls.js +16 -11
  130. package/lib/engine-components/OrbitControls.js.map +1 -1
  131. package/lib/engine-components/ReflectionProbe.js +2 -0
  132. package/lib/engine-components/ReflectionProbe.js.map +1 -1
  133. package/lib/engine-components/RigidBody.d.ts +12 -4
  134. package/lib/engine-components/RigidBody.js +18 -4
  135. package/lib/engine-components/RigidBody.js.map +1 -1
  136. package/lib/engine-components/SeeThrough.js +2 -2
  137. package/lib/engine-components/SeeThrough.js.map +1 -1
  138. package/lib/engine-components/api.d.ts +1 -1
  139. package/lib/engine-components/api.js +1 -1
  140. package/lib/engine-components/api.js.map +1 -1
  141. package/lib/engine-components/codegen/components.d.ts +3 -9
  142. package/lib/engine-components/codegen/components.js +3 -9
  143. package/lib/engine-components/codegen/components.js.map +1 -1
  144. package/lib/engine-components/postprocessing/Effects/Tonemapping.utils.d.ts +1 -1
  145. package/lib/engine-components/postprocessing/VolumeParameter.d.ts +2 -0
  146. package/lib/engine-components/postprocessing/VolumeParameter.js +4 -1
  147. package/lib/engine-components/postprocessing/VolumeParameter.js.map +1 -1
  148. package/lib/engine-components/timeline/PlayableDirector.d.ts +16 -6
  149. package/lib/engine-components/timeline/PlayableDirector.js +70 -62
  150. package/lib/engine-components/timeline/PlayableDirector.js.map +1 -1
  151. package/lib/engine-components/timeline/SignalAsset.d.ts +3 -1
  152. package/lib/engine-components/timeline/SignalAsset.js +1 -0
  153. package/lib/engine-components/timeline/SignalAsset.js.map +1 -1
  154. package/lib/engine-components/timeline/TimelineBuilder.d.ts +247 -0
  155. package/lib/engine-components/timeline/TimelineBuilder.js +400 -0
  156. package/lib/engine-components/timeline/TimelineBuilder.js.map +1 -0
  157. package/lib/engine-components/timeline/TimelineModels.d.ts +2 -1
  158. package/lib/engine-components/timeline/TimelineModels.js +3 -0
  159. package/lib/engine-components/timeline/TimelineModels.js.map +1 -1
  160. package/lib/engine-components/timeline/TimelineTracks.d.ts +23 -0
  161. package/lib/engine-components/timeline/TimelineTracks.js +71 -13
  162. package/lib/engine-components/timeline/TimelineTracks.js.map +1 -1
  163. package/lib/engine-components/timeline/index.d.ts +2 -1
  164. package/lib/engine-components/timeline/index.js +2 -0
  165. package/lib/engine-components/timeline/index.js.map +1 -1
  166. package/lib/engine-components/ui/Canvas.d.ts +1 -1
  167. package/lib/engine-components/ui/Canvas.js +2 -8
  168. package/lib/engine-components/ui/Canvas.js.map +1 -1
  169. package/lib/engine-components/ui/Text.d.ts +1 -0
  170. package/lib/engine-components/ui/Text.js +10 -7
  171. package/lib/engine-components/ui/Text.js.map +1 -1
  172. package/lib/engine-components/web/CursorFollow.js +21 -12
  173. package/lib/engine-components/web/CursorFollow.js.map +1 -1
  174. package/lib/engine-components/webxr/WebXRImageTracking.js +4 -0
  175. package/lib/engine-components/webxr/WebXRImageTracking.js.map +1 -1
  176. package/package.json +2 -83
  177. package/plugins/common/license.js +5 -2
  178. package/plugins/common/worker.js +9 -4
  179. package/plugins/vite/asap.js +17 -8
  180. package/plugins/vite/dependencies.js +29 -10
  181. package/plugins/vite/dependency-watcher.js +2 -2
  182. package/plugins/vite/editor-connection.js +3 -3
  183. package/plugins/vite/local-files-core.js +3 -3
  184. package/plugins/vite/local-files-utils.d.ts +3 -1
  185. package/plugins/vite/local-files-utils.js +29 -5
  186. package/plugins/vite/reload.js +1 -1
  187. package/plugins/vite/server.js +2 -1
  188. package/src/engine/api.ts +7 -0
  189. package/src/engine/codegen/register_types.ts +2 -10
  190. package/src/engine/debug/debug_spatial_console.ts +10 -7
  191. package/src/engine/engine_addressables.ts +6 -3
  192. package/src/engine/engine_audio.ts +184 -0
  193. package/src/engine/engine_components.ts +1 -1
  194. package/src/engine/engine_context.ts +4 -3
  195. package/src/engine/engine_disposable.ts +214 -0
  196. package/src/engine/engine_gameobject.ts +54 -159
  197. package/src/engine/engine_gltf_builtin_components.ts +7 -76
  198. package/src/engine/engine_init.ts +13 -3
  199. package/src/engine/engine_input.ts +1 -1
  200. package/src/engine/engine_instantiate_resolve.ts +407 -0
  201. package/src/engine/engine_license.ts +1 -1
  202. package/src/engine/engine_mainloop_utils.ts +7 -4
  203. package/src/engine/engine_networking.transport.websocket.ts +45 -0
  204. package/src/engine/engine_networking.ts +161 -137
  205. package/src/engine/engine_networking_blob.ts +1 -1
  206. package/src/engine/engine_networking_instantiate.ts +2 -2
  207. package/src/engine/engine_networking_types.ts +41 -1
  208. package/src/engine/engine_physics_rapier.ts +82 -27
  209. package/src/engine/engine_scenedata.ts +5 -3
  210. package/src/engine/engine_serialization_builtin_serializer.ts +64 -52
  211. package/src/engine/engine_serialization_core.ts +9 -0
  212. package/src/engine/engine_types.ts +24 -15
  213. package/src/engine/engine_util_decorator.ts +7 -2
  214. package/src/engine/engine_utils.ts +16 -5
  215. package/src/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +1 -1
  216. package/src/engine/webcomponents/needle menu/needle-menu.ts +1 -1
  217. package/src/engine/webcomponents/needle-engine.ts +10 -4
  218. package/src/engine/xr/NeedleXRSession.ts +48 -13
  219. package/src/engine/xr/events.ts +1 -1
  220. package/src/engine-components/Animation.ts +19 -16
  221. package/src/engine-components/Animator.ts +18 -11
  222. package/src/engine-components/AnimatorController.builder.ts +261 -0
  223. package/src/engine-components/AnimatorController.ts +23 -292
  224. package/src/engine-components/AudioSource.ts +130 -79
  225. package/src/engine-components/Collider.ts +66 -18
  226. package/src/engine-components/Component.ts +118 -20
  227. package/src/engine-components/DragControls.ts +18 -11
  228. package/src/engine-components/EventList.ts +45 -83
  229. package/src/engine-components/Joints.ts +20 -4
  230. package/src/engine-components/Light.ts +10 -2
  231. package/src/engine-components/Networking.ts +1 -1
  232. package/src/engine-components/OrbitControls.ts +18 -9
  233. package/src/engine-components/ReflectionProbe.ts +2 -0
  234. package/src/engine-components/RigidBody.ts +18 -4
  235. package/src/engine-components/SeeThrough.ts +2 -2
  236. package/src/engine-components/api.ts +1 -1
  237. package/src/engine-components/codegen/components.ts +3 -9
  238. package/src/engine-components/postprocessing/VolumeParameter.ts +4 -1
  239. package/src/engine-components/timeline/PlayableDirector.ts +67 -65
  240. package/src/engine-components/timeline/SignalAsset.ts +4 -1
  241. package/src/engine-components/timeline/TimelineBuilder.ts +564 -0
  242. package/src/engine-components/timeline/TimelineModels.ts +5 -1
  243. package/src/engine-components/timeline/TimelineTracks.ts +74 -13
  244. package/src/engine-components/timeline/index.ts +2 -1
  245. package/src/engine-components/ui/Canvas.ts +2 -8
  246. package/src/engine-components/ui/Text.ts +12 -8
  247. package/src/engine-components/web/CursorFollow.ts +21 -13
  248. package/src/engine-components/webxr/WebXRImageTracking.ts +2 -0
  249. package/dist/needle-engine.bundle-B-5Q2CpC.min.js +0 -1732
  250. package/dist/needle-engine.bundle-qZfVf_v-.umd.cjs +0 -1732
  251. package/lib/engine-components/AvatarLoader.d.ts +0 -80
  252. package/lib/engine-components/AvatarLoader.js +0 -232
  253. package/lib/engine-components/AvatarLoader.js.map +0 -1
  254. package/lib/engine-components/avatar/AvatarBlink_Simple.d.ts +0 -11
  255. package/lib/engine-components/avatar/AvatarBlink_Simple.js +0 -77
  256. package/lib/engine-components/avatar/AvatarBlink_Simple.js.map +0 -1
  257. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.d.ts +0 -14
  258. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js +0 -69
  259. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js.map +0 -1
  260. package/lib/engine-components/avatar/Avatar_Brain_LookAt.d.ts +0 -29
  261. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js +0 -122
  262. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js.map +0 -1
  263. package/lib/engine-components/avatar/Avatar_MouthShapes.d.ts +0 -15
  264. package/lib/engine-components/avatar/Avatar_MouthShapes.js +0 -80
  265. package/lib/engine-components/avatar/Avatar_MouthShapes.js.map +0 -1
  266. package/lib/engine-components/avatar/Avatar_MustacheShake.d.ts +0 -9
  267. package/lib/engine-components/avatar/Avatar_MustacheShake.js +0 -30
  268. package/lib/engine-components/avatar/Avatar_MustacheShake.js.map +0 -1
  269. package/src/engine-components/AvatarLoader.ts +0 -264
  270. package/src/engine-components/avatar/AvatarBlink_Simple.ts +0 -70
  271. package/src/engine-components/avatar/AvatarEyeLook_Rotation.ts +0 -64
  272. package/src/engine-components/avatar/Avatar_Brain_LookAt.ts +0 -140
  273. package/src/engine-components/avatar/Avatar_MouthShapes.ts +0 -84
  274. package/src/engine-components/avatar/Avatar_MustacheShake.ts +0 -32
@@ -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
  });
@@ -89,6 +89,15 @@ export class Animator extends Behaviour implements IAnimationComponent {
89
89
  return true;
90
90
  }
91
91
 
92
+ /**
93
+ * The current animator mixer, used for low-level control of animations. Owned by the AnimatorController
94
+ * @returns The current AnimationMixer, or null if no controller is assigned
95
+ * @see AnimatorController.mixer
96
+ */
97
+ get mixer() {
98
+ return this._animatorController?.mixer || null;
99
+ }
100
+
92
101
  /**
93
102
  * When enabled, animation will affect the root transform position and rotation
94
103
  */
@@ -401,17 +410,15 @@ export class Animator extends Behaviour implements IAnimationComponent {
401
410
  initializeRuntimeAnimatorController(force: boolean = false) {
402
411
  const shouldRun = (force || this.runtimeAnimatorController !== this._initializeWithRuntimeAnimatorController);
403
412
  if (this.runtimeAnimatorController && shouldRun) {
404
- const clone = this.runtimeAnimatorController.clone();
405
- this._initializeWithRuntimeAnimatorController = clone;
406
- if (clone) {
407
- console.assert(this.runtimeAnimatorController !== clone);
408
- this.runtimeAnimatorController = clone;
409
- console.assert(this.runtimeAnimatorController === clone);
410
- this.runtimeAnimatorController.bind(this);
411
- this.runtimeAnimatorController.setSpeed(this._speed);
412
- this.runtimeAnimatorController.normalizedStartOffset = this._normalizedStartOffset;
413
- }
414
- else console.warn("Could not clone animator controller", this.runtimeAnimatorController);
413
+ // Create a fresh controller from the (possibly remapped) model.
414
+ // The constructor deep-clones the model, preserving Object3D refs that
415
+ // the resolve system has already remapped to cloned objects.
416
+ const controller = new AnimatorController(this.runtimeAnimatorController.model);
417
+ this.runtimeAnimatorController = controller;
418
+ this._initializeWithRuntimeAnimatorController = controller;
419
+ controller.bind(this);
420
+ controller.setSpeed(this._speed);
421
+ controller.normalizedStartOffset = this._normalizedStartOffset;
415
422
  }
416
423
  }
417
424
 
@@ -0,0 +1,261 @@
1
+ import { AnimationClip } from "three";
2
+
3
+ import { AnimatorConditionMode, AnimatorControllerParameterType } from "../engine/extensions/NEEDLE_animator_controller_model.js";
4
+ import type { AnimatorControllerModel, Condition, Parameter, State, Transition } from "../engine/extensions/NEEDLE_animator_controller_model.js";
5
+ import { InstantiateIdProvider } from "../engine/engine_networking_instantiate.js";
6
+ import { AnimatorController } from "./AnimatorController.js";
7
+
8
+
9
+ /**
10
+ * Configuration for an animation state in the builder
11
+ */
12
+ export declare type StateOptions = {
13
+ /** The animation clip for this state */
14
+ clip: AnimationClip;
15
+ /** Whether the animation should loop (default: false) */
16
+ loop?: boolean;
17
+ /** Base speed multiplier (default: 1) */
18
+ speed?: number;
19
+ /** Name of a float parameter to multiply with speed */
20
+ speedParameter?: string;
21
+ /** Normalized cycle offset 0-1 (default: 0) */
22
+ cycleOffset?: number;
23
+ /** Name of a float parameter to use as cycle offset */
24
+ cycleOffsetParameter?: string;
25
+ }
26
+
27
+ /**
28
+ * Configuration for a transition in the builder
29
+ */
30
+ export declare type TransitionOptions = {
31
+ /** Duration of the crossfade in seconds (default: 0) */
32
+ duration?: number;
33
+ /** Normalized exit time 0-1 (default: 1). Only used when hasExitTime is true */
34
+ exitTime?: number;
35
+ /** Whether the transition waits for exitTime before transitioning (default: false) */
36
+ hasExitTime?: boolean;
37
+ /** Normalized offset into the destination state's animation (default: 0) */
38
+ offset?: number;
39
+ /** Whether duration is in seconds (true) or normalized (false) (default: false) */
40
+ hasFixedDuration?: boolean;
41
+ }
42
+
43
+ /** String condition modes for the builder, mapped to {@link AnimatorConditionMode} */
44
+ export type ConditionMode = "if" | "ifNot" | "greater" | "less" | "equals" | "notEqual";
45
+
46
+ function conditionModeToEnum(mode: ConditionMode): AnimatorConditionMode {
47
+ switch (mode) {
48
+ case "if": return AnimatorConditionMode.If;
49
+ case "ifNot": return AnimatorConditionMode.IfNot;
50
+ case "greater": return AnimatorConditionMode.Greater;
51
+ case "less": return AnimatorConditionMode.Less;
52
+ case "equals": return AnimatorConditionMode.Equals;
53
+ case "notEqual": return AnimatorConditionMode.NotEqual;
54
+ }
55
+ }
56
+
57
+ type BuilderTransition = {
58
+ to: string;
59
+ options: TransitionOptions;
60
+ conditions: Array<{ parameter: string; mode: ConditionMode; threshold: number }>;
61
+ };
62
+
63
+ type BuilderState = {
64
+ name: string;
65
+ options: StateOptions;
66
+ transitions: BuilderTransition[];
67
+ };
68
+
69
+ /**
70
+ * A fluent builder for creating {@link AnimatorController} instances from code.
71
+ *
72
+ * Use {@link AnimatorController.build} to create a new builder.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const controller = AnimatorController.build("CharacterController")
77
+ * .floatParameter("Speed", 0)
78
+ * .triggerParameter("Jump")
79
+ * .state("Idle", { clip: idleClip, loop: true })
80
+ * .state("Walk", { clip: walkClip, loop: true })
81
+ * .state("Jump", { clip: jumpClip })
82
+ * .transition("Idle", "Walk", { duration: 0.25 })
83
+ * .condition("Speed", "greater", 0.1)
84
+ * .transition("Walk", "Idle", { duration: 0.25 })
85
+ * .condition("Speed", "less", 0.1)
86
+ * .transition("*", "Jump", { duration: 0.1 })
87
+ * .condition("Jump", "if")
88
+ * .transition("Jump", "Idle", { hasExitTime: true, exitTime: 0.9, duration: 0.25 })
89
+ * .build();
90
+ * ```
91
+ *
92
+ * @category Animation and Sequencing
93
+ * @group Utilities
94
+ */
95
+ export class AnimatorControllerBuilder {
96
+ private _name: string;
97
+ private _parameters: Parameter[] = [];
98
+ private _states: BuilderState[] = [];
99
+ private _anyStateTransitions: BuilderTransition[] = [];
100
+ private _defaultStateName: string | null = null;
101
+ private _lastTransition: BuilderTransition | null = null;
102
+
103
+ constructor(name?: string) {
104
+ this._name = name ?? "AnimatorController";
105
+ }
106
+
107
+ /** Adds a float parameter */
108
+ floatParameter(name: string, defaultValue: number = 0): this {
109
+ this._parameters.push({ name, hash: this._parameters.length, type: AnimatorControllerParameterType.Float, value: defaultValue });
110
+ return this;
111
+ }
112
+
113
+ /** Adds an integer parameter */
114
+ intParameter(name: string, defaultValue: number = 0): this {
115
+ this._parameters.push({ name, hash: this._parameters.length, type: AnimatorControllerParameterType.Int, value: defaultValue });
116
+ return this;
117
+ }
118
+
119
+ /** Adds a boolean parameter */
120
+ boolParameter(name: string, defaultValue: boolean = false): this {
121
+ this._parameters.push({ name, hash: this._parameters.length, type: AnimatorControllerParameterType.Bool, value: defaultValue });
122
+ return this;
123
+ }
124
+
125
+ /** Adds a trigger parameter */
126
+ triggerParameter(name: string): this {
127
+ this._parameters.push({ name, hash: this._parameters.length, type: AnimatorControllerParameterType.Trigger, value: false });
128
+ return this;
129
+ }
130
+
131
+ /**
132
+ * Adds a state to the controller. The first state added becomes the default state.
133
+ * @param name - Unique name for the state
134
+ * @param options - State configuration including clip, loop, speed
135
+ */
136
+ state(name: string, options: StateOptions): this {
137
+ this._states.push({ name, options, transitions: [] });
138
+ return this;
139
+ }
140
+
141
+ /**
142
+ * Adds a transition between two states.
143
+ * Use `"*"` as the source to create a transition from any state.
144
+ * Chain `.condition()` calls after this to add conditions.
145
+ * @param from - Source state name, or `"*"` for any-state transition
146
+ * @param to - Destination state name
147
+ * @param options - Transition configuration
148
+ */
149
+ transition(from: string, to: string, options?: TransitionOptions): this {
150
+ const t: BuilderTransition = { to, options: options ?? {}, conditions: [] };
151
+ if (from === "*") {
152
+ this._anyStateTransitions.push(t);
153
+ }
154
+ else {
155
+ const state = this._states.find(s => s.name === from);
156
+ if (!state) throw new Error(`AnimatorControllerBuilder: source state "${from}" not found. Add it with .state() first.`);
157
+ state.transitions.push(t);
158
+ }
159
+ this._lastTransition = t;
160
+ return this;
161
+ }
162
+
163
+ /**
164
+ * Adds a condition to the most recently added transition.
165
+ * Multiple conditions on the same transition are AND-ed together.
166
+ * @param parameter - Name of the parameter to evaluate
167
+ * @param mode - Condition mode: `"if"`, `"ifNot"`, `"greater"`, `"less"`, `"equals"`, `"notEqual"`
168
+ * @param threshold - Comparison threshold for numeric conditions (default: 0)
169
+ */
170
+ condition(parameter: string, mode: ConditionMode, threshold: number = 0): this {
171
+ if (!this._lastTransition) throw new Error("AnimatorControllerBuilder: .condition() must be called after .transition()");
172
+ this._lastTransition.conditions.push({ parameter, mode, threshold });
173
+ return this;
174
+ }
175
+
176
+ /**
177
+ * Sets which state is the default/entry state.
178
+ * If not called, the first added state is used.
179
+ * @param name - Name of the state
180
+ */
181
+ defaultState(name: string): this {
182
+ this._defaultStateName = name;
183
+ return this;
184
+ }
185
+
186
+ /**
187
+ * Builds and returns the {@link AnimatorController}.
188
+ * Resolves all state name references to indices.
189
+ */
190
+ build(): AnimatorController {
191
+ const stateIndexMap = new Map<string, number>();
192
+ this._states.forEach((s, i) => stateIndexMap.set(s.name, i));
193
+
194
+ let defaultStateIndex = 0;
195
+ if (this._defaultStateName !== null) {
196
+ const idx = stateIndexMap.get(this._defaultStateName);
197
+ if (idx === undefined) throw new Error(`AnimatorControllerBuilder: default state "${this._defaultStateName}" not found`);
198
+ defaultStateIndex = idx;
199
+ }
200
+
201
+ const resolveTransition = (t: BuilderTransition): Transition => {
202
+ const destIndex = stateIndexMap.get(t.to);
203
+ if (destIndex === undefined) throw new Error(`AnimatorControllerBuilder: transition target "${t.to}" not found`);
204
+ return {
205
+ exitTime: t.options.exitTime ?? 1,
206
+ hasExitTime: t.options.hasExitTime ?? false,
207
+ duration: t.options.duration ?? 0,
208
+ offset: t.options.offset ?? 0,
209
+ hasFixedDuration: t.options.hasFixedDuration ?? false,
210
+ destinationState: destIndex,
211
+ conditions: t.conditions.map(c => ({
212
+ parameter: c.parameter,
213
+ mode: conditionModeToEnum(c.mode),
214
+ threshold: c.threshold,
215
+ })),
216
+ };
217
+ };
218
+
219
+ const states: State[] = this._states.map((s, index) => {
220
+ const transitions: Transition[] = s.transitions.map(resolveTransition);
221
+
222
+ // Replicate any-state transitions onto every state (except self-targeting)
223
+ for (const anyT of this._anyStateTransitions) {
224
+ const destIndex = stateIndexMap.get(anyT.to);
225
+ if (destIndex === index) continue;
226
+ transitions.push(resolveTransition(anyT));
227
+ }
228
+
229
+ return {
230
+ name: s.name,
231
+ hash: index,
232
+ motion: {
233
+ name: s.options.clip.name,
234
+ clip: s.options.clip,
235
+ isLooping: s.options.loop ?? false,
236
+ },
237
+ transitions,
238
+ behaviours: [],
239
+ speed: s.options.speed,
240
+ speedParameter: s.options.speedParameter,
241
+ cycleOffset: s.options.cycleOffset,
242
+ cycleOffsetParameter: s.options.cycleOffsetParameter,
243
+ };
244
+ });
245
+
246
+ const model: AnimatorControllerModel = {
247
+ name: this._name,
248
+ guid: new InstantiateIdProvider(Date.now()).generateUUID(),
249
+ parameters: this._parameters,
250
+ layers: [{
251
+ name: "Base Layer",
252
+ stateMachine: {
253
+ defaultState: defaultStateIndex,
254
+ states,
255
+ }
256
+ }],
257
+ };
258
+
259
+ return new AnimatorController(model);
260
+ }
261
+ }