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