@needle-tools/engine 4.11.5-next.f5ee735 → 4.11.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 (1116) hide show
  1. package/CHANGELOG.md +3958 -3958
  2. package/LICENSE.md +10 -10
  3. package/README.md +84 -84
  4. package/components.needle.json +1 -1
  5. package/dist/generateMeshBVH.worker-DwpFkqPR.js +21 -0
  6. package/dist/{gltf-progressive-DZrY8VT6.min.js → gltf-progressive-BmSygnAC.min.js} +2 -2
  7. package/dist/{gltf-progressive-DgYz5BYa.js → gltf-progressive-DnLBuGK5.js} +24 -24
  8. package/dist/{gltf-progressive-DWcmTMCh.umd.cjs → gltf-progressive-Rs-ojtXy.umd.cjs} +1 -1
  9. package/dist/{loader.worker-Dip-PthR.js → loader.worker-DWzfDpAl.js} +4 -4
  10. package/dist/{needle-engine.bundle-CpC_7cRL.umd.cjs → needle-engine.bundle-B8HwiMM3.umd.cjs} +125 -133
  11. package/dist/{needle-engine.bundle-DtudWpG2.js → needle-engine.bundle-Cxvg_Ilj.js} +3622 -3735
  12. package/dist/{needle-engine.bundle-DvGMCIAT.min.js → needle-engine.bundle-PcoEsc5V.min.js} +113 -121
  13. package/dist/needle-engine.d.ts +176 -179
  14. package/dist/needle-engine.js +343 -344
  15. package/dist/needle-engine.min.js +1 -1
  16. package/dist/needle-engine.umd.cjs +1 -1
  17. package/dist/{postprocessing-Dzq4RXcy.min.js → postprocessing-B5ksn9-G.min.js} +223 -147
  18. package/dist/{postprocessing-DEkzT9iD.umd.cjs → postprocessing-DZtb9Nnn.umd.cjs} +222 -146
  19. package/dist/{postprocessing-DqdcvaFn.js → postprocessing-__7s9wON.js} +1656 -1542
  20. package/dist/{three-DfMvBzXi.js → three-BCCkyCA5.js} +1 -7
  21. package/dist/{three-qj71I7J3.umd.cjs → three-Bf2NBxAw.umd.cjs} +2 -2
  22. package/dist/{three-B7CT31Bt.min.js → three-W7zWTcfP.min.js} +1 -1
  23. package/dist/{three-examples-CsW4_6LI.umd.cjs → three-examples-Dho7cuu4.umd.cjs} +4 -4
  24. package/dist/{three-examples-D1P7eEhn.min.js → three-examples-MsJjauyk.min.js} +10 -10
  25. package/dist/{three-examples-D1SK93ek.js → three-examples-y2GeYlze.js} +2 -20
  26. package/dist/{three-mesh-ui-C_uSB5dD.js → three-mesh-ui-3nSSizT4.js} +1 -1
  27. package/dist/{three-mesh-ui-LQ44s0AL.min.js → three-mesh-ui-CIez6qJQ.min.js} +1 -1
  28. package/dist/{three-mesh-ui-DpATDXwU.umd.cjs → three-mesh-ui-zsOOA5Pq.umd.cjs} +1 -1
  29. package/dist/{vendor-DhTcel4c.umd.cjs → vendor-BrxSfR_8.umd.cjs} +38 -38
  30. package/dist/{vendor-DtTGRuXh.min.js → vendor-C0rT6Ytq.min.js} +45 -45
  31. package/dist/{vendor-Dkpn1a8s.js → vendor-CoLbzydV.js} +3068 -3134
  32. package/lib/asap/needle-asap.d.ts +1 -1
  33. package/lib/asap/needle-asap.js +95 -95
  34. package/lib/asap/sessiongranted.d.ts +3 -3
  35. package/lib/asap/sessiongranted.js +65 -65
  36. package/lib/asap/utils.d.ts +1 -1
  37. package/lib/asap/utils.js +3 -3
  38. package/lib/engine/analytics/index.d.ts +6 -6
  39. package/lib/engine/analytics/index.js +12 -12
  40. package/lib/engine/analytics/lcp.d.ts +3 -3
  41. package/lib/engine/analytics/lcp.js +34 -34
  42. package/lib/engine/api.d.ts +86 -86
  43. package/lib/engine/api.js +85 -85
  44. package/lib/engine/assets/index.d.ts +11 -11
  45. package/lib/engine/assets/index.js +47 -47
  46. package/lib/engine/assets/static.d.ts +1 -1
  47. package/lib/engine/assets/static.js +4 -4
  48. package/lib/engine/codegen/register_types.d.ts +1 -1
  49. package/lib/engine/codegen/register_types.js +320 -320
  50. package/lib/engine/debug/debug.d.ts +15 -15
  51. package/lib/engine/debug/debug.js +44 -44
  52. package/lib/engine/debug/debug_console.d.ts +2 -2
  53. package/lib/engine/debug/debug_console.js +277 -277
  54. package/lib/engine/debug/debug_overlay.d.ts +22 -22
  55. package/lib/engine/debug/debug_overlay.js +316 -316
  56. package/lib/engine/debug/debug_spatial_console.d.ts +2 -2
  57. package/lib/engine/debug/debug_spatial_console.js +390 -390
  58. package/lib/engine/debug/debug_spector.d.ts +16 -16
  59. package/lib/engine/debug/debug_spector.js +27 -27
  60. package/lib/engine/debug/index.d.ts +2 -2
  61. package/lib/engine/debug/index.js +2 -2
  62. package/lib/engine/engine_addressables.d.ts +232 -232
  63. package/lib/engine/engine_addressables.js +684 -684
  64. package/lib/engine/engine_animation.d.ts +61 -61
  65. package/lib/engine/engine_animation.js +170 -170
  66. package/lib/engine/engine_application.d.ts +45 -52
  67. package/lib/engine/engine_application.js +104 -111
  68. package/lib/engine/engine_application.js.map +1 -1
  69. package/lib/engine/engine_assetdatabase.d.ts +25 -25
  70. package/lib/engine/engine_assetdatabase.js +352 -352
  71. package/lib/engine/engine_audio.d.ts +4 -4
  72. package/lib/engine/engine_audio.js +23 -23
  73. package/lib/engine/engine_camera.d.ts +39 -39
  74. package/lib/engine/engine_camera.fit.d.ts +113 -113
  75. package/lib/engine/engine_camera.fit.js +194 -194
  76. package/lib/engine/engine_camera.js +102 -102
  77. package/lib/engine/engine_components.d.ts +110 -110
  78. package/lib/engine/engine_components.js +374 -374
  79. package/lib/engine/engine_components_internal.d.ts +9 -9
  80. package/lib/engine/engine_components_internal.js +36 -36
  81. package/lib/engine/engine_constants.d.ts +10 -10
  82. package/lib/engine/engine_constants.js +41 -41
  83. package/lib/engine/engine_context.d.ts +523 -523
  84. package/lib/engine/engine_context.js +1781 -1784
  85. package/lib/engine/engine_context.js.map +1 -1
  86. package/lib/engine/engine_context_registry.d.ts +71 -71
  87. package/lib/engine/engine_context_registry.js +117 -117
  88. package/lib/engine/engine_coroutine.d.ts +35 -35
  89. package/lib/engine/engine_coroutine.js +52 -52
  90. package/lib/engine/engine_create_objects.d.ts +119 -119
  91. package/lib/engine/engine_create_objects.js +344 -344
  92. package/lib/engine/engine_default_parameters.d.ts +2 -2
  93. package/lib/engine/engine_default_parameters.js +3 -3
  94. package/lib/engine/engine_editor-sync.d.ts +21 -21
  95. package/lib/engine/engine_editor-sync.js +4 -4
  96. package/lib/engine/engine_feature_flags.d.ts +3 -3
  97. package/lib/engine/engine_feature_flags.js +5 -5
  98. package/lib/engine/engine_fileloader.d.ts +2 -2
  99. package/lib/engine/engine_fileloader.js +8 -8
  100. package/lib/engine/engine_gameobject.d.ts +68 -68
  101. package/lib/engine/engine_gameobject.js +676 -676
  102. package/lib/engine/engine_generic_utils.d.ts +1 -1
  103. package/lib/engine/engine_generic_utils.js +13 -13
  104. package/lib/engine/engine_gizmos.d.ts +151 -151
  105. package/lib/engine/engine_gizmos.js +549 -549
  106. package/lib/engine/engine_gltf.d.ts +12 -12
  107. package/lib/engine/engine_gltf.js +15 -15
  108. package/lib/engine/engine_gltf_builtin_components.d.ts +11 -11
  109. package/lib/engine/engine_gltf_builtin_components.js +341 -341
  110. package/lib/engine/engine_gltf_builtin_components.js.map +1 -1
  111. package/lib/engine/engine_hot_reload.d.ts +8 -8
  112. package/lib/engine/engine_hot_reload.js +197 -197
  113. package/lib/engine/engine_input.d.ts +362 -362
  114. package/lib/engine/engine_input.js +1297 -1297
  115. package/lib/engine/engine_input_utils.d.ts +2 -2
  116. package/lib/engine/engine_input_utils.js +22 -22
  117. package/lib/engine/engine_instancing.d.ts +19 -19
  118. package/lib/engine/engine_instancing.js +39 -39
  119. package/lib/engine/engine_license.d.ts +11 -11
  120. package/lib/engine/engine_license.js +369 -369
  121. package/lib/engine/engine_lifecycle_api.d.ts +83 -83
  122. package/lib/engine/engine_lifecycle_api.js +106 -106
  123. package/lib/engine/engine_lifecycle_functions_internal.d.ts +32 -32
  124. package/lib/engine/engine_lifecycle_functions_internal.js +146 -146
  125. package/lib/engine/engine_lightdata.d.ts +23 -23
  126. package/lib/engine/engine_lightdata.js +103 -103
  127. package/lib/engine/engine_loaders.callbacks.d.ts +98 -98
  128. package/lib/engine/engine_loaders.callbacks.js +87 -87
  129. package/lib/engine/engine_loaders.d.ts +42 -42
  130. package/lib/engine/engine_loaders.gltf.d.ts +13 -13
  131. package/lib/engine/engine_loaders.gltf.js +62 -62
  132. package/lib/engine/engine_loaders.js +341 -341
  133. package/lib/engine/engine_lods.d.ts +37 -37
  134. package/lib/engine/engine_lods.js +162 -162
  135. package/lib/engine/engine_mainloop_utils.d.ts +33 -33
  136. package/lib/engine/engine_mainloop_utils.js +478 -478
  137. package/lib/engine/engine_math.d.ts +114 -114
  138. package/lib/engine/engine_math.js +247 -247
  139. package/lib/engine/engine_modules.d.ts +36 -36
  140. package/lib/engine/engine_modules.js +85 -85
  141. package/lib/engine/engine_networking.d.ts +260 -260
  142. package/lib/engine/engine_networking.js +764 -764
  143. package/lib/engine/engine_networking_auto.d.ts +24 -24
  144. package/lib/engine/engine_networking_auto.js +310 -310
  145. package/lib/engine/engine_networking_blob.d.ts +48 -48
  146. package/lib/engine/engine_networking_blob.js +228 -228
  147. package/lib/engine/engine_networking_files.d.ts +35 -35
  148. package/lib/engine/engine_networking_files.js +172 -172
  149. package/lib/engine/engine_networking_files_default_components.d.ts +6 -6
  150. package/lib/engine/engine_networking_files_default_components.js +42 -42
  151. package/lib/engine/engine_networking_instantiate.d.ts +100 -100
  152. package/lib/engine/engine_networking_instantiate.js +362 -362
  153. package/lib/engine/engine_networking_peer.d.ts +15 -15
  154. package/lib/engine/engine_networking_peer.js +132 -132
  155. package/lib/engine/engine_networking_streams.d.ts +123 -123
  156. package/lib/engine/engine_networking_streams.js +656 -656
  157. package/lib/engine/engine_networking_types.d.ts +22 -22
  158. package/lib/engine/engine_networking_types.js +7 -7
  159. package/lib/engine/engine_networking_utils.d.ts +2 -2
  160. package/lib/engine/engine_networking_utils.js +20 -20
  161. package/lib/engine/engine_networking_websocket.d.ts +1 -1
  162. package/lib/engine/engine_networking_websocket.js +2 -2
  163. package/lib/engine/engine_patcher.d.ts +10 -10
  164. package/lib/engine/engine_patcher.js +142 -142
  165. package/lib/engine/engine_physics.d.ts +164 -164
  166. package/lib/engine/engine_physics.js +687 -691
  167. package/lib/engine/engine_physics.js.map +1 -1
  168. package/lib/engine/engine_physics.types.d.ts +40 -40
  169. package/lib/engine/engine_physics.types.js +33 -33
  170. package/lib/engine/engine_physics_rapier.d.ts +156 -156
  171. package/lib/engine/engine_physics_rapier.js +1460 -1460
  172. package/lib/engine/engine_playerview.d.ts +26 -26
  173. package/lib/engine/engine_playerview.js +64 -64
  174. package/lib/engine/engine_pmrem.d.ts +8 -8
  175. package/lib/engine/engine_pmrem.js +68 -68
  176. package/lib/engine/engine_scenelighting.d.ts +82 -82
  177. package/lib/engine/engine_scenelighting.js +245 -245
  178. package/lib/engine/engine_serialization.d.ts +3 -3
  179. package/lib/engine/engine_serialization.js +3 -3
  180. package/lib/engine/engine_serialization_builtin_serializer.d.ts +72 -72
  181. package/lib/engine/engine_serialization_builtin_serializer.js +403 -403
  182. package/lib/engine/engine_serialization_core.d.ts +94 -94
  183. package/lib/engine/engine_serialization_core.js +607 -607
  184. package/lib/engine/engine_serialization_decorator.d.ts +23 -23
  185. package/lib/engine/engine_serialization_decorator.js +67 -67
  186. package/lib/engine/engine_setup.d.ts +1 -1
  187. package/lib/engine/engine_setup.js +2 -2
  188. package/lib/engine/engine_shaders.d.ts +53 -53
  189. package/lib/engine/engine_shaders.js +252 -252
  190. package/lib/engine/engine_shims.d.ts +4 -4
  191. package/lib/engine/engine_shims.js +24 -24
  192. package/lib/engine/engine_test_utils.d.ts +39 -39
  193. package/lib/engine/engine_test_utils.js +83 -83
  194. package/lib/engine/engine_texture.d.ts +28 -28
  195. package/lib/engine/engine_texture.js +64 -64
  196. package/lib/engine/engine_three_utils.d.ts +210 -210
  197. package/lib/engine/engine_three_utils.js +792 -792
  198. package/lib/engine/engine_time.d.ts +51 -51
  199. package/lib/engine/engine_time.js +82 -82
  200. package/lib/engine/engine_time_utils.d.ts +88 -88
  201. package/lib/engine/engine_time_utils.js +215 -215
  202. package/lib/engine/engine_tonemapping.d.ts +6 -6
  203. package/lib/engine/engine_tonemapping.js +198 -198
  204. package/lib/engine/engine_types.d.ts +585 -585
  205. package/lib/engine/engine_types.js +95 -95
  206. package/lib/engine/engine_typestore.d.ts +28 -28
  207. package/lib/engine/engine_typestore.js +55 -55
  208. package/lib/engine/engine_util_decorator.d.ts +13 -13
  209. package/lib/engine/engine_util_decorator.js +116 -116
  210. package/lib/engine/engine_utils.d.ts +227 -227
  211. package/lib/engine/engine_utils.js +821 -821
  212. package/lib/engine/engine_utils_attributes.d.ts +48 -48
  213. package/lib/engine/engine_utils_attributes.js +69 -69
  214. package/lib/engine/engine_utils_format.d.ts +24 -24
  215. package/lib/engine/engine_utils_format.js +245 -245
  216. package/lib/engine/engine_utils_qrcode.d.ts +23 -23
  217. package/lib/engine/engine_utils_qrcode.js +233 -233
  218. package/lib/engine/engine_utils_screenshot.d.ts +159 -159
  219. package/lib/engine/engine_utils_screenshot.js +522 -522
  220. package/lib/engine/engine_utils_screenshot.xr.d.ts +5 -5
  221. package/lib/engine/engine_utils_screenshot.xr.js +90 -90
  222. package/lib/engine/engine_xr.d.ts +1 -1
  223. package/lib/engine/engine_xr.js +1 -1
  224. package/lib/engine/export/gltf/Writers.d.ts +19 -19
  225. package/lib/engine/export/gltf/Writers.js +24 -24
  226. package/lib/engine/export/gltf/index.d.ts +11 -11
  227. package/lib/engine/export/gltf/index.js +123 -123
  228. package/lib/engine/export/index.d.ts +2 -2
  229. package/lib/engine/export/index.js +2 -2
  230. package/lib/engine/export/state.d.ts +7 -7
  231. package/lib/engine/export/state.js +17 -17
  232. package/lib/engine/export/utils.d.ts +2 -2
  233. package/lib/engine/export/utils.js +7 -7
  234. package/lib/engine/extensions/EXT_texture_exr.d.ts +8 -8
  235. package/lib/engine/extensions/EXT_texture_exr.js +32 -32
  236. package/lib/engine/extensions/NEEDLE_animator_controller_model.d.ts +122 -122
  237. package/lib/engine/extensions/NEEDLE_animator_controller_model.js +95 -95
  238. package/lib/engine/extensions/NEEDLE_components.d.ts +35 -35
  239. package/lib/engine/extensions/NEEDLE_components.js +239 -239
  240. package/lib/engine/extensions/NEEDLE_gameobject_data.d.ts +10 -10
  241. package/lib/engine/extensions/NEEDLE_gameobject_data.js +57 -57
  242. package/lib/engine/extensions/NEEDLE_lighting_settings.d.ts +37 -37
  243. package/lib/engine/extensions/NEEDLE_lighting_settings.js +160 -160
  244. package/lib/engine/extensions/NEEDLE_lightmaps.d.ts +18 -18
  245. package/lib/engine/extensions/NEEDLE_lightmaps.js +99 -99
  246. package/lib/engine/extensions/NEEDLE_persistent_assets.d.ts +11 -11
  247. package/lib/engine/extensions/NEEDLE_persistent_assets.js +63 -63
  248. package/lib/engine/extensions/NEEDLE_progressive.d.ts +1 -1
  249. package/lib/engine/extensions/NEEDLE_progressive.js +1 -1
  250. package/lib/engine/extensions/NEEDLE_render_objects.d.ts +13 -13
  251. package/lib/engine/extensions/NEEDLE_render_objects.js +159 -159
  252. package/lib/engine/extensions/NEEDLE_techniques_webgl.d.ts +38 -38
  253. package/lib/engine/extensions/NEEDLE_techniques_webgl.js +564 -564
  254. package/lib/engine/extensions/extension_resolver.d.ts +4 -4
  255. package/lib/engine/extensions/extension_resolver.js +1 -1
  256. package/lib/engine/extensions/extension_utils.d.ts +12 -12
  257. package/lib/engine/extensions/extension_utils.js +152 -152
  258. package/lib/engine/extensions/extensions.d.ts +32 -32
  259. package/lib/engine/extensions/extensions.js +113 -113
  260. package/lib/engine/extensions/index.d.ts +6 -6
  261. package/lib/engine/extensions/index.js +6 -6
  262. package/lib/engine/extensions/usage_tracker.d.ts +13 -13
  263. package/lib/engine/extensions/usage_tracker.js +65 -65
  264. package/lib/engine/js-extensions/Camera.d.ts +1 -1
  265. package/lib/engine/js-extensions/Camera.js +39 -39
  266. package/lib/engine/js-extensions/ExtensionUtils.d.ts +9 -9
  267. package/lib/engine/js-extensions/ExtensionUtils.js +67 -67
  268. package/lib/engine/js-extensions/Layers.d.ts +6 -6
  269. package/lib/engine/js-extensions/Layers.js +22 -22
  270. package/lib/engine/js-extensions/Object3D.d.ts +141 -141
  271. package/lib/engine/js-extensions/Object3D.js +190 -190
  272. package/lib/engine/js-extensions/RGBAColor.d.ts +23 -23
  273. package/lib/engine/js-extensions/RGBAColor.js +111 -111
  274. package/lib/engine/js-extensions/Vector.d.ts +8 -8
  275. package/lib/engine/js-extensions/Vector.js +13 -13
  276. package/lib/engine/js-extensions/index.d.ts +6 -6
  277. package/lib/engine/js-extensions/index.js +5 -5
  278. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.d.ts +4 -4
  279. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +80 -80
  280. package/lib/engine/shaders/shaderData.d.ts +55 -55
  281. package/lib/engine/shaders/shaderData.js +58 -58
  282. package/lib/engine/tests/test_utils.d.ts +2 -2
  283. package/lib/engine/tests/test_utils.js +53 -53
  284. package/lib/engine/webcomponents/WebXRButtons.d.ts +56 -56
  285. package/lib/engine/webcomponents/WebXRButtons.js +230 -230
  286. package/lib/engine/webcomponents/api.d.ts +5 -5
  287. package/lib/engine/webcomponents/api.js +4 -4
  288. package/lib/engine/webcomponents/buttons.d.ts +53 -53
  289. package/lib/engine/webcomponents/buttons.js +270 -270
  290. package/lib/engine/webcomponents/fonts.d.ts +9 -9
  291. package/lib/engine/webcomponents/fonts.js +32 -32
  292. package/lib/engine/webcomponents/icons.d.ts +9 -9
  293. package/lib/engine/webcomponents/icons.js +52 -52
  294. package/lib/engine/webcomponents/index.d.ts +1 -1
  295. package/lib/engine/webcomponents/index.js +1 -1
  296. package/lib/engine/webcomponents/logo-element.d.ts +10 -10
  297. package/lib/engine/webcomponents/logo-element.js +91 -91
  298. package/lib/engine/webcomponents/needle menu/needle-menu-spatial.d.ts +37 -37
  299. package/lib/engine/webcomponents/needle menu/needle-menu-spatial.js +513 -513
  300. package/lib/engine/webcomponents/needle menu/needle-menu.d.ts +192 -192
  301. package/lib/engine/webcomponents/needle menu/needle-menu.js +1077 -1077
  302. package/lib/engine/webcomponents/needle-button.d.ts +34 -34
  303. package/lib/engine/webcomponents/needle-button.js +161 -161
  304. package/lib/engine/webcomponents/needle-engine.ar-overlay.d.ts +21 -21
  305. package/lib/engine/webcomponents/needle-engine.ar-overlay.js +166 -166
  306. package/lib/engine/webcomponents/needle-engine.attributes.d.ts +70 -70
  307. package/lib/engine/webcomponents/needle-engine.attributes.js +1 -1
  308. package/lib/engine/webcomponents/needle-engine.d.ts +116 -116
  309. package/lib/engine/webcomponents/needle-engine.extras.d.ts +6 -6
  310. package/lib/engine/webcomponents/needle-engine.extras.js +13 -13
  311. package/lib/engine/webcomponents/needle-engine.js +906 -906
  312. package/lib/engine/webcomponents/needle-engine.loading.d.ts +44 -44
  313. package/lib/engine/webcomponents/needle-engine.loading.js +336 -336
  314. package/lib/engine/xr/NeedleXRController.d.ts +314 -314
  315. package/lib/engine/xr/NeedleXRController.js +1057 -1057
  316. package/lib/engine/xr/NeedleXRSession.d.ts +342 -342
  317. package/lib/engine/xr/NeedleXRSession.js +1491 -1492
  318. package/lib/engine/xr/NeedleXRSession.js.map +1 -1
  319. package/lib/engine/xr/NeedleXRSync.d.ts +22 -22
  320. package/lib/engine/xr/NeedleXRSync.js +188 -188
  321. package/lib/engine/xr/SceneTransition.d.ts +18 -18
  322. package/lib/engine/xr/SceneTransition.js +69 -69
  323. package/lib/engine/xr/TempXRContext.d.ts +34 -34
  324. package/lib/engine/xr/TempXRContext.js +187 -187
  325. package/lib/engine/xr/XRRig.d.ts +7 -7
  326. package/lib/engine/xr/XRRig.js +1 -1
  327. package/lib/engine/xr/api.d.ts +6 -6
  328. package/lib/engine/xr/api.js +6 -6
  329. package/lib/engine/xr/events.d.ts +66 -66
  330. package/lib/engine/xr/events.js +93 -93
  331. package/lib/engine/xr/internal.d.ts +12 -12
  332. package/lib/engine/xr/internal.js +25 -25
  333. package/lib/engine/xr/usdz.d.ts +12 -12
  334. package/lib/engine/xr/usdz.js +29 -29
  335. package/lib/engine/xr/utils.d.ts +11 -11
  336. package/lib/engine/xr/utils.js +34 -34
  337. package/lib/engine-components/AlignmentConstraint.d.ts +10 -10
  338. package/lib/engine-components/AlignmentConstraint.js +39 -39
  339. package/lib/engine-components/Animation.d.ts +157 -157
  340. package/lib/engine-components/Animation.js +518 -518
  341. package/lib/engine-components/AnimationCurve.d.ts +43 -43
  342. package/lib/engine-components/AnimationCurve.js +162 -162
  343. package/lib/engine-components/AnimationUtils.d.ts +8 -8
  344. package/lib/engine-components/AnimationUtils.js +27 -27
  345. package/lib/engine-components/AnimationUtilsAutoplay.d.ts +1 -1
  346. package/lib/engine-components/AnimationUtilsAutoplay.js +34 -34
  347. package/lib/engine-components/Animator.d.ts +218 -218
  348. package/lib/engine-components/Animator.js +355 -355
  349. package/lib/engine-components/AnimatorController.d.ts +230 -230
  350. package/lib/engine-components/AnimatorController.js +1171 -1171
  351. package/lib/engine-components/AudioListener.d.ts +33 -33
  352. package/lib/engine-components/AudioListener.js +86 -86
  353. package/lib/engine-components/AudioSource.d.ts +217 -217
  354. package/lib/engine-components/AudioSource.js +635 -635
  355. package/lib/engine-components/AvatarLoader.d.ts +80 -80
  356. package/lib/engine-components/AvatarLoader.js +231 -231
  357. package/lib/engine-components/AxesHelper.d.ts +32 -32
  358. package/lib/engine-components/AxesHelper.js +67 -67
  359. package/lib/engine-components/BasicIKConstraint.d.ts +9 -9
  360. package/lib/engine-components/BasicIKConstraint.js +43 -43
  361. package/lib/engine-components/BoxCollider.d.ts +2 -2
  362. package/lib/engine-components/BoxCollider.js +2 -2
  363. package/lib/engine-components/BoxHelperComponent.d.ts +47 -47
  364. package/lib/engine-components/BoxHelperComponent.js +102 -102
  365. package/lib/engine-components/Camera.d.ts +233 -233
  366. package/lib/engine-components/Camera.js +704 -704
  367. package/lib/engine-components/CameraUtils.d.ts +1 -1
  368. package/lib/engine-components/CameraUtils.js +118 -118
  369. package/lib/engine-components/CharacterController.d.ts +55 -55
  370. package/lib/engine-components/CharacterController.js +236 -236
  371. package/lib/engine-components/Collider.d.ts +214 -214
  372. package/lib/engine-components/Collider.js +395 -395
  373. package/lib/engine-components/Component.d.ts +796 -796
  374. package/lib/engine-components/Component.js +923 -923
  375. package/lib/engine-components/ContactShadows.d.ts +113 -113
  376. package/lib/engine-components/ContactShadows.js +482 -482
  377. package/lib/engine-components/DeleteBox.d.ts +19 -19
  378. package/lib/engine-components/DeleteBox.js +58 -58
  379. package/lib/engine-components/DeviceFlag.d.ts +16 -16
  380. package/lib/engine-components/DeviceFlag.js +47 -47
  381. package/lib/engine-components/DragControls.d.ts +170 -170
  382. package/lib/engine-components/DragControls.js +1420 -1420
  383. package/lib/engine-components/DropListener.d.ts +224 -224
  384. package/lib/engine-components/DropListener.js +669 -669
  385. package/lib/engine-components/Duplicatable.d.ts +35 -35
  386. package/lib/engine-components/Duplicatable.js +205 -205
  387. package/lib/engine-components/EventList.d.ts +71 -71
  388. package/lib/engine-components/EventList.js +249 -249
  389. package/lib/engine-components/EventTrigger.d.ts +33 -33
  390. package/lib/engine-components/EventTrigger.js +75 -75
  391. package/lib/engine-components/EventType.d.ts +22 -22
  392. package/lib/engine-components/EventType.js +23 -23
  393. package/lib/engine-components/Fog.d.ts +22 -22
  394. package/lib/engine-components/Fog.js +61 -61
  395. package/lib/engine-components/Gizmos.d.ts +17 -17
  396. package/lib/engine-components/Gizmos.js +64 -64
  397. package/lib/engine-components/GridHelper.d.ts +20 -20
  398. package/lib/engine-components/GridHelper.js +54 -54
  399. package/lib/engine-components/GroundProjection.d.ts +70 -70
  400. package/lib/engine-components/GroundProjection.js +346 -346
  401. package/lib/engine-components/Interactable.d.ts +16 -16
  402. package/lib/engine-components/Interactable.js +16 -16
  403. package/lib/engine-components/Joints.d.ts +19 -19
  404. package/lib/engine-components/Joints.js +51 -51
  405. package/lib/engine-components/LODGroup.d.ts +35 -35
  406. package/lib/engine-components/LODGroup.js +152 -152
  407. package/lib/engine-components/Light.d.ts +180 -180
  408. package/lib/engine-components/Light.js +535 -535
  409. package/lib/engine-components/LookAtConstraint.d.ts +27 -27
  410. package/lib/engine-components/LookAtConstraint.js +47 -47
  411. package/lib/engine-components/NeedleMenu.d.ts +51 -51
  412. package/lib/engine-components/NeedleMenu.js +93 -93
  413. package/lib/engine-components/NestedGltf.d.ts +33 -33
  414. package/lib/engine-components/NestedGltf.js +97 -97
  415. package/lib/engine-components/Networking.d.ts +54 -54
  416. package/lib/engine-components/Networking.js +112 -112
  417. package/lib/engine-components/OffsetConstraint.d.ts +14 -14
  418. package/lib/engine-components/OffsetConstraint.js +65 -65
  419. package/lib/engine-components/OrbitControls.d.ts +315 -315
  420. package/lib/engine-components/OrbitControls.js +1046 -1046
  421. package/lib/engine-components/PlayerColor.d.ts +19 -19
  422. package/lib/engine-components/PlayerColor.js +94 -94
  423. package/lib/engine-components/ReflectionProbe.d.ts +32 -32
  424. package/lib/engine-components/ReflectionProbe.js +214 -214
  425. package/lib/engine-components/Renderer.d.ts +154 -154
  426. package/lib/engine-components/Renderer.js +824 -824
  427. package/lib/engine-components/RendererInstancing.d.ts +140 -142
  428. package/lib/engine-components/RendererInstancing.js +744 -777
  429. package/lib/engine-components/RendererInstancing.js.map +1 -1
  430. package/lib/engine-components/RendererLightmap.d.ts +33 -33
  431. package/lib/engine-components/RendererLightmap.js +192 -192
  432. package/lib/engine-components/RigidBody.d.ts +160 -160
  433. package/lib/engine-components/RigidBody.js +522 -522
  434. package/lib/engine-components/SceneSwitcher.d.ts +263 -263
  435. package/lib/engine-components/SceneSwitcher.js +971 -971
  436. package/lib/engine-components/ScreenCapture.d.ts +144 -144
  437. package/lib/engine-components/ScreenCapture.js +547 -547
  438. package/lib/engine-components/SeeThrough.d.ts +73 -73
  439. package/lib/engine-components/SeeThrough.js +249 -249
  440. package/lib/engine-components/ShadowCatcher.d.ts +33 -33
  441. package/lib/engine-components/ShadowCatcher.js +166 -166
  442. package/lib/engine-components/Skybox.d.ts +90 -100
  443. package/lib/engine-components/Skybox.js +426 -438
  444. package/lib/engine-components/Skybox.js.map +1 -1
  445. package/lib/engine-components/SmoothFollow.d.ts +34 -34
  446. package/lib/engine-components/SmoothFollow.js +82 -82
  447. package/lib/engine-components/SpatialTrigger.d.ts +102 -102
  448. package/lib/engine-components/SpatialTrigger.js +225 -225
  449. package/lib/engine-components/SpectatorCamera.d.ts +111 -111
  450. package/lib/engine-components/SpectatorCamera.js +715 -715
  451. package/lib/engine-components/SphereCollider.d.ts +2 -2
  452. package/lib/engine-components/SphereCollider.js +2 -2
  453. package/lib/engine-components/SpriteRenderer.d.ts +145 -145
  454. package/lib/engine-components/SpriteRenderer.js +491 -491
  455. package/lib/engine-components/SyncedCamera.d.ts +41 -41
  456. package/lib/engine-components/SyncedCamera.js +199 -199
  457. package/lib/engine-components/SyncedRoom.d.ts +106 -106
  458. package/lib/engine-components/SyncedRoom.js +371 -371
  459. package/lib/engine-components/SyncedTransform.d.ts +94 -94
  460. package/lib/engine-components/SyncedTransform.js +331 -331
  461. package/lib/engine-components/TestRunner.d.ts +16 -16
  462. package/lib/engine-components/TestRunner.js +102 -102
  463. package/lib/engine-components/TransformGizmo.d.ts +75 -75
  464. package/lib/engine-components/TransformGizmo.js +217 -217
  465. package/lib/engine-components/VideoPlayer.d.ts +184 -184
  466. package/lib/engine-components/VideoPlayer.js +978 -978
  467. package/lib/engine-components/Voip.d.ts +67 -67
  468. package/lib/engine-components/Voip.js +360 -360
  469. package/lib/engine-components/api.d.ts +53 -53
  470. package/lib/engine-components/api.js +52 -52
  471. package/lib/engine-components/avatar/AvatarBlink_Simple.d.ts +11 -11
  472. package/lib/engine-components/avatar/AvatarBlink_Simple.js +76 -76
  473. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.d.ts +14 -14
  474. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js +68 -68
  475. package/lib/engine-components/avatar/Avatar_Brain_LookAt.d.ts +29 -29
  476. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js +121 -121
  477. package/lib/engine-components/avatar/Avatar_MouthShapes.d.ts +15 -15
  478. package/lib/engine-components/avatar/Avatar_MouthShapes.js +79 -79
  479. package/lib/engine-components/avatar/Avatar_MustacheShake.d.ts +9 -9
  480. package/lib/engine-components/avatar/Avatar_MustacheShake.js +29 -29
  481. package/lib/engine-components/codegen/components.d.ts +227 -227
  482. package/lib/engine-components/codegen/components.js +229 -229
  483. package/lib/engine-components/debug/LogStats.d.ts +6 -6
  484. package/lib/engine-components/debug/LogStats.js +19 -19
  485. package/lib/engine-components/export/gltf/GltfExport.d.ts +30 -30
  486. package/lib/engine-components/export/gltf/GltfExport.js +246 -246
  487. package/lib/engine-components/export/gltf/index.d.ts +1 -1
  488. package/lib/engine-components/export/gltf/index.js +1 -1
  489. package/lib/engine-components/export/index.d.ts +1 -1
  490. package/lib/engine-components/export/index.js +1 -1
  491. package/lib/engine-components/export/usdz/Extension.d.ts +22 -22
  492. package/lib/engine-components/export/usdz/Extension.js +1 -1
  493. package/lib/engine-components/export/usdz/ThreeUSDZExporter.d.ts +164 -164
  494. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +1847 -1847
  495. package/lib/engine-components/export/usdz/USDZExporter.d.ts +137 -137
  496. package/lib/engine-components/export/usdz/USDZExporter.js +669 -669
  497. package/lib/engine-components/export/usdz/extensions/Animation.d.ts +106 -106
  498. package/lib/engine-components/export/usdz/extensions/Animation.js +1071 -1071
  499. package/lib/engine-components/export/usdz/extensions/DocumentExtension.d.ts +5 -5
  500. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js +6 -6
  501. package/lib/engine-components/export/usdz/extensions/NodeMaterialConverter.d.ts +10 -10
  502. package/lib/engine-components/export/usdz/extensions/NodeMaterialConverter.js +451 -451
  503. package/lib/engine-components/export/usdz/extensions/USDZText.d.ts +54 -54
  504. package/lib/engine-components/export/usdz/extensions/USDZText.js +203 -203
  505. package/lib/engine-components/export/usdz/extensions/USDZUI.d.ts +8 -8
  506. package/lib/engine-components/export/usdz/extensions/USDZUI.js +158 -158
  507. package/lib/engine-components/export/usdz/extensions/behavior/Actions.d.ts +30 -30
  508. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js +88 -88
  509. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.d.ts +10 -10
  510. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.js +86 -86
  511. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.d.ts +28 -28
  512. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js +290 -290
  513. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.d.ts +198 -198
  514. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js +1084 -1084
  515. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.d.ts +135 -135
  516. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js +548 -548
  517. package/lib/engine-components/export/usdz/extensions/behavior/PhysicsExtension.d.ts +7 -7
  518. package/lib/engine-components/export/usdz/extensions/behavior/PhysicsExtension.js +115 -115
  519. package/lib/engine-components/export/usdz/index.d.ts +3 -3
  520. package/lib/engine-components/export/usdz/index.js +2 -2
  521. package/lib/engine-components/export/usdz/utils/animationutils.d.ts +7 -7
  522. package/lib/engine-components/export/usdz/utils/animationutils.js +163 -163
  523. package/lib/engine-components/export/usdz/utils/quicklook.d.ts +2 -2
  524. package/lib/engine-components/export/usdz/utils/quicklook.js +43 -43
  525. package/lib/engine-components/particlesystem/ParticleSystem.d.ts +177 -177
  526. package/lib/engine-components/particlesystem/ParticleSystem.js +1176 -1176
  527. package/lib/engine-components/particlesystem/ParticleSystemModules.d.ts +526 -526
  528. package/lib/engine-components/particlesystem/ParticleSystemModules.js +1930 -1930
  529. package/lib/engine-components/particlesystem/ParticleSystemSubEmitter.d.ts +25 -25
  530. package/lib/engine-components/particlesystem/ParticleSystemSubEmitter.js +87 -87
  531. package/lib/engine-components/particlesystem/api.d.ts +2 -2
  532. package/lib/engine-components/particlesystem/api.js +2 -2
  533. package/lib/engine-components/physics/Attractor.d.ts +20 -20
  534. package/lib/engine-components/physics/Attractor.js +53 -53
  535. package/lib/engine-components/postprocessing/Effects/Antialiasing.d.ts +17 -17
  536. package/lib/engine-components/postprocessing/Effects/Antialiasing.js +59 -59
  537. package/lib/engine-components/postprocessing/Effects/BloomEffect.d.ts +46 -46
  538. package/lib/engine-components/postprocessing/Effects/BloomEffect.js +113 -113
  539. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.d.ts +11 -11
  540. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.js +39 -39
  541. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.d.ts +23 -23
  542. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.js +111 -111
  543. package/lib/engine-components/postprocessing/Effects/DepthOfField.d.ts +25 -25
  544. package/lib/engine-components/postprocessing/Effects/DepthOfField.js +104 -104
  545. package/lib/engine-components/postprocessing/Effects/EffectWrapper.d.ts +12 -12
  546. package/lib/engine-components/postprocessing/Effects/EffectWrapper.js +18 -18
  547. package/lib/engine-components/postprocessing/Effects/Pixelation.d.ts +11 -11
  548. package/lib/engine-components/postprocessing/Effects/Pixelation.js +32 -32
  549. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.d.ts +18 -18
  550. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.js +91 -91
  551. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.d.ts +70 -70
  552. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.js +176 -176
  553. package/lib/engine-components/postprocessing/Effects/Sharpening.d.ts +18 -18
  554. package/lib/engine-components/postprocessing/Effects/Sharpening.js +127 -127
  555. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.d.ts +17 -17
  556. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.js +70 -70
  557. package/lib/engine-components/postprocessing/Effects/Tonemapping.d.ts +19 -19
  558. package/lib/engine-components/postprocessing/Effects/Tonemapping.js +94 -94
  559. package/lib/engine-components/postprocessing/Effects/Tonemapping.utils.d.ts +13 -13
  560. package/lib/engine-components/postprocessing/Effects/Tonemapping.utils.js +51 -51
  561. package/lib/engine-components/postprocessing/Effects/Vignette.d.ts +15 -15
  562. package/lib/engine-components/postprocessing/Effects/Vignette.js +60 -60
  563. package/lib/engine-components/postprocessing/PostProcessingEffect.d.ts +90 -90
  564. package/lib/engine-components/postprocessing/PostProcessingEffect.js +168 -168
  565. package/lib/engine-components/postprocessing/PostProcessingHandler.d.ts +43 -43
  566. package/lib/engine-components/postprocessing/PostProcessingHandler.js +502 -502
  567. package/lib/engine-components/postprocessing/Volume.d.ts +92 -92
  568. package/lib/engine-components/postprocessing/Volume.js +387 -387
  569. package/lib/engine-components/postprocessing/VolumeParameter.d.ts +26 -26
  570. package/lib/engine-components/postprocessing/VolumeParameter.js +136 -136
  571. package/lib/engine-components/postprocessing/VolumeProfile.d.ts +15 -15
  572. package/lib/engine-components/postprocessing/VolumeProfile.js +60 -60
  573. package/lib/engine-components/postprocessing/index.d.ts +6 -6
  574. package/lib/engine-components/postprocessing/index.js +6 -6
  575. package/lib/engine-components/postprocessing/utils.d.ts +55 -55
  576. package/lib/engine-components/postprocessing/utils.js +119 -119
  577. package/lib/engine-components/splines/Spline.d.ts +61 -61
  578. package/lib/engine-components/splines/Spline.js +272 -272
  579. package/lib/engine-components/splines/SplineUtils.d.ts +15 -15
  580. package/lib/engine-components/splines/SplineUtils.js +35 -35
  581. package/lib/engine-components/splines/SplineWalker.d.ts +89 -89
  582. package/lib/engine-components/splines/SplineWalker.js +192 -192
  583. package/lib/engine-components/splines/index.d.ts +3 -3
  584. package/lib/engine-components/splines/index.js +3 -3
  585. package/lib/engine-components/timeline/PlayableDirector.d.ts +193 -193
  586. package/lib/engine-components/timeline/PlayableDirector.js +734 -734
  587. package/lib/engine-components/timeline/SignalAsset.d.ts +34 -34
  588. package/lib/engine-components/timeline/SignalAsset.js +140 -140
  589. package/lib/engine-components/timeline/TimelineModels.d.ts +135 -135
  590. package/lib/engine-components/timeline/TimelineModels.js +28 -28
  591. package/lib/engine-components/timeline/TimelineTracks.d.ts +118 -118
  592. package/lib/engine-components/timeline/TimelineTracks.js +903 -903
  593. package/lib/engine-components/timeline/index.d.ts +4 -4
  594. package/lib/engine-components/timeline/index.js +3 -3
  595. package/lib/engine-components/ui/BaseUIComponent.d.ts +48 -48
  596. package/lib/engine-components/ui/BaseUIComponent.js +170 -170
  597. package/lib/engine-components/ui/Button.d.ts +64 -64
  598. package/lib/engine-components/ui/Button.js +315 -315
  599. package/lib/engine-components/ui/Canvas.d.ts +74 -74
  600. package/lib/engine-components/ui/Canvas.js +407 -407
  601. package/lib/engine-components/ui/CanvasGroup.d.ts +19 -19
  602. package/lib/engine-components/ui/CanvasGroup.js +58 -58
  603. package/lib/engine-components/ui/EventSystem.d.ts +125 -125
  604. package/lib/engine-components/ui/EventSystem.js +764 -764
  605. package/lib/engine-components/ui/Graphic.d.ts +55 -55
  606. package/lib/engine-components/ui/Graphic.js +267 -267
  607. package/lib/engine-components/ui/Image.d.ts +35 -35
  608. package/lib/engine-components/ui/Image.js +116 -116
  609. package/lib/engine-components/ui/InputField.d.ts +42 -42
  610. package/lib/engine-components/ui/InputField.js +268 -268
  611. package/lib/engine-components/ui/Interfaces.d.ts +38 -38
  612. package/lib/engine-components/ui/Interfaces.js +12 -12
  613. package/lib/engine-components/ui/Layout.d.ts +84 -84
  614. package/lib/engine-components/ui/Layout.js +330 -330
  615. package/lib/engine-components/ui/Outline.d.ts +7 -7
  616. package/lib/engine-components/ui/Outline.js +20 -20
  617. package/lib/engine-components/ui/PointerEvents.d.ts +115 -115
  618. package/lib/engine-components/ui/PointerEvents.js +145 -145
  619. package/lib/engine-components/ui/RaycastUtils.d.ts +11 -11
  620. package/lib/engine-components/ui/RaycastUtils.js +69 -69
  621. package/lib/engine-components/ui/Raycaster.d.ts +48 -48
  622. package/lib/engine-components/ui/Raycaster.js +113 -113
  623. package/lib/engine-components/ui/RectTransform.d.ts +61 -61
  624. package/lib/engine-components/ui/RectTransform.js +356 -356
  625. package/lib/engine-components/ui/SpatialHtml.d.ts +8 -8
  626. package/lib/engine-components/ui/SpatialHtml.js +79 -79
  627. package/lib/engine-components/ui/Symbols.d.ts +1 -1
  628. package/lib/engine-components/ui/Symbols.js +1 -1
  629. package/lib/engine-components/ui/Text.d.ts +78 -78
  630. package/lib/engine-components/ui/Text.js +544 -544
  631. package/lib/engine-components/ui/Utils.d.ts +24 -24
  632. package/lib/engine-components/ui/Utils.js +90 -90
  633. package/lib/engine-components/ui/index.d.ts +1 -1
  634. package/lib/engine-components/ui/index.js +1 -1
  635. package/lib/engine-components/utils/EnvironmentScene.d.ts +5 -5
  636. package/lib/engine-components/utils/EnvironmentScene.js +205 -205
  637. package/lib/engine-components/utils/LookAt.d.ts +34 -34
  638. package/lib/engine-components/utils/LookAt.js +91 -91
  639. package/lib/engine-components/utils/OpenURL.d.ts +43 -43
  640. package/lib/engine-components/utils/OpenURL.js +120 -120
  641. package/lib/engine-components/web/Clickthrough.d.ts +26 -26
  642. package/lib/engine-components/web/Clickthrough.js +92 -92
  643. package/lib/engine-components/web/CursorFollow.d.ts +44 -44
  644. package/lib/engine-components/web/CursorFollow.js +132 -132
  645. package/lib/engine-components/web/HoverAnimation.d.ts +47 -47
  646. package/lib/engine-components/web/HoverAnimation.js +107 -107
  647. package/lib/engine-components/web/ScrollFollow.d.ts +128 -128
  648. package/lib/engine-components/web/ScrollFollow.js +430 -430
  649. package/lib/engine-components/web/ViewBox.d.ts +61 -61
  650. package/lib/engine-components/web/ViewBox.js +300 -300
  651. package/lib/engine-components/web/index.d.ts +5 -5
  652. package/lib/engine-components/web/index.js +5 -5
  653. package/lib/engine-components/webxr/Avatar.d.ts +25 -25
  654. package/lib/engine-components/webxr/Avatar.js +255 -255
  655. package/lib/engine-components/webxr/TeleportTarget.d.ts +10 -10
  656. package/lib/engine-components/webxr/TeleportTarget.js +10 -10
  657. package/lib/engine-components/webxr/WebARCameraBackground.d.ts +35 -35
  658. package/lib/engine-components/webxr/WebARCameraBackground.js +160 -160
  659. package/lib/engine-components/webxr/WebARSessionRoot.d.ts +99 -99
  660. package/lib/engine-components/webxr/WebARSessionRoot.js +772 -772
  661. package/lib/engine-components/webxr/WebXR.d.ts +259 -259
  662. package/lib/engine-components/webxr/WebXR.js +588 -592
  663. package/lib/engine-components/webxr/WebXR.js.map +1 -1
  664. package/lib/engine-components/webxr/WebXRAvatar.d.ts +27 -27
  665. package/lib/engine-components/webxr/WebXRAvatar.js +44 -44
  666. package/lib/engine-components/webxr/WebXRImageTracking.d.ts +139 -139
  667. package/lib/engine-components/webxr/WebXRImageTracking.js +590 -590
  668. package/lib/engine-components/webxr/WebXRPlaneTracking.d.ts +92 -92
  669. package/lib/engine-components/webxr/WebXRPlaneTracking.js +500 -500
  670. package/lib/engine-components/webxr/WebXRRig.d.ts +36 -36
  671. package/lib/engine-components/webxr/WebXRRig.js +76 -76
  672. package/lib/engine-components/webxr/XRFlag.d.ts +41 -41
  673. package/lib/engine-components/webxr/XRFlag.js +142 -142
  674. package/lib/engine-components/webxr/controllers/XRControllerFollow.d.ts +59 -59
  675. package/lib/engine-components/webxr/controllers/XRControllerFollow.js +132 -132
  676. package/lib/engine-components/webxr/controllers/XRControllerModel.d.ts +46 -46
  677. package/lib/engine-components/webxr/controllers/XRControllerModel.js +355 -355
  678. package/lib/engine-components/webxr/controllers/XRControllerMovement.d.ts +85 -85
  679. package/lib/engine-components/webxr/controllers/XRControllerMovement.js +517 -517
  680. package/lib/engine-components/webxr/index.d.ts +3 -3
  681. package/lib/engine-components/webxr/index.js +3 -3
  682. package/lib/engine-components/webxr/types.d.ts +3 -3
  683. package/lib/engine-components/webxr/types.js +1 -1
  684. package/lib/engine-components-experimental/Presentation.d.ts +7 -7
  685. package/lib/engine-components-experimental/Presentation.js +10 -10
  686. package/lib/engine-components-experimental/api.d.ts +4 -4
  687. package/lib/engine-components-experimental/api.js +4 -4
  688. package/lib/engine-components-experimental/networking/PlayerSync.d.ts +156 -156
  689. package/lib/engine-components-experimental/networking/PlayerSync.js +377 -377
  690. package/lib/engine-schemes/api.d.ts +12 -12
  691. package/lib/engine-schemes/api.js +12 -12
  692. package/lib/engine-schemes/schemes.d.ts +7 -7
  693. package/lib/engine-schemes/schemes.js +19 -19
  694. package/lib/engine-schemes/synced-camera-model.d.ts +25 -25
  695. package/lib/engine-schemes/synced-camera-model.js +67 -67
  696. package/lib/engine-schemes/synced-transform-model.d.ts +31 -31
  697. package/lib/engine-schemes/synced-transform-model.js +66 -66
  698. package/lib/engine-schemes/transform.d.ts +12 -12
  699. package/lib/engine-schemes/transform.js +39 -39
  700. package/lib/engine-schemes/vec2.d.ts +10 -10
  701. package/lib/engine-schemes/vec2.js +25 -25
  702. package/lib/engine-schemes/vec3.d.ts +11 -11
  703. package/lib/engine-schemes/vec3.js +29 -29
  704. package/lib/engine-schemes/vec4.d.ts +12 -12
  705. package/lib/engine-schemes/vec4.js +33 -33
  706. package/lib/engine-schemes/vr-user-state-buffer.d.ts +37 -37
  707. package/lib/engine-schemes/vr-user-state-buffer.js +110 -110
  708. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.d.ts +6 -6
  709. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.js +45 -45
  710. package/lib/needle-engine.d.ts +7 -7
  711. package/lib/needle-engine.js +64 -65
  712. package/lib/needle-engine.js.map +1 -1
  713. package/package.json +3 -3
  714. package/plugins/common/buildinfo.js +64 -64
  715. package/plugins/common/cloud.js +1 -1
  716. package/plugins/common/config.cjs +31 -31
  717. package/plugins/common/config.js +35 -35
  718. package/plugins/common/files.js +34 -34
  719. package/plugins/common/generator.js +10 -10
  720. package/plugins/common/license.js +452 -452
  721. package/plugins/common/logger.js +345 -345
  722. package/plugins/common/needle-engine.js +40 -40
  723. package/plugins/common/npm.js +15 -15
  724. package/plugins/common/timers.js +7 -7
  725. package/plugins/common/version.js +37 -37
  726. package/plugins/common/worker.js +128 -128
  727. package/plugins/gltf-packer.mjs +1 -1
  728. package/plugins/next/alias.cjs +39 -39
  729. package/plugins/next/license.cjs +24 -24
  730. package/plugins/next/meshbvhworker.cjs +18 -18
  731. package/plugins/next/next.js +141 -141
  732. package/plugins/types/index.d.ts +2 -2
  733. package/plugins/types/license.d.ts +24 -24
  734. package/plugins/types/needleConfig.d.ts +27 -27
  735. package/plugins/types/next.d.ts +2 -2
  736. package/plugins/types/userconfig.d.ts +131 -131
  737. package/plugins/types/vite.d.ts +13 -13
  738. package/plugins/types/webmanifest.d.ts +32 -32
  739. package/plugins/vite/alias.js +214 -214
  740. package/plugins/vite/asap.js +233 -233
  741. package/plugins/vite/build-pipeline.js +379 -379
  742. package/plugins/vite/build.js +22 -22
  743. package/plugins/vite/buildinfo.js +41 -41
  744. package/plugins/vite/config.js +106 -106
  745. package/plugins/vite/copyfiles.js +138 -138
  746. package/plugins/vite/defines.js +70 -70
  747. package/plugins/vite/dependencies.js +251 -251
  748. package/plugins/vite/dependency-watcher.js +242 -242
  749. package/plugins/vite/drop-client.js +76 -76
  750. package/plugins/vite/drop.js +87 -87
  751. package/plugins/vite/editor-connection.js +124 -124
  752. package/plugins/vite/facebook-instant-games.js +102 -102
  753. package/plugins/vite/gzip.js +5 -5
  754. package/plugins/vite/imports-logger.js +143 -143
  755. package/plugins/vite/index.js +154 -154
  756. package/plugins/vite/license.js +56 -56
  757. package/plugins/vite/local-files.js +440 -440
  758. package/plugins/vite/logger.client.js +343 -343
  759. package/plugins/vite/logger.js +100 -100
  760. package/plugins/vite/materialx.js +31 -31
  761. package/plugins/vite/meta.js +163 -163
  762. package/plugins/vite/needle-app.js +193 -193
  763. package/plugins/vite/npm.js +7 -7
  764. package/plugins/vite/peer.js +90 -90
  765. package/plugins/vite/poster-client.js +58 -58
  766. package/plugins/vite/poster.js +78 -78
  767. package/plugins/vite/pwa.js +604 -604
  768. package/plugins/vite/reload-client.js +15 -15
  769. package/plugins/vite/reload.js +351 -351
  770. package/plugins/vite/server.js +66 -66
  771. package/plugins/vite/transform-codegen.js +55 -55
  772. package/plugins/vite/transform.js +32 -32
  773. package/plugins/vite/vite-4.4-hack.js +31 -31
  774. package/src/asap/needle-asap.ts +111 -111
  775. package/src/asap/sessiongranted.ts +75 -75
  776. package/src/asap/utils.ts +4 -4
  777. package/src/engine/analytics/index.ts +10 -10
  778. package/src/engine/analytics/lcp.ts +35 -35
  779. package/src/engine/api.ts +86 -86
  780. package/src/engine/assets/index.ts +59 -59
  781. package/src/engine/assets/static.js +5 -5
  782. package/src/engine/codegen/register_types.ts +322 -322
  783. package/src/engine/debug/debug.ts +51 -51
  784. package/src/engine/debug/debug_console.ts +303 -303
  785. package/src/engine/debug/debug_overlay.ts +332 -332
  786. package/src/engine/debug/debug_spatial_console.ts +429 -429
  787. package/src/engine/debug/index.ts +1 -1
  788. package/src/engine/dist/engine_utils.js +82 -0
  789. package/src/engine/engine_addressables.ts +763 -763
  790. package/src/engine/engine_animation.ts +221 -221
  791. package/src/engine/engine_application.ts +113 -128
  792. package/src/engine/engine_assetdatabase.ts +396 -396
  793. package/src/engine/engine_audio.ts +24 -24
  794. package/src/engine/engine_camera.fit.ts +302 -302
  795. package/src/engine/engine_camera.ts +143 -143
  796. package/src/engine/engine_components.ts +366 -366
  797. package/src/engine/engine_components_internal.ts +40 -40
  798. package/src/engine/engine_constants.ts +52 -52
  799. package/src/engine/engine_context.ts +1947 -1951
  800. package/src/engine/engine_context_registry.ts +129 -129
  801. package/src/engine/engine_coroutine.ts +54 -54
  802. package/src/engine/engine_create_objects.ts +435 -435
  803. package/src/engine/engine_default_parameters.ts +3 -3
  804. package/src/engine/engine_editor-sync.ts +28 -28
  805. package/src/engine/engine_fileloader.js +9 -9
  806. package/src/engine/engine_gameobject.ts +775 -775
  807. package/src/engine/engine_generic_utils.js +13 -13
  808. package/src/engine/engine_gizmos.ts +594 -594
  809. package/src/engine/engine_gltf.ts +29 -29
  810. package/src/engine/engine_gltf_builtin_components.ts +404 -404
  811. package/src/engine/engine_hot_reload.ts +210 -210
  812. package/src/engine/engine_input.ts +1507 -1507
  813. package/src/engine/engine_input_utils.ts +23 -23
  814. package/src/engine/engine_instancing.ts +45 -45
  815. package/src/engine/engine_license.ts +386 -386
  816. package/src/engine/engine_lifecycle_api.ts +113 -113
  817. package/src/engine/engine_lifecycle_functions_internal.ts +193 -193
  818. package/src/engine/engine_lightdata.ts +127 -127
  819. package/src/engine/engine_loaders.callbacks.ts +137 -137
  820. package/src/engine/engine_loaders.gltf.ts +82 -82
  821. package/src/engine/engine_loaders.ts +383 -383
  822. package/src/engine/engine_lods.ts +189 -189
  823. package/src/engine/engine_mainloop_utils.ts +488 -488
  824. package/src/engine/engine_math.ts +282 -282
  825. package/src/engine/engine_modules.ts +83 -83
  826. package/src/engine/engine_networking.ts +862 -862
  827. package/src/engine/engine_networking_auto.ts +352 -352
  828. package/src/engine/engine_networking_blob.ts +275 -275
  829. package/src/engine/engine_networking_files.ts +217 -217
  830. package/src/engine/engine_networking_files_default_components.ts +58 -58
  831. package/src/engine/engine_networking_instantiate.ts +434 -434
  832. package/src/engine/engine_networking_peer.ts +159 -159
  833. package/src/engine/engine_networking_streams.ts +722 -722
  834. package/src/engine/engine_networking_types.ts +24 -24
  835. package/src/engine/engine_networking_utils.ts +23 -23
  836. package/src/engine/engine_networking_websocket.ts +2 -2
  837. package/src/engine/engine_patcher.ts +199 -199
  838. package/src/engine/engine_physics.ts +841 -845
  839. package/src/engine/engine_physics.types.ts +46 -46
  840. package/src/engine/engine_physics_rapier.ts +1603 -1603
  841. package/src/engine/engine_playerview.ts +80 -80
  842. package/src/engine/engine_pmrem.ts +83 -83
  843. package/src/engine/engine_scenelighting.ts +315 -315
  844. package/src/engine/engine_serialization.ts +2 -2
  845. package/src/engine/engine_serialization_builtin_serializer.ts +473 -473
  846. package/src/engine/engine_serialization_core.ts +720 -720
  847. package/src/engine/engine_serialization_decorator.ts +81 -81
  848. package/src/engine/engine_setup.ts +1 -1
  849. package/src/engine/engine_shaders.ts +267 -267
  850. package/src/engine/engine_shims.ts +32 -32
  851. package/src/engine/engine_test_utils.ts +109 -109
  852. package/src/engine/engine_texture.ts +82 -82
  853. package/src/engine/engine_three_utils.ts +941 -941
  854. package/src/engine/engine_time.ts +94 -94
  855. package/src/engine/engine_time_utils.ts +237 -237
  856. package/src/engine/engine_tonemapping.ts +209 -209
  857. package/src/engine/engine_types.ts +739 -739
  858. package/src/engine/engine_typestore.ts +63 -63
  859. package/src/engine/engine_util_decorator.ts +136 -136
  860. package/src/engine/engine_utils.ts +899 -899
  861. package/src/engine/engine_utils_attributes.ts +72 -72
  862. package/src/engine/engine_utils_format.ts +280 -280
  863. package/src/engine/engine_utils_qrcode.ts +266 -266
  864. package/src/engine/engine_utils_screenshot.ts +708 -708
  865. package/src/engine/engine_utils_screenshot.xr.ts +103 -103
  866. package/src/engine/export/gltf/Writers.ts +34 -34
  867. package/src/engine/export/gltf/index.ts +158 -158
  868. package/src/engine/export/index.ts +2 -2
  869. package/src/engine/export/state.ts +19 -19
  870. package/src/engine/export/utils.ts +9 -9
  871. package/src/engine/extensions/EXT_texture_exr.ts +50 -50
  872. package/src/engine/extensions/NEEDLE_animator_controller_model.ts +195 -195
  873. package/src/engine/extensions/NEEDLE_components.ts +290 -290
  874. package/src/engine/extensions/NEEDLE_gameobject_data.ts +81 -81
  875. package/src/engine/extensions/NEEDLE_lighting_settings.ts +188 -188
  876. package/src/engine/extensions/NEEDLE_lightmaps.ts +119 -119
  877. package/src/engine/extensions/NEEDLE_persistent_assets.ts +76 -76
  878. package/src/engine/extensions/NEEDLE_render_objects.ts +209 -209
  879. package/src/engine/extensions/NEEDLE_techniques_webgl.ts +640 -640
  880. package/src/engine/extensions/extension_resolver.ts +4 -4
  881. package/src/engine/extensions/extension_utils.ts +166 -166
  882. package/src/engine/extensions/extensions.ts +146 -146
  883. package/src/engine/extensions/index.ts +5 -5
  884. package/src/engine/extensions/usage_tracker.ts +100 -100
  885. package/src/engine/js-extensions/Camera.ts +37 -37
  886. package/src/engine/js-extensions/ExtensionUtils.ts +85 -85
  887. package/src/engine/js-extensions/Layers.ts +23 -23
  888. package/src/engine/js-extensions/Object3D.ts +384 -384
  889. package/src/engine/js-extensions/RGBAColor.ts +126 -126
  890. package/src/engine/js-extensions/Vector.ts +24 -24
  891. package/src/engine/js-extensions/index.ts +5 -5
  892. package/src/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +127 -127
  893. package/src/engine/shaders/shaderData.ts +67 -67
  894. package/src/engine/tests/test_utils.ts +63 -63
  895. package/src/engine/webcomponents/WebXRButtons.ts +260 -260
  896. package/src/engine/webcomponents/api.ts +6 -6
  897. package/src/engine/webcomponents/buttons.ts +298 -298
  898. package/src/engine/webcomponents/fonts.ts +41 -41
  899. package/src/engine/webcomponents/icons.ts +57 -57
  900. package/src/engine/webcomponents/index.ts +1 -1
  901. package/src/engine/webcomponents/logo-element.ts +103 -103
  902. package/src/engine/webcomponents/needle menu/needle-menu-spatial.ts +573 -573
  903. package/src/engine/webcomponents/needle menu/needle-menu.ts +1166 -1166
  904. package/src/engine/webcomponents/needle-button.ts +181 -181
  905. package/src/engine/webcomponents/needle-engine.ar-overlay.ts +186 -186
  906. package/src/engine/webcomponents/needle-engine.attributes.ts +84 -84
  907. package/src/engine/webcomponents/needle-engine.extras.ts +16 -16
  908. package/src/engine/webcomponents/needle-engine.loading.ts +404 -404
  909. package/src/engine/webcomponents/needle-engine.ts +959 -959
  910. package/src/engine/xr/NeedleXRController.ts +1182 -1182
  911. package/src/engine/xr/NeedleXRSession.ts +1656 -1658
  912. package/src/engine/xr/NeedleXRSync.ts +220 -220
  913. package/src/engine/xr/SceneTransition.ts +78 -78
  914. package/src/engine/xr/TempXRContext.ts +216 -216
  915. package/src/engine/xr/XRRig.ts +9 -9
  916. package/src/engine/xr/api.ts +5 -5
  917. package/src/engine/xr/events.ts +102 -102
  918. package/src/engine/xr/internal.ts +34 -34
  919. package/src/engine/xr/usdz.ts +30 -30
  920. package/src/engine/xr/utils.ts +39 -39
  921. package/src/engine-components/AlignmentConstraint.ts +36 -36
  922. package/src/engine-components/Animation.ts +567 -567
  923. package/src/engine-components/AnimationCurve.ts +153 -153
  924. package/src/engine-components/AnimationUtils.ts +28 -28
  925. package/src/engine-components/AnimationUtilsAutoplay.ts +38 -38
  926. package/src/engine-components/Animator.ts +398 -398
  927. package/src/engine-components/AnimatorController.ts +1315 -1315
  928. package/src/engine-components/AudioListener.ts +92 -92
  929. package/src/engine-components/AudioSource.ts +644 -644
  930. package/src/engine-components/AvatarLoader.ts +263 -263
  931. package/src/engine-components/AxesHelper.ts +59 -59
  932. package/src/engine-components/BasicIKConstraint.ts +54 -54
  933. package/src/engine-components/BoxCollider.ts +1 -1
  934. package/src/engine-components/BoxHelperComponent.ts +114 -114
  935. package/src/engine-components/Camera.ts +725 -725
  936. package/src/engine-components/CameraUtils.ts +132 -132
  937. package/src/engine-components/CharacterController.ts +253 -253
  938. package/src/engine-components/Collider.ts +400 -400
  939. package/src/engine-components/Component.ts +1301 -1301
  940. package/src/engine-components/ContactShadows.ts +535 -535
  941. package/src/engine-components/DeleteBox.ts +62 -62
  942. package/src/engine-components/DeviceFlag.ts +46 -46
  943. package/src/engine-components/DragControls.ts +1622 -1622
  944. package/src/engine-components/DropListener.ts +744 -744
  945. package/src/engine-components/Duplicatable.ts +199 -199
  946. package/src/engine-components/EventList.ts +283 -283
  947. package/src/engine-components/EventTrigger.ts +73 -73
  948. package/src/engine-components/EventType.ts +22 -22
  949. package/src/engine-components/Fog.ts +60 -60
  950. package/src/engine-components/Gizmos.ts +56 -56
  951. package/src/engine-components/GridHelper.ts +48 -48
  952. package/src/engine-components/GroundProjection.ts +359 -359
  953. package/src/engine-components/Interactable.ts +17 -17
  954. package/src/engine-components/Joints.ts +52 -52
  955. package/src/engine-components/LODGroup.ts +153 -153
  956. package/src/engine-components/Light.ts +558 -558
  957. package/src/engine-components/LookAtConstraint.ts +38 -38
  958. package/src/engine-components/NeedleMenu.ts +85 -85
  959. package/src/engine-components/NestedGltf.ts +98 -98
  960. package/src/engine-components/Networking.ts +114 -114
  961. package/src/engine-components/OffsetConstraint.ts +60 -60
  962. package/src/engine-components/OrbitControls.ts +1087 -1087
  963. package/src/engine-components/PlayerColor.ts +103 -103
  964. package/src/engine-components/ReflectionProbe.ts +232 -232
  965. package/src/engine-components/Renderer.ts +893 -893
  966. package/src/engine-components/RendererInstancing.ts +855 -891
  967. package/src/engine-components/RendererLightmap.ts +211 -211
  968. package/src/engine-components/RigidBody.ts +531 -531
  969. package/src/engine-components/SceneSwitcher.ts +1030 -1030
  970. package/src/engine-components/ScreenCapture.ts +592 -592
  971. package/src/engine-components/SeeThrough.ts +302 -302
  972. package/src/engine-components/ShadowCatcher.ts +172 -172
  973. package/src/engine-components/Skybox.ts +440 -451
  974. package/src/engine-components/SmoothFollow.ts +76 -76
  975. package/src/engine-components/SpatialTrigger.ts +229 -229
  976. package/src/engine-components/SpectatorCamera.ts +787 -787
  977. package/src/engine-components/SphereCollider.ts +1 -1
  978. package/src/engine-components/SpriteRenderer.ts +486 -486
  979. package/src/engine-components/SyncedCamera.ts +220 -220
  980. package/src/engine-components/SyncedRoom.ts +380 -380
  981. package/src/engine-components/SyncedTransform.ts +383 -383
  982. package/src/engine-components/TestRunner.ts +118 -118
  983. package/src/engine-components/TransformGizmo.ts +228 -228
  984. package/src/engine-components/VideoPlayer.ts +1025 -1025
  985. package/src/engine-components/Voip.ts +363 -363
  986. package/src/engine-components/api.ts +62 -62
  987. package/src/engine-components/avatar/AvatarBlink_Simple.ts +69 -69
  988. package/src/engine-components/avatar/AvatarEyeLook_Rotation.ts +63 -63
  989. package/src/engine-components/avatar/Avatar_Brain_LookAt.ts +139 -139
  990. package/src/engine-components/avatar/Avatar_MouthShapes.ts +83 -83
  991. package/src/engine-components/avatar/Avatar_MustacheShake.ts +31 -31
  992. package/src/engine-components/codegen/components.ts +228 -228
  993. package/src/engine-components/debug/LogStats.ts +22 -22
  994. package/src/engine-components/export/gltf/GltfExport.ts +265 -265
  995. package/src/engine-components/export/usdz/Extension.ts +24 -24
  996. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +2538 -2538
  997. package/src/engine-components/export/usdz/USDZExporter.ts +713 -713
  998. package/src/engine-components/export/usdz/extensions/Animation.ts +1204 -1204
  999. package/src/engine-components/export/usdz/extensions/DocumentExtension.ts +9 -9
  1000. package/src/engine-components/export/usdz/extensions/NodeMaterialConverter.ts +532 -532
  1001. package/src/engine-components/export/usdz/extensions/USDZText.ts +240 -240
  1002. package/src/engine-components/export/usdz/extensions/USDZUI.ts +189 -189
  1003. package/src/engine-components/export/usdz/extensions/behavior/Actions.ts +99 -99
  1004. package/src/engine-components/export/usdz/extensions/behavior/AudioExtension.ts +102 -102
  1005. package/src/engine-components/export/usdz/extensions/behavior/Behaviour.ts +320 -320
  1006. package/src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +1253 -1253
  1007. package/src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts +646 -646
  1008. package/src/engine-components/export/usdz/extensions/behavior/PhysicsExtension.ts +132 -132
  1009. package/src/engine-components/export/usdz/index.ts +2 -2
  1010. package/src/engine-components/export/usdz/utils/animationutils.ts +191 -191
  1011. package/src/engine-components/export/usdz/utils/quicklook.ts +50 -50
  1012. package/src/engine-components/particlesystem/ParticleSystem.ts +1287 -1287
  1013. package/src/engine-components/particlesystem/ParticleSystemModules.ts +1765 -1765
  1014. package/src/engine-components/particlesystem/ParticleSystemSubEmitter.ts +111 -111
  1015. package/src/engine-components/particlesystem/api.ts +1 -1
  1016. package/src/engine-components/physics/Attractor.ts +44 -44
  1017. package/src/engine-components/postprocessing/Effects/Antialiasing.ts +64 -64
  1018. package/src/engine-components/postprocessing/Effects/BloomEffect.ts +116 -116
  1019. package/src/engine-components/postprocessing/Effects/ChromaticAberration.ts +37 -37
  1020. package/src/engine-components/postprocessing/Effects/ColorAdjustments.ts +106 -106
  1021. package/src/engine-components/postprocessing/Effects/DepthOfField.ts +103 -103
  1022. package/src/engine-components/postprocessing/Effects/EffectWrapper.ts +25 -25
  1023. package/src/engine-components/postprocessing/Effects/Pixelation.ts +32 -32
  1024. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.ts +90 -90
  1025. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.ts +192 -192
  1026. package/src/engine-components/postprocessing/Effects/Sharpening.ts +143 -143
  1027. package/src/engine-components/postprocessing/Effects/TiltShiftEffect.ts +61 -61
  1028. package/src/engine-components/postprocessing/Effects/Tonemapping.ts +103 -103
  1029. package/src/engine-components/postprocessing/Effects/Tonemapping.utils.ts +60 -60
  1030. package/src/engine-components/postprocessing/Effects/Vignette.ts +59 -59
  1031. package/src/engine-components/postprocessing/PostProcessingEffect.ts +192 -192
  1032. package/src/engine-components/postprocessing/PostProcessingHandler.ts +586 -586
  1033. package/src/engine-components/postprocessing/Volume.ts +427 -427
  1034. package/src/engine-components/postprocessing/VolumeParameter.ts +158 -158
  1035. package/src/engine-components/postprocessing/VolumeProfile.ts +61 -61
  1036. package/src/engine-components/postprocessing/index.ts +5 -5
  1037. package/src/engine-components/postprocessing/utils.ts +154 -154
  1038. package/src/engine-components/splines/Spline.ts +287 -287
  1039. package/src/engine-components/splines/SplineUtils.ts +33 -33
  1040. package/src/engine-components/splines/SplineWalker.ts +195 -195
  1041. package/src/engine-components/splines/index.ts +2 -2
  1042. package/src/engine-components/timeline/PlayableDirector.ts +782 -782
  1043. package/src/engine-components/timeline/SignalAsset.ts +155 -155
  1044. package/src/engine-components/timeline/TimelineModels.ts +136 -136
  1045. package/src/engine-components/timeline/TimelineTracks.ts +994 -994
  1046. package/src/engine-components/timeline/index.ts +3 -3
  1047. package/src/engine-components/ui/BaseUIComponent.ts +203 -203
  1048. package/src/engine-components/ui/Button.ts +307 -307
  1049. package/src/engine-components/ui/Canvas.ts +419 -419
  1050. package/src/engine-components/ui/CanvasGroup.ts +54 -54
  1051. package/src/engine-components/ui/EventSystem.ts +853 -853
  1052. package/src/engine-components/ui/Graphic.ts +287 -287
  1053. package/src/engine-components/ui/Image.ts +112 -112
  1054. package/src/engine-components/ui/InputField.ts +321 -321
  1055. package/src/engine-components/ui/Interfaces.ts +57 -57
  1056. package/src/engine-components/ui/Layout.ts +334 -334
  1057. package/src/engine-components/ui/Outline.ts +13 -13
  1058. package/src/engine-components/ui/PointerEvents.ts +206 -206
  1059. package/src/engine-components/ui/RaycastUtils.ts +70 -70
  1060. package/src/engine-components/ui/Raycaster.ts +121 -121
  1061. package/src/engine-components/ui/RectTransform.ts +375 -375
  1062. package/src/engine-components/ui/SpatialHtml.ts +79 -79
  1063. package/src/engine-components/ui/Symbols.ts +1 -1
  1064. package/src/engine-components/ui/Text.ts +587 -587
  1065. package/src/engine-components/ui/Utils.ts +113 -113
  1066. package/src/engine-components/utils/EnvironmentScene.ts +245 -245
  1067. package/src/engine-components/utils/LookAt.ts +98 -98
  1068. package/src/engine-components/utils/OpenURL.ts +115 -115
  1069. package/src/engine-components/web/Clickthrough.ts +105 -105
  1070. package/src/engine-components/web/CursorFollow.ts +144 -144
  1071. package/src/engine-components/web/HoverAnimation.ts +101 -101
  1072. package/src/engine-components/web/ScrollFollow.ts +513 -513
  1073. package/src/engine-components/web/ViewBox.ts +320 -320
  1074. package/src/engine-components/web/index.ts +4 -4
  1075. package/src/engine-components/webxr/Avatar.ts +265 -265
  1076. package/src/engine-components/webxr/TeleportTarget.ts +13 -13
  1077. package/src/engine-components/webxr/WebARCameraBackground.ts +180 -180
  1078. package/src/engine-components/webxr/WebARSessionRoot.ts +882 -882
  1079. package/src/engine-components/webxr/WebXR.ts +612 -616
  1080. package/src/engine-components/webxr/WebXRAvatar.ts +66 -66
  1081. package/src/engine-components/webxr/WebXRImageTracking.ts +649 -649
  1082. package/src/engine-components/webxr/WebXRPlaneTracking.ts +570 -570
  1083. package/src/engine-components/webxr/WebXRRig.ts +81 -81
  1084. package/src/engine-components/webxr/XRFlag.ts +150 -150
  1085. package/src/engine-components/webxr/controllers/XRControllerFollow.ts +130 -130
  1086. package/src/engine-components/webxr/controllers/XRControllerModel.ts +377 -377
  1087. package/src/engine-components/webxr/controllers/XRControllerMovement.ts +561 -561
  1088. package/src/engine-components/webxr/index.ts +2 -2
  1089. package/src/engine-components/webxr/types.ts +3 -3
  1090. package/src/engine-components-experimental/Presentation.ts +13 -13
  1091. package/src/engine-components-experimental/api.ts +4 -4
  1092. package/src/engine-components-experimental/networking/PlayerSync.ts +401 -401
  1093. package/src/engine-schemes/COMPILE_SCHEMES.bat +3 -3
  1094. package/src/engine-schemes/COMPILE_TS.bat +11 -11
  1095. package/src/engine-schemes/README.md +1 -1
  1096. package/src/engine-schemes/api.ts +12 -12
  1097. package/src/engine-schemes/schemes.ts +28 -28
  1098. package/src/engine-schemes/synced-camera-model.ts +92 -92
  1099. package/src/engine-schemes/synced-transform-model.ts +90 -90
  1100. package/src/engine-schemes/syncedCamera.fbs +10 -10
  1101. package/src/engine-schemes/transform.ts +50 -50
  1102. package/src/engine-schemes/transforms.fbs +25 -25
  1103. package/src/engine-schemes/vec.fbs +19 -19
  1104. package/src/engine-schemes/vec2.ts +33 -33
  1105. package/src/engine-schemes/vec3.ts +38 -38
  1106. package/src/engine-schemes/vec4.ts +43 -43
  1107. package/src/engine-schemes/vr-user-state-buffer.ts +145 -145
  1108. package/src/engine-schemes/vrUserStateBuffer.fbs +17 -17
  1109. package/src/include/draco/draco_decoder.js +34 -34
  1110. package/src/include/ktx2/basis_transcoder.js +19 -19
  1111. package/src/include/needle/arial-msdf.json +1471 -1471
  1112. package/src/include/three/DragControls.js +231 -231
  1113. package/src/include/three/EXT_mesh_gpu_instancing_exporter.js +66 -66
  1114. package/src/needle-engine.ts +70 -72
  1115. package/dist/generateMeshBVH.worker-D1Vr8UHG.js +0 -21
  1116. package/src/engine/debug/debug_spector.ts +0 -43
@@ -1,1785 +1,1782 @@
1
- import 'three/examples/jsm/renderers/webgl-legacy/nodes/WebGLNodes.js';
2
- import { Color, DepthTexture, NearestFilter, NoToneMapping, Object3D, PCFSoftShadowMap, PerspectiveCamera, RGBAFormat, Scene, SRGBColorSpace, Texture, WebGLRenderer, WebGLRenderTarget } from 'three';
3
- /** @ts-ignore (not yet in types?) */
4
- import { BasicNodeLibrary } from "three";
5
- import * as Stats from 'three/examples/jsm/libs/stats.module.js';
6
- import { nodeFrame } from "three/examples/jsm/renderers/webgl-legacy/nodes/WebGLNodeBuilder.js";
7
- import { isDevEnvironment, LogType, showBalloonError, showBalloonMessage } from './debug/index.js';
8
- import { Addressables } from './engine_addressables.js';
9
- import { AnimationsRegistry } from './engine_animation.js';
10
- import { Application } from './engine_application.js';
11
- import { AssetDatabase } from './engine_assetdatabase.js';
12
- import { updateCameraFocusRect } from './engine_camera.js';
13
- import { VERSION } from './engine_constants.js';
14
- import { ContextEvent, ContextRegistry } from './engine_context_registry.js';
15
- import { WaitForPromise } from './engine_coroutine.js';
16
- import { ObjectUtils } from "./engine_create_objects.js";
17
- import { destroy, foreachComponent } from './engine_gameobject.js';
18
- import { getLoader } from './engine_gltf.js';
19
- import { Input } from './engine_input.js';
20
- import { invokeLifecycleFunctions } from './engine_lifecycle_functions_internal.js';
21
- import { LightDataRegistry } from './engine_lightdata.js';
22
- import { LODsManager } from "./engine_lods.js";
23
- import * as looputils from './engine_mainloop_utils.js';
24
- import { NetworkConnection } from './engine_networking.js';
25
- import { Physics } from './engine_physics.js';
26
- import { PlayerViewManager } from './engine_playerview.js';
27
- import { RendererData as SceneLighting } from './engine_scenelighting.js';
28
- import { logHierarchy } from './engine_three_utils.js';
29
- import { Time } from './engine_time.js';
30
- import { patchTonemapping } from './engine_tonemapping.js';
31
- import { deepClone, DeviceUtilities, getParam } from './engine_utils.js';
32
- import { NeedleMenu } from './webcomponents/needle menu/needle-menu.js';
33
- import { initSpectorIfAvailable } from './debug/debug_spector.js';
34
- const debug = getParam("debugcontext");
35
- const stats = getParam("stats");
36
- const debugActive = getParam("debugactive");
37
- const debugframerate = getParam("debugframerate");
38
- const debugCoroutine = getParam("debugcoroutine");
39
- // this is where functions that setup unity scenes will be pushed into
40
- // those will be accessed from our custom html element to load them into their context
41
- export const build_scene_functions = {};
42
- export class ContextArgs {
43
- name;
44
- /** for debugging only */
45
- alias;
46
- /** the hash is used as a seed when initially loading the scene files */
47
- hash;
48
- /** when true the context will not check if it's visible in the viewport and always update and render */
49
- runInBackground;
50
- /** the DOM element the context belongs to or is inside of (this does not have to be the canvas. use renderer.domElement if you want to access the dom canvas) */
51
- domElement;
52
- /** externally owned renderer */
53
- renderer;
54
- /** externally owned camera */
55
- camera;
56
- /** externally owned scene */
57
- scene;
58
- }
59
- export var FrameEvent;
60
- (function (FrameEvent) {
61
- FrameEvent[FrameEvent["Start"] = -1] = "Start";
62
- FrameEvent[FrameEvent["EarlyUpdate"] = 0] = "EarlyUpdate";
63
- FrameEvent[FrameEvent["Update"] = 1] = "Update";
64
- FrameEvent[FrameEvent["LateUpdate"] = 2] = "LateUpdate";
65
- FrameEvent[FrameEvent["OnBeforeRender"] = 3] = "OnBeforeRender";
66
- FrameEvent[FrameEvent["OnAfterRender"] = 4] = "OnAfterRender";
67
- FrameEvent[FrameEvent["PrePhysicsStep"] = 9] = "PrePhysicsStep";
68
- FrameEvent[FrameEvent["PostPhysicsStep"] = 10] = "PostPhysicsStep";
69
- FrameEvent[FrameEvent["Undefined"] = -1] = "Undefined";
70
- })(FrameEvent || (FrameEvent = {}));
71
- export function registerComponent(script, context) {
72
- if (!script)
73
- return;
74
- if (!script.isComponent) {
75
- if (isDevEnvironment() || debug)
76
- console.error("Registered script is not a Needle Engine component. \nThe script will be ignored. Please make sure your component extends \"Behaviour\" imported from \"@needle-tools/engine\"\n", script);
77
- return;
78
- }
79
- if (!context) {
80
- context = Context.Current;
81
- if (debug)
82
- console.warn("> Registering component without context");
83
- }
84
- const new_scripts = context?.new_scripts;
85
- if (!new_scripts.includes(script)) {
86
- new_scripts.push(script);
87
- }
88
- }
89
- /**
90
- * The Needle Engine context is the main access point that holds all the data and state of a Needle Engine application.
91
- * It can be used to access the {@link Context.scene}, {@link Context.renderer}, {@link Context.mainCamera}, {@link Context.input}, {@link Context.physics}, {@link Context.time}, {@link Context.connection} (networking), and more.
92
- *
93
- * The context is automatically created when using the `<needle-engine>` web component.
94
- *
95
- * @example Accessing the context from a [component](https://engine.needle.tools/docs/api/Behaviour):
96
- * ```typescript
97
- * import { Behaviour } from "@needle-tools/engine";
98
- * import { Mesh, BoxGeometry, MeshBasicMaterial } from "three";
99
- * export class MyScript extends Behaviour {
100
- * start() {
101
- * console.log("Hello from MyScript");
102
- * this.context.scene.add(new Mesh(new BoxGeometry(), new MeshBasicMaterial()));
103
- * }
104
- * }
105
- * ```
106
- *
107
- * @example Accessing the context from a [hook](https://engine.needle.tools/docs/scripting.html#hooks) without a component e.g. from a javascript module or svelte or react component.
108
- *
109
- * ```typescript
110
- * import { onStart } from "@needle-tools/engine";
111
- *
112
- * onStart((context) => {
113
- * console.log("Hello from onStart hook");
114
- * context.scene.add(new Mesh(new BoxGeometry(), new MeshBasicMaterial()));
115
- * });
116
- * ```
117
- *
118
- */
119
- export class Context {
120
- static _defaultTargetFramerate = { value: 90, toString() { return this.value; } };
121
- /** When a new context is created this is the framerate that will be used by default */
122
- static get DefaultTargetFrameRate() {
123
- return Context._defaultTargetFramerate.value;
124
- }
125
- /** When a new context is created this is the framerate that will be used by default */
126
- static set DefaultTargetFrameRate(val) {
127
- Context._defaultTargetFramerate.value = val;
128
- }
129
- static _defaultWebglRendererParameters = {
130
- antialias: true,
131
- alpha: false,
132
- // Note: this is due to a bug on OSX devices. See NE-5370
133
- powerPreference: (DeviceUtilities.isiOS() || DeviceUtilities.isMacOS()) ? "default" : "high-performance",
134
- stencil: true,
135
- // logarithmicDepthBuffer: true,
136
- // reverseDepthBuffer: true, // https://github.com/mrdoob/three.js/issues/29770
137
- };
138
- /** The default parameters that will be used when creating a new WebGLRenderer.
139
- * Modify in global context to change the default parameters for all new contexts.
140
- * @example
141
- * ```typescript
142
- * import { Context } from "@needle-tools/engine";
143
- * Context.DefaultWebGLRendererParameters.antialias = false;
144
- * ```
145
- */
146
- static get DefaultWebGLRendererParameters() {
147
- return Context._defaultWebglRendererParameters;
148
- }
149
- /** The needle engine version */
150
- get version() {
151
- return VERSION;
152
- }
153
- /** The currently active context. Only set during the update loops */
154
- static get Current() {
155
- return ContextRegistry.Current;
156
- }
157
- /** @internal this property should not be set by user code */
158
- static set Current(context) {
159
- ContextRegistry.Current = context;
160
- }
161
- static get All() {
162
- return ContextRegistry.All;
163
- }
164
- /** The name of the context */
165
- name;
166
- /** An alias for the context */
167
- alias;
168
- /** When the renderer or camera are managed by an external process (e.g. when running in r3f context).
169
- * When this is false you are responsible to call update(timestamp, xframe.
170
- * It is also currently assumed that rendering is handled performed by an external process
171
- * */
172
- isManagedExternally = false;
173
- /** set to true to pause the update loop. You can receive an event for it in your components.
174
- * Note that script updates will not be called when paused */
175
- isPaused = false;
176
- /** When enabled the application will run while not visible on the page */
177
- runInBackground = false;
178
- /**
179
- * Set to the target framerate you want your application to run in (you can use ?stats to check the fps)
180
- * Set to undefined if you want to run at the maximum framerate
181
- */
182
- targetFrameRate;
183
- /** Use a higher number for more accurate physics simulation.
184
- * When undefined physics steps will be 1 for mobile devices and 5 for desktop devices
185
- * Set to 0 to disable physics updates
186
- * TODO: changing physics steps is currently not supported because then forces that we get from the character controller and rigidbody et al are not correct anymore - this needs to be properly tested before making this configureable
187
- */
188
- physicsSteps = 1;
189
- /** used to append to loaded assets */
190
- hash;
191
- /** The `<needle-engine>` web component */
192
- domElement;
193
- appendHTMLElement(element) {
194
- if (this.domElement.shadowRoot)
195
- return this.domElement.shadowRoot.appendChild(element);
196
- else
197
- return this.domElement.appendChild(element);
198
- }
199
- get resolutionScaleFactor() { return this._resolutionScaleFactor; }
200
- /** use to scale the resolution up or down of the renderer. default is 1 */
201
- set resolutionScaleFactor(val) {
202
- if (val === this._resolutionScaleFactor)
203
- return;
204
- if (typeof val !== "number")
205
- return;
206
- if (val <= 0) {
207
- console.error("Invalid resolution scale factor", val);
208
- return;
209
- }
210
- this._resolutionScaleFactor = val;
211
- this.updateSize();
212
- }
213
- _resolutionScaleFactor = 1;
214
- // domElement.clientLeft etc doesnt return absolute position
215
- _boundingClientRectFrame = -1;
216
- _boundingClientRect = null;
217
- _domX;
218
- _domY;
219
- /** update bounding rects + domX, domY */
220
- calculateBoundingClientRect() {
221
- // workaround for mozilla webXR viewer
222
- if (this.xr) {
223
- this._domX = 0;
224
- this._domY = 0;
225
- return;
226
- }
227
- // TODO: cache this
228
- if (this._boundingClientRectFrame === this.time.frame)
229
- return;
230
- this._boundingClientRectFrame = this.time.frame;
231
- this._boundingClientRect = this.domElement.getBoundingClientRect();
232
- this._domX = this._boundingClientRect.x;
233
- this._domY = this._boundingClientRect.y;
234
- }
235
- /** The width of the `<needle-engine>` element on the website */
236
- get domWidth() {
237
- // for mozilla XR
238
- if (this.isInAR)
239
- return window.innerWidth;
240
- return this.domElement.clientWidth;
241
- }
242
- /** The height of the `<needle-engine>` element on the website */
243
- get domHeight() {
244
- // for mozilla XR
245
- if (this.isInAR)
246
- return window.innerHeight;
247
- return this.domElement.clientHeight;
248
- }
249
- /** the X position of the `<needle-engine>` element on the website */
250
- get domX() {
251
- this.calculateBoundingClientRect();
252
- return this._domX;
253
- }
254
- /** the Y position of the `<needle-engine>` element on the website */
255
- get domY() {
256
- this.calculateBoundingClientRect();
257
- return this._domY;
258
- }
259
- /**
260
- * Is a XR session currently active and presenting?
261
- * @returns true if the xr renderer is currently presenting
262
- */
263
- get isInXR() { return this.renderer?.xr?.isPresenting || false; }
264
- /** shorthand for `NeedleXRSession.active`
265
- * Automatically set by NeedleXRSession when a XR session is active
266
- * @returns the active XR session or null if no session is active
267
- * */
268
- xr = null;
269
- /**
270
- * Shorthand for `this.xr?.mode`. AR or VR
271
- * @returns the current XR session mode (immersive-vr or immersive-ar)
272
- */
273
- get xrSessionMode() { return this.xr?.mode; }
274
- /** Shorthand for `this.xrSessionMode === "immersive-vr"`
275
- * @returns true if a webxr VR session is currently active.
276
- */
277
- get isInVR() { return this.xrSessionMode === "immersive-vr"; }
278
- /**
279
- * Shorthand for `this.xrSessionMode === "immersive-ar"`
280
- * @returns true if a webxr AR session is currently active.
281
- */
282
- get isInAR() { return this.xrSessionMode === "immersive-ar"; }
283
- /** If a XR session is active and in pass through mode (immersive-ar on e.g. Quest)
284
- * @returns true if the XR session is in pass through mode
285
- */
286
- get isInPassThrough() { return this.xr ? this.xr.isPassThrough : false; }
287
- /** access the raw `XRSession` object (shorthand for `context.renderer.xr.getSession()`). For more control use `NeedleXRSession.active` */
288
- get xrSession() { return this.renderer?.xr?.getSession(); }
289
- /** @returns the latest XRFrame (if a XRSession is currently active)
290
- * @link https://developer.mozilla.org/en-US/docs/Web/API/XRFrame
291
- */
292
- get xrFrame() { return this._xrFrame; }
293
- /** @returns the current WebXR camera while the WebXRManager is active (shorthand for `context.renderer.xr.getCamera()`) */
294
- get xrCamera() { return this.renderer.xr.isPresenting ? this.renderer?.xr?.getCamera() : undefined; }
295
- _xrFrame = null;
296
- /**
297
- * The AR overlay element is used to display 2D HTML elements while a AR session is active.
298
- */
299
- get arOverlayElement() {
300
- const el = this.domElement;
301
- if (typeof el.getAROverlayContainer === "function")
302
- return el.getAROverlayContainer();
303
- return this.domElement;
304
- }
305
- /**
306
- * Current event of the update cycle (e.g. `FrameEvent.EarlyUpdate` or `FrameEvent.OnBeforeRender`)
307
- */
308
- get currentFrameEvent() {
309
- return this._currentFrameEvent;
310
- }
311
- _currentFrameEvent = FrameEvent.Undefined;
312
- /**
313
- * The scene contains all objects in the hierarchy and is automatically rendered by the context every frane.
314
- */
315
- scene;
316
- /**
317
- * The renderer is used to render the scene. It is automatically created when the context is created.
318
- */
319
- renderer;
320
- /**
321
- * The effect composer can be used to render postprocessing effects. If assigned then it will automatically render the scene every frame.
322
- */
323
- composer = null;
324
- // #region internal script lists
325
- /**
326
- * @internal All known components. Don't use directly
327
- */
328
- scripts = [];
329
- /**
330
- * @internal All paused components. Don't use directly
331
- */
332
- scripts_pausedChanged = [];
333
- /**
334
- * @internal All components that have a early update event. Don't use directly
335
- */
336
- scripts_earlyUpdate = [];
337
- /**
338
- * @internal All components that have a update event. Don't use directly
339
- */
340
- scripts_update = [];
341
- /**
342
- * @internal All components that have a late update event. Don't use directly
343
- */
344
- scripts_lateUpdate = [];
345
- /**
346
- * @internal All components that have a onBeforeRender event. Don't use directly
347
- */
348
- scripts_onBeforeRender = [];
349
- /**
350
- * @internal All components that have a onAfterRender event. Don't use directly
351
- */
352
- scripts_onAfterRender = [];
353
- /**
354
- * @internal All components that have coroutines. Don't use directly
355
- */
356
- scripts_WithCorroutines = [];
357
- /**
358
- * @internal Components with immersive-vr event methods. Don't use directly
359
- */
360
- scripts_immersive_vr = [];
361
- /**
362
- * @internal Components with immersive-ar event methods. Don't use directly
363
- */
364
- scripts_immersive_ar = [];
365
- /**
366
- * @internal Coroutine data
367
- */
368
- coroutines = {};
369
- /** callbacks called once after the context has been created */
370
- post_setup_callbacks = [];
371
- /** called every frame at the beginning of the frame (after component start events and before earlyUpdate) */
372
- pre_update_callbacks = [];
373
- /** called every frame before rendering (after all component events) */
374
- pre_render_callbacks = [];
375
- /** called every frame after rendering (after all component events) */
376
- post_render_callbacks = [];
377
- /** called every frame befroe update (this list is emptied every frame) */
378
- pre_update_oneshot_callbacks = [];
379
- /** @internal */
380
- new_scripts = [];
381
- /** @internal */
382
- new_script_start = [];
383
- /** @internal */
384
- new_scripts_pre_setup_callbacks = [];
385
- /** @internal */
386
- new_scripts_post_setup_callbacks = [];
387
- /** @internal */
388
- new_scripts_xr = [];
389
- // #endregion
390
- // #region Properties
391
- /**
392
- * The **main camera component** of the scene - this camera is used for rendering.
393
- * Use `setCurrentCamera` for updating the main camera.
394
- */
395
- mainCameraComponent = undefined;
396
- /**
397
- * The main camera of the scene - this camera is used for rendering
398
- * Use `setCurrentCamera` for updating the main camera.
399
- */
400
- get mainCamera() {
401
- if (this._mainCamera) {
402
- return this._mainCamera;
403
- }
404
- if (this.mainCameraComponent) {
405
- const cam = this.mainCameraComponent;
406
- if (!cam.threeCamera)
407
- cam.buildCamera();
408
- return cam.threeCamera;
409
- }
410
- if (!this._fallbackCamera) {
411
- this._fallbackCamera = new PerspectiveCamera(75, this.domWidth / this.domHeight, 0.1, 1000);
412
- }
413
- return this._fallbackCamera;
414
- }
415
- /** Set the main camera of the scene. If set to null the camera of the {@link mainCameraComponent} will be used - this camera is used for rendering */
416
- set mainCamera(cam) {
417
- this._mainCamera = cam;
418
- }
419
- _mainCamera = null;
420
- _fallbackCamera = null;
421
- /** access application state (e.g. if all audio should be muted) */
422
- application;
423
- /** access animation mixer used by components in the scene */
424
- animations;
425
- /** access timings (current frame number, deltaTime, timeScale, ...) */
426
- time;
427
- /** access input data (e.g. click or touch events) */
428
- input;
429
- /** access physics related methods (e.g. raycasting). To access the phyiscs engine use `context.physics.engine` */
430
- physics;
431
- /** access networking methods (use it to send or listen to messages or join a networking backend) */
432
- connection;
433
- /** @deprecated AssetDatabase is deprecated */
434
- assets;
435
- /** The main light in the scene */
436
- mainLight = null;
437
- /** @deprecated Use sceneLighting */
438
- get rendererData() { return this.sceneLighting; }
439
- /** Access the scene lighting manager to control lighting settings in the context */
440
- sceneLighting;
441
- addressables;
442
- lightmaps;
443
- players;
444
- /** Access the LODs manager to control LOD behavior in the context */
445
- lodsManager;
446
- /** Access the needle menu to add or remove buttons to the menu element */
447
- menu;
448
- /**
449
- * Checks if the context is fully created and ready
450
- * @returns true if the context is fully created and ready
451
- */
452
- get isCreated() { return this._isCreated; }
453
- /**
454
- * The source identifier(s) of the root scene(s) loaded into this context.
455
- * When using `<needle-engine>` web component this will be the `src` attribute(s).
456
- * @returns The source identifier for of the root scene
457
- */
458
- get rootSourceId() { return this.rootSceneSourceIdentifiers[0] || undefined; }
459
- _needsUpdateSize = false;
460
- _isCreated = false;
461
- _isCreating = false;
462
- _isVisible = false;
463
- _stats = stats ? new Stats.default() : null;
464
- constructor(args) {
465
- this.name = args?.name || "";
466
- this.alias = args?.alias;
467
- this.domElement = args?.domElement || document.body;
468
- this.hash = args?.hash;
469
- if (args?.renderer) {
470
- this.renderer = args.renderer;
471
- this.isManagedExternally = true;
472
- }
473
- if (args?.runInBackground !== undefined)
474
- this.runInBackground = args.runInBackground;
475
- if (args?.scene)
476
- this.scene = args.scene;
477
- else
478
- this.scene = new Scene();
479
- if (args?.camera)
480
- this._mainCamera = args.camera;
481
- this.application = new Application(this);
482
- this.time = new Time();
483
- this.input = new Input(this);
484
- this.physics = new Physics(this);
485
- this.connection = new NetworkConnection(this);
486
- // eslint-disable-next-line deprecation/deprecation
487
- this.assets = new AssetDatabase();
488
- this.sceneLighting = new SceneLighting(this);
489
- this.addressables = new Addressables(this);
490
- this.lightmaps = new LightDataRegistry(this);
491
- this.players = new PlayerViewManager(this);
492
- this.menu = new NeedleMenu(this);
493
- this.lodsManager = new LODsManager(this);
494
- this.animations = new AnimationsRegistry(this);
495
- const resizeCallback = () => this._needsUpdateSize = true;
496
- window.addEventListener('resize', resizeCallback);
497
- this._disposeCallbacks.push(() => window.removeEventListener('resize', resizeCallback));
498
- const resizeObserver = new ResizeObserver(_ => this._needsUpdateSize = true);
499
- resizeObserver.observe(this.domElement);
500
- this._disposeCallbacks.push(() => resizeObserver.disconnect());
501
- this._intersectionObserver = new IntersectionObserver(entries => {
502
- this._isVisible = entries[0].isIntersecting;
503
- });
504
- this._disposeCallbacks.push(() => this._intersectionObserver?.disconnect());
505
- ContextRegistry.register(this);
506
- }
507
- // #region Renderer
508
- /**
509
- * Calling this function will dispose the current renderer and create a new one which will then be assigned to the context. It can be used to create a new renderer with custom WebGLRendererParameters.
510
- * **Note**: Instead you can also modify the static `Context.DefaultWebGlRendererParameters` before the context is created.
511
- * **Note**: This method is recommended because it re-uses an potentially already existing canvas element. This is necessary to keep input event handlers from working (e.g. components like OrbitControls subscribe to input events on the canvas)
512
- * @returns {WebGLRenderer} the newly created renderer
513
- */
514
- createNewRenderer(params) {
515
- this.renderer?.dispose();
516
- params = { ...Context.DefaultWebGLRendererParameters, ...params };
517
- if (!params.canvas) {
518
- // get canvas already configured in the Needle Engine Web Component
519
- const canvas = this.domElement?.shadowRoot?.querySelector("canvas");
520
- if (canvas) {
521
- params.canvas = canvas;
522
- if (debug) {
523
- console.log("Using canvas from shadow root", canvas);
524
- }
525
- }
526
- }
527
- if (debug)
528
- console.log("Using Renderer Parameters:", params, this.domElement);
529
- this.renderer = new WebGLRenderer(params);
530
- this.renderer.debug.checkShaderErrors = isDevEnvironment() || getParam("checkshadererrors") === true;
531
- // some tonemapping other than "NONE" is required for adjusting exposure with EXR environments
532
- this.renderer.toneMappingExposure = 1; // range [0...inf] instead of the usual -15..15
533
- this.renderer.toneMapping = NoToneMapping; // could also set to LinearToneMapping, ACESFilmicToneMapping
534
- this.renderer.setClearColor(new Color('lightgrey'), 0);
535
- // // @ts-ignore
536
- // this.renderer.alpha = false;
537
- this.renderer.shadowMap.enabled = true;
538
- this.renderer.shadowMap.type = PCFSoftShadowMap;
539
- this.renderer.setSize(this.domWidth, this.domHeight);
540
- this.renderer.outputColorSpace = SRGBColorSpace;
541
- // Injecting the core nodes library here, like WebGPURenderer backends do
542
- //@ts-ignore
543
- this.renderer.nodes = {
544
- library: new BasicNodeLibrary(),
545
- modelViewMatrix: null,
546
- modelNormalViewMatrix: null,
547
- };
548
- // this.renderer.toneMapping = AgXToneMapping;
549
- this.lodsManager.setRenderer(this.renderer);
550
- this.input.bindEvents();
551
- initSpectorIfAvailable(this, this.renderer.domElement);
552
- return this.renderer;
553
- }
554
- _intersectionObserver = null;
555
- internalOnUpdateVisible() {
556
- this._intersectionObserver?.disconnect();
557
- this._intersectionObserver?.observe(this.domElement);
558
- }
559
- _disposeCallbacks = [];
560
- /** will request a renderer size update the next render call (will call updateSize the next update) */
561
- requestSizeUpdate() { this._needsUpdateSize = true; }
562
- /** Clamps the renderer max resolution. If undefined the max resolution is not clamped. Default is undefined */
563
- maxRenderResolution;
564
- /** Control the renderer devicePixelRatio.
565
- * **Options**
566
- * - `auto` - Needle Engine automatically sets the pixel ratio to the current window.devicePixelRatio.
567
- * - `manual` - Needle Engine will not change the renderer pixel ratio. You can set it manually.
568
- * - `number` - Needle Engine will set the pixel ratio to the given number. The change will be applied to the renderer and the composer (if used) at the end of the current frame.
569
- */
570
- get devicePixelRatio() { return this._devicePixelRatio; }
571
- set devicePixelRatio(val) {
572
- if (val !== this._devicePixelRatio) {
573
- this._devicePixelRatio = val;
574
- this._needsUpdateSize = true;
575
- }
576
- }
577
- _devicePixelRatio = "auto";
578
- /**
579
- * Update the renderer and canvas size. This is also automatically called when a DOM size change is detected.
580
- */
581
- updateSize(force = false) {
582
- if (force || (!this.isManagedExternally && this.renderer.xr?.isPresenting === false)) {
583
- this._needsUpdateSize = false;
584
- const scaleFactor = this.resolutionScaleFactor;
585
- let width = this.domWidth * scaleFactor;
586
- let height = this.domHeight * scaleFactor;
587
- if (this.maxRenderResolution) {
588
- this.maxRenderResolution.x = Math.max(1, this.maxRenderResolution.x);
589
- width = Math.min(this.maxRenderResolution.x, width);
590
- this.maxRenderResolution.y = Math.max(1, this.maxRenderResolution.y);
591
- height = Math.min(this.maxRenderResolution.y, height);
592
- }
593
- const camera = this.mainCamera;
594
- this.updateAspect(camera);
595
- this.renderer.setSize(width, height, true);
596
- // avoid setting pixel values here since this can cause pingpong updates
597
- // e.g. when system scale is set to 125%
598
- // https://github.com/needle-tools/needle-engine-support/issues/69
599
- this.renderer.domElement.style.width = "100%";
600
- this.renderer.domElement.style.height = "100%";
601
- const devicePixelRatio = typeof this.devicePixelRatio === "number"
602
- ? this.devicePixelRatio
603
- : this.devicePixelRatio === "auto"
604
- ? Math.min(2, window.devicePixelRatio) // clamp device pixel ratio to two for "automatic" mode
605
- : undefined;
606
- if (devicePixelRatio !== undefined) {
607
- this.renderer.setPixelRatio(devicePixelRatio);
608
- }
609
- if (this.composer) {
610
- this.composer.setSize?.call(this.composer, width, height);
611
- if (devicePixelRatio !== undefined && "setPixelRatio" in this.composer && typeof this.composer.setPixelRatio === "function")
612
- this.composer.setPixelRatio?.call(this.composer, window.devicePixelRatio);
613
- }
614
- }
615
- }
616
- /**
617
- * Update the camera aspect ratio or orthorgraphic camera size. This is automatically called when a DOM size change is detected.
618
- */
619
- updateAspect(camera, width, height) {
620
- if (!camera)
621
- return;
622
- if (width === undefined)
623
- width = this.domWidth;
624
- if (height === undefined)
625
- height = this.domHeight;
626
- const aspectRatio = width / height;
627
- if (camera.isPerspectiveCamera) {
628
- const cam = camera;
629
- const pa = cam.aspect;
630
- cam.aspect = aspectRatio;
631
- if (pa !== cam.aspect)
632
- camera.updateProjectionMatrix();
633
- }
634
- else if (camera.isOrthographicCamera) {
635
- const cam = camera;
636
- // Maintain the camera's current vertical size (top - bottom)
637
- const verticalSize = cam.top - cam.bottom;
638
- // Calculate new horizontal size based on aspect ratio
639
- const horizontalSize = verticalSize * aspectRatio;
640
- // Update camera bounds while maintaining center position
641
- const halfWidth = horizontalSize / 2;
642
- const halfHeight = verticalSize / 2;
643
- if (cam.left != -halfWidth || cam.top != halfHeight) {
644
- cam.left = -halfWidth;
645
- cam.right = halfWidth;
646
- cam.top = halfHeight;
647
- cam.bottom = -halfHeight;
648
- camera.updateProjectionMatrix();
649
- }
650
- }
651
- }
652
- /** This will recreate the whole needle engine context and dispose the whole scene content
653
- * All content will be reloaded (loading times might be faster due to browser caches)
654
- * All scripts will be recreated */
655
- recreate() {
656
- this.clear();
657
- this.create(this._originalCreationArgs);
658
- }
659
- _originalCreationArgs;
660
- /** @deprecated use create. This method will be removed in a future version */
661
- async onCreate(opts) {
662
- return this.create(opts);
663
- }
664
- /** @internal */
665
- async create(opts) {
666
- try {
667
- this._isCreating = true;
668
- if (opts !== this._originalCreationArgs)
669
- this._originalCreationArgs = deepClone(opts);
670
- window.addEventListener("unhandledrejection", this.onUnhandledRejection);
671
- const res = await this.internalOnCreate(opts);
672
- this._isCreated = res;
673
- return res;
674
- }
675
- finally {
676
- window.removeEventListener("unhandledrejection", this.onUnhandledRejection);
677
- this._isCreating = false;
678
- }
679
- }
680
- onUnhandledRejection = (event) => {
681
- this.onError(event.reason);
682
- };
683
- /** Dispatches an error */
684
- onError(error) {
685
- this.domElement.dispatchEvent(new CustomEvent("error", { detail: error }));
686
- }
687
- /**
688
- * Clears the context and destroys all scenes and objects in the scene.
689
- * The ContextCleared event is called at the end.
690
- * This is automatically called when e.g. the `src` attribute changes on `<needle-engine>`
691
- * or when the web component is removed from the DOM
692
- */
693
- clear() {
694
- ContextRegistry.dispatchCallback(ContextEvent.ContextClearing, this);
695
- invokeLifecycleFunctions(this, ContextEvent.ContextClearing);
696
- // NOTE: this does dispose the environment/background image too
697
- // which is probably not desired if it is set via the background-image attribute
698
- destroy(this.scene, true, true);
699
- this.scene = new Scene();
700
- this.addressables?.dispose();
701
- this.lightmaps?.clear();
702
- this.physics?.engine?.clearCaches();
703
- this.lodsManager.disable();
704
- this._onBeforeRenderListeners.clear();
705
- this._onAfterRenderListeners.clear();
706
- if (!this.isManagedExternally) {
707
- if (this.renderer) {
708
- this.renderer.renderLists.dispose();
709
- this.renderer.state.reset();
710
- this.renderer.resetState();
711
- }
712
- }
713
- // We do not want to clear the renderer here because when switching src we want to keep the last rendered frame in case the loading screen is not visible
714
- // if a user wants to see the background they can still call setClearAlpha(0) and clear manually
715
- ContextRegistry.dispatchCallback(ContextEvent.ContextCleared, this);
716
- }
717
- /**
718
- * Dispose all allocated resources and clears the scene. This is automatically called e.g. when the `<needle-engine>` component is removed from the DOM.
719
- */
720
- dispose() {
721
- this.internalOnDestroy();
722
- }
723
- /**@deprecated use dispose() */
724
- onDestroy() { this.internalOnDestroy(); }
725
- internalOnDestroy() {
726
- Context.Current = this;
727
- ContextRegistry.dispatchCallback(ContextEvent.ContextDestroying, this);
728
- invokeLifecycleFunctions(this, ContextEvent.ContextDestroying);
729
- this.clear();
730
- this.renderer?.setAnimationLoop(null);
731
- if (this.renderer) {
732
- this.renderer.setClearAlpha(0);
733
- this.renderer.clear();
734
- if (!this.isManagedExternally) {
735
- if (debug)
736
- console.log("Disposing renderer");
737
- this.renderer.dispose();
738
- }
739
- }
740
- this.scene = null;
741
- this.renderer = null;
742
- this.input.dispose();
743
- this.menu.onDestroy();
744
- this.animations.onDestroy();
745
- for (const cb of this._disposeCallbacks) {
746
- try {
747
- cb();
748
- }
749
- catch (e) {
750
- console.error("Error in on dispose callback:", e, cb);
751
- }
752
- }
753
- if (this.domElement?.parentElement) {
754
- this.domElement.parentElement.removeChild(this.domElement);
755
- }
756
- this._isCreated = false;
757
- ContextRegistry.dispatchCallback(ContextEvent.ContextDestroyed, this);
758
- invokeLifecycleFunctions(this, ContextEvent.ContextDestroyed);
759
- ContextRegistry.unregister(this);
760
- if (Context.Current === this) {
761
- //@ts-ignore
762
- Context.Current = null;
763
- }
764
- }
765
- /** @internal Automatically called by components when you call `startCoroutine`. Use `startCoroutine` instead */
766
- registerCoroutineUpdate(script, coroutine, evt) {
767
- if (typeof coroutine?.next !== "function") {
768
- console.error("Registered invalid coroutine function from " + script.name + "\nCoroutine functions must be generators: \"*myCoroutine() {...}\"\nStart a coroutine from a component by calling \"this.startCoroutine(myCoroutine())\"");
769
- return coroutine;
770
- }
771
- if (!this.coroutines[evt])
772
- this.coroutines[evt] = [];
773
- this.coroutines[evt].push({ comp: script, main: coroutine });
774
- return coroutine;
775
- }
776
- /** @internal Automatically called by components. */
777
- unregisterCoroutineUpdate(coroutine, evt) {
778
- if (!this.coroutines[evt])
779
- return;
780
- const idx = this.coroutines[evt].findIndex(c => c.main === coroutine);
781
- if (idx >= 0)
782
- this.coroutines[evt].splice(idx, 1);
783
- }
784
- /** @internal Automatically called */
785
- stopAllCoroutinesFrom(script) {
786
- for (const evt in this.coroutines) {
787
- const rout = this.coroutines[evt];
788
- for (let i = rout.length - 1; i >= 0; i--) {
789
- const r = rout[i];
790
- if (r.comp === script) {
791
- rout.splice(i, 1);
792
- }
793
- }
794
- }
795
- }
796
- _cameraStack = [];
797
- /** Change the main camera */
798
- setCurrentCamera(cam) {
799
- if (!cam)
800
- return;
801
- if (!cam.threeCamera)
802
- cam.buildCamera(); // < to build camera
803
- if (!cam.threeCamera) {
804
- console.warn("Camera component is missing camera", cam);
805
- return;
806
- }
807
- const index = this._cameraStack.indexOf(cam);
808
- if (index >= 0)
809
- this._cameraStack.splice(index, 1);
810
- this._cameraStack.push(cam);
811
- this.mainCameraComponent = cam;
812
- const camera = cam.threeCamera;
813
- if (camera.isPerspectiveCamera)
814
- this.updateAspect(camera);
815
- this.mainCameraComponent?.applyClearFlagsIfIsActiveCamera();
816
- }
817
- /**
818
- * Remove the camera from the mainCamera stack (if it has been set before with `setCurrentCamera`)
819
- */
820
- removeCamera(cam) {
821
- if (!cam)
822
- return;
823
- const index = this._cameraStack.indexOf(cam);
824
- if (index >= 0)
825
- this._cameraStack.splice(index, 1);
826
- if (this.mainCameraComponent === cam) {
827
- this.mainCameraComponent = undefined;
828
- if (this._cameraStack.length > 0) {
829
- const last = this._cameraStack[this._cameraStack.length - 1];
830
- this.setCurrentCamera(last);
831
- }
832
- }
833
- }
834
- // #region onBeforeRender / onAfterRender listeners
835
- _onBeforeRenderListeners = new Map();
836
- _onAfterRenderListeners = new Map();
837
- /** Use to subscribe to onBeforeRender events on threejs objects.
838
- * @link https://threejs.org/docs/#api/en/core/Object3D.onBeforeRender
839
- */
840
- addBeforeRenderListener(target, callback) {
841
- if (!this._onBeforeRenderListeners.has(target.uuid)) {
842
- const arr = [];
843
- this._onBeforeRenderListeners.set(target.uuid, arr);
844
- target.onBeforeRender = this._createRenderCallbackWrapper(arr);
845
- }
846
- this._onBeforeRenderListeners.get(target.uuid).push(callback);
847
- }
848
- /** Remove callback from three `onBeforeRender` event (if it has been added with `addBeforeRenderListener(...)`)
849
- * @link https://threejs.org/docs/#api/en/core/Object3D.onBeforeRender
850
- */
851
- removeBeforeRenderListener(target, callback) {
852
- if (this._onBeforeRenderListeners.has(target.uuid)) {
853
- const arr = this._onBeforeRenderListeners.get(target.uuid);
854
- const idx = arr.indexOf(callback);
855
- if (idx >= 0)
856
- arr.splice(idx, 1);
857
- }
858
- }
859
- /**
860
- * Subscribe to onAfterRender events on threejs objects
861
- * @link https://threejs.org/docs/#api/en/core/Object3D.onAfterRender
862
- */
863
- addAfterRenderListener(target, callback) {
864
- if (!this._onAfterRenderListeners.has(target.uuid)) {
865
- const arr = [];
866
- this._onAfterRenderListeners.set(target.uuid, arr);
867
- target.onAfterRender = this._createRenderCallbackWrapper(arr);
868
- }
869
- this._onAfterRenderListeners.get(target.uuid)?.push(callback);
870
- }
871
- /**
872
- * Remove from onAfterRender events on threejs objects
873
- * @link https://threejs.org/docs/#api/en/core/Object3D.onAfterRender
874
- */
875
- removeAfterRenderListener(target, callback) {
876
- if (this._onAfterRenderListeners.has(target.uuid)) {
877
- const arr = this._onAfterRenderListeners.get(target.uuid);
878
- const idx = arr.indexOf(callback);
879
- if (idx >= 0)
880
- arr.splice(idx, 1);
881
- }
882
- }
883
- _createRenderCallbackWrapper(array) {
884
- return (renderer, scene, camera, geometry, material, group) => {
885
- for (let i = 0; i < array.length; i++) {
886
- const fn = array[i];
887
- fn(renderer, scene, camera, geometry, material, group);
888
- }
889
- };
890
- }
891
- _requireDepthTexture = false;
892
- _requireColorTexture = false;
893
- _renderTarget;
894
- _isRendering = false;
895
- /** @returns true while the WebGL renderer is rendering (between onBeforeRender and onAfterRender events) */
896
- get isRendering() { return this._isRendering; }
897
- setRequireDepth(val) {
898
- this._requireDepthTexture = val;
899
- }
900
- setRequireColor(val) {
901
- this._requireColorTexture = val;
902
- }
903
- get depthTexture() {
904
- return this._renderTarget?.depthTexture || null;
905
- }
906
- get opaqueColorTexture() {
907
- return this._renderTarget?.texture || null;
908
- }
909
- /** @returns true if the `<needle-engine>` DOM element is visible on screen (`context.domElement`) */
910
- get isVisibleToUser() {
911
- if (this.isInXR)
912
- return true;
913
- if (!this._isVisible)
914
- return false;
915
- // Make sure not to call getComputedStyle multiple times per frame
916
- if (!this._needsVisibleUpdate && this._lastStyleComputedResult !== undefined)
917
- return this._lastStyleComputedResult;
918
- this._needsVisibleUpdate = false;
919
- const style = getComputedStyle(this.domElement);
920
- this._lastStyleComputedResult = style.visibility !== "hidden" && style.display !== "none" && style.opacity !== "0";
921
- return this._lastStyleComputedResult;
922
- }
923
- _needsVisibleUpdate = true;
924
- _lastStyleComputedResult = undefined;
925
- _createId = 0;
926
- // #region internal create
927
- async internalOnCreate(opts) {
928
- const createId = ++this._createId;
929
- if (debug)
930
- console.log("Creating context", this.name, opts);
931
- // wait for async imported dependencies to be loaded
932
- // see https://linear.app/needle/issue/NE-4445
933
- const dependenciesReady = globalThis["needle:dependencies:ready"];
934
- if (dependenciesReady instanceof Promise) {
935
- if (debug)
936
- console.log("Waiting for dependencies to be ready");
937
- await dependenciesReady
938
- .catch(err => {
939
- if (debug || isDevEnvironment()) {
940
- showBalloonError("Needle Engine dependencies failed to load. Please check the console for more details");
941
- const printedError = false;
942
- if (err instanceof ReferenceError) {
943
- let offendingComponentName = "YourComponentName";
944
- const offendingComponentStartIndex = err.message.indexOf("'");
945
- if (offendingComponentStartIndex > 0) {
946
- const offendingComponentEndIndex = err.message.indexOf("'", offendingComponentStartIndex + 1);
947
- if (offendingComponentEndIndex > 0) {
948
- const name = err.message.substring(offendingComponentStartIndex + 1, offendingComponentEndIndex);
949
- if (name.length > 3)
950
- offendingComponentName = name;
951
- }
952
- }
953
- console.error(`Needle Engine dependencies failed to load:\n\n# Make sure you don't have circular imports in your scripts!\n\nPossible solutions: \n→ Replace @serializable(${offendingComponentName}) in your script with @serializable(Behaviour)\n→ If you only need type information try importing the type only, e.g: import { type ${offendingComponentName} }\n\n---`, err);
954
- return;
955
- }
956
- if (!printedError) {
957
- console.error("Needle Engine dependencies failed to load", err);
958
- }
959
- }
960
- })
961
- .then(() => {
962
- if (debug)
963
- console.log("Needle Engine dependencies are ready");
964
- });
965
- }
966
- this.clear();
967
- const oldRenderer = this.renderer;
968
- // We only need to create a new renderer if we don't have one yet
969
- // We do prevent creating a new renderer here to avoid flickering when the context is created while the content is still being loaded. This can be the case where CSS transformations update the layout (e.g. scale() while loading + old canvas disposed but in the DOM layout.)
970
- const needsNewRenderer = !oldRenderer || oldRenderer["isDisposed"] === true;
971
- // stop the animation loop if its running during creation
972
- // since we do not want to start enabling scripts etc before they are deserialized
973
- if (this.isManagedExternally === false && (needsNewRenderer)) {
974
- this.createNewRenderer();
975
- }
976
- else {
977
- this.lodsManager.setRenderer(this.renderer);
978
- }
979
- this.renderer?.setAnimationLoop(null);
980
- Context.Current = this;
981
- await ContextRegistry.dispatchCallback(ContextEvent.ContextCreationStart, this);
982
- // load and create scene
983
- let prepare_succeeded = true;
984
- let loadedFiles;
985
- try {
986
- Context.Current = this;
987
- if (opts) {
988
- loadedFiles = await this.internalLoadInitialContent(createId, opts);
989
- }
990
- else
991
- loadedFiles = [];
992
- }
993
- catch (err) {
994
- console.error(err);
995
- prepare_succeeded = false;
996
- }
997
- if (!prepare_succeeded) {
998
- this.onError("Failed to load initial content");
999
- return false;
1000
- }
1001
- if (createId !== this._createId || opts?.abortSignal?.aborted) {
1002
- return false;
1003
- }
1004
- this.internalOnUpdateVisible();
1005
- if (!this.renderer) {
1006
- if (debug)
1007
- console.warn("Context has no renderer (perhaps it was disconnected?", this.domElement.isConnected);
1008
- return false;
1009
- }
1010
- if (!this.isManagedExternally && !this.domElement.shadowRoot) {
1011
- this.domElement.prepend(this.renderer.domElement);
1012
- }
1013
- Context.Current = this;
1014
- // TODO: we could configure if we need physics
1015
- // await this.physics.engine?.initialize();
1016
- // Setup
1017
- Context.Current = this;
1018
- for (let i = 0; i < this.new_scripts.length; i++) {
1019
- const script = this.new_scripts[i];
1020
- if (script.gameObject !== undefined && script.gameObject !== null) {
1021
- if (script.gameObject.userData === undefined)
1022
- script.gameObject.userData = {};
1023
- if (script.gameObject.userData.components === undefined)
1024
- script.gameObject.userData.components = [];
1025
- const arr = script.gameObject.userData.components;
1026
- if (!arr.includes(script))
1027
- arr.push(script);
1028
- }
1029
- // if (script.gameObject && !this.raycastTargets.includes(script.gameObject)) {
1030
- // this.raycastTargets.push(script.gameObject);
1031
- // }
1032
- }
1033
- // const context = new SerializationContext(this.scene);
1034
- // for (let i = 0; i < this.new_scripts.length; i++) {
1035
- // const script = this.new_scripts[i];
1036
- // const ser = script as unknown as ISerializable;
1037
- // if (ser.$serializedTypes === undefined) continue;
1038
- // context.context = this;
1039
- // context.object = script.gameObject;
1040
- // deserializeObject(ser, script, context);
1041
- // }
1042
- // resolve post setup callbacks (things that rely on threejs objects having references to components)
1043
- if (this.post_setup_callbacks) {
1044
- for (let i = 0; i < this.post_setup_callbacks.length; i++) {
1045
- Context.Current = this;
1046
- await this.post_setup_callbacks[i](this);
1047
- }
1048
- }
1049
- if (!this._mainCamera) {
1050
- Context.Current = this;
1051
- let camera = null;
1052
- foreachComponent(this.scene, comp => {
1053
- const cam = comp;
1054
- if (cam?.isCamera) {
1055
- looputils.updateActiveInHierarchyWithoutEventCall(cam.gameObject);
1056
- if (!cam.activeAndEnabled)
1057
- return undefined;
1058
- if (cam.tag === "MainCamera") {
1059
- camera = cam;
1060
- return true;
1061
- }
1062
- else
1063
- camera = cam;
1064
- }
1065
- return undefined;
1066
- });
1067
- if (camera) {
1068
- this.setCurrentCamera(camera);
1069
- }
1070
- else {
1071
- const res = ContextRegistry.dispatchCallback(ContextEvent.MissingCamera, this, { files: loadedFiles });
1072
- if (!res && !this.mainCamera && !this.isManagedExternally)
1073
- console.warn("Missing camera in main scene", this);
1074
- }
1075
- }
1076
- this.input.bindEvents();
1077
- Context.Current = this;
1078
- looputils.processNewScripts(this);
1079
- // We have to step once so that colliders that have been created in onEnable can be raycasted in start
1080
- if (this.physics.engine) {
1081
- this.physics.engine?.step(0);
1082
- this.physics.engine?.postStep();
1083
- }
1084
- // const mainCam = this.mainCameraComponent as Camera;
1085
- // if (mainCam) {
1086
- // mainCam.applyClearFlagsIfIsActiveCamera();
1087
- // }
1088
- if (!this.isManagedExternally && this.composer && this.mainCamera) {
1089
- // TODO: import postprocessing async
1090
- // const renderPass = new RenderPass(this.scene, this.mainCamera);
1091
- // this.renderer.setSize(this.domWidth, this.domHeight);
1092
- // this.composer.addPass(renderPass);
1093
- // this.composer.setSize(this.domWidth, this.domHeight);
1094
- }
1095
- this._needsUpdateSize = true;
1096
- if (this._stats) {
1097
- this._stats.showPanel(0);
1098
- this._stats.dom.style.position = "absolute"; // (default is fixed)
1099
- this.domElement.shadowRoot?.appendChild(this._stats.dom);
1100
- }
1101
- if (debug)
1102
- logHierarchy(this.scene, true);
1103
- // If no target framerate was set we use the default
1104
- if (this.targetFrameRate === undefined) {
1105
- if (debug)
1106
- console.warn("No target framerate set, using default", Context.DefaultTargetFrameRate);
1107
- // the _defaultTargetFramerate is intentionally an object so it can be changed at any time if not explictly set by the user
1108
- this.targetFrameRate = Context._defaultTargetFramerate;
1109
- }
1110
- else if (debug)
1111
- console.log("Target framerate set to", this.targetFrameRate);
1112
- this._dispatchReadyAfterFrame = true;
1113
- const res = ContextRegistry.dispatchCallback(ContextEvent.ContextCreated, this, { files: loadedFiles });
1114
- if (res) {
1115
- const domElement = this.domElement;
1116
- if ("internalSetLoadingMessage" in domElement && typeof domElement.internalSetLoadingMessage === "function")
1117
- domElement?.internalSetLoadingMessage("finish loading");
1118
- await res;
1119
- }
1120
- if (opts?.abortSignal?.aborted) {
1121
- return false;
1122
- }
1123
- const mainIdentifier = this.rootSourceId;
1124
- if (mainIdentifier)
1125
- this.sceneLighting.enable(mainIdentifier);
1126
- invokeLifecycleFunctions(this, ContextEvent.ContextCreated);
1127
- if (debug)
1128
- console.log("Context Created...", this.renderer, this.renderer.domElement);
1129
- this._isCreating = false;
1130
- if (!this.isManagedExternally && !opts?.abortSignal?.aborted)
1131
- this.restartRenderLoop();
1132
- return true;
1133
- }
1134
- rootSceneSourceIdentifiers = [];
1135
- async internalLoadInitialContent(createId, args) {
1136
- this.rootSceneSourceIdentifiers.length = 0;
1137
- const results = new Array();
1138
- // early out if we dont have any files to load
1139
- if (args.files.length === 0)
1140
- return results;
1141
- const files = [...args.files];
1142
- this.rootSceneSourceIdentifiers.push(...files);
1143
- const progressArg = {
1144
- name: "",
1145
- progress: null,
1146
- index: 0,
1147
- count: files.length
1148
- };
1149
- const loader = getLoader();
1150
- // this hash should be constant since it is used to initialize the UIDProvider per initially loaded scene
1151
- const loadingHash = 0;
1152
- for (let i = 0; i < files.length; i++) {
1153
- if (args.abortSignal?.aborted) {
1154
- if (debug)
1155
- console.log("Aborting loading because of abort signal");
1156
- break;
1157
- }
1158
- // abort loading if the create id has changed
1159
- if (createId !== this._createId) {
1160
- if (debug)
1161
- console.log("Aborting loading because create id changed", createId, this._createId);
1162
- break;
1163
- }
1164
- const file = files[i];
1165
- args?.onLoadingStart?.call(this, i, file);
1166
- if (debug)
1167
- console.log("Context Load " + file);
1168
- const res = await loader.loadSync(this, file, file, loadingHash, prog => {
1169
- if (args.abortSignal?.aborted)
1170
- return;
1171
- progressArg.name = file;
1172
- progressArg.progress = prog;
1173
- progressArg.index = i;
1174
- progressArg.count = files.length;
1175
- args.onLoadingProgress?.call(this, progressArg);
1176
- });
1177
- args?.onLoadingFinished?.call(this, i, file, res ?? null);
1178
- if (res) {
1179
- results.push({
1180
- src: file,
1181
- file: res
1182
- });
1183
- }
1184
- else {
1185
- // a file could not be loaded
1186
- console.warn("Could not load file: " + file);
1187
- }
1188
- }
1189
- // if the id was changed while still loading
1190
- // then we want to cleanup/destroy previously loaded files
1191
- if (createId !== this._createId || args.abortSignal?.aborted) {
1192
- if (debug)
1193
- console.log("Aborting loading because create id changed or abort signal was set", createId, this._createId);
1194
- for (const res of results) {
1195
- if (res && res.file) {
1196
- for (const scene of res.file.scenes)
1197
- destroy(scene, true, true);
1198
- }
1199
- }
1200
- }
1201
- // otherwise we want to add the loaded files to the current scene
1202
- else {
1203
- let anyModelFound = false;
1204
- for (const res of results) {
1205
- if (res && res.file) {
1206
- // TODO: should we load all scenes in a glTF here?
1207
- if (res.file.scene) {
1208
- anyModelFound = true;
1209
- this.scene.add(res.file.scene);
1210
- }
1211
- else {
1212
- console.warn("No scene found in loaded file");
1213
- }
1214
- }
1215
- }
1216
- // If the loaded files do not contain ANY model
1217
- // We then attempt to create a mesh from each material in the loaded files to visualize it
1218
- // It's ok to do this at this point because we know the context has been cleared because the whole `src` attribute has been set
1219
- if (!anyModelFound) {
1220
- for (const res of results) {
1221
- if (res && res.file && "parser" in res.file) {
1222
- let y = 0;
1223
- if (!Array.isArray(res.file.parser.json.materials))
1224
- continue;
1225
- for (let i = 0; i < res.file.parser.json.materials.length; i++) {
1226
- const mat = await res.file.parser.getDependency("material", i);
1227
- const parent = new Object3D();
1228
- parent.position.x = i * 1.1;
1229
- parent.position.y = y;
1230
- this.scene.add(parent);
1231
- ObjectUtils.createPrimitive("ShaderBall", {
1232
- parent,
1233
- material: mat
1234
- });
1235
- }
1236
- y += 1;
1237
- }
1238
- }
1239
- }
1240
- }
1241
- return results;
1242
- }
1243
- /** Sets the animation loop.
1244
- * Can not be done while creating the context or when disposed
1245
- **/
1246
- restartRenderLoop() {
1247
- if (!this.renderer) {
1248
- console.error("Can not start render loop without renderer");
1249
- return false;
1250
- }
1251
- if (this._isCreating) {
1252
- console.warn("Can not start render loop while creating context");
1253
- return false;
1254
- }
1255
- this.renderer.setAnimationLoop((timestamp, frame) => {
1256
- if (this.isManagedExternally)
1257
- return;
1258
- this.update(timestamp, frame);
1259
- });
1260
- return true;
1261
- }
1262
- _renderlooperrors = 0;
1263
- /** Performs a full update step including script callbacks, rendering (unless isManagedExternally is set to false) and post render callbacks */
1264
- update(timestamp, frame) {
1265
- if (frame === undefined)
1266
- frame = null;
1267
- if (isDevEnvironment() || debug || looputils.hasNewScripts()) {
1268
- try {
1269
- //performance.mark('update.start');
1270
- this.internalStep(timestamp, frame);
1271
- this._renderlooperrors = 0;
1272
- //performance.mark('update.end');
1273
- //performance.measure('NE Frame', 'update.start', 'update.end');
1274
- }
1275
- catch (err) {
1276
- this._renderlooperrors += 1;
1277
- if ((isDevEnvironment() || debug) && (err instanceof Error || err instanceof TypeError))
1278
- showBalloonMessage(`Caught unhandled exception during render-loop - see console for details.`, LogType.Error);
1279
- console.error("Frame #" + this.time.frame + "\n", err);
1280
- if (this._renderlooperrors >= 3) {
1281
- console.warn("Stopping render loop due to error");
1282
- this.renderer.setAnimationLoop(null);
1283
- }
1284
- this.domElement.dispatchEvent(new CustomEvent("error", { detail: err }));
1285
- }
1286
- }
1287
- else {
1288
- this.internalStep(timestamp, frame);
1289
- }
1290
- }
1291
- /** Call to **manually** perform physics steps.
1292
- * By default the context uses the `physicsSteps` property to perform steps during the update loop
1293
- * If you just want to increase the accuracy of physics you can instead set the `physicsSteps` property to a higher value
1294
- * */
1295
- updatePhysics(steps) {
1296
- this.internalUpdatePhysics(steps);
1297
- }
1298
- /**
1299
- * Set a rect or dom element. The camera center will be moved to the center of the rect.
1300
- * This is useful if you have Needle Engine embedded in a HTML layout and while you want the webgl background to fill e.g. the whole screen you want to move the camera center to free space.
1301
- * For that you can simply pass in the rect or HMTL div that you want the camera to center on.
1302
- * @param rect The focus rect or null to disable
1303
- * @param settings Optional settings for the focus rect. These will override the `focusRectSettings` property
1304
- */
1305
- setCameraFocusRect(rect, settings) {
1306
- const oldRect = this._focusRect;
1307
- this._focusRect = rect;
1308
- if (settings) {
1309
- Object.assign(this.focusRectSettings, settings);
1310
- }
1311
- if (settings?.damping === undefined) {
1312
- // if the new rect is on screen then set damping
1313
- if (oldRect) {
1314
- let domRect = oldRect;
1315
- if (oldRect instanceof HTMLElement) {
1316
- domRect = oldRect.getBoundingClientRect();
1317
- }
1318
- if (domRect && "top" in domRect) {
1319
- const allowedDistance = 100;
1320
- const isVisible = domRect.bottom >= -allowedDistance && domRect.right >= -allowedDistance && domRect.top <= window.innerHeight + allowedDistance && domRect.left <= window.innerWidth + allowedDistance;
1321
- if (isVisible)
1322
- this.focusRectSettings.damping = .2;
1323
- }
1324
- }
1325
- }
1326
- }
1327
- get focusRect() { return this._focusRect; }
1328
- get focusRectSize() {
1329
- const rect = this._focusRect;
1330
- if (rect && (rect instanceof DOMRect || ("width" in rect && "height" in rect && "x" in rect && "y" in rect))) {
1331
- return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
1332
- }
1333
- else if (rect instanceof HTMLElement) {
1334
- const r = rect.getBoundingClientRect();
1335
- return { x: r.x, y: r.y, width: r.width, height: r.height };
1336
- }
1337
- return null;
1338
- }
1339
- /** Settings when a focus rect is set. Use `setCameraFocusRect(...)` to do so.
1340
- * This can be used to offset the renderer center e.g. to a specific DOM element.
1341
- */
1342
- focusRectSettings = {
1343
- /** Controls how fast the rect is centered. Smaller values mean the rect is centered faster.
1344
- * A minimum value of 0 means the rect is centered instantly.
1345
- * @default 0
1346
- */
1347
- damping: 0,
1348
- /**
1349
- * Zoom factor when a focus rect is set.
1350
- */
1351
- zoom: 1,
1352
- /**
1353
- * Additional offset in pixels from the center of the rect
1354
- */
1355
- offsetX: 0,
1356
- /**
1357
- * Additional offset in pixels from the center of the rect
1358
- */
1359
- offsetY: 0,
1360
- };
1361
- _focusRect = null;
1362
- _lastTimestamp = 0;
1363
- _accumulatedTime = 0;
1364
- _dispatchReadyAfterFrame = false;
1365
- // TODO: we need to skip after render callbacks if the render loop is managed externally. When changing this we also need to to update the r3f sample
1366
- internalStep(timestamp, frame) {
1367
- if (this.internalOnBeforeRender(timestamp, frame) === false)
1368
- return;
1369
- this.internalOnRender();
1370
- this.internalOnAfterRender();
1371
- }
1372
- internalOnBeforeRender(timestamp, frame) {
1373
- // If we don't auto reset we get wrong stats in WebXR. AutoReset was turned off to support custom blits and count them too
1374
- // But when we're using postprocessing we need to reset manually: https://discourse.threejs.org/t/accessing-draw-calls-when-using-effectcomposer
1375
- this.renderer.info.autoReset = frame ? true : false;
1376
- if (this.renderer.info.autoReset === false) {
1377
- this.renderer.info.reset();
1378
- }
1379
- this._needsVisibleUpdate = true;
1380
- const sessionStarted = frame !== null && this._xrFrame === null;
1381
- this._xrFrame = frame;
1382
- if (sessionStarted) {
1383
- this.domElement.dispatchEvent(new CustomEvent("xr-session-started", { detail: { context: this, session: this.xrSession, frame: frame } }));
1384
- }
1385
- this._currentFrameEvent = FrameEvent.Undefined;
1386
- if (this.isManagedExternally === false && this.isInXR === false && this.targetFrameRate !== undefined) {
1387
- if (this._lastTimestamp === 0)
1388
- this._lastTimestamp = timestamp;
1389
- this._accumulatedTime += (timestamp - this._lastTimestamp) / 1000;
1390
- this._lastTimestamp = timestamp;
1391
- let targetFrameRate = this.targetFrameRate;
1392
- if (typeof targetFrameRate === "object")
1393
- targetFrameRate = targetFrameRate.value;
1394
- // if(debug) console.log(this._accumulatedTime, (1 / (targetFrameRate)))
1395
- if (this._accumulatedTime < (1 / (targetFrameRate + 1))) {
1396
- return false;
1397
- }
1398
- this._accumulatedTime = 0;
1399
- }
1400
- this._stats?.begin();
1401
- Context.Current = this;
1402
- if (this.onHandlePaused())
1403
- return false;
1404
- Context.Current = this;
1405
- this.time.update();
1406
- if (debugframerate)
1407
- console.log("FPS", (this.time.smoothedFps).toFixed(0));
1408
- looputils.processNewScripts(this);
1409
- looputils.updateIsActive(this.scene);
1410
- looputils.processStart(this);
1411
- invokeLifecycleFunctions(this, FrameEvent.Start);
1412
- while (this._cameraStack.length > 0 && (!this.mainCameraComponent || this.mainCameraComponent.destroyed)) {
1413
- this._cameraStack.splice(this._cameraStack.length - 1, 1);
1414
- const last = this._cameraStack[this._cameraStack.length - 1];
1415
- this.setCurrentCamera(last);
1416
- }
1417
- if (this.pre_update_oneshot_callbacks) {
1418
- for (const i in this.pre_update_oneshot_callbacks) {
1419
- this.pre_update_oneshot_callbacks[i]();
1420
- }
1421
- this.pre_update_oneshot_callbacks.length = 0;
1422
- }
1423
- if (this.pre_update_callbacks) {
1424
- for (const i in this.pre_update_callbacks) {
1425
- this.pre_update_callbacks[i]();
1426
- }
1427
- }
1428
- this._currentFrameEvent = FrameEvent.EarlyUpdate;
1429
- for (let i = 0; i < this.scripts_earlyUpdate.length; i++) {
1430
- const script = this.scripts_earlyUpdate[i];
1431
- if (!script.activeAndEnabled)
1432
- continue;
1433
- if (script.earlyUpdate !== undefined) {
1434
- Context.Current = this;
1435
- script.earlyUpdate();
1436
- }
1437
- }
1438
- this.executeCoroutines(FrameEvent.EarlyUpdate);
1439
- invokeLifecycleFunctions(this, FrameEvent.EarlyUpdate);
1440
- this._currentFrameEvent = FrameEvent.Update;
1441
- for (let i = 0; i < this.scripts_update.length; i++) {
1442
- const script = this.scripts_update[i];
1443
- if (!script.activeAndEnabled)
1444
- continue;
1445
- if (script.update !== undefined) {
1446
- Context.Current = this;
1447
- script.update();
1448
- }
1449
- }
1450
- this.executeCoroutines(FrameEvent.Update);
1451
- invokeLifecycleFunctions(this, FrameEvent.Update);
1452
- this._currentFrameEvent = FrameEvent.LateUpdate;
1453
- for (let i = 0; i < this.scripts_lateUpdate.length; i++) {
1454
- const script = this.scripts_lateUpdate[i];
1455
- if (!script.activeAndEnabled)
1456
- continue;
1457
- if (script.lateUpdate !== undefined) {
1458
- Context.Current = this;
1459
- script.lateUpdate();
1460
- }
1461
- }
1462
- // this.mainLight = null;
1463
- this.executeCoroutines(FrameEvent.LateUpdate);
1464
- invokeLifecycleFunctions(this, FrameEvent.LateUpdate);
1465
- if (this.physicsSteps === undefined) {
1466
- this.physicsSteps = 1;
1467
- }
1468
- if (this.physics.engine && this.physicsSteps > 0) {
1469
- this.internalUpdatePhysics(this.physicsSteps);
1470
- }
1471
- if (this.isVisibleToUser || this.runInBackground) {
1472
- this._currentFrameEvent = FrameEvent.OnBeforeRender;
1473
- // should we move these callbacks in the regular three onBeforeRender events?
1474
- for (let i = 0; i < this.scripts_onBeforeRender.length; i++) {
1475
- const script = this.scripts_onBeforeRender[i];
1476
- if (!script.activeAndEnabled)
1477
- continue;
1478
- // if(script.isActiveAndEnabled === false) continue;
1479
- if (script.onBeforeRender !== undefined) {
1480
- Context.Current = this;
1481
- script.onBeforeRender(frame);
1482
- }
1483
- }
1484
- this.executeCoroutines(FrameEvent.OnBeforeRender);
1485
- invokeLifecycleFunctions(this, FrameEvent.OnBeforeRender);
1486
- if (this._needsUpdateSize)
1487
- this.updateSize();
1488
- if (this.pre_render_callbacks) {
1489
- for (const i in this.pre_render_callbacks) {
1490
- this.pre_render_callbacks[i](frame);
1491
- }
1492
- }
1493
- if (this._focusRect) {
1494
- if (this.mainCamera instanceof PerspectiveCamera) {
1495
- const settings = this.focusRectSettings;
1496
- const dt = settings.damping > 0 ? this.time.deltaTime / settings.damping : 1;
1497
- updateCameraFocusRect(this._focusRect, this.focusRectSettings, dt, this.mainCamera, this.renderer);
1498
- }
1499
- }
1500
- }
1501
- return true;
1502
- }
1503
- internalUpdatePhysics(steps) {
1504
- if (!this.physics.engine)
1505
- return false;
1506
- const physicsSteps = steps;
1507
- const dt = this.time.deltaTime / physicsSteps;
1508
- for (let i = 0; i < physicsSteps; i++) {
1509
- this._currentFrameEvent = FrameEvent.PrePhysicsStep;
1510
- this.executeCoroutines(FrameEvent.PrePhysicsStep);
1511
- this.physics.engine.step(dt);
1512
- this._currentFrameEvent = FrameEvent.PostPhysicsStep;
1513
- this.executeCoroutines(FrameEvent.PostPhysicsStep);
1514
- }
1515
- this.physics.engine.postStep();
1516
- return true;
1517
- }
1518
- internalOnRender() {
1519
- if (!this.isManagedExternally) {
1520
- // when loading assets we compile them async after GLTFLoader is done
1521
- // but as a fallback we still register them (if e.g. there's no camera for compile async)
1522
- looputils.runPrewarm(this);
1523
- this._currentFrameEvent = FrameEvent.Undefined;
1524
- nodeFrame.camera = this.mainCamera;
1525
- nodeFrame.update();
1526
- this.renderNow();
1527
- this._currentFrameEvent = FrameEvent.OnAfterRender;
1528
- }
1529
- }
1530
- internalOnAfterRender() {
1531
- if (this.isVisibleToUser || this.runInBackground) {
1532
- for (let i = 0; i < this.scripts_onAfterRender.length; i++) {
1533
- const script = this.scripts_onAfterRender[i];
1534
- if (!script.activeAndEnabled)
1535
- continue;
1536
- if (script.onAfterRender !== undefined) {
1537
- Context.Current = this;
1538
- script.onAfterRender();
1539
- }
1540
- }
1541
- this.executeCoroutines(FrameEvent.OnAfterRender);
1542
- invokeLifecycleFunctions(this, FrameEvent.OnAfterRender);
1543
- if (this.post_render_callbacks) {
1544
- for (const i in this.post_render_callbacks) {
1545
- this.post_render_callbacks[i]();
1546
- }
1547
- }
1548
- }
1549
- this._currentFrameEvent = -1;
1550
- this.connection.sendBufferedMessagesNow();
1551
- if (this._stats) {
1552
- this._stats.end();
1553
- const dt = this.time.fps < 20 ? 50 : 150;
1554
- if (this.time.frameCount % dt === 0)
1555
- console.log(this.renderer.info.render.calls + " DrawCalls", "\nRender:", { ...this.renderer.info.render }, "\nMemory:", { ...this.renderer.info.memory }, "\nTarget Framerate: " + this.targetFrameRate);
1556
- }
1557
- if (this._dispatchReadyAfterFrame) {
1558
- this._dispatchReadyAfterFrame = false;
1559
- this.domElement.dispatchEvent(new CustomEvent("ready"));
1560
- ContextRegistry.dispatchCallback(ContextEvent.ContextFirstFrameRendered, this);
1561
- }
1562
- }
1563
- _tempClearColor = new Color();
1564
- _tempClearColor2 = new Color();
1565
- renderNow(camera) {
1566
- if (!camera) {
1567
- camera = this.mainCamera;
1568
- if (!camera)
1569
- return false;
1570
- }
1571
- this.handleRendererContextLost();
1572
- this._isRendering = true;
1573
- this.renderRequiredTextures();
1574
- if (this.renderer.toneMapping !== NoToneMapping)
1575
- patchTonemapping(this);
1576
- if (this.composer && !this.isInXR) {
1577
- // if a camera is passed in we need to check if we need to update the composer's camera
1578
- if (camera && "setMainCamera" in this.composer) {
1579
- const currentPassesCamera = this.composer.passes[0]?.mainCamera;
1580
- if (currentPassesCamera != camera)
1581
- this.composer.setMainCamera(camera);
1582
- }
1583
- const backgroundColor = this.renderer.getClearColor(this._tempClearColor);
1584
- const clearAlpha = this.renderer.getClearAlpha();
1585
- this._tempClearColor2.copy(backgroundColor);
1586
- this.renderer.setClearColor(backgroundColor.convertSRGBToLinear(), this.renderer.getClearAlpha());
1587
- this.composer.render(this.time.deltaTime);
1588
- this.renderer.setClearColor(this._tempClearColor2, clearAlpha); // restore clear color
1589
- }
1590
- else if (camera) {
1591
- // Workaround for issue on Vision Pro –
1592
- // depth buffer is not cleared between eye draws, despite the spec...
1593
- if (this.isInXR && DeviceUtilities.isMacOS())
1594
- this.renderer.clearDepth();
1595
- this.renderer.render(this.scene, camera);
1596
- }
1597
- this._isRendering = false;
1598
- return true;
1599
- }
1600
- _contextRestoreTries = 0;
1601
- handleRendererContextLost() {
1602
- // Try to restore the context every x frames
1603
- if (this.time.frame % 10 && this.renderer.getContext().isContextLost()) {
1604
- if (this._contextRestoreTries++ < 100) {
1605
- console.warn("Attempting to recover WebGL context...");
1606
- this.renderer.forceContextRestore();
1607
- }
1608
- }
1609
- }
1610
- /** returns true if we should return out of the frame loop */
1611
- _wasPaused = false;
1612
- onHandlePaused() {
1613
- const paused = this.evaluatePaused();
1614
- if (this._wasPaused !== paused) {
1615
- if (debugActive)
1616
- console.log("Paused?", paused, "context:" + this.alias);
1617
- for (let i = 0; i < this.scripts_pausedChanged.length; i++) {
1618
- const script = this.scripts_pausedChanged[i];
1619
- if (!script.activeAndEnabled)
1620
- continue;
1621
- if (script.onPausedChanged !== undefined) {
1622
- Context.Current = this;
1623
- script.onPausedChanged(paused, this._wasPaused);
1624
- }
1625
- }
1626
- }
1627
- this._wasPaused = paused;
1628
- return paused;
1629
- }
1630
- evaluatePaused() {
1631
- if (this.isInXR)
1632
- return false;
1633
- if (this.isPaused)
1634
- return true;
1635
- // if the element is not visible use the runInBackground flag to determine if we should continue
1636
- if (this.runInBackground) {
1637
- return false;
1638
- }
1639
- const paused = !this.isVisibleToUser;
1640
- return paused;
1641
- }
1642
- renderRequiredTextures() {
1643
- if (!this.mainCamera)
1644
- return;
1645
- if (!this._requireDepthTexture && !this._requireColorTexture)
1646
- return;
1647
- if (!this._renderTarget) {
1648
- this._renderTarget = new WebGLRenderTarget(this.domWidth, this.domHeight);
1649
- if (this._requireDepthTexture) {
1650
- const dt = new DepthTexture(this.domWidth, this.domHeight);
1651
- ;
1652
- this._renderTarget.depthTexture = dt;
1653
- }
1654
- if (this._requireColorTexture) {
1655
- this._renderTarget.texture = new Texture();
1656
- this._renderTarget.texture.generateMipmaps = false;
1657
- this._renderTarget.texture.minFilter = NearestFilter;
1658
- this._renderTarget.texture.magFilter = NearestFilter;
1659
- this._renderTarget.texture.format = RGBAFormat;
1660
- }
1661
- }
1662
- const rt = this._renderTarget;
1663
- if (rt.texture) {
1664
- rt.texture.colorSpace = this.renderer.outputColorSpace;
1665
- }
1666
- const prevTarget = this.renderer.getRenderTarget();
1667
- this.renderer.setRenderTarget(rt);
1668
- this.renderer.render(this.scene, this.mainCamera);
1669
- this.renderer.setRenderTarget(prevTarget);
1670
- }
1671
- executeCoroutines(evt) {
1672
- if (this.coroutines[evt]) {
1673
- const evts = this.coroutines[evt];
1674
- for (let i = 0; i < evts.length; i++) {
1675
- try {
1676
- const evt = evts[i];
1677
- // TODO we might want to keep coroutines playing even if the component is disabled or inactive
1678
- const remove = !evt.comp || evt.comp.destroyed || !evt.main || evt.comp["enabled"] === false;
1679
- if (remove) {
1680
- if (debugCoroutine)
1681
- console.log("Removing coroutine", evt.comp, evt.comp["enabled"]);
1682
- evts.splice(i, 1);
1683
- --i;
1684
- continue;
1685
- }
1686
- const iter = evt.chained;
1687
- if (iter && iter.length > 0) {
1688
- const last = iter[iter.length - 1];
1689
- const res = last.next();
1690
- if (res.done) {
1691
- iter.pop();
1692
- }
1693
- if (isGenerator(res)) {
1694
- if (!evt.chained)
1695
- evt.chained = [];
1696
- evt.chained.push(res.value);
1697
- }
1698
- if (!res.done)
1699
- continue;
1700
- }
1701
- const res = evt.main.next();
1702
- if (res.done === true) {
1703
- evts.splice(i, 1);
1704
- --i;
1705
- continue;
1706
- }
1707
- const val = res.value;
1708
- if (isGenerator(val)) {
1709
- // invoke once if its a generator
1710
- // this means e.g. WaitForFrame(1) works and will capture
1711
- // the frame it was created
1712
- const gen = val;
1713
- const res = gen.next();
1714
- if (res.done)
1715
- continue;
1716
- if (!evt.chained)
1717
- evt.chained = [];
1718
- evt.chained.push(val);
1719
- }
1720
- else if (val instanceof Promise) {
1721
- // If its a promise we want to wait for it to resolve
1722
- const prom = val;
1723
- if (!evt.chained)
1724
- evt.chained = [];
1725
- const nested = WaitForPromise(prom);
1726
- evt.chained?.push(nested);
1727
- continue;
1728
- }
1729
- }
1730
- catch (e) {
1731
- console.error(e);
1732
- }
1733
- }
1734
- }
1735
- function isGenerator(val) {
1736
- if (val) {
1737
- if (val.next && val.return) {
1738
- return true;
1739
- }
1740
- }
1741
- return false;
1742
- }
1743
- }
1744
- }
1745
- // const scene = new Scene();
1746
- // const useComposer = utils.getParam("postfx");
1747
- // const renderer = new WebGLRenderer({ antialias: true });
1748
- // const composer = useComposer ? new EffectComposer(renderer) : undefined;
1749
- // renderer.setClearColor(new Color('lightgrey'), 0)
1750
- // renderer.antialias = true;
1751
- // renderer.alpha = false;
1752
- // renderer.shadowMap.enabled = true;
1753
- // renderer.shadowMap.type = PCFSoftShadowMap;
1754
- // renderer.setSize(window.innerWidth, window.innerHeight);
1755
- // renderer.outputEncoding = sRGBEncoding;
1756
- // renderer.physicallyCorrectLights = true;
1757
- // document.body.appendChild(renderer.domElement);
1758
- // // generation pushes loading requests in this array
1759
- // const sceneData: {
1760
- // mainCamera: Camera | undefined
1761
- // } = {
1762
- // preparing: [],
1763
- // resolving: [],
1764
- // scripts: [],
1765
- // raycastTargets: [],
1766
- // mainCamera: undefined,
1767
- // mainCameraComponent: undefined,
1768
- // };
1769
- // // contains a list of functions to be called after loading is done
1770
- // const post_setup_callbacks = [];
1771
- // const pre_render_Callbacks = [];
1772
- // const post_render_callbacks = [];
1773
- // const new_scripts = [];
1774
- // const new_scripts_post_setup_callbacks = [];
1775
- // const new_scripts_pre_setup_callbacks = [];
1776
- // export {
1777
- // scene, renderer, composer,
1778
- // new_scripts,
1779
- // new_scripts_post_setup_callbacks, new_scripts_pre_setup_callbacks,
1780
- // sceneData,
1781
- // post_setup_callbacks,
1782
- // pre_render_Callbacks,
1783
- // post_render_callbacks
1784
- // }
1
+ import 'three/examples/jsm/renderers/webgl-legacy/nodes/WebGLNodes.js';
2
+ import { Color, DepthTexture, NearestFilter, NoToneMapping, Object3D, PCFSoftShadowMap, PerspectiveCamera, RGBAFormat, Scene, SRGBColorSpace, Texture, WebGLRenderer, WebGLRenderTarget } from 'three';
3
+ /** @ts-ignore (not yet in types?) */
4
+ import { BasicNodeLibrary } from "three";
5
+ import * as Stats from 'three/examples/jsm/libs/stats.module.js';
6
+ import { nodeFrame } from "three/examples/jsm/renderers/webgl-legacy/nodes/WebGLNodeBuilder.js";
7
+ import { isDevEnvironment, LogType, showBalloonError, showBalloonMessage } from './debug/index.js';
8
+ import { Addressables } from './engine_addressables.js';
9
+ import { AnimationsRegistry } from './engine_animation.js';
10
+ import { Application } from './engine_application.js';
11
+ import { AssetDatabase } from './engine_assetdatabase.js';
12
+ import { updateCameraFocusRect } from './engine_camera.js';
13
+ import { VERSION } from './engine_constants.js';
14
+ import { ContextEvent, ContextRegistry } from './engine_context_registry.js';
15
+ import { WaitForPromise } from './engine_coroutine.js';
16
+ import { ObjectUtils } from "./engine_create_objects.js";
17
+ import { destroy, foreachComponent } from './engine_gameobject.js';
18
+ import { getLoader } from './engine_gltf.js';
19
+ import { Input } from './engine_input.js';
20
+ import { invokeLifecycleFunctions } from './engine_lifecycle_functions_internal.js';
21
+ import { LightDataRegistry } from './engine_lightdata.js';
22
+ import { LODsManager } from "./engine_lods.js";
23
+ import * as looputils from './engine_mainloop_utils.js';
24
+ import { NetworkConnection } from './engine_networking.js';
25
+ import { Physics } from './engine_physics.js';
26
+ import { PlayerViewManager } from './engine_playerview.js';
27
+ import { RendererData as SceneLighting } from './engine_scenelighting.js';
28
+ import { logHierarchy } from './engine_three_utils.js';
29
+ import { Time } from './engine_time.js';
30
+ import { patchTonemapping } from './engine_tonemapping.js';
31
+ import { deepClone, DeviceUtilities, getParam } from './engine_utils.js';
32
+ import { NeedleMenu } from './webcomponents/needle menu/needle-menu.js';
33
+ const debug = getParam("debugcontext");
34
+ const stats = getParam("stats");
35
+ const debugActive = getParam("debugactive");
36
+ const debugframerate = getParam("debugframerate");
37
+ const debugCoroutine = getParam("debugcoroutine");
38
+ // this is where functions that setup unity scenes will be pushed into
39
+ // those will be accessed from our custom html element to load them into their context
40
+ export const build_scene_functions = {};
41
+ export class ContextArgs {
42
+ name;
43
+ /** for debugging only */
44
+ alias;
45
+ /** the hash is used as a seed when initially loading the scene files */
46
+ hash;
47
+ /** when true the context will not check if it's visible in the viewport and always update and render */
48
+ runInBackground;
49
+ /** the DOM element the context belongs to or is inside of (this does not have to be the canvas. use renderer.domElement if you want to access the dom canvas) */
50
+ domElement;
51
+ /** externally owned renderer */
52
+ renderer;
53
+ /** externally owned camera */
54
+ camera;
55
+ /** externally owned scene */
56
+ scene;
57
+ }
58
+ export var FrameEvent;
59
+ (function (FrameEvent) {
60
+ FrameEvent[FrameEvent["Start"] = -1] = "Start";
61
+ FrameEvent[FrameEvent["EarlyUpdate"] = 0] = "EarlyUpdate";
62
+ FrameEvent[FrameEvent["Update"] = 1] = "Update";
63
+ FrameEvent[FrameEvent["LateUpdate"] = 2] = "LateUpdate";
64
+ FrameEvent[FrameEvent["OnBeforeRender"] = 3] = "OnBeforeRender";
65
+ FrameEvent[FrameEvent["OnAfterRender"] = 4] = "OnAfterRender";
66
+ FrameEvent[FrameEvent["PrePhysicsStep"] = 9] = "PrePhysicsStep";
67
+ FrameEvent[FrameEvent["PostPhysicsStep"] = 10] = "PostPhysicsStep";
68
+ FrameEvent[FrameEvent["Undefined"] = -1] = "Undefined";
69
+ })(FrameEvent || (FrameEvent = {}));
70
+ export function registerComponent(script, context) {
71
+ if (!script)
72
+ return;
73
+ if (!script.isComponent) {
74
+ if (isDevEnvironment() || debug)
75
+ console.error("Registered script is not a Needle Engine component. \nThe script will be ignored. Please make sure your component extends \"Behaviour\" imported from \"@needle-tools/engine\"\n", script);
76
+ return;
77
+ }
78
+ if (!context) {
79
+ context = Context.Current;
80
+ if (debug)
81
+ console.warn("> Registering component without context");
82
+ }
83
+ const new_scripts = context?.new_scripts;
84
+ if (!new_scripts.includes(script)) {
85
+ new_scripts.push(script);
86
+ }
87
+ }
88
+ /**
89
+ * The Needle Engine context is the main access point that holds all the data and state of a Needle Engine application.
90
+ * It can be used to access the {@link Context.scene}, {@link Context.renderer}, {@link Context.mainCamera}, {@link Context.input}, {@link Context.physics}, {@link Context.time}, {@link Context.connection} (networking), and more.
91
+ *
92
+ * The context is automatically created when using the `<needle-engine>` web component.
93
+ *
94
+ * @example Accessing the context from a [component](https://engine.needle.tools/docs/api/Behaviour):
95
+ * ```typescript
96
+ * import { Behaviour } from "@needle-tools/engine";
97
+ * import { Mesh, BoxGeometry, MeshBasicMaterial } from "three";
98
+ * export class MyScript extends Behaviour {
99
+ * start() {
100
+ * console.log("Hello from MyScript");
101
+ * this.context.scene.add(new Mesh(new BoxGeometry(), new MeshBasicMaterial()));
102
+ * }
103
+ * }
104
+ * ```
105
+ *
106
+ * @example Accessing the context from a [hook](https://engine.needle.tools/docs/scripting.html#hooks) without a component e.g. from a javascript module or svelte or react component.
107
+ *
108
+ * ```typescript
109
+ * import { onStart } from "@needle-tools/engine";
110
+ *
111
+ * onStart((context) => {
112
+ * console.log("Hello from onStart hook");
113
+ * context.scene.add(new Mesh(new BoxGeometry(), new MeshBasicMaterial()));
114
+ * });
115
+ * ```
116
+ *
117
+ */
118
+ export class Context {
119
+ static _defaultTargetFramerate = { value: 90, toString() { return this.value; } };
120
+ /** When a new context is created this is the framerate that will be used by default */
121
+ static get DefaultTargetFrameRate() {
122
+ return Context._defaultTargetFramerate.value;
123
+ }
124
+ /** When a new context is created this is the framerate that will be used by default */
125
+ static set DefaultTargetFrameRate(val) {
126
+ Context._defaultTargetFramerate.value = val;
127
+ }
128
+ static _defaultWebglRendererParameters = {
129
+ antialias: true,
130
+ alpha: false,
131
+ // Note: this is due to a bug on OSX devices. See NE-5370
132
+ powerPreference: (DeviceUtilities.isiOS() || DeviceUtilities.isMacOS()) ? "default" : "high-performance",
133
+ stencil: true,
134
+ // logarithmicDepthBuffer: true,
135
+ // reverseDepthBuffer: true, // https://github.com/mrdoob/three.js/issues/29770
136
+ };
137
+ /** The default parameters that will be used when creating a new WebGLRenderer.
138
+ * Modify in global context to change the default parameters for all new contexts.
139
+ * @example
140
+ * ```typescript
141
+ * import { Context } from "@needle-tools/engine";
142
+ * Context.DefaultWebGLRendererParameters.antialias = false;
143
+ * ```
144
+ */
145
+ static get DefaultWebGLRendererParameters() {
146
+ return Context._defaultWebglRendererParameters;
147
+ }
148
+ /** The needle engine version */
149
+ get version() {
150
+ return VERSION;
151
+ }
152
+ /** The currently active context. Only set during the update loops */
153
+ static get Current() {
154
+ return ContextRegistry.Current;
155
+ }
156
+ /** @internal this property should not be set by user code */
157
+ static set Current(context) {
158
+ ContextRegistry.Current = context;
159
+ }
160
+ static get All() {
161
+ return ContextRegistry.All;
162
+ }
163
+ /** The name of the context */
164
+ name;
165
+ /** An alias for the context */
166
+ alias;
167
+ /** When the renderer or camera are managed by an external process (e.g. when running in r3f context).
168
+ * When this is false you are responsible to call update(timestamp, xframe.
169
+ * It is also currently assumed that rendering is handled performed by an external process
170
+ * */
171
+ isManagedExternally = false;
172
+ /** set to true to pause the update loop. You can receive an event for it in your components.
173
+ * Note that script updates will not be called when paused */
174
+ isPaused = false;
175
+ /** When enabled the application will run while not visible on the page */
176
+ runInBackground = false;
177
+ /**
178
+ * Set to the target framerate you want your application to run in (you can use ?stats to check the fps)
179
+ * Set to undefined if you want to run at the maximum framerate
180
+ */
181
+ targetFrameRate;
182
+ /** Use a higher number for more accurate physics simulation.
183
+ * When undefined physics steps will be 1 for mobile devices and 5 for desktop devices
184
+ * Set to 0 to disable physics updates
185
+ * TODO: changing physics steps is currently not supported because then forces that we get from the character controller and rigidbody et al are not correct anymore - this needs to be properly tested before making this configureable
186
+ */
187
+ physicsSteps = 1;
188
+ /** used to append to loaded assets */
189
+ hash;
190
+ /** The `<needle-engine>` web component */
191
+ domElement;
192
+ appendHTMLElement(element) {
193
+ if (this.domElement.shadowRoot)
194
+ return this.domElement.shadowRoot.appendChild(element);
195
+ else
196
+ return this.domElement.appendChild(element);
197
+ }
198
+ get resolutionScaleFactor() { return this._resolutionScaleFactor; }
199
+ /** use to scale the resolution up or down of the renderer. default is 1 */
200
+ set resolutionScaleFactor(val) {
201
+ if (val === this._resolutionScaleFactor)
202
+ return;
203
+ if (typeof val !== "number")
204
+ return;
205
+ if (val <= 0) {
206
+ console.error("Invalid resolution scale factor", val);
207
+ return;
208
+ }
209
+ this._resolutionScaleFactor = val;
210
+ this.updateSize();
211
+ }
212
+ _resolutionScaleFactor = 1;
213
+ // domElement.clientLeft etc doesnt return absolute position
214
+ _boundingClientRectFrame = -1;
215
+ _boundingClientRect = null;
216
+ _domX;
217
+ _domY;
218
+ /** update bounding rects + domX, domY */
219
+ calculateBoundingClientRect() {
220
+ // workaround for mozilla webXR viewer
221
+ if (this.xr) {
222
+ this._domX = 0;
223
+ this._domY = 0;
224
+ return;
225
+ }
226
+ // TODO: cache this
227
+ if (this._boundingClientRectFrame === this.time.frame)
228
+ return;
229
+ this._boundingClientRectFrame = this.time.frame;
230
+ this._boundingClientRect = this.domElement.getBoundingClientRect();
231
+ this._domX = this._boundingClientRect.x;
232
+ this._domY = this._boundingClientRect.y;
233
+ }
234
+ /** The width of the `<needle-engine>` element on the website */
235
+ get domWidth() {
236
+ // for mozilla XR
237
+ if (this.isInAR)
238
+ return window.innerWidth;
239
+ return this.domElement.clientWidth;
240
+ }
241
+ /** The height of the `<needle-engine>` element on the website */
242
+ get domHeight() {
243
+ // for mozilla XR
244
+ if (this.isInAR)
245
+ return window.innerHeight;
246
+ return this.domElement.clientHeight;
247
+ }
248
+ /** the X position of the `<needle-engine>` element on the website */
249
+ get domX() {
250
+ this.calculateBoundingClientRect();
251
+ return this._domX;
252
+ }
253
+ /** the Y position of the `<needle-engine>` element on the website */
254
+ get domY() {
255
+ this.calculateBoundingClientRect();
256
+ return this._domY;
257
+ }
258
+ /**
259
+ * Is a XR session currently active and presenting?
260
+ * @returns true if the xr renderer is currently presenting
261
+ */
262
+ get isInXR() { return this.renderer?.xr?.isPresenting || false; }
263
+ /** shorthand for `NeedleXRSession.active`
264
+ * Automatically set by NeedleXRSession when a XR session is active
265
+ * @returns the active XR session or null if no session is active
266
+ * */
267
+ xr = null;
268
+ /**
269
+ * Shorthand for `this.xr?.mode`. AR or VR
270
+ * @returns the current XR session mode (immersive-vr or immersive-ar)
271
+ */
272
+ get xrSessionMode() { return this.xr?.mode; }
273
+ /** Shorthand for `this.xrSessionMode === "immersive-vr"`
274
+ * @returns true if a webxr VR session is currently active.
275
+ */
276
+ get isInVR() { return this.xrSessionMode === "immersive-vr"; }
277
+ /**
278
+ * Shorthand for `this.xrSessionMode === "immersive-ar"`
279
+ * @returns true if a webxr AR session is currently active.
280
+ */
281
+ get isInAR() { return this.xrSessionMode === "immersive-ar"; }
282
+ /** If a XR session is active and in pass through mode (immersive-ar on e.g. Quest)
283
+ * @returns true if the XR session is in pass through mode
284
+ */
285
+ get isInPassThrough() { return this.xr ? this.xr.isPassThrough : false; }
286
+ /** access the raw `XRSession` object (shorthand for `context.renderer.xr.getSession()`). For more control use `NeedleXRSession.active` */
287
+ get xrSession() { return this.renderer?.xr?.getSession(); }
288
+ /** @returns the latest XRFrame (if a XRSession is currently active)
289
+ * @link https://developer.mozilla.org/en-US/docs/Web/API/XRFrame
290
+ */
291
+ get xrFrame() { return this._xrFrame; }
292
+ /** @returns the current WebXR camera while the WebXRManager is active (shorthand for `context.renderer.xr.getCamera()`) */
293
+ get xrCamera() { return this.renderer.xr.isPresenting ? this.renderer?.xr?.getCamera() : undefined; }
294
+ _xrFrame = null;
295
+ /**
296
+ * The AR overlay element is used to display 2D HTML elements while a AR session is active.
297
+ */
298
+ get arOverlayElement() {
299
+ const el = this.domElement;
300
+ if (typeof el.getAROverlayContainer === "function")
301
+ return el.getAROverlayContainer();
302
+ return this.domElement;
303
+ }
304
+ /**
305
+ * Current event of the update cycle (e.g. `FrameEvent.EarlyUpdate` or `FrameEvent.OnBeforeRender`)
306
+ */
307
+ get currentFrameEvent() {
308
+ return this._currentFrameEvent;
309
+ }
310
+ _currentFrameEvent = FrameEvent.Undefined;
311
+ /**
312
+ * The scene contains all objects in the hierarchy and is automatically rendered by the context every frane.
313
+ */
314
+ scene;
315
+ /**
316
+ * The renderer is used to render the scene. It is automatically created when the context is created.
317
+ */
318
+ renderer;
319
+ /**
320
+ * The effect composer can be used to render postprocessing effects. If assigned then it will automatically render the scene every frame.
321
+ */
322
+ composer = null;
323
+ // #region internal script lists
324
+ /**
325
+ * @internal All known components. Don't use directly
326
+ */
327
+ scripts = [];
328
+ /**
329
+ * @internal All paused components. Don't use directly
330
+ */
331
+ scripts_pausedChanged = [];
332
+ /**
333
+ * @internal All components that have a early update event. Don't use directly
334
+ */
335
+ scripts_earlyUpdate = [];
336
+ /**
337
+ * @internal All components that have a update event. Don't use directly
338
+ */
339
+ scripts_update = [];
340
+ /**
341
+ * @internal All components that have a late update event. Don't use directly
342
+ */
343
+ scripts_lateUpdate = [];
344
+ /**
345
+ * @internal All components that have a onBeforeRender event. Don't use directly
346
+ */
347
+ scripts_onBeforeRender = [];
348
+ /**
349
+ * @internal All components that have a onAfterRender event. Don't use directly
350
+ */
351
+ scripts_onAfterRender = [];
352
+ /**
353
+ * @internal All components that have coroutines. Don't use directly
354
+ */
355
+ scripts_WithCorroutines = [];
356
+ /**
357
+ * @internal Components with immersive-vr event methods. Don't use directly
358
+ */
359
+ scripts_immersive_vr = [];
360
+ /**
361
+ * @internal Components with immersive-ar event methods. Don't use directly
362
+ */
363
+ scripts_immersive_ar = [];
364
+ /**
365
+ * @internal Coroutine data
366
+ */
367
+ coroutines = {};
368
+ /** callbacks called once after the context has been created */
369
+ post_setup_callbacks = [];
370
+ /** called every frame at the beginning of the frame (after component start events and before earlyUpdate) */
371
+ pre_update_callbacks = [];
372
+ /** called every frame before rendering (after all component events) */
373
+ pre_render_callbacks = [];
374
+ /** called every frame after rendering (after all component events) */
375
+ post_render_callbacks = [];
376
+ /** called every frame befroe update (this list is emptied every frame) */
377
+ pre_update_oneshot_callbacks = [];
378
+ /** @internal */
379
+ new_scripts = [];
380
+ /** @internal */
381
+ new_script_start = [];
382
+ /** @internal */
383
+ new_scripts_pre_setup_callbacks = [];
384
+ /** @internal */
385
+ new_scripts_post_setup_callbacks = [];
386
+ /** @internal */
387
+ new_scripts_xr = [];
388
+ // #endregion
389
+ // #region Properties
390
+ /**
391
+ * The **main camera component** of the scene - this camera is used for rendering.
392
+ * Use `setCurrentCamera` for updating the main camera.
393
+ */
394
+ mainCameraComponent = undefined;
395
+ /**
396
+ * The main camera of the scene - this camera is used for rendering
397
+ * Use `setCurrentCamera` for updating the main camera.
398
+ */
399
+ get mainCamera() {
400
+ if (this._mainCamera) {
401
+ return this._mainCamera;
402
+ }
403
+ if (this.mainCameraComponent) {
404
+ const cam = this.mainCameraComponent;
405
+ if (!cam.threeCamera)
406
+ cam.buildCamera();
407
+ return cam.threeCamera;
408
+ }
409
+ if (!this._fallbackCamera) {
410
+ this._fallbackCamera = new PerspectiveCamera(75, this.domWidth / this.domHeight, 0.1, 1000);
411
+ }
412
+ return this._fallbackCamera;
413
+ }
414
+ /** Set the main camera of the scene. If set to null the camera of the {@link mainCameraComponent} will be used - this camera is used for rendering */
415
+ set mainCamera(cam) {
416
+ this._mainCamera = cam;
417
+ }
418
+ _mainCamera = null;
419
+ _fallbackCamera = null;
420
+ /** access application state (e.g. if all audio should be muted) */
421
+ application;
422
+ /** access animation mixer used by components in the scene */
423
+ animations;
424
+ /** access timings (current frame number, deltaTime, timeScale, ...) */
425
+ time;
426
+ /** access input data (e.g. click or touch events) */
427
+ input;
428
+ /** access physics related methods (e.g. raycasting). To access the phyiscs engine use `context.physics.engine` */
429
+ physics;
430
+ /** access networking methods (use it to send or listen to messages or join a networking backend) */
431
+ connection;
432
+ /** @deprecated AssetDatabase is deprecated */
433
+ assets;
434
+ /** The main light in the scene */
435
+ mainLight = null;
436
+ /** @deprecated Use sceneLighting */
437
+ get rendererData() { return this.sceneLighting; }
438
+ /** Access the scene lighting manager to control lighting settings in the context */
439
+ sceneLighting;
440
+ addressables;
441
+ lightmaps;
442
+ players;
443
+ /** Access the LODs manager to control LOD behavior in the context */
444
+ lodsManager;
445
+ /** Access the needle menu to add or remove buttons to the menu element */
446
+ menu;
447
+ /**
448
+ * Checks if the context is fully created and ready
449
+ * @returns true if the context is fully created and ready
450
+ */
451
+ get isCreated() { return this._isCreated; }
452
+ /**
453
+ * The source identifier(s) of the root scene(s) loaded into this context.
454
+ * When using `<needle-engine>` web component this will be the `src` attribute(s).
455
+ * @returns The source identifier for of the root scene
456
+ */
457
+ get rootSourceId() { return this.rootSceneSourceIdentifiers[0] || undefined; }
458
+ _needsUpdateSize = false;
459
+ _isCreated = false;
460
+ _isCreating = false;
461
+ _isVisible = false;
462
+ _stats = stats ? new Stats.default() : null;
463
+ constructor(args) {
464
+ this.name = args?.name || "";
465
+ this.alias = args?.alias;
466
+ this.domElement = args?.domElement || document.body;
467
+ this.hash = args?.hash;
468
+ if (args?.renderer) {
469
+ this.renderer = args.renderer;
470
+ this.isManagedExternally = true;
471
+ }
472
+ if (args?.runInBackground !== undefined)
473
+ this.runInBackground = args.runInBackground;
474
+ if (args?.scene)
475
+ this.scene = args.scene;
476
+ else
477
+ this.scene = new Scene();
478
+ if (args?.camera)
479
+ this._mainCamera = args.camera;
480
+ this.application = new Application(this);
481
+ this.time = new Time();
482
+ this.input = new Input(this);
483
+ this.physics = new Physics(this);
484
+ this.connection = new NetworkConnection(this);
485
+ // eslint-disable-next-line deprecation/deprecation
486
+ this.assets = new AssetDatabase();
487
+ this.sceneLighting = new SceneLighting(this);
488
+ this.addressables = new Addressables(this);
489
+ this.lightmaps = new LightDataRegistry(this);
490
+ this.players = new PlayerViewManager(this);
491
+ this.menu = new NeedleMenu(this);
492
+ this.lodsManager = new LODsManager(this);
493
+ this.animations = new AnimationsRegistry(this);
494
+ const resizeCallback = () => this._needsUpdateSize = true;
495
+ window.addEventListener('resize', resizeCallback);
496
+ this._disposeCallbacks.push(() => window.removeEventListener('resize', resizeCallback));
497
+ const resizeObserver = new ResizeObserver(_ => this._needsUpdateSize = true);
498
+ resizeObserver.observe(this.domElement);
499
+ this._disposeCallbacks.push(() => resizeObserver.disconnect());
500
+ this._intersectionObserver = new IntersectionObserver(entries => {
501
+ this._isVisible = entries[0].isIntersecting;
502
+ });
503
+ this._disposeCallbacks.push(() => this._intersectionObserver?.disconnect());
504
+ ContextRegistry.register(this);
505
+ }
506
+ // #region Renderer
507
+ /**
508
+ * Calling this function will dispose the current renderer and create a new one which will then be assigned to the context. It can be used to create a new renderer with custom WebGLRendererParameters.
509
+ * **Note**: Instead you can also modify the static `Context.DefaultWebGlRendererParameters` before the context is created.
510
+ * **Note**: This method is recommended because it re-uses an potentially already existing canvas element. This is necessary to keep input event handlers from working (e.g. components like OrbitControls subscribe to input events on the canvas)
511
+ * @returns {WebGLRenderer} the newly created renderer
512
+ */
513
+ createNewRenderer(params) {
514
+ this.renderer?.dispose();
515
+ params = { ...Context.DefaultWebGLRendererParameters, ...params };
516
+ if (!params.canvas) {
517
+ // get canvas already configured in the Needle Engine Web Component
518
+ const canvas = this.domElement?.shadowRoot?.querySelector("canvas");
519
+ if (canvas) {
520
+ params.canvas = canvas;
521
+ if (debug) {
522
+ console.log("Using canvas from shadow root", canvas);
523
+ }
524
+ }
525
+ }
526
+ if (debug)
527
+ console.log("Using Renderer Parameters:", params, this.domElement);
528
+ this.renderer = new WebGLRenderer(params);
529
+ this.renderer.debug.checkShaderErrors = isDevEnvironment() || getParam("checkshadererrors") === true;
530
+ // some tonemapping other than "NONE" is required for adjusting exposure with EXR environments
531
+ this.renderer.toneMappingExposure = 1; // range [0...inf] instead of the usual -15..15
532
+ this.renderer.toneMapping = NoToneMapping; // could also set to LinearToneMapping, ACESFilmicToneMapping
533
+ this.renderer.setClearColor(new Color('lightgrey'), 0);
534
+ // // @ts-ignore
535
+ // this.renderer.alpha = false;
536
+ this.renderer.shadowMap.enabled = true;
537
+ this.renderer.shadowMap.type = PCFSoftShadowMap;
538
+ this.renderer.setSize(this.domWidth, this.domHeight);
539
+ this.renderer.outputColorSpace = SRGBColorSpace;
540
+ // Injecting the core nodes library here, like WebGPURenderer backends do
541
+ //@ts-ignore
542
+ this.renderer.nodes = {
543
+ library: new BasicNodeLibrary(),
544
+ modelViewMatrix: null,
545
+ modelNormalViewMatrix: null,
546
+ };
547
+ // this.renderer.toneMapping = AgXToneMapping;
548
+ this.lodsManager.setRenderer(this.renderer);
549
+ this.input.bindEvents();
550
+ return this.renderer;
551
+ }
552
+ _intersectionObserver = null;
553
+ internalOnUpdateVisible() {
554
+ this._intersectionObserver?.disconnect();
555
+ this._intersectionObserver?.observe(this.domElement);
556
+ }
557
+ _disposeCallbacks = [];
558
+ /** will request a renderer size update the next render call (will call updateSize the next update) */
559
+ requestSizeUpdate() { this._needsUpdateSize = true; }
560
+ /** Clamps the renderer max resolution. If undefined the max resolution is not clamped. Default is undefined */
561
+ maxRenderResolution;
562
+ /** Control the renderer devicePixelRatio.
563
+ * **Options**
564
+ * - `auto` - Needle Engine automatically sets the pixel ratio to the current window.devicePixelRatio.
565
+ * - `manual` - Needle Engine will not change the renderer pixel ratio. You can set it manually.
566
+ * - `number` - Needle Engine will set the pixel ratio to the given number. The change will be applied to the renderer and the composer (if used) at the end of the current frame.
567
+ */
568
+ get devicePixelRatio() { return this._devicePixelRatio; }
569
+ set devicePixelRatio(val) {
570
+ if (val !== this._devicePixelRatio) {
571
+ this._devicePixelRatio = val;
572
+ this._needsUpdateSize = true;
573
+ }
574
+ }
575
+ _devicePixelRatio = "auto";
576
+ /**
577
+ * Update the renderer and canvas size. This is also automatically called when a DOM size change is detected.
578
+ */
579
+ updateSize(force = false) {
580
+ if (force || (!this.isManagedExternally && this.renderer.xr?.isPresenting === false)) {
581
+ this._needsUpdateSize = false;
582
+ const scaleFactor = this.resolutionScaleFactor;
583
+ let width = this.domWidth * scaleFactor;
584
+ let height = this.domHeight * scaleFactor;
585
+ if (this.maxRenderResolution) {
586
+ this.maxRenderResolution.x = Math.max(1, this.maxRenderResolution.x);
587
+ width = Math.min(this.maxRenderResolution.x, width);
588
+ this.maxRenderResolution.y = Math.max(1, this.maxRenderResolution.y);
589
+ height = Math.min(this.maxRenderResolution.y, height);
590
+ }
591
+ const camera = this.mainCamera;
592
+ this.updateAspect(camera);
593
+ this.renderer.setSize(width, height, true);
594
+ // avoid setting pixel values here since this can cause pingpong updates
595
+ // e.g. when system scale is set to 125%
596
+ // https://github.com/needle-tools/needle-engine-support/issues/69
597
+ this.renderer.domElement.style.width = "100%";
598
+ this.renderer.domElement.style.height = "100%";
599
+ const devicePixelRatio = typeof this.devicePixelRatio === "number"
600
+ ? this.devicePixelRatio
601
+ : this.devicePixelRatio === "auto"
602
+ ? Math.min(2, window.devicePixelRatio) // clamp device pixel ratio to two for "automatic" mode
603
+ : undefined;
604
+ if (devicePixelRatio !== undefined) {
605
+ this.renderer.setPixelRatio(devicePixelRatio);
606
+ }
607
+ if (this.composer) {
608
+ this.composer.setSize?.call(this.composer, width, height);
609
+ if (devicePixelRatio !== undefined && "setPixelRatio" in this.composer && typeof this.composer.setPixelRatio === "function")
610
+ this.composer.setPixelRatio?.call(this.composer, window.devicePixelRatio);
611
+ }
612
+ }
613
+ }
614
+ /**
615
+ * Update the camera aspect ratio or orthorgraphic camera size. This is automatically called when a DOM size change is detected.
616
+ */
617
+ updateAspect(camera, width, height) {
618
+ if (!camera)
619
+ return;
620
+ if (width === undefined)
621
+ width = this.domWidth;
622
+ if (height === undefined)
623
+ height = this.domHeight;
624
+ const aspectRatio = width / height;
625
+ if (camera.isPerspectiveCamera) {
626
+ const cam = camera;
627
+ const pa = cam.aspect;
628
+ cam.aspect = aspectRatio;
629
+ if (pa !== cam.aspect)
630
+ camera.updateProjectionMatrix();
631
+ }
632
+ else if (camera.isOrthographicCamera) {
633
+ const cam = camera;
634
+ // Maintain the camera's current vertical size (top - bottom)
635
+ const verticalSize = cam.top - cam.bottom;
636
+ // Calculate new horizontal size based on aspect ratio
637
+ const horizontalSize = verticalSize * aspectRatio;
638
+ // Update camera bounds while maintaining center position
639
+ const halfWidth = horizontalSize / 2;
640
+ const halfHeight = verticalSize / 2;
641
+ if (cam.left != -halfWidth || cam.top != halfHeight) {
642
+ cam.left = -halfWidth;
643
+ cam.right = halfWidth;
644
+ cam.top = halfHeight;
645
+ cam.bottom = -halfHeight;
646
+ camera.updateProjectionMatrix();
647
+ }
648
+ }
649
+ }
650
+ /** This will recreate the whole needle engine context and dispose the whole scene content
651
+ * All content will be reloaded (loading times might be faster due to browser caches)
652
+ * All scripts will be recreated */
653
+ recreate() {
654
+ this.clear();
655
+ this.create(this._originalCreationArgs);
656
+ }
657
+ _originalCreationArgs;
658
+ /** @deprecated use create. This method will be removed in a future version */
659
+ async onCreate(opts) {
660
+ return this.create(opts);
661
+ }
662
+ /** @internal */
663
+ async create(opts) {
664
+ try {
665
+ this._isCreating = true;
666
+ if (opts !== this._originalCreationArgs)
667
+ this._originalCreationArgs = deepClone(opts);
668
+ window.addEventListener("unhandledrejection", this.onUnhandledRejection);
669
+ const res = await this.internalOnCreate(opts);
670
+ this._isCreated = res;
671
+ return res;
672
+ }
673
+ finally {
674
+ window.removeEventListener("unhandledrejection", this.onUnhandledRejection);
675
+ this._isCreating = false;
676
+ }
677
+ }
678
+ onUnhandledRejection = (event) => {
679
+ this.onError(event.reason);
680
+ };
681
+ /** Dispatches an error */
682
+ onError(error) {
683
+ this.domElement.dispatchEvent(new CustomEvent("error", { detail: error }));
684
+ }
685
+ /**
686
+ * Clears the context and destroys all scenes and objects in the scene.
687
+ * The ContextCleared event is called at the end.
688
+ * This is automatically called when e.g. the `src` attribute changes on `<needle-engine>`
689
+ * or when the web component is removed from the DOM
690
+ */
691
+ clear() {
692
+ ContextRegistry.dispatchCallback(ContextEvent.ContextClearing, this);
693
+ invokeLifecycleFunctions(this, ContextEvent.ContextClearing);
694
+ // NOTE: this does dispose the environment/background image too
695
+ // which is probably not desired if it is set via the background-image attribute
696
+ destroy(this.scene, true, true);
697
+ this.scene = new Scene();
698
+ this.addressables?.dispose();
699
+ this.lightmaps?.clear();
700
+ this.physics?.engine?.clearCaches();
701
+ this.lodsManager.disable();
702
+ this._onBeforeRenderListeners.clear();
703
+ this._onAfterRenderListeners.clear();
704
+ if (!this.isManagedExternally) {
705
+ if (this.renderer) {
706
+ this.renderer.renderLists.dispose();
707
+ this.renderer.state.reset();
708
+ this.renderer.resetState();
709
+ }
710
+ }
711
+ // We do not want to clear the renderer here because when switching src we want to keep the last rendered frame in case the loading screen is not visible
712
+ // if a user wants to see the background they can still call setClearAlpha(0) and clear manually
713
+ ContextRegistry.dispatchCallback(ContextEvent.ContextCleared, this);
714
+ }
715
+ /**
716
+ * Dispose all allocated resources and clears the scene. This is automatically called e.g. when the `<needle-engine>` component is removed from the DOM.
717
+ */
718
+ dispose() {
719
+ this.internalOnDestroy();
720
+ }
721
+ /**@deprecated use dispose() */
722
+ onDestroy() { this.internalOnDestroy(); }
723
+ internalOnDestroy() {
724
+ Context.Current = this;
725
+ ContextRegistry.dispatchCallback(ContextEvent.ContextDestroying, this);
726
+ invokeLifecycleFunctions(this, ContextEvent.ContextDestroying);
727
+ this.clear();
728
+ this.renderer?.setAnimationLoop(null);
729
+ if (this.renderer) {
730
+ this.renderer.setClearAlpha(0);
731
+ this.renderer.clear();
732
+ if (!this.isManagedExternally) {
733
+ if (debug)
734
+ console.log("Disposing renderer");
735
+ this.renderer.dispose();
736
+ }
737
+ }
738
+ this.scene = null;
739
+ this.renderer = null;
740
+ this.input.dispose();
741
+ this.menu.onDestroy();
742
+ this.animations.onDestroy();
743
+ for (const cb of this._disposeCallbacks) {
744
+ try {
745
+ cb();
746
+ }
747
+ catch (e) {
748
+ console.error("Error in on dispose callback:", e, cb);
749
+ }
750
+ }
751
+ if (this.domElement?.parentElement) {
752
+ this.domElement.parentElement.removeChild(this.domElement);
753
+ }
754
+ this._isCreated = false;
755
+ ContextRegistry.dispatchCallback(ContextEvent.ContextDestroyed, this);
756
+ invokeLifecycleFunctions(this, ContextEvent.ContextDestroyed);
757
+ ContextRegistry.unregister(this);
758
+ if (Context.Current === this) {
759
+ //@ts-ignore
760
+ Context.Current = null;
761
+ }
762
+ }
763
+ /** @internal Automatically called by components when you call `startCoroutine`. Use `startCoroutine` instead */
764
+ registerCoroutineUpdate(script, coroutine, evt) {
765
+ if (typeof coroutine?.next !== "function") {
766
+ console.error("Registered invalid coroutine function from " + script.name + "\nCoroutine functions must be generators: \"*myCoroutine() {...}\"\nStart a coroutine from a component by calling \"this.startCoroutine(myCoroutine())\"");
767
+ return coroutine;
768
+ }
769
+ if (!this.coroutines[evt])
770
+ this.coroutines[evt] = [];
771
+ this.coroutines[evt].push({ comp: script, main: coroutine });
772
+ return coroutine;
773
+ }
774
+ /** @internal Automatically called by components. */
775
+ unregisterCoroutineUpdate(coroutine, evt) {
776
+ if (!this.coroutines[evt])
777
+ return;
778
+ const idx = this.coroutines[evt].findIndex(c => c.main === coroutine);
779
+ if (idx >= 0)
780
+ this.coroutines[evt].splice(idx, 1);
781
+ }
782
+ /** @internal Automatically called */
783
+ stopAllCoroutinesFrom(script) {
784
+ for (const evt in this.coroutines) {
785
+ const rout = this.coroutines[evt];
786
+ for (let i = rout.length - 1; i >= 0; i--) {
787
+ const r = rout[i];
788
+ if (r.comp === script) {
789
+ rout.splice(i, 1);
790
+ }
791
+ }
792
+ }
793
+ }
794
+ _cameraStack = [];
795
+ /** Change the main camera */
796
+ setCurrentCamera(cam) {
797
+ if (!cam)
798
+ return;
799
+ if (!cam.threeCamera)
800
+ cam.buildCamera(); // < to build camera
801
+ if (!cam.threeCamera) {
802
+ console.warn("Camera component is missing camera", cam);
803
+ return;
804
+ }
805
+ const index = this._cameraStack.indexOf(cam);
806
+ if (index >= 0)
807
+ this._cameraStack.splice(index, 1);
808
+ this._cameraStack.push(cam);
809
+ this.mainCameraComponent = cam;
810
+ const camera = cam.threeCamera;
811
+ if (camera.isPerspectiveCamera)
812
+ this.updateAspect(camera);
813
+ this.mainCameraComponent?.applyClearFlagsIfIsActiveCamera();
814
+ }
815
+ /**
816
+ * Remove the camera from the mainCamera stack (if it has been set before with `setCurrentCamera`)
817
+ */
818
+ removeCamera(cam) {
819
+ if (!cam)
820
+ return;
821
+ const index = this._cameraStack.indexOf(cam);
822
+ if (index >= 0)
823
+ this._cameraStack.splice(index, 1);
824
+ if (this.mainCameraComponent === cam) {
825
+ this.mainCameraComponent = undefined;
826
+ if (this._cameraStack.length > 0) {
827
+ const last = this._cameraStack[this._cameraStack.length - 1];
828
+ this.setCurrentCamera(last);
829
+ }
830
+ }
831
+ }
832
+ // #region onBeforeRender / onAfterRender listeners
833
+ _onBeforeRenderListeners = new Map();
834
+ _onAfterRenderListeners = new Map();
835
+ /** Use to subscribe to onBeforeRender events on threejs objects.
836
+ * @link https://threejs.org/docs/#api/en/core/Object3D.onBeforeRender
837
+ */
838
+ addBeforeRenderListener(target, callback) {
839
+ if (!this._onBeforeRenderListeners.has(target.uuid)) {
840
+ const arr = [];
841
+ this._onBeforeRenderListeners.set(target.uuid, arr);
842
+ target.onBeforeRender = this._createRenderCallbackWrapper(arr);
843
+ }
844
+ this._onBeforeRenderListeners.get(target.uuid).push(callback);
845
+ }
846
+ /** Remove callback from three `onBeforeRender` event (if it has been added with `addBeforeRenderListener(...)`)
847
+ * @link https://threejs.org/docs/#api/en/core/Object3D.onBeforeRender
848
+ */
849
+ removeBeforeRenderListener(target, callback) {
850
+ if (this._onBeforeRenderListeners.has(target.uuid)) {
851
+ const arr = this._onBeforeRenderListeners.get(target.uuid);
852
+ const idx = arr.indexOf(callback);
853
+ if (idx >= 0)
854
+ arr.splice(idx, 1);
855
+ }
856
+ }
857
+ /**
858
+ * Subscribe to onAfterRender events on threejs objects
859
+ * @link https://threejs.org/docs/#api/en/core/Object3D.onAfterRender
860
+ */
861
+ addAfterRenderListener(target, callback) {
862
+ if (!this._onAfterRenderListeners.has(target.uuid)) {
863
+ const arr = [];
864
+ this._onAfterRenderListeners.set(target.uuid, arr);
865
+ target.onAfterRender = this._createRenderCallbackWrapper(arr);
866
+ }
867
+ this._onAfterRenderListeners.get(target.uuid)?.push(callback);
868
+ }
869
+ /**
870
+ * Remove from onAfterRender events on threejs objects
871
+ * @link https://threejs.org/docs/#api/en/core/Object3D.onAfterRender
872
+ */
873
+ removeAfterRenderListener(target, callback) {
874
+ if (this._onAfterRenderListeners.has(target.uuid)) {
875
+ const arr = this._onAfterRenderListeners.get(target.uuid);
876
+ const idx = arr.indexOf(callback);
877
+ if (idx >= 0)
878
+ arr.splice(idx, 1);
879
+ }
880
+ }
881
+ _createRenderCallbackWrapper(array) {
882
+ return (renderer, scene, camera, geometry, material, group) => {
883
+ for (let i = 0; i < array.length; i++) {
884
+ const fn = array[i];
885
+ fn(renderer, scene, camera, geometry, material, group);
886
+ }
887
+ };
888
+ }
889
+ _requireDepthTexture = false;
890
+ _requireColorTexture = false;
891
+ _renderTarget;
892
+ _isRendering = false;
893
+ /** @returns true while the WebGL renderer is rendering (between onBeforeRender and onAfterRender events) */
894
+ get isRendering() { return this._isRendering; }
895
+ setRequireDepth(val) {
896
+ this._requireDepthTexture = val;
897
+ }
898
+ setRequireColor(val) {
899
+ this._requireColorTexture = val;
900
+ }
901
+ get depthTexture() {
902
+ return this._renderTarget?.depthTexture || null;
903
+ }
904
+ get opaqueColorTexture() {
905
+ return this._renderTarget?.texture || null;
906
+ }
907
+ /** @returns true if the `<needle-engine>` DOM element is visible on screen (`context.domElement`) */
908
+ get isVisibleToUser() {
909
+ if (this.isInXR)
910
+ return true;
911
+ if (!this._isVisible)
912
+ return false;
913
+ // Make sure not to call getComputedStyle multiple times per frame
914
+ if (!this._needsVisibleUpdate && this._lastStyleComputedResult !== undefined)
915
+ return this._lastStyleComputedResult;
916
+ this._needsVisibleUpdate = false;
917
+ const style = getComputedStyle(this.domElement);
918
+ this._lastStyleComputedResult = style.visibility !== "hidden" && style.display !== "none" && style.opacity !== "0";
919
+ return this._lastStyleComputedResult;
920
+ }
921
+ _needsVisibleUpdate = true;
922
+ _lastStyleComputedResult = undefined;
923
+ _createId = 0;
924
+ // #region internal create
925
+ async internalOnCreate(opts) {
926
+ const createId = ++this._createId;
927
+ if (debug)
928
+ console.log("Creating context", this.name, opts);
929
+ // wait for async imported dependencies to be loaded
930
+ // see https://linear.app/needle/issue/NE-4445
931
+ const dependenciesReady = globalThis["needle:dependencies:ready"];
932
+ if (dependenciesReady instanceof Promise) {
933
+ if (debug)
934
+ console.log("Waiting for dependencies to be ready");
935
+ await dependenciesReady
936
+ .catch(err => {
937
+ if (debug || isDevEnvironment()) {
938
+ showBalloonError("Needle Engine dependencies failed to load. Please check the console for more details");
939
+ const printedError = false;
940
+ if (err instanceof ReferenceError) {
941
+ let offendingComponentName = "YourComponentName";
942
+ const offendingComponentStartIndex = err.message.indexOf("'");
943
+ if (offendingComponentStartIndex > 0) {
944
+ const offendingComponentEndIndex = err.message.indexOf("'", offendingComponentStartIndex + 1);
945
+ if (offendingComponentEndIndex > 0) {
946
+ const name = err.message.substring(offendingComponentStartIndex + 1, offendingComponentEndIndex);
947
+ if (name.length > 3)
948
+ offendingComponentName = name;
949
+ }
950
+ }
951
+ console.error(`Needle Engine dependencies failed to load:\n\n# Make sure you don't have circular imports in your scripts!\n\nPossible solutions: \n→ Replace @serializable(${offendingComponentName}) in your script with @serializable(Behaviour)\n→ If you only need type information try importing the type only, e.g: import { type ${offendingComponentName} }\n\n---`, err);
952
+ return;
953
+ }
954
+ if (!printedError) {
955
+ console.error("Needle Engine dependencies failed to load", err);
956
+ }
957
+ }
958
+ })
959
+ .then(() => {
960
+ if (debug)
961
+ console.log("Needle Engine dependencies are ready");
962
+ });
963
+ }
964
+ this.clear();
965
+ const oldRenderer = this.renderer;
966
+ // We only need to create a new renderer if we don't have one yet
967
+ // We do prevent creating a new renderer here to avoid flickering when the context is created while the content is still being loaded. This can be the case where CSS transformations update the layout (e.g. scale() while loading + old canvas disposed but in the DOM layout.)
968
+ const needsNewRenderer = !oldRenderer || oldRenderer["isDisposed"] === true;
969
+ // stop the animation loop if its running during creation
970
+ // since we do not want to start enabling scripts etc before they are deserialized
971
+ if (this.isManagedExternally === false && (needsNewRenderer)) {
972
+ this.createNewRenderer();
973
+ }
974
+ else {
975
+ this.lodsManager.setRenderer(this.renderer);
976
+ }
977
+ this.renderer?.setAnimationLoop(null);
978
+ Context.Current = this;
979
+ await ContextRegistry.dispatchCallback(ContextEvent.ContextCreationStart, this);
980
+ // load and create scene
981
+ let prepare_succeeded = true;
982
+ let loadedFiles;
983
+ try {
984
+ Context.Current = this;
985
+ if (opts) {
986
+ loadedFiles = await this.internalLoadInitialContent(createId, opts);
987
+ }
988
+ else
989
+ loadedFiles = [];
990
+ }
991
+ catch (err) {
992
+ console.error(err);
993
+ prepare_succeeded = false;
994
+ }
995
+ if (!prepare_succeeded) {
996
+ this.onError("Failed to load initial content");
997
+ return false;
998
+ }
999
+ if (createId !== this._createId || opts?.abortSignal?.aborted) {
1000
+ return false;
1001
+ }
1002
+ this.internalOnUpdateVisible();
1003
+ if (!this.renderer) {
1004
+ if (debug)
1005
+ console.warn("Context has no renderer (perhaps it was disconnected?", this.domElement.isConnected);
1006
+ return false;
1007
+ }
1008
+ if (!this.isManagedExternally && !this.domElement.shadowRoot) {
1009
+ this.domElement.prepend(this.renderer.domElement);
1010
+ }
1011
+ Context.Current = this;
1012
+ // TODO: we could configure if we need physics
1013
+ // await this.physics.engine?.initialize();
1014
+ // Setup
1015
+ Context.Current = this;
1016
+ for (let i = 0; i < this.new_scripts.length; i++) {
1017
+ const script = this.new_scripts[i];
1018
+ if (script.gameObject !== undefined && script.gameObject !== null) {
1019
+ if (script.gameObject.userData === undefined)
1020
+ script.gameObject.userData = {};
1021
+ if (script.gameObject.userData.components === undefined)
1022
+ script.gameObject.userData.components = [];
1023
+ const arr = script.gameObject.userData.components;
1024
+ if (!arr.includes(script))
1025
+ arr.push(script);
1026
+ }
1027
+ // if (script.gameObject && !this.raycastTargets.includes(script.gameObject)) {
1028
+ // this.raycastTargets.push(script.gameObject);
1029
+ // }
1030
+ }
1031
+ // const context = new SerializationContext(this.scene);
1032
+ // for (let i = 0; i < this.new_scripts.length; i++) {
1033
+ // const script = this.new_scripts[i];
1034
+ // const ser = script as unknown as ISerializable;
1035
+ // if (ser.$serializedTypes === undefined) continue;
1036
+ // context.context = this;
1037
+ // context.object = script.gameObject;
1038
+ // deserializeObject(ser, script, context);
1039
+ // }
1040
+ // resolve post setup callbacks (things that rely on threejs objects having references to components)
1041
+ if (this.post_setup_callbacks) {
1042
+ for (let i = 0; i < this.post_setup_callbacks.length; i++) {
1043
+ Context.Current = this;
1044
+ await this.post_setup_callbacks[i](this);
1045
+ }
1046
+ }
1047
+ if (!this._mainCamera) {
1048
+ Context.Current = this;
1049
+ let camera = null;
1050
+ foreachComponent(this.scene, comp => {
1051
+ const cam = comp;
1052
+ if (cam?.isCamera) {
1053
+ looputils.updateActiveInHierarchyWithoutEventCall(cam.gameObject);
1054
+ if (!cam.activeAndEnabled)
1055
+ return undefined;
1056
+ if (cam.tag === "MainCamera") {
1057
+ camera = cam;
1058
+ return true;
1059
+ }
1060
+ else
1061
+ camera = cam;
1062
+ }
1063
+ return undefined;
1064
+ });
1065
+ if (camera) {
1066
+ this.setCurrentCamera(camera);
1067
+ }
1068
+ else {
1069
+ const res = ContextRegistry.dispatchCallback(ContextEvent.MissingCamera, this, { files: loadedFiles });
1070
+ if (!res && !this.mainCamera && !this.isManagedExternally)
1071
+ console.warn("Missing camera in main scene", this);
1072
+ }
1073
+ }
1074
+ this.input.bindEvents();
1075
+ Context.Current = this;
1076
+ looputils.processNewScripts(this);
1077
+ // We have to step once so that colliders that have been created in onEnable can be raycasted in start
1078
+ if (this.physics.engine) {
1079
+ this.physics.engine?.step(0);
1080
+ this.physics.engine?.postStep();
1081
+ }
1082
+ // const mainCam = this.mainCameraComponent as Camera;
1083
+ // if (mainCam) {
1084
+ // mainCam.applyClearFlagsIfIsActiveCamera();
1085
+ // }
1086
+ if (!this.isManagedExternally && this.composer && this.mainCamera) {
1087
+ // TODO: import postprocessing async
1088
+ // const renderPass = new RenderPass(this.scene, this.mainCamera);
1089
+ // this.renderer.setSize(this.domWidth, this.domHeight);
1090
+ // this.composer.addPass(renderPass);
1091
+ // this.composer.setSize(this.domWidth, this.domHeight);
1092
+ }
1093
+ this._needsUpdateSize = true;
1094
+ if (this._stats) {
1095
+ this._stats.showPanel(0);
1096
+ this._stats.dom.style.position = "absolute"; // (default is fixed)
1097
+ this.domElement.shadowRoot?.appendChild(this._stats.dom);
1098
+ }
1099
+ if (debug)
1100
+ logHierarchy(this.scene, true);
1101
+ // If no target framerate was set we use the default
1102
+ if (this.targetFrameRate === undefined) {
1103
+ if (debug)
1104
+ console.warn("No target framerate set, using default", Context.DefaultTargetFrameRate);
1105
+ // the _defaultTargetFramerate is intentionally an object so it can be changed at any time if not explictly set by the user
1106
+ this.targetFrameRate = Context._defaultTargetFramerate;
1107
+ }
1108
+ else if (debug)
1109
+ console.log("Target framerate set to", this.targetFrameRate);
1110
+ this._dispatchReadyAfterFrame = true;
1111
+ const res = ContextRegistry.dispatchCallback(ContextEvent.ContextCreated, this, { files: loadedFiles });
1112
+ if (res) {
1113
+ const domElement = this.domElement;
1114
+ if ("internalSetLoadingMessage" in domElement && typeof domElement.internalSetLoadingMessage === "function")
1115
+ domElement?.internalSetLoadingMessage("finish loading");
1116
+ await res;
1117
+ }
1118
+ if (opts?.abortSignal?.aborted) {
1119
+ return false;
1120
+ }
1121
+ const mainIdentifier = this.rootSourceId;
1122
+ if (mainIdentifier)
1123
+ this.sceneLighting.enable(mainIdentifier);
1124
+ invokeLifecycleFunctions(this, ContextEvent.ContextCreated);
1125
+ if (debug)
1126
+ console.log("Context Created...", this.renderer, this.renderer.domElement);
1127
+ this._isCreating = false;
1128
+ if (!this.isManagedExternally && !opts?.abortSignal?.aborted)
1129
+ this.restartRenderLoop();
1130
+ return true;
1131
+ }
1132
+ rootSceneSourceIdentifiers = [];
1133
+ async internalLoadInitialContent(createId, args) {
1134
+ this.rootSceneSourceIdentifiers.length = 0;
1135
+ const results = new Array();
1136
+ // early out if we dont have any files to load
1137
+ if (args.files.length === 0)
1138
+ return results;
1139
+ const files = [...args.files];
1140
+ this.rootSceneSourceIdentifiers.push(...files);
1141
+ const progressArg = {
1142
+ name: "",
1143
+ progress: null,
1144
+ index: 0,
1145
+ count: files.length
1146
+ };
1147
+ const loader = getLoader();
1148
+ // this hash should be constant since it is used to initialize the UIDProvider per initially loaded scene
1149
+ const loadingHash = 0;
1150
+ for (let i = 0; i < files.length; i++) {
1151
+ if (args.abortSignal?.aborted) {
1152
+ if (debug)
1153
+ console.log("Aborting loading because of abort signal");
1154
+ break;
1155
+ }
1156
+ // abort loading if the create id has changed
1157
+ if (createId !== this._createId) {
1158
+ if (debug)
1159
+ console.log("Aborting loading because create id changed", createId, this._createId);
1160
+ break;
1161
+ }
1162
+ const file = files[i];
1163
+ args?.onLoadingStart?.call(this, i, file);
1164
+ if (debug)
1165
+ console.log("Context Load " + file);
1166
+ const res = await loader.loadSync(this, file, file, loadingHash, prog => {
1167
+ if (args.abortSignal?.aborted)
1168
+ return;
1169
+ progressArg.name = file;
1170
+ progressArg.progress = prog;
1171
+ progressArg.index = i;
1172
+ progressArg.count = files.length;
1173
+ args.onLoadingProgress?.call(this, progressArg);
1174
+ });
1175
+ args?.onLoadingFinished?.call(this, i, file, res ?? null);
1176
+ if (res) {
1177
+ results.push({
1178
+ src: file,
1179
+ file: res
1180
+ });
1181
+ }
1182
+ else {
1183
+ // a file could not be loaded
1184
+ console.warn("Could not load file: " + file);
1185
+ }
1186
+ }
1187
+ // if the id was changed while still loading
1188
+ // then we want to cleanup/destroy previously loaded files
1189
+ if (createId !== this._createId || args.abortSignal?.aborted) {
1190
+ if (debug)
1191
+ console.log("Aborting loading because create id changed or abort signal was set", createId, this._createId);
1192
+ for (const res of results) {
1193
+ if (res && res.file) {
1194
+ for (const scene of res.file.scenes)
1195
+ destroy(scene, true, true);
1196
+ }
1197
+ }
1198
+ }
1199
+ // otherwise we want to add the loaded files to the current scene
1200
+ else {
1201
+ let anyModelFound = false;
1202
+ for (const res of results) {
1203
+ if (res && res.file) {
1204
+ // TODO: should we load all scenes in a glTF here?
1205
+ if (res.file.scene) {
1206
+ anyModelFound = true;
1207
+ this.scene.add(res.file.scene);
1208
+ }
1209
+ else {
1210
+ console.warn("No scene found in loaded file");
1211
+ }
1212
+ }
1213
+ }
1214
+ // If the loaded files do not contain ANY model
1215
+ // We then attempt to create a mesh from each material in the loaded files to visualize it
1216
+ // It's ok to do this at this point because we know the context has been cleared because the whole `src` attribute has been set
1217
+ if (!anyModelFound) {
1218
+ for (const res of results) {
1219
+ if (res && res.file && "parser" in res.file) {
1220
+ let y = 0;
1221
+ if (!Array.isArray(res.file.parser.json.materials))
1222
+ continue;
1223
+ for (let i = 0; i < res.file.parser.json.materials.length; i++) {
1224
+ const mat = await res.file.parser.getDependency("material", i);
1225
+ const parent = new Object3D();
1226
+ parent.position.x = i * 1.1;
1227
+ parent.position.y = y;
1228
+ this.scene.add(parent);
1229
+ ObjectUtils.createPrimitive("ShaderBall", {
1230
+ parent,
1231
+ material: mat
1232
+ });
1233
+ }
1234
+ y += 1;
1235
+ }
1236
+ }
1237
+ }
1238
+ }
1239
+ return results;
1240
+ }
1241
+ /** Sets the animation loop.
1242
+ * Can not be done while creating the context or when disposed
1243
+ **/
1244
+ restartRenderLoop() {
1245
+ if (!this.renderer) {
1246
+ console.error("Can not start render loop without renderer");
1247
+ return false;
1248
+ }
1249
+ if (this._isCreating) {
1250
+ console.warn("Can not start render loop while creating context");
1251
+ return false;
1252
+ }
1253
+ this.renderer.setAnimationLoop((timestamp, frame) => {
1254
+ if (this.isManagedExternally)
1255
+ return;
1256
+ this.update(timestamp, frame);
1257
+ });
1258
+ return true;
1259
+ }
1260
+ _renderlooperrors = 0;
1261
+ /** Performs a full update step including script callbacks, rendering (unless isManagedExternally is set to false) and post render callbacks */
1262
+ update(timestamp, frame) {
1263
+ if (frame === undefined)
1264
+ frame = null;
1265
+ if (isDevEnvironment() || debug || looputils.hasNewScripts()) {
1266
+ try {
1267
+ //performance.mark('update.start');
1268
+ this.internalStep(timestamp, frame);
1269
+ this._renderlooperrors = 0;
1270
+ //performance.mark('update.end');
1271
+ //performance.measure('NE Frame', 'update.start', 'update.end');
1272
+ }
1273
+ catch (err) {
1274
+ this._renderlooperrors += 1;
1275
+ if ((isDevEnvironment() || debug) && (err instanceof Error || err instanceof TypeError))
1276
+ showBalloonMessage(`Caught unhandled exception during render-loop - see console for details.`, LogType.Error);
1277
+ console.error("Frame #" + this.time.frame + "\n", err);
1278
+ if (this._renderlooperrors >= 3) {
1279
+ console.warn("Stopping render loop due to error");
1280
+ this.renderer.setAnimationLoop(null);
1281
+ }
1282
+ this.domElement.dispatchEvent(new CustomEvent("error", { detail: err }));
1283
+ }
1284
+ }
1285
+ else {
1286
+ this.internalStep(timestamp, frame);
1287
+ }
1288
+ }
1289
+ /** Call to **manually** perform physics steps.
1290
+ * By default the context uses the `physicsSteps` property to perform steps during the update loop
1291
+ * If you just want to increase the accuracy of physics you can instead set the `physicsSteps` property to a higher value
1292
+ * */
1293
+ updatePhysics(steps) {
1294
+ this.internalUpdatePhysics(steps);
1295
+ }
1296
+ /**
1297
+ * Set a rect or dom element. The camera center will be moved to the center of the rect.
1298
+ * This is useful if you have Needle Engine embedded in a HTML layout and while you want the webgl background to fill e.g. the whole screen you want to move the camera center to free space.
1299
+ * For that you can simply pass in the rect or HMTL div that you want the camera to center on.
1300
+ * @param rect The focus rect or null to disable
1301
+ * @param settings Optional settings for the focus rect. These will override the `focusRectSettings` property
1302
+ */
1303
+ setCameraFocusRect(rect, settings) {
1304
+ const oldRect = this._focusRect;
1305
+ this._focusRect = rect;
1306
+ if (settings) {
1307
+ Object.assign(this.focusRectSettings, settings);
1308
+ }
1309
+ if (settings?.damping === undefined) {
1310
+ // if the new rect is on screen then set damping
1311
+ if (oldRect) {
1312
+ let domRect = oldRect;
1313
+ if (oldRect instanceof HTMLElement) {
1314
+ domRect = oldRect.getBoundingClientRect();
1315
+ }
1316
+ if (domRect && "top" in domRect) {
1317
+ const allowedDistance = 100;
1318
+ const isVisible = domRect.bottom >= -allowedDistance && domRect.right >= -allowedDistance && domRect.top <= window.innerHeight + allowedDistance && domRect.left <= window.innerWidth + allowedDistance;
1319
+ if (isVisible)
1320
+ this.focusRectSettings.damping = .2;
1321
+ }
1322
+ }
1323
+ }
1324
+ }
1325
+ get focusRect() { return this._focusRect; }
1326
+ get focusRectSize() {
1327
+ const rect = this._focusRect;
1328
+ if (rect && (rect instanceof DOMRect || ("width" in rect && "height" in rect && "x" in rect && "y" in rect))) {
1329
+ return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
1330
+ }
1331
+ else if (rect instanceof HTMLElement) {
1332
+ const r = rect.getBoundingClientRect();
1333
+ return { x: r.x, y: r.y, width: r.width, height: r.height };
1334
+ }
1335
+ return null;
1336
+ }
1337
+ /** Settings when a focus rect is set. Use `setCameraFocusRect(...)` to do so.
1338
+ * This can be used to offset the renderer center e.g. to a specific DOM element.
1339
+ */
1340
+ focusRectSettings = {
1341
+ /** Controls how fast the rect is centered. Smaller values mean the rect is centered faster.
1342
+ * A minimum value of 0 means the rect is centered instantly.
1343
+ * @default 0
1344
+ */
1345
+ damping: 0,
1346
+ /**
1347
+ * Zoom factor when a focus rect is set.
1348
+ */
1349
+ zoom: 1,
1350
+ /**
1351
+ * Additional offset in pixels from the center of the rect
1352
+ */
1353
+ offsetX: 0,
1354
+ /**
1355
+ * Additional offset in pixels from the center of the rect
1356
+ */
1357
+ offsetY: 0,
1358
+ };
1359
+ _focusRect = null;
1360
+ _lastTimestamp = 0;
1361
+ _accumulatedTime = 0;
1362
+ _dispatchReadyAfterFrame = false;
1363
+ // TODO: we need to skip after render callbacks if the render loop is managed externally. When changing this we also need to to update the r3f sample
1364
+ internalStep(timestamp, frame) {
1365
+ if (this.internalOnBeforeRender(timestamp, frame) === false)
1366
+ return;
1367
+ this.internalOnRender();
1368
+ this.internalOnAfterRender();
1369
+ }
1370
+ internalOnBeforeRender(timestamp, frame) {
1371
+ // If we don't auto reset we get wrong stats in WebXR. AutoReset was turned off to support custom blits and count them too
1372
+ // But when we're using postprocessing we need to reset manually: https://discourse.threejs.org/t/accessing-draw-calls-when-using-effectcomposer
1373
+ this.renderer.info.autoReset = frame ? true : false;
1374
+ if (this.renderer.info.autoReset === false) {
1375
+ this.renderer.info.reset();
1376
+ }
1377
+ this._needsVisibleUpdate = true;
1378
+ const sessionStarted = frame !== null && this._xrFrame === null;
1379
+ this._xrFrame = frame;
1380
+ if (sessionStarted) {
1381
+ this.domElement.dispatchEvent(new CustomEvent("xr-session-started", { detail: { context: this, session: this.xrSession, frame: frame } }));
1382
+ }
1383
+ this._currentFrameEvent = FrameEvent.Undefined;
1384
+ if (this.isManagedExternally === false && this.isInXR === false && this.targetFrameRate !== undefined) {
1385
+ if (this._lastTimestamp === 0)
1386
+ this._lastTimestamp = timestamp;
1387
+ this._accumulatedTime += (timestamp - this._lastTimestamp) / 1000;
1388
+ this._lastTimestamp = timestamp;
1389
+ let targetFrameRate = this.targetFrameRate;
1390
+ if (typeof targetFrameRate === "object")
1391
+ targetFrameRate = targetFrameRate.value;
1392
+ // if(debug) console.log(this._accumulatedTime, (1 / (targetFrameRate)))
1393
+ if (this._accumulatedTime < (1 / (targetFrameRate + 1))) {
1394
+ return false;
1395
+ }
1396
+ this._accumulatedTime = 0;
1397
+ }
1398
+ this._stats?.begin();
1399
+ Context.Current = this;
1400
+ if (this.onHandlePaused())
1401
+ return false;
1402
+ Context.Current = this;
1403
+ this.time.update();
1404
+ if (debugframerate)
1405
+ console.log("FPS", (this.time.smoothedFps).toFixed(0));
1406
+ looputils.processNewScripts(this);
1407
+ looputils.updateIsActive(this.scene);
1408
+ looputils.processStart(this);
1409
+ invokeLifecycleFunctions(this, FrameEvent.Start);
1410
+ while (this._cameraStack.length > 0 && (!this.mainCameraComponent || this.mainCameraComponent.destroyed)) {
1411
+ this._cameraStack.splice(this._cameraStack.length - 1, 1);
1412
+ const last = this._cameraStack[this._cameraStack.length - 1];
1413
+ this.setCurrentCamera(last);
1414
+ }
1415
+ if (this.pre_update_oneshot_callbacks) {
1416
+ for (const i in this.pre_update_oneshot_callbacks) {
1417
+ this.pre_update_oneshot_callbacks[i]();
1418
+ }
1419
+ this.pre_update_oneshot_callbacks.length = 0;
1420
+ }
1421
+ if (this.pre_update_callbacks) {
1422
+ for (const i in this.pre_update_callbacks) {
1423
+ this.pre_update_callbacks[i]();
1424
+ }
1425
+ }
1426
+ this._currentFrameEvent = FrameEvent.EarlyUpdate;
1427
+ for (let i = 0; i < this.scripts_earlyUpdate.length; i++) {
1428
+ const script = this.scripts_earlyUpdate[i];
1429
+ if (!script.activeAndEnabled)
1430
+ continue;
1431
+ if (script.earlyUpdate !== undefined) {
1432
+ Context.Current = this;
1433
+ script.earlyUpdate();
1434
+ }
1435
+ }
1436
+ this.executeCoroutines(FrameEvent.EarlyUpdate);
1437
+ invokeLifecycleFunctions(this, FrameEvent.EarlyUpdate);
1438
+ this._currentFrameEvent = FrameEvent.Update;
1439
+ for (let i = 0; i < this.scripts_update.length; i++) {
1440
+ const script = this.scripts_update[i];
1441
+ if (!script.activeAndEnabled)
1442
+ continue;
1443
+ if (script.update !== undefined) {
1444
+ Context.Current = this;
1445
+ script.update();
1446
+ }
1447
+ }
1448
+ this.executeCoroutines(FrameEvent.Update);
1449
+ invokeLifecycleFunctions(this, FrameEvent.Update);
1450
+ this._currentFrameEvent = FrameEvent.LateUpdate;
1451
+ for (let i = 0; i < this.scripts_lateUpdate.length; i++) {
1452
+ const script = this.scripts_lateUpdate[i];
1453
+ if (!script.activeAndEnabled)
1454
+ continue;
1455
+ if (script.lateUpdate !== undefined) {
1456
+ Context.Current = this;
1457
+ script.lateUpdate();
1458
+ }
1459
+ }
1460
+ // this.mainLight = null;
1461
+ this.executeCoroutines(FrameEvent.LateUpdate);
1462
+ invokeLifecycleFunctions(this, FrameEvent.LateUpdate);
1463
+ if (this.physicsSteps === undefined) {
1464
+ this.physicsSteps = 1;
1465
+ }
1466
+ if (this.physics.engine && this.physicsSteps > 0) {
1467
+ this.internalUpdatePhysics(this.physicsSteps);
1468
+ }
1469
+ if (this.isVisibleToUser || this.runInBackground) {
1470
+ this._currentFrameEvent = FrameEvent.OnBeforeRender;
1471
+ // should we move these callbacks in the regular three onBeforeRender events?
1472
+ for (let i = 0; i < this.scripts_onBeforeRender.length; i++) {
1473
+ const script = this.scripts_onBeforeRender[i];
1474
+ if (!script.activeAndEnabled)
1475
+ continue;
1476
+ // if(script.isActiveAndEnabled === false) continue;
1477
+ if (script.onBeforeRender !== undefined) {
1478
+ Context.Current = this;
1479
+ script.onBeforeRender(frame);
1480
+ }
1481
+ }
1482
+ this.executeCoroutines(FrameEvent.OnBeforeRender);
1483
+ invokeLifecycleFunctions(this, FrameEvent.OnBeforeRender);
1484
+ if (this._needsUpdateSize)
1485
+ this.updateSize();
1486
+ if (this.pre_render_callbacks) {
1487
+ for (const i in this.pre_render_callbacks) {
1488
+ this.pre_render_callbacks[i](frame);
1489
+ }
1490
+ }
1491
+ if (this._focusRect) {
1492
+ if (this.mainCamera instanceof PerspectiveCamera) {
1493
+ const settings = this.focusRectSettings;
1494
+ const dt = settings.damping > 0 ? this.time.deltaTime / settings.damping : 1;
1495
+ updateCameraFocusRect(this._focusRect, this.focusRectSettings, dt, this.mainCamera, this.renderer);
1496
+ }
1497
+ }
1498
+ }
1499
+ return true;
1500
+ }
1501
+ internalUpdatePhysics(steps) {
1502
+ if (!this.physics.engine)
1503
+ return false;
1504
+ const physicsSteps = steps;
1505
+ const dt = this.time.deltaTime / physicsSteps;
1506
+ for (let i = 0; i < physicsSteps; i++) {
1507
+ this._currentFrameEvent = FrameEvent.PrePhysicsStep;
1508
+ this.executeCoroutines(FrameEvent.PrePhysicsStep);
1509
+ this.physics.engine.step(dt);
1510
+ this._currentFrameEvent = FrameEvent.PostPhysicsStep;
1511
+ this.executeCoroutines(FrameEvent.PostPhysicsStep);
1512
+ }
1513
+ this.physics.engine.postStep();
1514
+ return true;
1515
+ }
1516
+ internalOnRender() {
1517
+ if (!this.isManagedExternally) {
1518
+ // when loading assets we compile them async after GLTFLoader is done
1519
+ // but as a fallback we still register them (if e.g. there's no camera for compile async)
1520
+ looputils.runPrewarm(this);
1521
+ this._currentFrameEvent = FrameEvent.Undefined;
1522
+ nodeFrame.camera = this.mainCamera;
1523
+ nodeFrame.update();
1524
+ this.renderNow();
1525
+ this._currentFrameEvent = FrameEvent.OnAfterRender;
1526
+ }
1527
+ }
1528
+ internalOnAfterRender() {
1529
+ if (this.isVisibleToUser || this.runInBackground) {
1530
+ for (let i = 0; i < this.scripts_onAfterRender.length; i++) {
1531
+ const script = this.scripts_onAfterRender[i];
1532
+ if (!script.activeAndEnabled)
1533
+ continue;
1534
+ if (script.onAfterRender !== undefined) {
1535
+ Context.Current = this;
1536
+ script.onAfterRender();
1537
+ }
1538
+ }
1539
+ this.executeCoroutines(FrameEvent.OnAfterRender);
1540
+ invokeLifecycleFunctions(this, FrameEvent.OnAfterRender);
1541
+ if (this.post_render_callbacks) {
1542
+ for (const i in this.post_render_callbacks) {
1543
+ this.post_render_callbacks[i]();
1544
+ }
1545
+ }
1546
+ }
1547
+ this._currentFrameEvent = -1;
1548
+ this.connection.sendBufferedMessagesNow();
1549
+ if (this._stats) {
1550
+ this._stats.end();
1551
+ if (this.time.frameCount % 150 === 0)
1552
+ console.log(this.renderer.info.render.calls + " DrawCalls", "\nRender:", { ...this.renderer.info.render }, "\nMemory:", { ...this.renderer.info.memory }, "\nTarget Framerate: " + this.targetFrameRate);
1553
+ }
1554
+ if (this._dispatchReadyAfterFrame) {
1555
+ this._dispatchReadyAfterFrame = false;
1556
+ this.domElement.dispatchEvent(new CustomEvent("ready"));
1557
+ ContextRegistry.dispatchCallback(ContextEvent.ContextFirstFrameRendered, this);
1558
+ }
1559
+ }
1560
+ _tempClearColor = new Color();
1561
+ _tempClearColor2 = new Color();
1562
+ renderNow(camera) {
1563
+ if (!camera) {
1564
+ camera = this.mainCamera;
1565
+ if (!camera)
1566
+ return false;
1567
+ }
1568
+ this.handleRendererContextLost();
1569
+ this._isRendering = true;
1570
+ this.renderRequiredTextures();
1571
+ if (this.renderer.toneMapping !== NoToneMapping)
1572
+ patchTonemapping(this);
1573
+ if (this.composer && !this.isInXR) {
1574
+ // if a camera is passed in we need to check if we need to update the composer's camera
1575
+ if (camera && "setMainCamera" in this.composer) {
1576
+ const currentPassesCamera = this.composer.passes[0]?.mainCamera;
1577
+ if (currentPassesCamera != camera)
1578
+ this.composer.setMainCamera(camera);
1579
+ }
1580
+ const backgroundColor = this.renderer.getClearColor(this._tempClearColor);
1581
+ const clearAlpha = this.renderer.getClearAlpha();
1582
+ this._tempClearColor2.copy(backgroundColor);
1583
+ this.renderer.setClearColor(backgroundColor.convertSRGBToLinear(), this.renderer.getClearAlpha());
1584
+ this.composer.render(this.time.deltaTime);
1585
+ this.renderer.setClearColor(this._tempClearColor2, clearAlpha); // restore clear color
1586
+ }
1587
+ else if (camera) {
1588
+ // Workaround for issue on Vision Pro –
1589
+ // depth buffer is not cleared between eye draws, despite the spec...
1590
+ if (this.isInXR && DeviceUtilities.isMacOS())
1591
+ this.renderer.clearDepth();
1592
+ this.renderer.render(this.scene, camera);
1593
+ }
1594
+ this._isRendering = false;
1595
+ return true;
1596
+ }
1597
+ _contextRestoreTries = 0;
1598
+ handleRendererContextLost() {
1599
+ // Try to restore the context every x frames
1600
+ if (this.time.frame % 10 && this.renderer.getContext().isContextLost()) {
1601
+ if (this._contextRestoreTries++ < 100) {
1602
+ console.warn("Attempting to recover WebGL context...");
1603
+ this.renderer.forceContextRestore();
1604
+ }
1605
+ }
1606
+ }
1607
+ /** returns true if we should return out of the frame loop */
1608
+ _wasPaused = false;
1609
+ onHandlePaused() {
1610
+ const paused = this.evaluatePaused();
1611
+ if (this._wasPaused !== paused) {
1612
+ if (debugActive)
1613
+ console.log("Paused?", paused, "context:" + this.alias);
1614
+ for (let i = 0; i < this.scripts_pausedChanged.length; i++) {
1615
+ const script = this.scripts_pausedChanged[i];
1616
+ if (!script.activeAndEnabled)
1617
+ continue;
1618
+ if (script.onPausedChanged !== undefined) {
1619
+ Context.Current = this;
1620
+ script.onPausedChanged(paused, this._wasPaused);
1621
+ }
1622
+ }
1623
+ }
1624
+ this._wasPaused = paused;
1625
+ return paused;
1626
+ }
1627
+ evaluatePaused() {
1628
+ if (this.isInXR)
1629
+ return false;
1630
+ if (this.isPaused)
1631
+ return true;
1632
+ // if the element is not visible use the runInBackground flag to determine if we should continue
1633
+ if (this.runInBackground) {
1634
+ return false;
1635
+ }
1636
+ const paused = !this.isVisibleToUser;
1637
+ return paused;
1638
+ }
1639
+ renderRequiredTextures() {
1640
+ if (!this.mainCamera)
1641
+ return;
1642
+ if (!this._requireDepthTexture && !this._requireColorTexture)
1643
+ return;
1644
+ if (!this._renderTarget) {
1645
+ this._renderTarget = new WebGLRenderTarget(this.domWidth, this.domHeight);
1646
+ if (this._requireDepthTexture) {
1647
+ const dt = new DepthTexture(this.domWidth, this.domHeight);
1648
+ ;
1649
+ this._renderTarget.depthTexture = dt;
1650
+ }
1651
+ if (this._requireColorTexture) {
1652
+ this._renderTarget.texture = new Texture();
1653
+ this._renderTarget.texture.generateMipmaps = false;
1654
+ this._renderTarget.texture.minFilter = NearestFilter;
1655
+ this._renderTarget.texture.magFilter = NearestFilter;
1656
+ this._renderTarget.texture.format = RGBAFormat;
1657
+ }
1658
+ }
1659
+ const rt = this._renderTarget;
1660
+ if (rt.texture) {
1661
+ rt.texture.colorSpace = this.renderer.outputColorSpace;
1662
+ }
1663
+ const prevTarget = this.renderer.getRenderTarget();
1664
+ this.renderer.setRenderTarget(rt);
1665
+ this.renderer.render(this.scene, this.mainCamera);
1666
+ this.renderer.setRenderTarget(prevTarget);
1667
+ }
1668
+ executeCoroutines(evt) {
1669
+ if (this.coroutines[evt]) {
1670
+ const evts = this.coroutines[evt];
1671
+ for (let i = 0; i < evts.length; i++) {
1672
+ try {
1673
+ const evt = evts[i];
1674
+ // TODO we might want to keep coroutines playing even if the component is disabled or inactive
1675
+ const remove = !evt.comp || evt.comp.destroyed || !evt.main || evt.comp["enabled"] === false;
1676
+ if (remove) {
1677
+ if (debugCoroutine)
1678
+ console.log("Removing coroutine", evt.comp, evt.comp["enabled"]);
1679
+ evts.splice(i, 1);
1680
+ --i;
1681
+ continue;
1682
+ }
1683
+ const iter = evt.chained;
1684
+ if (iter && iter.length > 0) {
1685
+ const last = iter[iter.length - 1];
1686
+ const res = last.next();
1687
+ if (res.done) {
1688
+ iter.pop();
1689
+ }
1690
+ if (isGenerator(res)) {
1691
+ if (!evt.chained)
1692
+ evt.chained = [];
1693
+ evt.chained.push(res.value);
1694
+ }
1695
+ if (!res.done)
1696
+ continue;
1697
+ }
1698
+ const res = evt.main.next();
1699
+ if (res.done === true) {
1700
+ evts.splice(i, 1);
1701
+ --i;
1702
+ continue;
1703
+ }
1704
+ const val = res.value;
1705
+ if (isGenerator(val)) {
1706
+ // invoke once if its a generator
1707
+ // this means e.g. WaitForFrame(1) works and will capture
1708
+ // the frame it was created
1709
+ const gen = val;
1710
+ const res = gen.next();
1711
+ if (res.done)
1712
+ continue;
1713
+ if (!evt.chained)
1714
+ evt.chained = [];
1715
+ evt.chained.push(val);
1716
+ }
1717
+ else if (val instanceof Promise) {
1718
+ // If its a promise we want to wait for it to resolve
1719
+ const prom = val;
1720
+ if (!evt.chained)
1721
+ evt.chained = [];
1722
+ const nested = WaitForPromise(prom);
1723
+ evt.chained?.push(nested);
1724
+ continue;
1725
+ }
1726
+ }
1727
+ catch (e) {
1728
+ console.error(e);
1729
+ }
1730
+ }
1731
+ }
1732
+ function isGenerator(val) {
1733
+ if (val) {
1734
+ if (val.next && val.return) {
1735
+ return true;
1736
+ }
1737
+ }
1738
+ return false;
1739
+ }
1740
+ }
1741
+ }
1742
+ // const scene = new Scene();
1743
+ // const useComposer = utils.getParam("postfx");
1744
+ // const renderer = new WebGLRenderer({ antialias: true });
1745
+ // const composer = useComposer ? new EffectComposer(renderer) : undefined;
1746
+ // renderer.setClearColor(new Color('lightgrey'), 0)
1747
+ // renderer.antialias = true;
1748
+ // renderer.alpha = false;
1749
+ // renderer.shadowMap.enabled = true;
1750
+ // renderer.shadowMap.type = PCFSoftShadowMap;
1751
+ // renderer.setSize(window.innerWidth, window.innerHeight);
1752
+ // renderer.outputEncoding = sRGBEncoding;
1753
+ // renderer.physicallyCorrectLights = true;
1754
+ // document.body.appendChild(renderer.domElement);
1755
+ // // generation pushes loading requests in this array
1756
+ // const sceneData: {
1757
+ // mainCamera: Camera | undefined
1758
+ // } = {
1759
+ // preparing: [],
1760
+ // resolving: [],
1761
+ // scripts: [],
1762
+ // raycastTargets: [],
1763
+ // mainCamera: undefined,
1764
+ // mainCameraComponent: undefined,
1765
+ // };
1766
+ // // contains a list of functions to be called after loading is done
1767
+ // const post_setup_callbacks = [];
1768
+ // const pre_render_Callbacks = [];
1769
+ // const post_render_callbacks = [];
1770
+ // const new_scripts = [];
1771
+ // const new_scripts_post_setup_callbacks = [];
1772
+ // const new_scripts_pre_setup_callbacks = [];
1773
+ // export {
1774
+ // scene, renderer, composer,
1775
+ // new_scripts,
1776
+ // new_scripts_post_setup_callbacks, new_scripts_pre_setup_callbacks,
1777
+ // sceneData,
1778
+ // post_setup_callbacks,
1779
+ // pre_render_Callbacks,
1780
+ // post_render_callbacks
1781
+ // }
1785
1782
  //# sourceMappingURL=engine_context.js.map