@needle-tools/engine 4.7.0-alpha → 4.7.0-next.22483b4

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 (1067) hide show
  1. package/CHANGELOG.md +3768 -3768
  2. package/LICENSE.md +10 -10
  3. package/README.md +64 -64
  4. package/components.needle.json +1 -1
  5. package/dist/generateMeshBVH.worker-BaNp_Xtp.js +25 -0
  6. package/dist/{gltf-progressive-Bm9eEfgu.min.js → gltf-progressive-Bl4okF1b.min.js} +1 -1
  7. package/dist/{gltf-progressive-GjIqwSG3.js → gltf-progressive-DSpdn0QT.js} +2 -2
  8. package/dist/{gltf-progressive-Dn6o99rH.umd.cjs → gltf-progressive-P8b8a0qY.umd.cjs} +1 -1
  9. package/dist/{needle-engine.bundle-BEUFTdl6.min.js → needle-engine.bundle-BnGp0ASs.min.js} +75 -74
  10. package/dist/{needle-engine.bundle-CK8cQ3FX.umd.cjs → needle-engine.bundle-CP5AJL1A.umd.cjs} +66 -65
  11. package/dist/{needle-engine.bundle-CXtflnL6.js → needle-engine.bundle-RgZNbAgp.js} +1185 -1176
  12. package/dist/needle-engine.d.ts +130 -129
  13. package/dist/needle-engine.js +4 -4
  14. package/dist/needle-engine.min.js +1 -1
  15. package/dist/needle-engine.umd.cjs +1 -1
  16. package/dist/{postprocessing-8AQTeZsr.umd.cjs → postprocessing-CjW23fio.umd.cjs} +2 -2
  17. package/dist/{postprocessing-BkxN_08g.js → postprocessing-DYLNOL3W.js} +3 -3
  18. package/dist/{postprocessing-BDScN9yu.min.js → postprocessing-xYQWCHFu.min.js} +2 -2
  19. package/dist/{three-DMrv-4ar.umd.cjs → three-B_hneGZr.umd.cjs} +4 -4
  20. package/dist/{three-Bz6X1mrw.js → three-DrqIzZTH.js} +4198 -4198
  21. package/dist/{three-Boa-jOq-.min.js → three-DuDKwKB8.min.js} +33 -33
  22. package/dist/{three-examples-GggCDHv0.js → three-examples-B50TT3Iu.js} +5 -5
  23. package/dist/{three-examples-DuVhxqft.min.js → three-examples-DaDLBuy6.min.js} +14 -14
  24. package/dist/{three-examples-C7ryg8vN.umd.cjs → three-examples-X3OadjXB.umd.cjs} +3 -3
  25. package/dist/{three-mesh-ui-CY6Izc7C.min.js → three-mesh-ui-B3p3gyUz.min.js} +1 -1
  26. package/dist/{three-mesh-ui-CwlN0FUC.umd.cjs → three-mesh-ui-CQiIQIlA.umd.cjs} +1 -1
  27. package/dist/{three-mesh-ui-CLNOfsWn.js → three-mesh-ui-CxuWt7m-.js} +1 -1
  28. package/dist/{vendor-zxXa3Dmr.min.js → vendor-BlSxe9JJ.min.js} +3 -3
  29. package/dist/{vendor-BSD1RQIh.js → vendor-BmYIgaS1.js} +3 -3
  30. package/dist/{vendor-DHr4aqIZ.umd.cjs → vendor-Cavtu3CP.umd.cjs} +3 -3
  31. package/lib/asap/needle-asap.d.ts +1 -1
  32. package/lib/asap/needle-asap.js +95 -95
  33. package/lib/asap/sessiongranted.d.ts +3 -3
  34. package/lib/asap/sessiongranted.js +65 -65
  35. package/lib/asap/utils.d.ts +1 -1
  36. package/lib/asap/utils.js +3 -3
  37. package/lib/engine/analytics/index.d.ts +6 -6
  38. package/lib/engine/analytics/index.js +12 -12
  39. package/lib/engine/analytics/lcp.d.ts +3 -3
  40. package/lib/engine/analytics/lcp.js +34 -34
  41. package/lib/engine/api.d.ts +82 -82
  42. package/lib/engine/api.js +81 -81
  43. package/lib/engine/assets/index.d.ts +11 -11
  44. package/lib/engine/assets/index.js +47 -47
  45. package/lib/engine/assets/static.d.ts +1 -1
  46. package/lib/engine/assets/static.js +4 -4
  47. package/lib/engine/codegen/register_types.d.ts +1 -1
  48. package/lib/engine/codegen/register_types.js +300 -300
  49. package/lib/engine/debug/debug.d.ts +15 -15
  50. package/lib/engine/debug/debug.js +44 -44
  51. package/lib/engine/debug/debug_console.d.ts +2 -2
  52. package/lib/engine/debug/debug_console.js +307 -307
  53. package/lib/engine/debug/debug_overlay.d.ts +22 -22
  54. package/lib/engine/debug/debug_overlay.js +316 -316
  55. package/lib/engine/debug/debug_spatial_console.d.ts +2 -2
  56. package/lib/engine/debug/debug_spatial_console.js +390 -390
  57. package/lib/engine/debug/index.d.ts +2 -2
  58. package/lib/engine/debug/index.js +2 -2
  59. package/lib/engine/engine_addressables.d.ts +166 -166
  60. package/lib/engine/engine_addressables.js +608 -608
  61. package/lib/engine/engine_animation.d.ts +43 -43
  62. package/lib/engine/engine_animation.js +133 -133
  63. package/lib/engine/engine_application.d.ts +45 -45
  64. package/lib/engine/engine_application.js +104 -104
  65. package/lib/engine/engine_assetdatabase.d.ts +25 -25
  66. package/lib/engine/engine_assetdatabase.js +346 -346
  67. package/lib/engine/engine_audio.d.ts +4 -4
  68. package/lib/engine/engine_audio.js +23 -23
  69. package/lib/engine/engine_camera.d.ts +13 -13
  70. package/lib/engine/engine_camera.js +30 -30
  71. package/lib/engine/engine_components.d.ts +110 -110
  72. package/lib/engine/engine_components.js +374 -374
  73. package/lib/engine/engine_components_internal.d.ts +9 -9
  74. package/lib/engine/engine_components_internal.js +36 -36
  75. package/lib/engine/engine_constants.d.ts +10 -10
  76. package/lib/engine/engine_constants.js +41 -41
  77. package/lib/engine/engine_context.d.ts +475 -475
  78. package/lib/engine/engine_context.js +1673 -1672
  79. package/lib/engine/engine_context.js.map +1 -1
  80. package/lib/engine/engine_context_registry.d.ts +71 -71
  81. package/lib/engine/engine_context_registry.js +117 -117
  82. package/lib/engine/engine_coroutine.d.ts +35 -35
  83. package/lib/engine/engine_coroutine.js +52 -52
  84. package/lib/engine/engine_create_objects.d.ts +119 -119
  85. package/lib/engine/engine_create_objects.js +320 -320
  86. package/lib/engine/engine_default_parameters.d.ts +2 -2
  87. package/lib/engine/engine_default_parameters.js +3 -3
  88. package/lib/engine/engine_editor-sync.d.ts +21 -21
  89. package/lib/engine/engine_editor-sync.js +4 -4
  90. package/lib/engine/engine_fileloader.d.ts +2 -2
  91. package/lib/engine/engine_fileloader.js +8 -8
  92. package/lib/engine/engine_gameobject.d.ts +68 -68
  93. package/lib/engine/engine_gameobject.js +619 -619
  94. package/lib/engine/engine_generic_utils.d.ts +1 -1
  95. package/lib/engine/engine_generic_utils.js +13 -13
  96. package/lib/engine/engine_gizmos.d.ts +149 -149
  97. package/lib/engine/engine_gizmos.js +530 -530
  98. package/lib/engine/engine_gltf.d.ts +12 -12
  99. package/lib/engine/engine_gltf.js +15 -15
  100. package/lib/engine/engine_gltf_builtin_components.d.ts +11 -11
  101. package/lib/engine/engine_gltf_builtin_components.js +341 -341
  102. package/lib/engine/engine_hot_reload.d.ts +8 -8
  103. package/lib/engine/engine_hot_reload.js +197 -197
  104. package/lib/engine/engine_input.d.ts +362 -362
  105. package/lib/engine/engine_input.js +1294 -1294
  106. package/lib/engine/engine_input_utils.d.ts +2 -2
  107. package/lib/engine/engine_input_utils.js +22 -22
  108. package/lib/engine/engine_instancing.d.ts +19 -19
  109. package/lib/engine/engine_instancing.js +39 -39
  110. package/lib/engine/engine_license.d.ts +11 -11
  111. package/lib/engine/engine_license.js +369 -369
  112. package/lib/engine/engine_lifecycle_api.d.ts +83 -83
  113. package/lib/engine/engine_lifecycle_api.js +106 -106
  114. package/lib/engine/engine_lifecycle_functions_internal.d.ts +32 -32
  115. package/lib/engine/engine_lifecycle_functions_internal.js +146 -146
  116. package/lib/engine/engine_lightdata.d.ts +23 -23
  117. package/lib/engine/engine_lightdata.js +91 -91
  118. package/lib/engine/engine_loaders.callbacks.d.ts +97 -97
  119. package/lib/engine/engine_loaders.callbacks.js +86 -86
  120. package/lib/engine/engine_loaders.d.ts +48 -48
  121. package/lib/engine/engine_loaders.gltf.d.ts +13 -13
  122. package/lib/engine/engine_loaders.gltf.js +62 -62
  123. package/lib/engine/engine_loaders.js +337 -337
  124. package/lib/engine/engine_lods.d.ts +31 -31
  125. package/lib/engine/engine_lods.js +146 -146
  126. package/lib/engine/engine_mainloop_utils.d.ts +32 -32
  127. package/lib/engine/engine_mainloop_utils.js +466 -466
  128. package/lib/engine/engine_math.d.ts +114 -114
  129. package/lib/engine/engine_math.js +247 -247
  130. package/lib/engine/engine_modules.d.ts +36 -36
  131. package/lib/engine/engine_modules.js +85 -85
  132. package/lib/engine/engine_networking.d.ts +260 -260
  133. package/lib/engine/engine_networking.js +764 -764
  134. package/lib/engine/engine_networking_auto.d.ts +24 -24
  135. package/lib/engine/engine_networking_auto.js +310 -310
  136. package/lib/engine/engine_networking_blob.d.ts +48 -48
  137. package/lib/engine/engine_networking_blob.js +228 -228
  138. package/lib/engine/engine_networking_files.d.ts +35 -35
  139. package/lib/engine/engine_networking_files.js +172 -172
  140. package/lib/engine/engine_networking_files_default_components.d.ts +6 -6
  141. package/lib/engine/engine_networking_files_default_components.js +42 -42
  142. package/lib/engine/engine_networking_instantiate.d.ts +100 -100
  143. package/lib/engine/engine_networking_instantiate.js +345 -345
  144. package/lib/engine/engine_networking_peer.d.ts +15 -15
  145. package/lib/engine/engine_networking_peer.js +132 -132
  146. package/lib/engine/engine_networking_streams.d.ts +123 -123
  147. package/lib/engine/engine_networking_streams.js +645 -645
  148. package/lib/engine/engine_networking_types.d.ts +22 -22
  149. package/lib/engine/engine_networking_types.js +7 -7
  150. package/lib/engine/engine_networking_utils.d.ts +2 -2
  151. package/lib/engine/engine_networking_utils.js +20 -20
  152. package/lib/engine/engine_networking_websocket.d.ts +1 -1
  153. package/lib/engine/engine_networking_websocket.js +2 -2
  154. package/lib/engine/engine_patcher.d.ts +10 -10
  155. package/lib/engine/engine_patcher.js +142 -142
  156. package/lib/engine/engine_physics.d.ts +152 -152
  157. package/lib/engine/engine_physics.js +645 -645
  158. package/lib/engine/engine_physics.types.d.ts +40 -40
  159. package/lib/engine/engine_physics.types.js +33 -33
  160. package/lib/engine/engine_physics_rapier.d.ts +147 -147
  161. package/lib/engine/engine_physics_rapier.js +1433 -1433
  162. package/lib/engine/engine_playerview.d.ts +26 -26
  163. package/lib/engine/engine_playerview.js +64 -64
  164. package/lib/engine/engine_scenelighting.d.ts +71 -71
  165. package/lib/engine/engine_scenelighting.js +226 -226
  166. package/lib/engine/engine_serialization.d.ts +3 -3
  167. package/lib/engine/engine_serialization.js +3 -3
  168. package/lib/engine/engine_serialization_builtin_serializer.d.ts +72 -72
  169. package/lib/engine/engine_serialization_builtin_serializer.js +403 -403
  170. package/lib/engine/engine_serialization_core.d.ts +94 -94
  171. package/lib/engine/engine_serialization_core.js +607 -607
  172. package/lib/engine/engine_serialization_decorator.d.ts +23 -23
  173. package/lib/engine/engine_serialization_decorator.js +66 -66
  174. package/lib/engine/engine_setup.d.ts +1 -1
  175. package/lib/engine/engine_setup.js +2 -2
  176. package/lib/engine/engine_shaders.d.ts +53 -53
  177. package/lib/engine/engine_shaders.js +252 -252
  178. package/lib/engine/engine_shims.d.ts +4 -4
  179. package/lib/engine/engine_shims.js +24 -24
  180. package/lib/engine/engine_test_utils.d.ts +39 -39
  181. package/lib/engine/engine_test_utils.js +83 -83
  182. package/lib/engine/engine_texture.d.ts +28 -28
  183. package/lib/engine/engine_texture.js +64 -64
  184. package/lib/engine/engine_three_utils.d.ts +204 -204
  185. package/lib/engine/engine_three_utils.js +784 -784
  186. package/lib/engine/engine_time.d.ts +51 -51
  187. package/lib/engine/engine_time.js +82 -82
  188. package/lib/engine/engine_time_utils.d.ts +88 -88
  189. package/lib/engine/engine_time_utils.js +215 -215
  190. package/lib/engine/engine_tonemapping.d.ts +6 -6
  191. package/lib/engine/engine_tonemapping.js +197 -197
  192. package/lib/engine/engine_types.d.ts +578 -578
  193. package/lib/engine/engine_types.js +95 -95
  194. package/lib/engine/engine_typestore.d.ts +28 -28
  195. package/lib/engine/engine_typestore.js +55 -55
  196. package/lib/engine/engine_util_decorator.d.ts +13 -13
  197. package/lib/engine/engine_util_decorator.js +116 -116
  198. package/lib/engine/engine_utils.d.ts +248 -248
  199. package/lib/engine/engine_utils.js +1012 -1012
  200. package/lib/engine/engine_utils_format.d.ts +24 -24
  201. package/lib/engine/engine_utils_format.js +239 -239
  202. package/lib/engine/engine_utils_screenshot.d.ts +159 -159
  203. package/lib/engine/engine_utils_screenshot.js +522 -522
  204. package/lib/engine/engine_utils_screenshot.xr.d.ts +5 -5
  205. package/lib/engine/engine_utils_screenshot.xr.js +90 -90
  206. package/lib/engine/engine_xr.d.ts +1 -1
  207. package/lib/engine/engine_xr.js +1 -1
  208. package/lib/engine/export/gltf/Writers.d.ts +19 -19
  209. package/lib/engine/export/gltf/Writers.js +24 -24
  210. package/lib/engine/export/gltf/index.d.ts +11 -11
  211. package/lib/engine/export/gltf/index.js +123 -123
  212. package/lib/engine/export/index.d.ts +2 -2
  213. package/lib/engine/export/index.js +2 -2
  214. package/lib/engine/export/state.d.ts +7 -7
  215. package/lib/engine/export/state.js +17 -17
  216. package/lib/engine/export/utils.d.ts +2 -2
  217. package/lib/engine/export/utils.js +7 -7
  218. package/lib/engine/extensions/EXT_texture_exr.d.ts +8 -8
  219. package/lib/engine/extensions/EXT_texture_exr.js +32 -32
  220. package/lib/engine/extensions/NEEDLE_animator_controller_model.d.ts +122 -122
  221. package/lib/engine/extensions/NEEDLE_animator_controller_model.js +95 -95
  222. package/lib/engine/extensions/NEEDLE_components.d.ts +35 -35
  223. package/lib/engine/extensions/NEEDLE_components.js +220 -220
  224. package/lib/engine/extensions/NEEDLE_gameobject_data.d.ts +10 -10
  225. package/lib/engine/extensions/NEEDLE_gameobject_data.js +57 -57
  226. package/lib/engine/extensions/NEEDLE_lighting_settings.d.ts +37 -37
  227. package/lib/engine/extensions/NEEDLE_lighting_settings.js +157 -157
  228. package/lib/engine/extensions/NEEDLE_lightmaps.d.ts +18 -18
  229. package/lib/engine/extensions/NEEDLE_lightmaps.js +99 -99
  230. package/lib/engine/extensions/NEEDLE_persistent_assets.d.ts +11 -11
  231. package/lib/engine/extensions/NEEDLE_persistent_assets.js +63 -63
  232. package/lib/engine/extensions/NEEDLE_progressive.d.ts +1 -1
  233. package/lib/engine/extensions/NEEDLE_progressive.js +1 -1
  234. package/lib/engine/extensions/NEEDLE_render_objects.d.ts +13 -13
  235. package/lib/engine/extensions/NEEDLE_render_objects.js +159 -159
  236. package/lib/engine/extensions/NEEDLE_techniques_webgl.d.ts +38 -38
  237. package/lib/engine/extensions/NEEDLE_techniques_webgl.js +564 -564
  238. package/lib/engine/extensions/extension_resolver.d.ts +4 -4
  239. package/lib/engine/extensions/extension_resolver.js +1 -1
  240. package/lib/engine/extensions/extension_utils.d.ts +12 -12
  241. package/lib/engine/extensions/extension_utils.js +152 -152
  242. package/lib/engine/extensions/extensions.d.ts +32 -32
  243. package/lib/engine/extensions/extensions.js +107 -107
  244. package/lib/engine/extensions/index.d.ts +6 -6
  245. package/lib/engine/extensions/index.js +6 -6
  246. package/lib/engine/extensions/usage_tracker.d.ts +13 -13
  247. package/lib/engine/extensions/usage_tracker.js +65 -65
  248. package/lib/engine/js-extensions/Camera.d.ts +1 -1
  249. package/lib/engine/js-extensions/Camera.js +39 -39
  250. package/lib/engine/js-extensions/ExtensionUtils.d.ts +9 -9
  251. package/lib/engine/js-extensions/ExtensionUtils.js +67 -67
  252. package/lib/engine/js-extensions/Layers.d.ts +6 -6
  253. package/lib/engine/js-extensions/Layers.js +22 -22
  254. package/lib/engine/js-extensions/Object3D.d.ts +120 -120
  255. package/lib/engine/js-extensions/Object3D.js +136 -136
  256. package/lib/engine/js-extensions/RGBAColor.d.ts +23 -23
  257. package/lib/engine/js-extensions/RGBAColor.js +111 -111
  258. package/lib/engine/js-extensions/Vector.d.ts +3 -3
  259. package/lib/engine/js-extensions/Vector.js +13 -13
  260. package/lib/engine/js-extensions/index.d.ts +5 -5
  261. package/lib/engine/js-extensions/index.js +5 -5
  262. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.d.ts +4 -4
  263. package/lib/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +79 -79
  264. package/lib/engine/shaders/shaderData.d.ts +55 -55
  265. package/lib/engine/shaders/shaderData.js +58 -58
  266. package/lib/engine/tests/test_utils.d.ts +2 -2
  267. package/lib/engine/tests/test_utils.js +53 -53
  268. package/lib/engine/webcomponents/WebXRButtons.d.ts +56 -56
  269. package/lib/engine/webcomponents/WebXRButtons.js +230 -230
  270. package/lib/engine/webcomponents/api.d.ts +5 -5
  271. package/lib/engine/webcomponents/api.js +4 -4
  272. package/lib/engine/webcomponents/buttons.d.ts +51 -51
  273. package/lib/engine/webcomponents/buttons.js +264 -264
  274. package/lib/engine/webcomponents/fonts.d.ts +9 -9
  275. package/lib/engine/webcomponents/fonts.js +32 -32
  276. package/lib/engine/webcomponents/icons.d.ts +9 -9
  277. package/lib/engine/webcomponents/icons.js +52 -52
  278. package/lib/engine/webcomponents/index.d.ts +1 -1
  279. package/lib/engine/webcomponents/index.js +1 -1
  280. package/lib/engine/webcomponents/logo-element.d.ts +10 -10
  281. package/lib/engine/webcomponents/logo-element.js +67 -67
  282. package/lib/engine/webcomponents/needle menu/needle-menu-spatial.d.ts +37 -37
  283. package/lib/engine/webcomponents/needle menu/needle-menu-spatial.js +513 -513
  284. package/lib/engine/webcomponents/needle menu/needle-menu.d.ts +155 -155
  285. package/lib/engine/webcomponents/needle menu/needle-menu.js +1029 -1029
  286. package/lib/engine/webcomponents/needle-button.d.ts +34 -34
  287. package/lib/engine/webcomponents/needle-button.js +161 -161
  288. package/lib/engine/webcomponents/needle-engine.ar-overlay.d.ts +21 -21
  289. package/lib/engine/webcomponents/needle-engine.ar-overlay.js +166 -166
  290. package/lib/engine/webcomponents/needle-engine.attributes.d.ts +69 -69
  291. package/lib/engine/webcomponents/needle-engine.attributes.js +1 -1
  292. package/lib/engine/webcomponents/needle-engine.d.ts +116 -116
  293. package/lib/engine/webcomponents/needle-engine.extras.d.ts +6 -6
  294. package/lib/engine/webcomponents/needle-engine.extras.js +13 -13
  295. package/lib/engine/webcomponents/needle-engine.js +821 -821
  296. package/lib/engine/webcomponents/needle-engine.loading.d.ts +44 -44
  297. package/lib/engine/webcomponents/needle-engine.loading.js +341 -341
  298. package/lib/engine/xr/NeedleXRController.d.ts +313 -313
  299. package/lib/engine/xr/NeedleXRController.js +1007 -1007
  300. package/lib/engine/xr/NeedleXRSession.d.ts +340 -340
  301. package/lib/engine/xr/NeedleXRSession.js +1463 -1463
  302. package/lib/engine/xr/NeedleXRSync.d.ts +22 -22
  303. package/lib/engine/xr/NeedleXRSync.js +188 -188
  304. package/lib/engine/xr/SceneTransition.d.ts +18 -18
  305. package/lib/engine/xr/SceneTransition.js +69 -69
  306. package/lib/engine/xr/TempXRContext.d.ts +34 -34
  307. package/lib/engine/xr/TempXRContext.js +187 -187
  308. package/lib/engine/xr/XRRig.d.ts +7 -7
  309. package/lib/engine/xr/XRRig.js +1 -1
  310. package/lib/engine/xr/api.d.ts +6 -6
  311. package/lib/engine/xr/api.js +6 -6
  312. package/lib/engine/xr/events.d.ts +66 -66
  313. package/lib/engine/xr/events.js +93 -93
  314. package/lib/engine/xr/internal.d.ts +12 -12
  315. package/lib/engine/xr/internal.js +25 -25
  316. package/lib/engine/xr/usdz.d.ts +12 -12
  317. package/lib/engine/xr/usdz.js +29 -29
  318. package/lib/engine/xr/utils.d.ts +11 -11
  319. package/lib/engine/xr/utils.js +34 -34
  320. package/lib/engine-components/AlignmentConstraint.d.ts +10 -10
  321. package/lib/engine-components/AlignmentConstraint.js +39 -39
  322. package/lib/engine-components/Animation.d.ts +156 -156
  323. package/lib/engine-components/Animation.js +508 -508
  324. package/lib/engine-components/AnimationCurve.d.ts +40 -40
  325. package/lib/engine-components/AnimationCurve.js +159 -159
  326. package/lib/engine-components/AnimationUtils.d.ts +8 -8
  327. package/lib/engine-components/AnimationUtils.js +27 -27
  328. package/lib/engine-components/AnimationUtilsAutoplay.d.ts +1 -1
  329. package/lib/engine-components/AnimationUtilsAutoplay.js +39 -39
  330. package/lib/engine-components/Animator.d.ts +217 -217
  331. package/lib/engine-components/Animator.js +354 -354
  332. package/lib/engine-components/AnimatorController.d.ts +227 -227
  333. package/lib/engine-components/AnimatorController.js +1152 -1152
  334. package/lib/engine-components/AudioListener.d.ts +33 -33
  335. package/lib/engine-components/AudioListener.js +86 -86
  336. package/lib/engine-components/AudioSource.d.ts +217 -217
  337. package/lib/engine-components/AudioSource.js +635 -635
  338. package/lib/engine-components/AvatarLoader.d.ts +80 -80
  339. package/lib/engine-components/AvatarLoader.js +231 -231
  340. package/lib/engine-components/AxesHelper.d.ts +32 -32
  341. package/lib/engine-components/AxesHelper.js +67 -67
  342. package/lib/engine-components/BasicIKConstraint.d.ts +9 -9
  343. package/lib/engine-components/BasicIKConstraint.js +43 -43
  344. package/lib/engine-components/BoxCollider.d.ts +2 -2
  345. package/lib/engine-components/BoxCollider.js +2 -2
  346. package/lib/engine-components/BoxHelperComponent.d.ts +47 -47
  347. package/lib/engine-components/BoxHelperComponent.js +102 -102
  348. package/lib/engine-components/Camera.d.ts +231 -231
  349. package/lib/engine-components/Camera.js +700 -700
  350. package/lib/engine-components/CameraUtils.d.ts +1 -1
  351. package/lib/engine-components/CameraUtils.js +121 -121
  352. package/lib/engine-components/CharacterController.d.ts +55 -55
  353. package/lib/engine-components/CharacterController.js +236 -236
  354. package/lib/engine-components/Collider.d.ts +188 -188
  355. package/lib/engine-components/Collider.js +369 -369
  356. package/lib/engine-components/Component.d.ts +792 -792
  357. package/lib/engine-components/Component.js +920 -920
  358. package/lib/engine-components/ContactShadows.d.ts +94 -94
  359. package/lib/engine-components/ContactShadows.js +453 -453
  360. package/lib/engine-components/DeleteBox.d.ts +19 -19
  361. package/lib/engine-components/DeleteBox.js +58 -58
  362. package/lib/engine-components/DeviceFlag.d.ts +16 -16
  363. package/lib/engine-components/DeviceFlag.js +47 -47
  364. package/lib/engine-components/DragControls.d.ts +170 -170
  365. package/lib/engine-components/DragControls.js +1421 -1421
  366. package/lib/engine-components/DropListener.d.ts +215 -215
  367. package/lib/engine-components/DropListener.js +642 -642
  368. package/lib/engine-components/Duplicatable.d.ts +35 -35
  369. package/lib/engine-components/Duplicatable.js +202 -202
  370. package/lib/engine-components/EventList.d.ts +54 -54
  371. package/lib/engine-components/EventList.js +232 -232
  372. package/lib/engine-components/EventTrigger.d.ts +33 -33
  373. package/lib/engine-components/EventTrigger.js +75 -75
  374. package/lib/engine-components/EventType.d.ts +22 -22
  375. package/lib/engine-components/EventType.js +23 -23
  376. package/lib/engine-components/Fog.d.ts +22 -22
  377. package/lib/engine-components/Fog.js +61 -61
  378. package/lib/engine-components/Gizmos.d.ts +17 -17
  379. package/lib/engine-components/Gizmos.js +64 -64
  380. package/lib/engine-components/GridHelper.d.ts +20 -20
  381. package/lib/engine-components/GridHelper.js +54 -54
  382. package/lib/engine-components/GroundProjection.d.ts +67 -67
  383. package/lib/engine-components/GroundProjection.js +343 -343
  384. package/lib/engine-components/Interactable.d.ts +12 -12
  385. package/lib/engine-components/Interactable.js +12 -12
  386. package/lib/engine-components/Joints.d.ts +19 -19
  387. package/lib/engine-components/Joints.js +51 -51
  388. package/lib/engine-components/LODGroup.d.ts +35 -35
  389. package/lib/engine-components/LODGroup.js +152 -152
  390. package/lib/engine-components/Light.d.ts +180 -180
  391. package/lib/engine-components/Light.js +535 -535
  392. package/lib/engine-components/LookAtConstraint.d.ts +19 -19
  393. package/lib/engine-components/LookAtConstraint.js +35 -35
  394. package/lib/engine-components/NeedleMenu.d.ts +50 -50
  395. package/lib/engine-components/NeedleMenu.js +92 -92
  396. package/lib/engine-components/NestedGltf.d.ts +25 -25
  397. package/lib/engine-components/NestedGltf.js +88 -88
  398. package/lib/engine-components/Networking.d.ts +54 -54
  399. package/lib/engine-components/Networking.js +112 -112
  400. package/lib/engine-components/OffsetConstraint.d.ts +14 -14
  401. package/lib/engine-components/OffsetConstraint.js +65 -65
  402. package/lib/engine-components/OrbitControls.d.ts +268 -268
  403. package/lib/engine-components/OrbitControls.js +1015 -1015
  404. package/lib/engine-components/PlayerColor.d.ts +19 -19
  405. package/lib/engine-components/PlayerColor.js +94 -94
  406. package/lib/engine-components/ReflectionProbe.d.ts +28 -28
  407. package/lib/engine-components/ReflectionProbe.js +204 -204
  408. package/lib/engine-components/Renderer.d.ts +153 -153
  409. package/lib/engine-components/Renderer.js +834 -834
  410. package/lib/engine-components/RendererInstancing.d.ts +140 -140
  411. package/lib/engine-components/RendererInstancing.js +744 -744
  412. package/lib/engine-components/RendererLightmap.d.ts +24 -24
  413. package/lib/engine-components/RendererLightmap.js +182 -182
  414. package/lib/engine-components/RigidBody.d.ts +155 -155
  415. package/lib/engine-components/RigidBody.js +517 -517
  416. package/lib/engine-components/SceneSwitcher.d.ts +263 -263
  417. package/lib/engine-components/SceneSwitcher.js +971 -971
  418. package/lib/engine-components/ScreenCapture.d.ts +144 -144
  419. package/lib/engine-components/ScreenCapture.js +547 -547
  420. package/lib/engine-components/ShadowCatcher.d.ts +33 -33
  421. package/lib/engine-components/ShadowCatcher.js +166 -166
  422. package/lib/engine-components/Skybox.d.ts +88 -88
  423. package/lib/engine-components/Skybox.js +469 -469
  424. package/lib/engine-components/SmoothFollow.d.ts +34 -34
  425. package/lib/engine-components/SmoothFollow.js +82 -82
  426. package/lib/engine-components/SpatialTrigger.d.ts +102 -102
  427. package/lib/engine-components/SpatialTrigger.js +225 -225
  428. package/lib/engine-components/SpectatorCamera.d.ts +111 -111
  429. package/lib/engine-components/SpectatorCamera.js +715 -715
  430. package/lib/engine-components/SphereCollider.d.ts +2 -2
  431. package/lib/engine-components/SphereCollider.js +2 -2
  432. package/lib/engine-components/SpriteRenderer.d.ts +132 -132
  433. package/lib/engine-components/SpriteRenderer.js +472 -472
  434. package/lib/engine-components/SyncedCamera.d.ts +41 -41
  435. package/lib/engine-components/SyncedCamera.js +199 -199
  436. package/lib/engine-components/SyncedRoom.d.ts +106 -106
  437. package/lib/engine-components/SyncedRoom.js +371 -371
  438. package/lib/engine-components/SyncedTransform.d.ts +94 -94
  439. package/lib/engine-components/SyncedTransform.js +331 -331
  440. package/lib/engine-components/TestRunner.d.ts +16 -16
  441. package/lib/engine-components/TestRunner.js +102 -102
  442. package/lib/engine-components/TransformGizmo.d.ts +75 -75
  443. package/lib/engine-components/TransformGizmo.js +209 -209
  444. package/lib/engine-components/VideoPlayer.d.ts +184 -184
  445. package/lib/engine-components/VideoPlayer.js +978 -978
  446. package/lib/engine-components/Voip.d.ts +67 -67
  447. package/lib/engine-components/Voip.js +360 -360
  448. package/lib/engine-components/api.d.ts +51 -51
  449. package/lib/engine-components/api.js +50 -50
  450. package/lib/engine-components/avatar/AvatarBlink_Simple.d.ts +11 -11
  451. package/lib/engine-components/avatar/AvatarBlink_Simple.js +76 -76
  452. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.d.ts +14 -14
  453. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js +68 -68
  454. package/lib/engine-components/avatar/Avatar_Brain_LookAt.d.ts +29 -29
  455. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js +121 -121
  456. package/lib/engine-components/avatar/Avatar_MouthShapes.d.ts +15 -15
  457. package/lib/engine-components/avatar/Avatar_MouthShapes.js +79 -79
  458. package/lib/engine-components/avatar/Avatar_MustacheShake.d.ts +9 -9
  459. package/lib/engine-components/avatar/Avatar_MustacheShake.js +29 -29
  460. package/lib/engine-components/codegen/components.d.ts +216 -216
  461. package/lib/engine-components/codegen/components.js +218 -218
  462. package/lib/engine-components/debug/LogStats.d.ts +5 -5
  463. package/lib/engine-components/debug/LogStats.js +18 -18
  464. package/lib/engine-components/export/gltf/GltfExport.d.ts +30 -30
  465. package/lib/engine-components/export/gltf/GltfExport.js +246 -246
  466. package/lib/engine-components/export/gltf/index.d.ts +1 -1
  467. package/lib/engine-components/export/gltf/index.js +1 -1
  468. package/lib/engine-components/export/index.d.ts +1 -1
  469. package/lib/engine-components/export/index.js +1 -1
  470. package/lib/engine-components/export/usdz/Extension.d.ts +22 -22
  471. package/lib/engine-components/export/usdz/Extension.js +1 -1
  472. package/lib/engine-components/export/usdz/ThreeUSDZExporter.d.ts +162 -162
  473. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +1789 -1789
  474. package/lib/engine-components/export/usdz/USDZExporter.d.ts +130 -130
  475. package/lib/engine-components/export/usdz/USDZExporter.js +663 -663
  476. package/lib/engine-components/export/usdz/extensions/Animation.d.ts +106 -106
  477. package/lib/engine-components/export/usdz/extensions/Animation.js +1071 -1071
  478. package/lib/engine-components/export/usdz/extensions/DocumentExtension.d.ts +5 -5
  479. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js +6 -6
  480. package/lib/engine-components/export/usdz/extensions/NodeMaterialConverter.d.ts +10 -10
  481. package/lib/engine-components/export/usdz/extensions/NodeMaterialConverter.js +451 -451
  482. package/lib/engine-components/export/usdz/extensions/USDZText.d.ts +54 -54
  483. package/lib/engine-components/export/usdz/extensions/USDZText.js +203 -203
  484. package/lib/engine-components/export/usdz/extensions/USDZUI.d.ts +8 -8
  485. package/lib/engine-components/export/usdz/extensions/USDZUI.js +158 -158
  486. package/lib/engine-components/export/usdz/extensions/behavior/Actions.d.ts +30 -30
  487. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js +88 -88
  488. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.d.ts +10 -10
  489. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.js +86 -86
  490. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.d.ts +28 -28
  491. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js +290 -290
  492. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.d.ts +190 -190
  493. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js +1060 -1060
  494. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.d.ts +135 -135
  495. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js +548 -548
  496. package/lib/engine-components/export/usdz/extensions/behavior/PhysicsExtension.d.ts +7 -7
  497. package/lib/engine-components/export/usdz/extensions/behavior/PhysicsExtension.js +115 -115
  498. package/lib/engine-components/export/usdz/index.d.ts +3 -3
  499. package/lib/engine-components/export/usdz/index.js +2 -2
  500. package/lib/engine-components/export/usdz/utils/animationutils.d.ts +7 -7
  501. package/lib/engine-components/export/usdz/utils/animationutils.js +163 -163
  502. package/lib/engine-components/export/usdz/utils/quicklook.d.ts +2 -2
  503. package/lib/engine-components/export/usdz/utils/quicklook.js +43 -43
  504. package/lib/engine-components/particlesystem/ParticleSystem.d.ts +177 -177
  505. package/lib/engine-components/particlesystem/ParticleSystem.js +1176 -1176
  506. package/lib/engine-components/particlesystem/ParticleSystemModules.d.ts +526 -526
  507. package/lib/engine-components/particlesystem/ParticleSystemModules.js +1930 -1930
  508. package/lib/engine-components/particlesystem/ParticleSystemSubEmitter.d.ts +25 -25
  509. package/lib/engine-components/particlesystem/ParticleSystemSubEmitter.js +87 -87
  510. package/lib/engine-components/particlesystem/api.d.ts +2 -2
  511. package/lib/engine-components/particlesystem/api.js +2 -2
  512. package/lib/engine-components/postprocessing/Effects/Antialiasing.d.ts +17 -17
  513. package/lib/engine-components/postprocessing/Effects/Antialiasing.js +59 -52
  514. package/lib/engine-components/postprocessing/Effects/Antialiasing.js.map +1 -1
  515. package/lib/engine-components/postprocessing/Effects/BloomEffect.d.ts +46 -46
  516. package/lib/engine-components/postprocessing/Effects/BloomEffect.js +113 -113
  517. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.d.ts +11 -11
  518. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.js +39 -39
  519. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.d.ts +23 -23
  520. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.js +111 -111
  521. package/lib/engine-components/postprocessing/Effects/DepthOfField.d.ts +25 -25
  522. package/lib/engine-components/postprocessing/Effects/DepthOfField.js +104 -104
  523. package/lib/engine-components/postprocessing/Effects/EffectWrapper.d.ts +12 -12
  524. package/lib/engine-components/postprocessing/Effects/EffectWrapper.js +18 -18
  525. package/lib/engine-components/postprocessing/Effects/Pixelation.d.ts +11 -11
  526. package/lib/engine-components/postprocessing/Effects/Pixelation.js +32 -32
  527. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.d.ts +18 -18
  528. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.js +91 -91
  529. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.d.ts +70 -70
  530. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.js +176 -176
  531. package/lib/engine-components/postprocessing/Effects/Sharpening.d.ts +18 -18
  532. package/lib/engine-components/postprocessing/Effects/Sharpening.js +127 -127
  533. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.d.ts +17 -17
  534. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.js +70 -70
  535. package/lib/engine-components/postprocessing/Effects/Tonemapping.d.ts +19 -19
  536. package/lib/engine-components/postprocessing/Effects/Tonemapping.js +94 -94
  537. package/lib/engine-components/postprocessing/Effects/Tonemapping.utils.d.ts +13 -13
  538. package/lib/engine-components/postprocessing/Effects/Tonemapping.utils.js +51 -51
  539. package/lib/engine-components/postprocessing/Effects/Vignette.d.ts +15 -15
  540. package/lib/engine-components/postprocessing/Effects/Vignette.js +60 -60
  541. package/lib/engine-components/postprocessing/PostProcessingEffect.d.ts +90 -90
  542. package/lib/engine-components/postprocessing/PostProcessingEffect.js +168 -168
  543. package/lib/engine-components/postprocessing/PostProcessingHandler.d.ts +42 -42
  544. package/lib/engine-components/postprocessing/PostProcessingHandler.js +494 -480
  545. package/lib/engine-components/postprocessing/PostProcessingHandler.js.map +1 -1
  546. package/lib/engine-components/postprocessing/Volume.d.ts +90 -90
  547. package/lib/engine-components/postprocessing/Volume.js +385 -385
  548. package/lib/engine-components/postprocessing/VolumeParameter.d.ts +26 -26
  549. package/lib/engine-components/postprocessing/VolumeParameter.js +136 -136
  550. package/lib/engine-components/postprocessing/VolumeProfile.d.ts +15 -15
  551. package/lib/engine-components/postprocessing/VolumeProfile.js +60 -60
  552. package/lib/engine-components/postprocessing/index.d.ts +6 -6
  553. package/lib/engine-components/postprocessing/index.js +6 -6
  554. package/lib/engine-components/postprocessing/utils.d.ts +55 -55
  555. package/lib/engine-components/postprocessing/utils.js +119 -119
  556. package/lib/engine-components/timeline/PlayableDirector.d.ts +163 -163
  557. package/lib/engine-components/timeline/PlayableDirector.js +686 -686
  558. package/lib/engine-components/timeline/SignalAsset.d.ts +24 -24
  559. package/lib/engine-components/timeline/SignalAsset.js +130 -130
  560. package/lib/engine-components/timeline/TimelineModels.d.ts +89 -89
  561. package/lib/engine-components/timeline/TimelineModels.js +22 -22
  562. package/lib/engine-components/timeline/TimelineTracks.d.ts +110 -110
  563. package/lib/engine-components/timeline/TimelineTracks.js +879 -879
  564. package/lib/engine-components/timeline/index.d.ts +4 -4
  565. package/lib/engine-components/timeline/index.js +3 -3
  566. package/lib/engine-components/ui/BaseUIComponent.d.ts +48 -48
  567. package/lib/engine-components/ui/BaseUIComponent.js +170 -170
  568. package/lib/engine-components/ui/Button.d.ts +64 -64
  569. package/lib/engine-components/ui/Button.js +315 -315
  570. package/lib/engine-components/ui/Canvas.d.ts +74 -74
  571. package/lib/engine-components/ui/Canvas.js +407 -407
  572. package/lib/engine-components/ui/CanvasGroup.d.ts +19 -19
  573. package/lib/engine-components/ui/CanvasGroup.js +58 -58
  574. package/lib/engine-components/ui/EventSystem.d.ts +125 -125
  575. package/lib/engine-components/ui/EventSystem.js +765 -765
  576. package/lib/engine-components/ui/Graphic.d.ts +55 -55
  577. package/lib/engine-components/ui/Graphic.js +255 -255
  578. package/lib/engine-components/ui/Image.d.ts +35 -35
  579. package/lib/engine-components/ui/Image.js +116 -116
  580. package/lib/engine-components/ui/InputField.d.ts +42 -42
  581. package/lib/engine-components/ui/InputField.js +268 -268
  582. package/lib/engine-components/ui/Interfaces.d.ts +38 -38
  583. package/lib/engine-components/ui/Interfaces.js +12 -12
  584. package/lib/engine-components/ui/Layout.d.ts +84 -84
  585. package/lib/engine-components/ui/Layout.js +330 -330
  586. package/lib/engine-components/ui/Outline.d.ts +7 -7
  587. package/lib/engine-components/ui/Outline.js +20 -20
  588. package/lib/engine-components/ui/PointerEvents.d.ts +115 -115
  589. package/lib/engine-components/ui/PointerEvents.js +145 -145
  590. package/lib/engine-components/ui/RaycastUtils.d.ts +11 -11
  591. package/lib/engine-components/ui/RaycastUtils.js +67 -67
  592. package/lib/engine-components/ui/Raycaster.d.ts +30 -30
  593. package/lib/engine-components/ui/Raycaster.js +95 -95
  594. package/lib/engine-components/ui/RectTransform.d.ts +61 -61
  595. package/lib/engine-components/ui/RectTransform.js +356 -356
  596. package/lib/engine-components/ui/SpatialHtml.d.ts +8 -8
  597. package/lib/engine-components/ui/SpatialHtml.js +79 -79
  598. package/lib/engine-components/ui/Symbols.d.ts +1 -1
  599. package/lib/engine-components/ui/Symbols.js +1 -1
  600. package/lib/engine-components/ui/Text.d.ts +78 -78
  601. package/lib/engine-components/ui/Text.js +539 -539
  602. package/lib/engine-components/ui/Utils.d.ts +24 -24
  603. package/lib/engine-components/ui/Utils.js +90 -90
  604. package/lib/engine-components/ui/index.d.ts +1 -1
  605. package/lib/engine-components/ui/index.js +1 -1
  606. package/lib/engine-components/utils/EnvironmentScene.d.ts +5 -5
  607. package/lib/engine-components/utils/EnvironmentScene.js +205 -205
  608. package/lib/engine-components/utils/LookAt.d.ts +31 -31
  609. package/lib/engine-components/utils/LookAt.js +82 -82
  610. package/lib/engine-components/utils/OpenURL.d.ts +42 -42
  611. package/lib/engine-components/utils/OpenURL.js +119 -119
  612. package/lib/engine-components/webxr/Avatar.d.ts +25 -25
  613. package/lib/engine-components/webxr/Avatar.js +255 -255
  614. package/lib/engine-components/webxr/TeleportTarget.d.ts +7 -7
  615. package/lib/engine-components/webxr/TeleportTarget.js +7 -7
  616. package/lib/engine-components/webxr/WebARCameraBackground.d.ts +30 -30
  617. package/lib/engine-components/webxr/WebARCameraBackground.js +155 -155
  618. package/lib/engine-components/webxr/WebARSessionRoot.d.ts +98 -98
  619. package/lib/engine-components/webxr/WebARSessionRoot.js +770 -770
  620. package/lib/engine-components/webxr/WebXR.d.ts +232 -232
  621. package/lib/engine-components/webxr/WebXR.js +561 -561
  622. package/lib/engine-components/webxr/WebXRAvatar.d.ts +27 -27
  623. package/lib/engine-components/webxr/WebXRAvatar.js +44 -44
  624. package/lib/engine-components/webxr/WebXRImageTracking.d.ts +86 -86
  625. package/lib/engine-components/webxr/WebXRImageTracking.js +471 -471
  626. package/lib/engine-components/webxr/WebXRPlaneTracking.d.ts +92 -92
  627. package/lib/engine-components/webxr/WebXRPlaneTracking.js +500 -500
  628. package/lib/engine-components/webxr/WebXRRig.d.ts +32 -32
  629. package/lib/engine-components/webxr/WebXRRig.js +72 -72
  630. package/lib/engine-components/webxr/XRFlag.d.ts +38 -38
  631. package/lib/engine-components/webxr/XRFlag.js +139 -139
  632. package/lib/engine-components/webxr/controllers/XRControllerFollow.d.ts +47 -47
  633. package/lib/engine-components/webxr/controllers/XRControllerFollow.js +120 -120
  634. package/lib/engine-components/webxr/controllers/XRControllerModel.d.ts +43 -43
  635. package/lib/engine-components/webxr/controllers/XRControllerModel.js +352 -352
  636. package/lib/engine-components/webxr/controllers/XRControllerMovement.d.ts +78 -78
  637. package/lib/engine-components/webxr/controllers/XRControllerMovement.js +506 -506
  638. package/lib/engine-components/webxr/index.d.ts +3 -3
  639. package/lib/engine-components/webxr/index.js +3 -3
  640. package/lib/engine-components/webxr/types.d.ts +3 -3
  641. package/lib/engine-components/webxr/types.js +1 -1
  642. package/lib/engine-components-experimental/Presentation.d.ts +6 -6
  643. package/lib/engine-components-experimental/Presentation.js +9 -9
  644. package/lib/engine-components-experimental/api.d.ts +4 -4
  645. package/lib/engine-components-experimental/api.js +4 -4
  646. package/lib/engine-components-experimental/networking/PlayerSync.d.ts +156 -156
  647. package/lib/engine-components-experimental/networking/PlayerSync.js +377 -377
  648. package/lib/engine-schemes/api.d.ts +12 -12
  649. package/lib/engine-schemes/api.js +12 -12
  650. package/lib/engine-schemes/schemes.d.ts +7 -7
  651. package/lib/engine-schemes/schemes.js +19 -19
  652. package/lib/engine-schemes/synced-camera-model.d.ts +25 -25
  653. package/lib/engine-schemes/synced-camera-model.js +67 -67
  654. package/lib/engine-schemes/synced-transform-model.d.ts +31 -31
  655. package/lib/engine-schemes/synced-transform-model.js +66 -66
  656. package/lib/engine-schemes/transform.d.ts +12 -12
  657. package/lib/engine-schemes/transform.js +39 -39
  658. package/lib/engine-schemes/vec2.d.ts +10 -10
  659. package/lib/engine-schemes/vec2.js +25 -25
  660. package/lib/engine-schemes/vec3.d.ts +11 -11
  661. package/lib/engine-schemes/vec3.js +29 -29
  662. package/lib/engine-schemes/vec4.d.ts +12 -12
  663. package/lib/engine-schemes/vec4.js +33 -33
  664. package/lib/engine-schemes/vr-user-state-buffer.d.ts +37 -37
  665. package/lib/engine-schemes/vr-user-state-buffer.js +110 -110
  666. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.d.ts +6 -6
  667. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.js +45 -45
  668. package/lib/needle-engine.d.ts +7 -7
  669. package/lib/needle-engine.js +64 -64
  670. package/package.json +2 -2
  671. package/plugins/common/buildinfo.js +64 -64
  672. package/plugins/common/cloud.js +1 -1
  673. package/plugins/common/config.cjs +31 -31
  674. package/plugins/common/config.js +35 -35
  675. package/plugins/common/files.js +31 -31
  676. package/plugins/common/generator.js +10 -10
  677. package/plugins/common/license.js +452 -452
  678. package/plugins/common/npm.js +15 -15
  679. package/plugins/common/timers.js +7 -7
  680. package/plugins/common/version.js +37 -37
  681. package/plugins/gltf-packer.mjs +1 -1
  682. package/plugins/next/alias.cjs +39 -39
  683. package/plugins/next/license.cjs +24 -24
  684. package/plugins/next/meshbvhworker.cjs +18 -18
  685. package/plugins/next/next.js +141 -141
  686. package/plugins/types/index.d.ts +2 -2
  687. package/plugins/types/license.d.ts +24 -24
  688. package/plugins/types/needleConfig.d.ts +27 -27
  689. package/plugins/types/next.d.ts +2 -2
  690. package/plugins/types/userconfig.d.ts +122 -120
  691. package/plugins/types/vite.d.ts +13 -13
  692. package/plugins/types/webmanifest.d.ts +32 -32
  693. package/plugins/vite/alias.js +184 -174
  694. package/plugins/vite/asap.js +251 -251
  695. package/plugins/vite/build-pipeline.js +371 -371
  696. package/plugins/vite/build.js +19 -19
  697. package/plugins/vite/buildinfo.js +41 -41
  698. package/plugins/vite/config.js +106 -106
  699. package/plugins/vite/copyfiles.js +138 -138
  700. package/plugins/vite/defines.js +70 -70
  701. package/plugins/vite/dependencies.js +232 -232
  702. package/plugins/vite/dependency-watcher.js +237 -237
  703. package/plugins/vite/drop-client.js +76 -76
  704. package/plugins/vite/drop.js +87 -87
  705. package/plugins/vite/editor-connection.js +124 -124
  706. package/plugins/vite/facebook-instant-games.js +102 -102
  707. package/plugins/vite/gzip.js +5 -5
  708. package/plugins/vite/imports-logger.js +143 -143
  709. package/plugins/vite/index.js +145 -143
  710. package/plugins/vite/license.js +56 -56
  711. package/plugins/vite/local-files.js +440 -440
  712. package/plugins/vite/materialx.js +30 -0
  713. package/plugins/vite/meta.js +163 -163
  714. package/plugins/vite/npm.js +7 -7
  715. package/plugins/vite/peer.js +29 -29
  716. package/plugins/vite/poster-client.js +73 -73
  717. package/plugins/vite/poster.js +79 -79
  718. package/plugins/vite/pwa.js +604 -604
  719. package/plugins/vite/reload-client.js +15 -15
  720. package/plugins/vite/reload.js +351 -351
  721. package/plugins/vite/server.js +66 -66
  722. package/plugins/vite/transform-codegen.js +55 -55
  723. package/plugins/vite/transform.js +32 -31
  724. package/plugins/vite/vite-4.4-hack.js +31 -31
  725. package/src/asap/needle-asap.ts +111 -111
  726. package/src/asap/sessiongranted.ts +75 -75
  727. package/src/asap/utils.ts +4 -4
  728. package/src/engine/analytics/index.ts +10 -10
  729. package/src/engine/analytics/lcp.ts +35 -35
  730. package/src/engine/api.ts +82 -82
  731. package/src/engine/assets/index.ts +59 -59
  732. package/src/engine/assets/static.js +5 -5
  733. package/src/engine/codegen/register_types.ts +300 -300
  734. package/src/engine/debug/debug.ts +51 -51
  735. package/src/engine/debug/debug_console.ts +333 -333
  736. package/src/engine/debug/debug_overlay.ts +332 -332
  737. package/src/engine/debug/debug_spatial_console.ts +429 -429
  738. package/src/engine/debug/index.ts +1 -1
  739. package/src/engine/engine_addressables.ts +680 -680
  740. package/src/engine/engine_animation.ts +145 -145
  741. package/src/engine/engine_application.ts +113 -113
  742. package/src/engine/engine_assetdatabase.ts +389 -389
  743. package/src/engine/engine_audio.ts +24 -24
  744. package/src/engine/engine_camera.ts +39 -39
  745. package/src/engine/engine_components.ts +366 -366
  746. package/src/engine/engine_components_internal.ts +40 -40
  747. package/src/engine/engine_constants.ts +52 -52
  748. package/src/engine/engine_context.ts +1824 -1823
  749. package/src/engine/engine_context_registry.ts +129 -129
  750. package/src/engine/engine_coroutine.ts +54 -54
  751. package/src/engine/engine_create_objects.ts +411 -411
  752. package/src/engine/engine_default_parameters.ts +3 -3
  753. package/src/engine/engine_editor-sync.ts +28 -28
  754. package/src/engine/engine_fileloader.js +9 -9
  755. package/src/engine/engine_gameobject.ts +712 -712
  756. package/src/engine/engine_generic_utils.js +13 -13
  757. package/src/engine/engine_gizmos.ts +577 -577
  758. package/src/engine/engine_gltf.ts +29 -29
  759. package/src/engine/engine_gltf_builtin_components.ts +403 -403
  760. package/src/engine/engine_hot_reload.ts +210 -210
  761. package/src/engine/engine_input.ts +1500 -1500
  762. package/src/engine/engine_input_utils.ts +23 -23
  763. package/src/engine/engine_instancing.ts +45 -45
  764. package/src/engine/engine_license.ts +386 -386
  765. package/src/engine/engine_lifecycle_api.ts +113 -113
  766. package/src/engine/engine_lifecycle_functions_internal.ts +193 -193
  767. package/src/engine/engine_lightdata.ts +117 -117
  768. package/src/engine/engine_loaders.callbacks.ts +136 -136
  769. package/src/engine/engine_loaders.gltf.ts +82 -82
  770. package/src/engine/engine_loaders.ts +378 -378
  771. package/src/engine/engine_lods.ts +168 -168
  772. package/src/engine/engine_mainloop_utils.ts +472 -472
  773. package/src/engine/engine_math.ts +282 -282
  774. package/src/engine/engine_modules.ts +83 -83
  775. package/src/engine/engine_networking.ts +862 -862
  776. package/src/engine/engine_networking_auto.ts +352 -352
  777. package/src/engine/engine_networking_blob.ts +275 -275
  778. package/src/engine/engine_networking_files.ts +217 -217
  779. package/src/engine/engine_networking_files_default_components.ts +58 -58
  780. package/src/engine/engine_networking_instantiate.ts +419 -419
  781. package/src/engine/engine_networking_peer.ts +159 -159
  782. package/src/engine/engine_networking_streams.ts +713 -713
  783. package/src/engine/engine_networking_types.ts +24 -24
  784. package/src/engine/engine_networking_utils.ts +23 -23
  785. package/src/engine/engine_networking_websocket.ts +2 -2
  786. package/src/engine/engine_patcher.ts +199 -199
  787. package/src/engine/engine_physics.ts +783 -783
  788. package/src/engine/engine_physics.types.ts +46 -46
  789. package/src/engine/engine_physics_rapier.ts +1577 -1577
  790. package/src/engine/engine_playerview.ts +80 -80
  791. package/src/engine/engine_scenelighting.ts +294 -294
  792. package/src/engine/engine_serialization.ts +2 -2
  793. package/src/engine/engine_serialization_builtin_serializer.ts +473 -473
  794. package/src/engine/engine_serialization_core.ts +720 -720
  795. package/src/engine/engine_serialization_decorator.ts +80 -80
  796. package/src/engine/engine_setup.ts +1 -1
  797. package/src/engine/engine_shaders.ts +267 -267
  798. package/src/engine/engine_shims.ts +32 -32
  799. package/src/engine/engine_test_utils.ts +109 -109
  800. package/src/engine/engine_texture.ts +82 -82
  801. package/src/engine/engine_three_utils.ts +925 -925
  802. package/src/engine/engine_time.ts +94 -94
  803. package/src/engine/engine_time_utils.ts +237 -237
  804. package/src/engine/engine_tonemapping.ts +208 -208
  805. package/src/engine/engine_types.ts +730 -730
  806. package/src/engine/engine_typestore.ts +63 -63
  807. package/src/engine/engine_util_decorator.ts +136 -136
  808. package/src/engine/engine_utils.ts +1115 -1115
  809. package/src/engine/engine_utils_format.ts +273 -273
  810. package/src/engine/engine_utils_screenshot.ts +708 -708
  811. package/src/engine/engine_utils_screenshot.xr.ts +103 -103
  812. package/src/engine/export/gltf/Writers.ts +34 -34
  813. package/src/engine/export/gltf/index.ts +158 -158
  814. package/src/engine/export/index.ts +2 -2
  815. package/src/engine/export/state.ts +19 -19
  816. package/src/engine/export/utils.ts +9 -9
  817. package/src/engine/extensions/EXT_texture_exr.ts +50 -50
  818. package/src/engine/extensions/NEEDLE_animator_controller_model.ts +195 -195
  819. package/src/engine/extensions/NEEDLE_components.ts +268 -268
  820. package/src/engine/extensions/NEEDLE_gameobject_data.ts +81 -81
  821. package/src/engine/extensions/NEEDLE_lighting_settings.ts +185 -185
  822. package/src/engine/extensions/NEEDLE_lightmaps.ts +119 -119
  823. package/src/engine/extensions/NEEDLE_persistent_assets.ts +76 -76
  824. package/src/engine/extensions/NEEDLE_render_objects.ts +209 -209
  825. package/src/engine/extensions/NEEDLE_techniques_webgl.ts +640 -640
  826. package/src/engine/extensions/extension_resolver.ts +4 -4
  827. package/src/engine/extensions/extension_utils.ts +166 -166
  828. package/src/engine/extensions/extensions.ts +140 -140
  829. package/src/engine/extensions/index.ts +5 -5
  830. package/src/engine/extensions/usage_tracker.ts +100 -100
  831. package/src/engine/js-extensions/Camera.ts +37 -37
  832. package/src/engine/js-extensions/ExtensionUtils.ts +85 -85
  833. package/src/engine/js-extensions/Layers.ts +23 -23
  834. package/src/engine/js-extensions/Object3D.ts +296 -296
  835. package/src/engine/js-extensions/RGBAColor.ts +126 -126
  836. package/src/engine/js-extensions/Vector.ts +18 -18
  837. package/src/engine/js-extensions/index.ts +4 -4
  838. package/src/engine/physics/workers/mesh-bvh/GenerateMeshBVHWorker.js +125 -125
  839. package/src/engine/shaders/shaderData.ts +67 -67
  840. package/src/engine/tests/test_utils.ts +63 -63
  841. package/src/engine/webcomponents/WebXRButtons.ts +260 -260
  842. package/src/engine/webcomponents/api.ts +6 -6
  843. package/src/engine/webcomponents/buttons.ts +292 -292
  844. package/src/engine/webcomponents/fonts.ts +41 -41
  845. package/src/engine/webcomponents/icons.ts +57 -57
  846. package/src/engine/webcomponents/index.ts +1 -1
  847. package/src/engine/webcomponents/logo-element.ts +78 -78
  848. package/src/engine/webcomponents/needle menu/needle-menu-spatial.ts +573 -573
  849. package/src/engine/webcomponents/needle menu/needle-menu.ts +1118 -1118
  850. package/src/engine/webcomponents/needle-button.ts +181 -181
  851. package/src/engine/webcomponents/needle-engine.ar-overlay.ts +186 -186
  852. package/src/engine/webcomponents/needle-engine.attributes.ts +82 -82
  853. package/src/engine/webcomponents/needle-engine.extras.ts +16 -16
  854. package/src/engine/webcomponents/needle-engine.loading.ts +373 -373
  855. package/src/engine/webcomponents/needle-engine.ts +860 -860
  856. package/src/engine/xr/NeedleXRController.ts +1125 -1125
  857. package/src/engine/xr/NeedleXRSession.ts +1624 -1624
  858. package/src/engine/xr/NeedleXRSync.ts +220 -220
  859. package/src/engine/xr/SceneTransition.ts +78 -78
  860. package/src/engine/xr/TempXRContext.ts +216 -216
  861. package/src/engine/xr/XRRig.ts +9 -9
  862. package/src/engine/xr/api.ts +5 -5
  863. package/src/engine/xr/events.ts +102 -102
  864. package/src/engine/xr/internal.ts +34 -34
  865. package/src/engine/xr/usdz.ts +30 -30
  866. package/src/engine/xr/utils.ts +39 -39
  867. package/src/engine-components/AlignmentConstraint.ts +36 -36
  868. package/src/engine-components/Animation.ts +557 -557
  869. package/src/engine-components/AnimationCurve.ts +150 -150
  870. package/src/engine-components/AnimationUtils.ts +28 -28
  871. package/src/engine-components/AnimationUtilsAutoplay.ts +43 -43
  872. package/src/engine-components/Animator.ts +397 -397
  873. package/src/engine-components/AnimatorController.ts +1293 -1293
  874. package/src/engine-components/AudioListener.ts +92 -92
  875. package/src/engine-components/AudioSource.ts +644 -644
  876. package/src/engine-components/AvatarLoader.ts +263 -263
  877. package/src/engine-components/AxesHelper.ts +59 -59
  878. package/src/engine-components/BasicIKConstraint.ts +54 -54
  879. package/src/engine-components/BoxCollider.ts +1 -1
  880. package/src/engine-components/BoxHelperComponent.ts +114 -114
  881. package/src/engine-components/Camera.ts +719 -719
  882. package/src/engine-components/CameraUtils.ts +136 -136
  883. package/src/engine-components/CharacterController.ts +253 -253
  884. package/src/engine-components/Collider.ts +374 -374
  885. package/src/engine-components/Component.ts +1297 -1297
  886. package/src/engine-components/ContactShadows.ts +506 -506
  887. package/src/engine-components/DeleteBox.ts +62 -62
  888. package/src/engine-components/DeviceFlag.ts +46 -46
  889. package/src/engine-components/DragControls.ts +1623 -1623
  890. package/src/engine-components/DropListener.ts +713 -713
  891. package/src/engine-components/Duplicatable.ts +198 -198
  892. package/src/engine-components/EventList.ts +266 -266
  893. package/src/engine-components/EventTrigger.ts +74 -74
  894. package/src/engine-components/EventType.ts +22 -22
  895. package/src/engine-components/Fog.ts +60 -60
  896. package/src/engine-components/Gizmos.ts +56 -56
  897. package/src/engine-components/GridHelper.ts +48 -48
  898. package/src/engine-components/GroundProjection.ts +356 -356
  899. package/src/engine-components/Interactable.ts +14 -14
  900. package/src/engine-components/Joints.ts +52 -52
  901. package/src/engine-components/LODGroup.ts +153 -153
  902. package/src/engine-components/Light.ts +558 -558
  903. package/src/engine-components/LookAtConstraint.ts +25 -25
  904. package/src/engine-components/NeedleMenu.ts +84 -84
  905. package/src/engine-components/NestedGltf.ts +86 -86
  906. package/src/engine-components/Networking.ts +114 -114
  907. package/src/engine-components/OffsetConstraint.ts +60 -60
  908. package/src/engine-components/OrbitControls.ts +1074 -1074
  909. package/src/engine-components/PlayerColor.ts +103 -103
  910. package/src/engine-components/ReflectionProbe.ts +220 -220
  911. package/src/engine-components/Renderer.ts +903 -903
  912. package/src/engine-components/RendererInstancing.ts +855 -855
  913. package/src/engine-components/RendererLightmap.ts +198 -198
  914. package/src/engine-components/RigidBody.ts +526 -526
  915. package/src/engine-components/SceneSwitcher.ts +1030 -1030
  916. package/src/engine-components/ScreenCapture.ts +592 -592
  917. package/src/engine-components/ShadowCatcher.ts +172 -172
  918. package/src/engine-components/Skybox.ts +475 -475
  919. package/src/engine-components/SmoothFollow.ts +76 -76
  920. package/src/engine-components/SpatialTrigger.ts +229 -229
  921. package/src/engine-components/SpectatorCamera.ts +787 -787
  922. package/src/engine-components/SphereCollider.ts +1 -1
  923. package/src/engine-components/SpriteRenderer.ts +468 -468
  924. package/src/engine-components/SyncedCamera.ts +220 -220
  925. package/src/engine-components/SyncedRoom.ts +380 -380
  926. package/src/engine-components/SyncedTransform.ts +383 -383
  927. package/src/engine-components/TestRunner.ts +118 -118
  928. package/src/engine-components/TransformGizmo.ts +219 -219
  929. package/src/engine-components/VideoPlayer.ts +1025 -1025
  930. package/src/engine-components/Voip.ts +363 -363
  931. package/src/engine-components/api.ts +60 -60
  932. package/src/engine-components/avatar/AvatarBlink_Simple.ts +69 -69
  933. package/src/engine-components/avatar/AvatarEyeLook_Rotation.ts +63 -63
  934. package/src/engine-components/avatar/Avatar_Brain_LookAt.ts +139 -139
  935. package/src/engine-components/avatar/Avatar_MouthShapes.ts +83 -83
  936. package/src/engine-components/avatar/Avatar_MustacheShake.ts +31 -31
  937. package/src/engine-components/codegen/components.ts +217 -217
  938. package/src/engine-components/debug/LogStats.ts +21 -21
  939. package/src/engine-components/export/gltf/GltfExport.ts +265 -265
  940. package/src/engine-components/export/usdz/Extension.ts +24 -24
  941. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +2426 -2426
  942. package/src/engine-components/export/usdz/USDZExporter.ts +705 -705
  943. package/src/engine-components/export/usdz/extensions/Animation.ts +1204 -1204
  944. package/src/engine-components/export/usdz/extensions/DocumentExtension.ts +9 -9
  945. package/src/engine-components/export/usdz/extensions/NodeMaterialConverter.ts +532 -532
  946. package/src/engine-components/export/usdz/extensions/USDZText.ts +240 -240
  947. package/src/engine-components/export/usdz/extensions/USDZUI.ts +189 -189
  948. package/src/engine-components/export/usdz/extensions/behavior/Actions.ts +99 -99
  949. package/src/engine-components/export/usdz/extensions/behavior/AudioExtension.ts +102 -102
  950. package/src/engine-components/export/usdz/extensions/behavior/Behaviour.ts +320 -320
  951. package/src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +1225 -1225
  952. package/src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts +646 -646
  953. package/src/engine-components/export/usdz/extensions/behavior/PhysicsExtension.ts +132 -132
  954. package/src/engine-components/export/usdz/index.ts +2 -2
  955. package/src/engine-components/export/usdz/utils/animationutils.ts +191 -191
  956. package/src/engine-components/export/usdz/utils/quicklook.ts +50 -50
  957. package/src/engine-components/particlesystem/ParticleSystem.ts +1287 -1287
  958. package/src/engine-components/particlesystem/ParticleSystemModules.ts +1765 -1765
  959. package/src/engine-components/particlesystem/ParticleSystemSubEmitter.ts +111 -111
  960. package/src/engine-components/particlesystem/api.ts +1 -1
  961. package/src/engine-components/postprocessing/Effects/Antialiasing.ts +64 -57
  962. package/src/engine-components/postprocessing/Effects/BloomEffect.ts +116 -116
  963. package/src/engine-components/postprocessing/Effects/ChromaticAberration.ts +37 -37
  964. package/src/engine-components/postprocessing/Effects/ColorAdjustments.ts +106 -106
  965. package/src/engine-components/postprocessing/Effects/DepthOfField.ts +103 -103
  966. package/src/engine-components/postprocessing/Effects/EffectWrapper.ts +25 -25
  967. package/src/engine-components/postprocessing/Effects/Pixelation.ts +32 -32
  968. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.ts +90 -90
  969. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.ts +192 -192
  970. package/src/engine-components/postprocessing/Effects/Sharpening.ts +143 -143
  971. package/src/engine-components/postprocessing/Effects/TiltShiftEffect.ts +61 -61
  972. package/src/engine-components/postprocessing/Effects/Tonemapping.ts +103 -103
  973. package/src/engine-components/postprocessing/Effects/Tonemapping.utils.ts +60 -60
  974. package/src/engine-components/postprocessing/Effects/Vignette.ts +59 -59
  975. package/src/engine-components/postprocessing/PostProcessingEffect.ts +192 -192
  976. package/src/engine-components/postprocessing/PostProcessingHandler.ts +573 -560
  977. package/src/engine-components/postprocessing/Volume.ts +426 -426
  978. package/src/engine-components/postprocessing/VolumeParameter.ts +158 -158
  979. package/src/engine-components/postprocessing/VolumeProfile.ts +61 -61
  980. package/src/engine-components/postprocessing/index.ts +5 -5
  981. package/src/engine-components/postprocessing/utils.ts +154 -154
  982. package/src/engine-components/timeline/PlayableDirector.ts +722 -722
  983. package/src/engine-components/timeline/SignalAsset.ts +144 -144
  984. package/src/engine-components/timeline/TimelineModels.ts +92 -92
  985. package/src/engine-components/timeline/TimelineTracks.ts +967 -967
  986. package/src/engine-components/timeline/index.ts +3 -3
  987. package/src/engine-components/ui/BaseUIComponent.ts +203 -203
  988. package/src/engine-components/ui/Button.ts +307 -307
  989. package/src/engine-components/ui/Canvas.ts +419 -419
  990. package/src/engine-components/ui/CanvasGroup.ts +54 -54
  991. package/src/engine-components/ui/EventSystem.ts +854 -854
  992. package/src/engine-components/ui/Graphic.ts +275 -275
  993. package/src/engine-components/ui/Image.ts +112 -112
  994. package/src/engine-components/ui/InputField.ts +321 -321
  995. package/src/engine-components/ui/Interfaces.ts +57 -57
  996. package/src/engine-components/ui/Layout.ts +334 -334
  997. package/src/engine-components/ui/Outline.ts +13 -13
  998. package/src/engine-components/ui/PointerEvents.ts +206 -206
  999. package/src/engine-components/ui/RaycastUtils.ts +69 -69
  1000. package/src/engine-components/ui/Raycaster.ts +102 -102
  1001. package/src/engine-components/ui/RectTransform.ts +375 -375
  1002. package/src/engine-components/ui/SpatialHtml.ts +79 -79
  1003. package/src/engine-components/ui/Symbols.ts +1 -1
  1004. package/src/engine-components/ui/Text.ts +578 -578
  1005. package/src/engine-components/ui/Utils.ts +113 -113
  1006. package/src/engine-components/utils/EnvironmentScene.ts +245 -245
  1007. package/src/engine-components/utils/LookAt.ts +88 -88
  1008. package/src/engine-components/utils/OpenURL.ts +114 -114
  1009. package/src/engine-components/webxr/Avatar.ts +265 -265
  1010. package/src/engine-components/webxr/TeleportTarget.ts +9 -9
  1011. package/src/engine-components/webxr/WebARCameraBackground.ts +175 -175
  1012. package/src/engine-components/webxr/WebARSessionRoot.ts +880 -880
  1013. package/src/engine-components/webxr/WebXR.ts +585 -585
  1014. package/src/engine-components/webxr/WebXRAvatar.ts +66 -66
  1015. package/src/engine-components/webxr/WebXRImageTracking.ts +519 -519
  1016. package/src/engine-components/webxr/WebXRPlaneTracking.ts +570 -570
  1017. package/src/engine-components/webxr/WebXRRig.ts +77 -77
  1018. package/src/engine-components/webxr/XRFlag.ts +147 -147
  1019. package/src/engine-components/webxr/controllers/XRControllerFollow.ts +118 -118
  1020. package/src/engine-components/webxr/controllers/XRControllerModel.ts +373 -373
  1021. package/src/engine-components/webxr/controllers/XRControllerMovement.ts +550 -550
  1022. package/src/engine-components/webxr/index.ts +2 -2
  1023. package/src/engine-components/webxr/types.ts +3 -3
  1024. package/src/engine-components-experimental/Presentation.ts +12 -12
  1025. package/src/engine-components-experimental/api.ts +4 -4
  1026. package/src/engine-components-experimental/networking/PlayerSync.ts +401 -401
  1027. package/src/engine-schemes/COMPILE_SCHEMES.bat +3 -3
  1028. package/src/engine-schemes/COMPILE_TS.bat +11 -11
  1029. package/src/engine-schemes/README.md +1 -1
  1030. package/src/engine-schemes/api.ts +12 -12
  1031. package/src/engine-schemes/schemes.ts +28 -28
  1032. package/src/engine-schemes/synced-camera-model.ts +92 -92
  1033. package/src/engine-schemes/synced-transform-model.ts +90 -90
  1034. package/src/engine-schemes/syncedCamera.fbs +10 -10
  1035. package/src/engine-schemes/transform.ts +50 -50
  1036. package/src/engine-schemes/transforms.fbs +25 -25
  1037. package/src/engine-schemes/vec.fbs +19 -19
  1038. package/src/engine-schemes/vec2.ts +33 -33
  1039. package/src/engine-schemes/vec3.ts +38 -38
  1040. package/src/engine-schemes/vec4.ts +43 -43
  1041. package/src/engine-schemes/vr-user-state-buffer.ts +145 -145
  1042. package/src/engine-schemes/vrUserStateBuffer.fbs +17 -17
  1043. package/src/include/draco/draco_decoder.js +34 -34
  1044. package/src/include/ktx2/basis_transcoder.js +21 -21
  1045. package/src/include/needle/arial-msdf.json +1471 -1471
  1046. package/src/include/three/DragControls.js +231 -231
  1047. package/src/include/three/EXT_mesh_gpu_instancing_exporter.js +66 -66
  1048. package/src/needle-engine.ts +70 -70
  1049. package/dist/generateMeshBVH.worker-Cdfpaq5W.js +0 -25
  1050. package/src/engine-schemes/dist/api.js +0 -17
  1051. package/src/engine-schemes/dist/api.js.meta +0 -7
  1052. package/src/engine-schemes/dist/schemes.js +0 -25
  1053. package/src/engine-schemes/dist/schemes.js.meta +0 -7
  1054. package/src/engine-schemes/dist/synced-camera-model.js +0 -74
  1055. package/src/engine-schemes/dist/synced-camera-model.js.meta +0 -7
  1056. package/src/engine-schemes/dist/synced-transform-model.js +0 -73
  1057. package/src/engine-schemes/dist/synced-transform-model.js.meta +0 -7
  1058. package/src/engine-schemes/dist/transform.js +0 -46
  1059. package/src/engine-schemes/dist/transform.js.meta +0 -7
  1060. package/src/engine-schemes/dist/vec2.js +0 -32
  1061. package/src/engine-schemes/dist/vec2.js.meta +0 -7
  1062. package/src/engine-schemes/dist/vec3.js +0 -36
  1063. package/src/engine-schemes/dist/vec3.js.meta +0 -7
  1064. package/src/engine-schemes/dist/vec4.js +0 -40
  1065. package/src/engine-schemes/dist/vec4.js.meta +0 -7
  1066. package/src/engine-schemes/dist/vr-user-state-buffer.js +0 -110
  1067. package/src/engine-schemes/dist/vr-user-state-buffer.js.meta +0 -7
@@ -1,2427 +1,2427 @@
1
- import '../../../engine/engine_shims.js';
2
-
3
- import {
4
- AnimationClip,
5
- Bone,
6
- BufferAttribute,
7
- BufferGeometry,
8
- Color,
9
- DoubleSide,
10
- InterleavedBufferAttribute,
11
- LinearFilter,
12
- Material,
13
- MathUtils,
14
- Matrix4,
15
- Mesh,
16
- MeshBasicMaterial,
17
- MeshPhysicalMaterial,
18
- MeshStandardMaterial,
19
- Object3D,
20
- OrthographicCamera,
21
- PerspectiveCamera,
22
- PlaneGeometry,
23
- Quaternion,
24
- RGBAFormat,
25
- Scene,
26
- ShaderMaterial,
27
- SkinnedMesh,
28
- SRGBColorSpace,
29
- Texture,
30
- Uniform,
31
- UnsignedByteType,
32
- Vector3,
33
- Vector4,
34
- WebGLRenderer,
35
- WebGLRenderTarget
36
- } from 'three';
37
- import * as fflate from 'three/examples/jsm/libs/fflate.module.js';
38
-
39
- import { VERSION } from "../../../engine/engine_constants.js";
40
- import type { OffscreenCanvasExt } from '../../../engine/engine_shims.js';
41
- import { Progress } from '../../../engine/engine_time_utils.js';
42
- import { BehaviorExtension } from '../../api.js';
43
- import type { IUSDExporterExtension } from './Extension.js';
44
- import type { AnimationExtension } from './extensions/Animation.js';
45
- import type { PhysicsExtension } from './extensions/behavior/PhysicsExtension.js';
46
- import {buildNodeMaterial} from './extensions/NodeMaterialConverter.js';
47
-
48
- type MeshPhysicalNodeMaterial = import("three/src/materials/nodes/MeshPhysicalNodeMaterial.js").default;
49
-
50
- function makeNameSafe( str ) {
51
- // Remove characters that are not allowed in USD ASCII identifiers
52
- str = str.replace( /[^a-zA-Z0-9_]/g, '' );
53
-
54
- // If name doesn't start with a-zA-Z_, add _ to the beginning – required by USD
55
- if ( !str.match( /^[a-zA-Z_]/ ) )
56
- str = '_' + str;
57
-
58
- return str;
59
- }
60
-
61
- function makeDisplayNameSafe(str) {
62
- str = str.replace("\"", "\\\"");
63
- return str;
64
- }
65
-
66
- // TODO check if this works when bones in the skeleton are completely unordered
67
- function findCommonAncestor(objects: Object3D[]): Object3D | null {
68
- if (objects.length === 0) return null;
69
-
70
- const ancestors = objects.map((obj) => {
71
- const objAncestors = new Array<Object3D>();
72
- while (obj.parent) {
73
- objAncestors.unshift(obj.parent);
74
- obj = obj.parent;
75
- }
76
- return objAncestors;
77
- });
78
-
79
- //@ts-ignore – findLast seems to be missing in TypeScript types pre-5.x
80
- const commonAncestor = ancestors[0].findLast((ancestor) => {
81
- return ancestors.every((a) => a.includes(ancestor));
82
- });
83
-
84
- return commonAncestor || null;
85
- }
86
-
87
- function findStructuralNodesInBoneHierarchy(bones: Array<Object3D>) {
88
-
89
- const commonAncestor = findCommonAncestor(bones);
90
- // find all structural nodes – parents of bones that are not bones themselves
91
- const structuralNodes = new Set<Object3D>();
92
- for ( const bone of bones ) {
93
- let current = bone.parent;
94
- while ( current && current !== commonAncestor ) {
95
- if ( !bones.includes(current) ) {
96
- structuralNodes.add(current);
97
- }
98
- current = current.parent;
99
- }
100
- }
101
-
102
- return structuralNodes;
103
- }
104
-
105
- declare type USDObjectTransform = {
106
- position: Vector3 | null;
107
- quaternion: Quaternion | null;
108
- scale: Vector3 | null;
109
- }
110
-
111
- const PositionIdentity = new Vector3();
112
- const QuaternionIdentity = new Quaternion();
113
- const ScaleIdentity = new Vector3(1,1,1);
114
-
115
- class USDObject {
116
-
117
- static USDObject_export_id = 0;
118
-
119
- uuid: string;
120
- name: string;
121
- /** If no type is provided, type is chosen automatically (Xform or Mesh) */
122
- type?: string;
123
- /** MaterialBindingAPI and SkelBindingAPI are handled automatically, extra schemas can be added here */
124
- extraSchemas: string[] = [];
125
- displayName?: string;
126
- visibility?: "inherited" | "invisible"; // defaults to "inherited" in USD
127
- getMatrix(): Matrix4 {
128
- if (!this.transform) return new Matrix4();
129
- const { position, quaternion, scale } = this.transform;
130
- const matrix = new Matrix4();
131
- matrix.compose(position || PositionIdentity, quaternion || QuaternionIdentity, scale || ScaleIdentity);
132
- return matrix;
133
- }
134
- setMatrix( value ) {
135
- if (!value || !(value instanceof Matrix4)) {
136
- this.transform = null;
137
- return;
138
- }
139
- const position = new Vector3();
140
- const quaternion = new Quaternion();
141
- const scale = new Vector3();
142
- value.decompose(position, quaternion, scale);
143
- this.transform = { position, quaternion, scale };
144
- }
145
- /** @deprecated Use `transform`, or `getMatrix()` if you really need the matrix */
146
- get matrix() { return this.getMatrix(); }
147
- /** @deprecated Use `transform`, or `setMatrix()` if you really need the matrix */
148
- set matrix( value ) { this.setMatrix( value ); }
149
-
150
- transform: USDObjectTransform | null = null;
151
- private _isDynamic: boolean;
152
- get isDynamic() { return this._isDynamic; }
153
- private set isDynamic( value ) { this._isDynamic = value; }
154
- geometry: BufferGeometry | null;
155
- material: MeshStandardMaterial | MeshBasicMaterial | Material | MeshPhysicalNodeMaterial | null;
156
- camera: PerspectiveCamera | OrthographicCamera | null;
157
- parent: USDObject | null;
158
- skinnedMesh: SkinnedMesh | null;
159
- children: Array<USDObject | null> = [];
160
- animations: AnimationClip[] | null;
161
- _eventListeners: {};
162
-
163
- // these are for tracking which xformops are needed
164
- needsTranslate: boolean = false;
165
- needsOrient: boolean = false;
166
- needsScale: boolean = false;
167
-
168
- static createEmptyParent( object: USDObject ) {
169
-
170
- const emptyParent = new USDObject( MathUtils.generateUUID(), object.name + '_empty_' + ( USDObject.USDObject_export_id ++ ), object.transform );
171
- const parent = object.parent;
172
- if (parent) parent.add( emptyParent );
173
- emptyParent.add( object );
174
- emptyParent.isDynamic = true;
175
- object.transform = null;
176
- return emptyParent;
177
-
178
- }
179
-
180
- static createEmpty() {
181
-
182
- const empty = new USDObject( MathUtils.generateUUID(), 'Empty_' + ( USDObject.USDObject_export_id ++ ) );
183
- empty.isDynamic = true;
184
- return empty;
185
- }
186
-
187
- constructor( id, name, transform: USDObjectTransform | null = null, mesh: BufferGeometry | null = null, material: MeshStandardMaterial | MeshBasicMaterial | MeshPhysicalNodeMaterial | Material | null = null, camera: PerspectiveCamera | OrthographicCamera | null = null, skinnedMesh: SkinnedMesh | null = null, animations: AnimationClip[] | null = null ) {
188
-
189
- this.uuid = id;
190
- this.name = makeNameSafe( name );
191
- this.displayName = name;
192
-
193
- if (!transform) this.transform = null;
194
- else this.transform = {
195
- position: transform.position?.clone() || null,
196
- quaternion: transform.quaternion?.clone() || null,
197
- scale: transform.scale?.clone() || null
198
- };
199
- this.geometry = mesh;
200
- this.material = material;
201
- this.camera = camera;
202
- this.parent = null;
203
- this.children = [];
204
- this._eventListeners = {};
205
- this._isDynamic = false;
206
- this.skinnedMesh = skinnedMesh;
207
- this.animations = animations;
208
-
209
- }
210
-
211
- is( obj ) {
212
-
213
- if ( ! obj ) return false;
214
- return this.uuid === obj.uuid;
215
-
216
- }
217
-
218
- isEmpty() {
219
-
220
- return ! this.geometry;
221
-
222
- }
223
-
224
- clone() {
225
-
226
- const clone = new USDObject( MathUtils.generateUUID(), this.name, this.transform, this.geometry, this.material );
227
- clone.isDynamic = this.isDynamic;
228
- return clone;
229
-
230
- }
231
-
232
- deepClone() {
233
-
234
- const clone = this.clone();
235
- for ( const child of this.children ) {
236
-
237
- if ( !child ) continue;
238
- clone.add( child.deepClone() );
239
-
240
- }
241
-
242
- return clone;
243
-
244
- }
245
-
246
- getPath() {
247
-
248
- let current = this.parent;
249
- let path = this.name;
250
- while ( current ) {
251
-
252
- // StageRoot has a special path right now since there's additional Xforms for encapsulation.
253
- // Better would be to actually model them as part of our object graph, but they're written separately,
254
- // so currently we don't and instead work around that here.
255
- const currentName = current.parent ? current.name : (current.name + "/Scenes/Scene");
256
- path = currentName + '/' + path;
257
- current = current.parent;
258
-
259
- }
260
-
261
- return '</' + path + '>';
262
-
263
- }
264
-
265
- add( child ) {
266
-
267
- if ( child.parent ) {
268
-
269
- child.parent.remove( child );
270
-
271
- }
272
-
273
- child.parent = this;
274
- this.children.push( child );
275
-
276
- }
277
-
278
- remove( child ) {
279
-
280
- const index = this.children.indexOf( child );
281
- if ( index >= 0 ) {
282
-
283
- if ( child.parent === this ) child.parent = null;
284
- this.children.splice( index, 1 );
285
-
286
- }
287
-
288
- }
289
-
290
- addEventListener( evt, listener: ( writer: USDWriter, context: USDZExporterContext ) => void ) {
291
-
292
- if ( ! this._eventListeners[ evt ] ) this._eventListeners[ evt ] = [];
293
- this._eventListeners[ evt ].push( listener );
294
-
295
- }
296
-
297
- removeEventListener( evt, listener: ( writer: USDWriter, context: USDZExporterContext ) => void ) {
298
-
299
- if ( ! this._eventListeners[ evt ] ) return;
300
- const index = this._eventListeners[ evt ].indexOf( listener );
301
- if ( index >= 0 ) {
302
-
303
- this._eventListeners[ evt ].splice( index, 1 );
304
-
305
- }
306
-
307
- }
308
-
309
- onSerialize( writer, context ) {
310
-
311
- const listeners = this._eventListeners[ 'serialize' ];
312
- if ( listeners ) listeners.forEach( listener => listener( writer, context ) );
313
-
314
- }
315
-
316
- }
317
-
318
-
319
- class USDDocument extends USDObject {
320
-
321
- stageLength: number;
322
-
323
- get isDocumentRoot() {
324
-
325
- return true;
326
-
327
- }
328
- get isDynamic() {
329
-
330
- return false;
331
-
332
- }
333
-
334
- constructor() {
335
-
336
- super(undefined, 'StageRoot', null, null, null, null);
337
- this.children = [];
338
- this.stageLength = 200;
339
-
340
- }
341
-
342
- add( child: USDObject ) {
343
-
344
- child.parent = this;
345
- this.children.push( child );
346
-
347
- }
348
-
349
- remove( child: USDObject ) {
350
-
351
- const index = this.children.indexOf( child );
352
- if ( index >= 0 ) {
353
-
354
- if ( child.parent === this ) child.parent = null;
355
- this.children.splice( index, 1 );
356
-
357
- }
358
-
359
- }
360
-
361
- traverse( callback: ( object: USDObject ) => void, current: USDObject | null = null ) {
362
-
363
- if ( current !== null ) callback( current );
364
- else current = this;
365
- if ( current.children ) {
366
-
367
- for ( const child of current.children ) {
368
-
369
- this.traverse( callback, child );
370
-
371
- }
372
-
373
- }
374
-
375
- }
376
-
377
- findById( uuid: string ) {
378
-
379
- let found = false;
380
- function search( current: USDObject ): USDObject | undefined {
381
-
382
- if ( found ) return undefined;
383
- if ( current.uuid === uuid ) {
384
-
385
- found = true;
386
- return current;
387
-
388
- }
389
-
390
- if ( current.children ) {
391
-
392
- for ( const child of current.children ) {
393
-
394
- if (!child) continue;
395
- const res = search( child );
396
- if ( res ) return res;
397
-
398
- }
399
-
400
- }
401
- return undefined;
402
- }
403
-
404
- return search( this );
405
-
406
- }
407
-
408
- buildHeader( _context: USDZExporterContext ) {
409
- const animationExtension = _context.extensions?.find( ext => ext?.extensionName === 'animation' ) as AnimationExtension | undefined;
410
- const behaviorExtension = _context.extensions?.find( ext => ext?.extensionName === 'Behaviour' ) as BehaviorExtension | undefined;
411
- const physicsExtension = _context.extensions?.find( ext => ext?.extensionName === 'Physics' ) as PhysicsExtension | undefined;
412
- const startTimeCode = animationExtension?.getStartTimeCode() ?? 0;
413
- const endTimeCode = animationExtension?.getEndTimeCode() ?? 0;
414
-
415
- let comment = "";
416
- const registeredClips = animationExtension?.registeredClips;
417
- if (registeredClips) {
418
- for ( const clip of registeredClips ) {
419
- comment += `\t# Animation: ${clip.name}, start=${animationExtension.getStartTimeByClip(clip) * 60}, length=${clip.duration * 60}\n`;
420
- }
421
- }
422
- const comments = comment;
423
-
424
- return `#usda 1.0
425
- (
426
- customLayerData = {
427
- string creator = "Needle Engine ${VERSION}"
428
- dictionary Needle = {
429
- bool animations = ${animationExtension ? 1 : 0}
430
- bool interactive = ${behaviorExtension ? 1 : 0}
431
- bool physics = ${physicsExtension ? 1 : 0}
432
- bool quickLookCompatible = ${_context.quickLookCompatible ? 1 : 0}
433
- }
434
- }
435
- defaultPrim = "${makeNameSafe( this.name )}"
436
- metersPerUnit = 1
437
- upAxis = "Y"
438
- startTimeCode = ${startTimeCode}
439
- endTimeCode = ${endTimeCode}
440
- timeCodesPerSecond = 60
441
- framesPerSecond = 60
442
- doc = """Generated by Needle Engine USDZ Exporter ${VERSION}"""
443
- ${comments}
444
- )
445
- `;
446
-
447
- }
448
-
449
- }
450
-
451
- const newLine = '\n';
452
- const materialRoot = '</StageRoot/Materials';
453
-
454
- class USDWriter {
455
- str: string;
456
- indent: number;
457
-
458
- constructor() {
459
-
460
- this.str = '';
461
- this.indent = 0;
462
-
463
- }
464
-
465
- clear() {
466
-
467
- this.str = '';
468
- this.indent = 0;
469
-
470
- }
471
-
472
- beginBlock( str: string | undefined = undefined, char = '{', createNewLine = true ) {
473
-
474
- if ( str !== undefined ) {
475
- str = this.applyIndent( str );
476
- this.str += str;
477
- if ( createNewLine ) {
478
- this.str += newLine;
479
- this.str += this.applyIndent( char );
480
- }
481
- else {
482
- this.str += " " + char;
483
- }
484
- }
485
- else {
486
- this.str += this.applyIndent( char );
487
- }
488
-
489
- this.str += newLine;
490
- this.indent += 1;
491
-
492
- }
493
-
494
- closeBlock( char = '}' ) {
495
-
496
- this.indent -= 1;
497
- this.str += this.applyIndent( char ) + newLine;
498
-
499
- }
500
-
501
- beginArray( str ) {
502
-
503
- str = this.applyIndent( str + ' = [' );
504
- this.str += str;
505
- this.str += newLine;
506
- this.indent += 1;
507
-
508
- }
509
-
510
- closeArray() {
511
-
512
- this.indent -= 1;
513
- this.str += this.applyIndent( ']' ) + newLine;
514
-
515
- }
516
-
517
- appendLine( str = '' ) {
518
-
519
- str = this.applyIndent( str );
520
- this.str += str;
521
- this.str += newLine;
522
-
523
- }
524
-
525
- toString() {
526
-
527
- return this.str;
528
-
529
- }
530
-
531
- applyIndent( str ) {
532
-
533
- let indents = '';
534
- for ( let i = 0; i < this.indent; i ++ ) indents += '\t';
535
- return indents + str;
536
-
537
- }
538
-
539
- }
540
-
541
- declare type TextureMap = {[name: string]: {texture: Texture, scale?: Vector4}};
542
-
543
- class USDZExporterContext {
544
- root?: Object3D;
545
- exporter: USDZExporter;
546
- extensions: Array<IUSDExporterExtension> = [];
547
- quickLookCompatible: boolean;
548
- exportInvisible: boolean;
549
- materials: Map<string, Material>;
550
- textures: TextureMap;
551
- files: { [path: string]: Uint8Array | [Uint8Array, fflate.ZipOptions] | null | any }
552
- document: USDDocument;
553
- output: string;
554
- animations: AnimationClip[];
555
-
556
- constructor( root: Object3D | undefined, exporter: USDZExporter, options: {
557
- extensions?: Array<IUSDExporterExtension>,
558
- quickLookCompatible: boolean,
559
- exportInvisible: boolean,
560
- } ) {
561
-
562
- this.root = root;
563
- this.exporter = exporter;
564
- this.quickLookCompatible = options.quickLookCompatible;
565
- this.exportInvisible = options.exportInvisible;
566
-
567
- if ( options.extensions )
568
- this.extensions = options.extensions;
569
-
570
- this.materials = new Map();
571
- this.textures = {};
572
- this.files = {};
573
- this.document = new USDDocument();
574
- this.output = '';
575
- this.animations = [];
576
-
577
- }
578
-
579
- }
580
-
581
- /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_anchoring_type) */
582
- export type Anchoring = "plane" | "image" | "face" | "none"
583
- /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_planeanchoring_alignment) */
584
- export type Alignment = "horizontal" | "vertical" | "any";
585
-
586
- class USDZExporterOptions {
587
- ar: {
588
- anchoring: { type: Anchoring },
589
- planeAnchoring: { alignment: Alignment },
590
- } = {
591
- anchoring: { type: 'plane' },
592
- planeAnchoring: { alignment: 'horizontal' }
593
- };
594
- quickLookCompatible: boolean = false;
595
- extensions: any[] = [];
596
- maxTextureSize: number = 4096;
597
- exportInvisible: boolean = false;
598
- }
599
-
600
- class USDZExporter {
601
- debug: boolean;
602
- pruneUnusedNodes: boolean;
603
- sceneAnchoringOptions: USDZExporterOptions = new USDZExporterOptions();
604
- extensions: Array<IUSDExporterExtension> = [];
605
- keepObject?: (object: Object3D) => boolean;
606
- beforeWritingDocument?: () => void;
607
-
608
- constructor() {
609
-
610
- this.debug = false;
611
- this.pruneUnusedNodes = true;
612
-
613
- }
614
-
615
- async parse(scene: Object3D | undefined, options: USDZExporterOptions = new USDZExporterOptions()) {
616
-
617
- options = Object.assign( new USDZExporterOptions(), options );
618
-
619
- this.sceneAnchoringOptions = options;
620
- const context = new USDZExporterContext( scene, this, options );
621
- this.extensions = context.extensions;
622
-
623
- const files = context.files;
624
- const modelFileName = 'model.usda';
625
-
626
- // model file should be first in USDZ archive so we init it here
627
- files[ modelFileName ] = null;
628
-
629
- const materials = context.materials;
630
- const textures = context.textures;
631
-
632
- Progress.report('export-usdz', "Invoking onBeforeBuildDocument");
633
- await invokeAll( context, 'onBeforeBuildDocument' );
634
- Progress.report('export-usdz', "Done onBeforeBuildDocument");
635
-
636
- Progress.report('export-usdz', "Reparent bones to common ancestor");
637
-
638
- // Find all skeletons and reparent them to their skelroot / armature / uppermost bone parent.
639
- // This may not be correct in all cases.
640
- const reparentings: Array<{ object: Object3D, originalParent: Object3D | null, newParent: Object3D }> = [];
641
- const allReparentingObjects = new Set<string>();
642
- scene?.traverse(object => {
643
- if (!options.exportInvisible && !object.visible) return;
644
-
645
- if (object instanceof SkinnedMesh) {
646
- const bones = object.skeleton.bones as Bone[];
647
-
648
- const commonAncestor = findCommonAncestor(bones);
649
- if (commonAncestor) {
650
- const newReparenting = { object, originalParent: object.parent, newParent: commonAncestor };
651
- reparentings.push( newReparenting );
652
-
653
- // keep track of which nodes are important for skeletal export consistency
654
- allReparentingObjects.add(newReparenting.object.uuid);
655
- if (newReparenting.newParent) allReparentingObjects.add(newReparenting.newParent.uuid);
656
- if (newReparenting.originalParent) allReparentingObjects.add(newReparenting.originalParent.uuid);
657
- }
658
- }
659
- });
660
-
661
- for ( const reparenting of reparentings ) {
662
- const { object, originalParent, newParent } = reparenting;
663
- newParent.add( object );
664
- }
665
-
666
- Progress.report('export-usdz', "Traversing hierarchy");
667
- if (scene) traverse( scene, context.document, context, this.keepObject);
668
-
669
- // Root object should have identity matrix
670
- // so that root transformations don't end up in the resulting file.
671
- // if (context.document.children?.length > 0)
672
- // context.document.children[0]?.matrix.identity(); //.multiply(new Matrix4().makeRotationY(Math.PI));
673
-
674
- Progress.report('export-usdz', "Invoking onAfterBuildDocument");
675
- await invokeAll( context, 'onAfterBuildDocument' );
676
-
677
- // At this point, we know all animated objects, all skinned mesh objects, and all objects targeted by behaviors.
678
- // We can prune all empty nodes (no geometry or material) depth-first.
679
- // This avoids unnecessary export of e.g. animated bones as nodes when they have no children
680
- // (for example, a sword attached to a hand still needs that entire hierarchy exported)
681
- const behaviorExt = context.extensions.find( ext => ext.extensionName === 'Behaviour' ) as BehaviorExtension | undefined;
682
- const allBehaviorTargets = behaviorExt?.getAllTargetUuids() ?? new Set<string>();
683
-
684
- // Prune pass. Depth-first removal of nodes that don't affect the outcome of the scene.
685
- if (this.pruneUnusedNodes) {
686
- const options = {
687
- allBehaviorTargets,
688
- debug: false,
689
- boneReparentings: allReparentingObjects,
690
- quickLookCompatible: context.quickLookCompatible,
691
- };
692
- if (this.debug) logUsdHierarchy(context.document, "Hierarchy BEFORE pruning", options);
693
- prune( context.document, options );
694
- if (this.debug) logUsdHierarchy(context.document, "Hierarchy AFTER pruning");
695
- }
696
- else if (this.debug) {
697
- console.log("Pruning of empty nodes is disabled. This may result in a larger USDZ file.");
698
- }
699
-
700
- Progress.report('export-usdz', { message: "Parsing document", autoStep: 10 });
701
- await parseDocument( context, () => {
702
- // injected after stageRoot.
703
- // TODO property use context/writer instead of string concat
704
- Progress.report('export-usdz', "Building materials");
705
- const result = buildMaterials( materials, textures, options.quickLookCompatible );
706
- return result;
707
- } );
708
-
709
- Progress.report("export-usdz", "Invoking onAfterSerialize");
710
- await invokeAll( context, 'onAfterSerialize' );
711
-
712
- // repair the parenting again
713
- for ( const reparenting of reparentings ) {
714
- const { object, originalParent, newParent } = reparenting;
715
- if (originalParent)
716
- originalParent.add( object );
717
- }
718
-
719
- // Moved into parseDocument callback for proper defaultPrim encapsulation
720
- // context.output += buildMaterials( materials, textures, options.quickLookCompatible );
721
-
722
- // callback for validating after all export has been done
723
- context.exporter?.beforeWritingDocument?.();
724
-
725
- const header = context.document.buildHeader( context );
726
- const final = header + '\n' + context.output;
727
-
728
- // full output file
729
- if ( this.debug ) console.log( final );
730
-
731
- files[ modelFileName ] = fflate.strToU8( final );
732
- context.output = '';
733
-
734
- Progress.report("export-usdz", { message: "Exporting textures", autoStep: 10 });
735
- Progress.start("export-usdz-textures", { parentScope: "export-usdz", logTimings: false });
736
- const decompressionRenderer = new WebGLRenderer( {
737
- antialias: false,
738
- alpha: true,
739
- premultipliedAlpha: false,
740
- preserveDrawingBuffer: true
741
- } );
742
-
743
- const textureCount = Object.keys(textures).length;
744
- Progress.report("export-usdz-textures", { totalSteps: textureCount * 3, currentStep: 0 });
745
- const convertTexture = async (id: string) => {
746
-
747
- const textureData = textures[ id ];
748
- const texture = textureData.texture;
749
-
750
- const isRGBA = formatsWithAlphaChannel.includes( texture.format );
751
-
752
- // Change: we need to always read back the texture now, otherwise the unpremultiplied workflow doesn't work.
753
- let img: ImageReadbackResult = {
754
- imageData: texture.image
755
- };
756
-
757
- Progress.report("export-usdz-textures", { message: "read back texture", autoStep: true });
758
- const anyColorScale = textureData.scale !== undefined && textureData.scale.x !== 1 && textureData.scale.y !== 1 && textureData.scale.z !== 1 && textureData.scale.w !== 1;
759
- // @ts-ignore
760
- if ( texture.isCompressedTexture || texture.isRenderTargetTexture || anyColorScale ) {
761
- img = await decompressGpuTexture( texture, options.maxTextureSize, decompressionRenderer, textureData.scale );
762
- }
763
-
764
- Progress.report("export-usdz-textures", { message: "convert texture to canvas", autoStep: true });
765
- const canvas = await imageToCanvasUnpremultiplied( img.imageBitmap || img.imageData, options.maxTextureSize ).catch( err => {
766
- console.error("Error converting texture to canvas", texture, err);
767
- });
768
-
769
- if ( canvas ) {
770
-
771
- Progress.report("export-usdz-textures", { message: "convert canvas to blob", autoStep: true });
772
- const blob = await canvas.convertToBlob( {type: isRGBA ? 'image/png' : 'image/jpeg', quality: 0.95 } );
773
- files[ `textures/${id}.${isRGBA ? 'png' : 'jpg'}` ] = new Uint8Array( await blob.arrayBuffer() );
774
-
775
- } else {
776
-
777
- console.warn( 'Can`t export texture: ', texture );
778
-
779
- }
780
- };
781
-
782
- for ( const id in textures ) {
783
-
784
- await convertTexture( id );
785
- }
786
-
787
- decompressionRenderer.dispose();
788
-
789
- // 64 byte alignment
790
- // https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109
791
-
792
- Progress.end("export-usdz-textures");
793
-
794
- let offset = 0;
795
-
796
- for ( const filename in files ) {
797
-
798
- const file = files[ filename ];
799
- const headerSize = 34 + filename.length;
800
-
801
- offset += headerSize;
802
-
803
- const offsetMod64 = offset & 63;
804
-
805
- if ( offsetMod64 !== 4 ) {
806
-
807
- const padLength = 64 - offsetMod64;
808
- const padding = new Uint8Array( padLength );
809
-
810
- files[ filename ] = [ file, { extra: { 12345: padding } } ];
811
-
812
- }
813
-
814
- offset = file.length;
815
-
816
- }
817
-
818
- Progress.report("export-usdz", "zip archive");
819
- return fflate.zipSync( files, { level: 0 } );
820
-
821
- }
822
-
823
- }
824
-
825
- function traverse( object: Object3D, parentModel: USDObject, context: USDZExporterContext, keepObject?: (object: Object3D) => boolean ) {
826
-
827
- if (!context.exportInvisible && !object.visible) return;
828
-
829
- let model: USDObject | undefined = undefined;
830
- let geometry: BufferGeometry | undefined = undefined;
831
- let material: Material | Material[] | undefined = undefined;
832
-
833
- const transform: USDObjectTransform = { position: object.position, quaternion: object.quaternion, scale: object.scale };
834
- if (object.position.x === 0 && object.position.y === 0 && object.position.z === 0)
835
- transform.position = null;
836
- if (object.quaternion.x === 0 && object.quaternion.y === 0 && object.quaternion.z === 0 && object.quaternion.w === 1)
837
- transform.quaternion = null;
838
- if (object.scale.x === 1 && object.scale.y === 1 && object.scale.z === 1)
839
- transform.scale = null;
840
-
841
- if (object instanceof Mesh || object instanceof SkinnedMesh) {
842
- geometry = object.geometry;
843
- material = object.material;
844
- }
845
-
846
- // API for an explicit choice to discard this object – for example, a geometry that should not be exported,
847
- // but childs should still be exported.
848
- if (keepObject && !keepObject(object)) {
849
- geometry = undefined;
850
- material = undefined;
851
- }
852
-
853
- if ( (object instanceof Mesh || object instanceof SkinnedMesh) &&
854
- material && typeof material === 'object' &&
855
- (material instanceof MeshStandardMaterial ||
856
- material instanceof MeshBasicMaterial ||
857
- // material instanceof MeshPhysicalNodeMaterial ||
858
- (material as any).isMeshPhysicalNodeMaterial ||
859
- (material instanceof Material && material.type === "MeshLineMaterial"))) {
860
-
861
- const name = getObjectId( object );
862
- const skinnedMeshObject = object instanceof SkinnedMesh ? object : null;
863
- model = new USDObject( object.uuid, name, transform, geometry, material as any, undefined, skinnedMeshObject, object.animations );
864
-
865
- } else if ( object instanceof PerspectiveCamera || object instanceof OrthographicCamera ) {
866
-
867
- const name = getObjectId( object );
868
- model = new USDObject( object.uuid, name, transform, undefined, undefined, object );
869
-
870
- } else {
871
-
872
- const name = getObjectId( object );
873
- model = new USDObject( object.uuid, name, transform, undefined, undefined, undefined, undefined, object.animations );
874
-
875
- }
876
-
877
- if ( model ) {
878
-
879
- model.displayName = object.userData?.name || object.name;
880
- model.visibility = object.visible ? undefined : "invisible";
881
-
882
- if ( parentModel ) {
883
-
884
- parentModel.add( model );
885
-
886
- }
887
-
888
- parentModel = model;
889
-
890
- if ( context.extensions ) {
891
-
892
- for ( const ext of context.extensions ) {
893
-
894
- if ( ext.onExportObject ) ext.onExportObject.call( ext, object, model, context );
895
-
896
- }
897
-
898
- }
899
-
900
- } else {
901
-
902
- const name = getObjectId( object );
903
- const empty = new USDObject( object.uuid, name, { position: object.position, quaternion: object.quaternion, scale: object.scale } );
904
- if ( parentModel ) {
905
-
906
- parentModel.add( empty );
907
-
908
- }
909
-
910
- parentModel = empty;
911
-
912
- }
913
-
914
- for ( const ch of object.children ) {
915
-
916
- traverse( ch, parentModel, context, keepObject );
917
-
918
- }
919
-
920
- }
921
-
922
- function logUsdHierarchy( object: USDObject, prefix: string, ...extraLogObjects: any[] ) {
923
-
924
- const item = {};
925
- let itemCount = 0;
926
-
927
- function collectItem( object: USDObject, current) {
928
- itemCount++;
929
- let name = object.displayName || object.name;
930
- name += " (" + object.uuid + ")";
931
- const hasAny = object.geometry || object.material || object.camera || object.skinnedMesh;
932
- if (hasAny) {
933
- name += " (" + (object.geometry ? "geo, " : "") + (object.material ? "mat, " : "") + (object.camera ? "cam, " : "") + (object.skinnedMesh ? "skin, " : "") + ")";
934
- }
935
- current[name] = {};
936
- const props = { object };
937
- if (object.material) props['mat'] = true;
938
- if (object.geometry) props['geo'] = true;
939
- if (object.camera) props['cam'] = true;
940
- if (object.skinnedMesh) props['skin'] = true;
941
-
942
- current[name]._self = props;
943
- for ( const child of object.children ) {
944
- if (child) {
945
- collectItem(child, current[name]);
946
- }
947
- }
948
- }
949
-
950
- collectItem(object, item);
951
-
952
- console.log(prefix + " (" + itemCount + " nodes)", item, ...extraLogObjects);
953
- }
954
-
955
- function prune ( object: USDObject, options : {
956
- allBehaviorTargets: Set<string>,
957
- debug: boolean,
958
- boneReparentings: Set<string>,
959
- quickLookCompatible: boolean,
960
- } ) {
961
-
962
- let allChildsWerePruned = true;
963
-
964
- const prunedChilds = new Array<USDObject>();
965
- const keptChilds = new Array<USDObject>();
966
-
967
- if (object.children.length === 0) {
968
- allChildsWerePruned = true;
969
- }
970
- else {
971
- const childs = [...object.children];
972
- for ( const child of childs ) {
973
- if (child) {
974
- const childWasPruned = prune(child, options);
975
- if (options.debug) {
976
- if (childWasPruned) prunedChilds.push(child);
977
- else keptChilds.push(child);
978
- }
979
- allChildsWerePruned = allChildsWerePruned && childWasPruned;
980
- }
981
- }
982
- }
983
-
984
- // check if this object is referenced by any behavior
985
- const isBehaviorSourceOrTarget = options.allBehaviorTargets.has(object.uuid);
986
-
987
- // check if this object has any material or geometry
988
- const isVisible = object.geometry || object.material || (object.camera && !options.quickLookCompatible) || object.skinnedMesh || false;
989
-
990
- // check if this object is part of any reparenting
991
- const isBoneReparenting = options.boneReparentings.has(object.uuid);
992
-
993
- const canBePruned = allChildsWerePruned && !isBehaviorSourceOrTarget && !isVisible && !isBoneReparenting;
994
-
995
- if (canBePruned) {
996
- if (options.debug) console.log("Pruned object:", (object.displayName || object.name) + " (" + object.uuid + ")", {
997
- isVisible,
998
- isBehaviorSourceOrTarget,
999
- allChildsWerePruned,
1000
- isBoneReparenting,
1001
- object,
1002
- prunedChilds,
1003
- keptChilds
1004
- });
1005
- object.parent?.remove(object);
1006
- }
1007
- else {
1008
- if (options.debug) console.log("Kept object:", (object.displayName || object.name) + " (" + object.uuid + ")", {
1009
- isVisible,
1010
- isBehaviorSourceOrTarget,
1011
- allChildsWerePruned,
1012
- isBoneReparenting,
1013
- object,
1014
- prunedChilds,
1015
- keptChilds
1016
- });
1017
- }
1018
-
1019
- // if it has no children and is not a behavior source or target, and is not visible, prune it
1020
- return canBePruned;
1021
- }
1022
-
1023
- async function parseDocument( context: USDZExporterContext, afterStageRoot: () => string ) {
1024
-
1025
- Progress.start("export-usdz-resources", "export-usdz");
1026
- const resources: Array<() => void> = [];
1027
- for ( const child of context.document.children ) {
1028
- addResources( child, context, resources );
1029
- }
1030
- // addResources now only collects promises for better progress reporting.
1031
- // We are resolving them here and reporting progress on that:
1032
- const total = resources.length;
1033
- for (let i = 0; i < total; i++) {
1034
- Progress.report("export-usdz-resources", { totalSteps: total, currentStep: i });
1035
- await new Promise<void>((resolve, _reject) => {
1036
- resources[i]();
1037
- resolve();
1038
- });
1039
- }
1040
- Progress.end("export-usdz-resources");
1041
-
1042
- const writer = new USDWriter();
1043
- const arAnchoringOptions = context.exporter.sceneAnchoringOptions.ar;
1044
-
1045
- writer.beginBlock( `def Xform "${context.document.name}"` );
1046
-
1047
- writer.beginBlock( `def Scope "Scenes" (
1048
- kind = "sceneLibrary"
1049
- )` );
1050
-
1051
- writer.beginBlock( `def Xform "Scene"`, '(', false);
1052
- writer.appendLine( `apiSchemas = ["Preliminary_AnchoringAPI"]` );
1053
- writer.appendLine( `customData = {`);
1054
- writer.appendLine( ` bool preliminary_collidesWithEnvironment = 0` );
1055
- writer.appendLine( ` string sceneName = "Scene"`);
1056
- writer.appendLine( `}` );
1057
- writer.appendLine( `sceneName = "Scene"` );
1058
- writer.closeBlock( ')' );
1059
- writer.beginBlock();
1060
-
1061
- writer.appendLine( `token preliminary:anchoring:type = "${arAnchoringOptions.anchoring.type}"` );
1062
- if (arAnchoringOptions.anchoring.type === 'plane')
1063
- writer.appendLine( `token preliminary:planeAnchoring:alignment = "${arAnchoringOptions.planeAnchoring.alignment}"` );
1064
- // bit hacky as we don't have a callback here yet. Relies on the fact that the image is named identical in the ImageTracking extension.
1065
- if (arAnchoringOptions.anchoring.type === 'image')
1066
- writer.appendLine( `rel preliminary:imageAnchoring:referenceImage = </${context.document.name}/Scenes/Scene/AnchoringReferenceImage>` );
1067
- writer.appendLine();
1068
-
1069
- const count = (object: USDObject | null) => {
1070
- if (!object) return 0;
1071
- let total = 1;
1072
- for ( const child of object.children ) total += count( child );
1073
- return total;
1074
- }
1075
- const totalXformCount = count(context.document);
1076
- Progress.start("export-usdz-xforms", "export-usdz");
1077
- Progress.report("export-usdz-xforms", { totalSteps: totalXformCount, currentStep: 1 });
1078
-
1079
- for ( const child of context.document.children ) {
1080
- buildXform( child, writer, context );
1081
- }
1082
- Progress.end("export-usdz-xforms");
1083
-
1084
- Progress.report("export-usdz", "invoke onAfterHierarchy");
1085
- invokeAll( context, 'onAfterHierarchy', writer );
1086
-
1087
- writer.closeBlock();
1088
- writer.closeBlock();
1089
- writer.appendLine(afterStageRoot());
1090
- writer.closeBlock();
1091
-
1092
- Progress.report("export-usdz", "write to string")
1093
- context.output += writer.toString();
1094
-
1095
- }
1096
-
1097
- function addResources( object: USDObject | null, context: USDZExporterContext, resources: Array<() => void>) {
1098
-
1099
- if ( !object ) return;
1100
-
1101
- const geometry = object.geometry;
1102
- const material = object.material;
1103
-
1104
- if ( geometry ) {
1105
-
1106
- if ( material && (
1107
- 'isMeshStandardMaterial' in material && material.isMeshStandardMaterial ||
1108
- 'isMeshBasicMaterial' in material && material.isMeshBasicMaterial ||
1109
- material.type === "MeshLineMaterial"
1110
- ) ) { // TODO convert unlit to lit+emissive
1111
-
1112
- const geometryFileName = 'geometries/' + getGeometryName(geometry, object.name) + '.usda';
1113
-
1114
- if ( ! ( geometryFileName in context.files ) ) {
1115
-
1116
- const action = () => {
1117
- const meshObject = buildMeshObject( geometry, object.skinnedMesh?.skeleton?.bones, context.quickLookCompatible );
1118
- context.files[ geometryFileName ] = buildUSDFileAsString( meshObject, context);
1119
- };
1120
-
1121
- resources.push(action);
1122
-
1123
- }
1124
-
1125
- } else {
1126
-
1127
- console.warn( 'NeedleUSDZExporter: Unsupported material type (USDZ only supports MeshStandardMaterial)', material?.name );
1128
-
1129
- }
1130
-
1131
- }
1132
-
1133
- if ( material ) {
1134
-
1135
- if ( ! ( material.uuid in context.materials ) ) {
1136
-
1137
- context.materials[ material.uuid ] = material;
1138
-
1139
- }
1140
- }
1141
-
1142
- for ( const ch of object.children ) {
1143
-
1144
- addResources( ch, context, resources );
1145
-
1146
- }
1147
-
1148
- }
1149
-
1150
- async function invokeAll( context: USDZExporterContext, name: string, writer: USDWriter | null = null ) {
1151
-
1152
- if ( context.extensions ) {
1153
-
1154
- for ( const ext of context.extensions ) {
1155
-
1156
- if ( !ext ) continue;
1157
-
1158
- if ( typeof ext[ name ] === 'function' ) {
1159
-
1160
- const method = ext[ name ];
1161
- const res = method.call( ext, context, writer );
1162
- if(res instanceof Promise) {
1163
- await res;
1164
- }
1165
- }
1166
-
1167
- }
1168
-
1169
- }
1170
-
1171
- }
1172
- let _renderer: WebGLRenderer | null = null;
1173
- let renderTarget: WebGLRenderTarget | null = null;
1174
- let fullscreenQuadGeometry: PlaneGeometry | null;
1175
- let fullscreenQuadMaterial: ShaderMaterial | null;
1176
- let fullscreenQuad: Mesh | null;
1177
-
1178
- declare type ImageReadbackResult = {
1179
- imageData: ImageData;
1180
- imageBitmap?: ImageBitmap;
1181
- }
1182
-
1183
- /** Reads back a texture from the GPU (can be compressed, a render texture, or anything), optionally applies RGBA colorScale to it, and returns CPU data for further usage.
1184
- * Note that there are WebGL / WebGPU rules preventing some use of data between WebGL contexts.
1185
- */
1186
- async function decompressGpuTexture( texture, maxTextureSize = Infinity, renderer: WebGLRenderer | null = null, colorScale: Vector4 | undefined = undefined): Promise<ImageReadbackResult> {
1187
-
1188
- if ( ! fullscreenQuadGeometry ) fullscreenQuadGeometry = new PlaneGeometry( 2, 2, 1, 1 );
1189
- if ( ! fullscreenQuadMaterial ) fullscreenQuadMaterial = new ShaderMaterial( {
1190
- uniforms: {
1191
- blitTexture: new Uniform( texture ),
1192
- flipY: new Uniform( false ),
1193
- scale: new Uniform( new Vector4( 1, 1, 1, 1 ) ),
1194
- },
1195
- vertexShader: `
1196
- varying vec2 vUv;
1197
- uniform bool flipY;
1198
- void main(){
1199
- vUv = uv;
1200
- if (flipY)
1201
- vUv.y = 1. - vUv.y;
1202
- gl_Position = vec4(position.xy * 1.0,0.,.999999);
1203
- }`,
1204
- fragmentShader: `
1205
- uniform sampler2D blitTexture;
1206
- uniform vec4 scale;
1207
- varying vec2 vUv;
1208
-
1209
- void main(){
1210
- gl_FragColor = vec4(vUv.xy, 0, 1);
1211
-
1212
- #ifdef IS_SRGB
1213
- gl_FragColor = sRGBTransferOETF( texture2D( blitTexture, vUv) );
1214
- #else
1215
- gl_FragColor = texture2D( blitTexture, vUv);
1216
- #endif
1217
-
1218
- gl_FragColor.rgba *= scale.rgba;
1219
- }`
1220
- } );
1221
-
1222
- // update uniforms
1223
- const uniforms = fullscreenQuadMaterial.uniforms;
1224
- uniforms.blitTexture.value = texture;
1225
- uniforms.flipY.value = false;
1226
- uniforms.scale.value = new Vector4( 1, 1, 1, 1 );
1227
- if ( colorScale !== undefined ) uniforms.scale.value.copy( colorScale );
1228
-
1229
- fullscreenQuadMaterial.defines.IS_SRGB = texture.colorSpace == SRGBColorSpace;
1230
- fullscreenQuadMaterial.needsUpdate = true;
1231
-
1232
- if ( ! fullscreenQuad ) {
1233
-
1234
- fullscreenQuad = new Mesh( fullscreenQuadGeometry, fullscreenQuadMaterial );
1235
- fullscreenQuad.frustumCulled = false;
1236
-
1237
- }
1238
-
1239
- const _camera = new PerspectiveCamera();
1240
- const _scene = new Scene();
1241
- _scene.add( fullscreenQuad );
1242
-
1243
- if ( ! renderer ) {
1244
-
1245
- renderer = _renderer = new WebGLRenderer( { antialias: false, alpha: true, premultipliedAlpha: false, preserveDrawingBuffer: true } );
1246
-
1247
- }
1248
-
1249
- const width = Math.min( texture.image.width, maxTextureSize );
1250
- const height = Math.min( texture.image.height, maxTextureSize );
1251
-
1252
- // dispose render target if the size is wrong
1253
- if ( renderTarget && ( renderTarget.width !== width || renderTarget.height !== height ) ) {
1254
-
1255
- renderTarget.dispose();
1256
- renderTarget = null;
1257
-
1258
- }
1259
-
1260
- if ( ! renderTarget ) {
1261
-
1262
- renderTarget = new WebGLRenderTarget( width, height, { format: RGBAFormat, type: UnsignedByteType, minFilter: LinearFilter, magFilter: LinearFilter } );
1263
- }
1264
-
1265
- renderer.setRenderTarget( renderTarget );
1266
- renderer.setSize( width, height );
1267
- renderer.clear();
1268
- renderer.render( _scene, _camera );
1269
-
1270
- if ( _renderer ) {
1271
-
1272
- _renderer.dispose();
1273
- _renderer = null;
1274
-
1275
- }
1276
-
1277
- const buffer = new Uint8ClampedArray( renderTarget.width * renderTarget.height * 4 );
1278
- renderer.readRenderTargetPixels( renderTarget, 0, 0, renderTarget.width, renderTarget.height, buffer );
1279
- const imageData = new ImageData( buffer, renderTarget.width, renderTarget.height, undefined );
1280
- const bmp = await createImageBitmap( imageData, { premultiplyAlpha: "none" } );
1281
- return {
1282
- imageData,
1283
- imageBitmap: bmp
1284
- };
1285
-
1286
- }
1287
-
1288
- /** Checks if the given image is of a type with readable data and width/height */
1289
- function isImageBitmap( image ) {
1290
-
1291
- return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
1292
- ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
1293
- ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
1294
- ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap );
1295
-
1296
- }
1297
-
1298
- /** This method uses a 'bitmaprenderer' context and doesn't do any pixel manipulation.
1299
- * This way, we can keep the alpha channel as it was, but we're losing the ability to do pixel manipulations or resize operations. */
1300
- async function imageToCanvasUnpremultiplied( image: ImageBitmapSource & { width: number, height: number }, maxTextureSize = 4096) {
1301
-
1302
- const scale = maxTextureSize / Math.max( image.width, image.height );
1303
- const width = image.width * Math.min( 1, scale );
1304
- const height = image.height * Math.min( 1, scale );
1305
-
1306
- const canvas = new OffscreenCanvas( width, height );
1307
- const settings: ImageBitmapOptions = { premultiplyAlpha: "none" };
1308
- if (image.width !== width) settings.resizeWidth = width;
1309
- if (image.height !== height) settings.resizeHeight = height;
1310
-
1311
- const imageBitmap = await createImageBitmap(image, settings);
1312
- const ctx = canvas.getContext("bitmaprenderer") as ImageBitmapRenderingContext | null;
1313
- if (ctx) {
1314
- ctx.transferFromImageBitmap(imageBitmap);
1315
- }
1316
- return canvas as OffscreenCanvasExt;
1317
- }
1318
-
1319
- /** This method uses a '2d' canvas context for pixel manipulation, and can apply a color scale or Y flip to the given image.
1320
- * Unfortunately, canvas always uses premultiplied data, and thus images with low alpha values (or multiplying by a=0) will result in black pixels.
1321
- */
1322
- async function imageToCanvas( image: HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap, color: Vector4 | undefined = undefined, flipY = false, maxTextureSize = 4096 ) {
1323
-
1324
- if ( isImageBitmap( image ) ) {
1325
-
1326
- // max. canvas size on Safari is still 4096x4096
1327
- const scale = maxTextureSize / Math.max( image.width, image.height );
1328
-
1329
- const canvas = new OffscreenCanvas( image.width * Math.min( 1, scale ), image.height * Math.min( 1, scale ) );
1330
-
1331
- const context = canvas.getContext( '2d', { alpha: true, premultipliedAlpha: false } ) as OffscreenCanvasRenderingContext2D;
1332
- if (!context) throw new Error('Could not get canvas 2D context');
1333
-
1334
- if ( flipY === true ) {
1335
-
1336
- context.translate( 0, canvas.height );
1337
- context.scale( 1, - 1 );
1338
-
1339
- }
1340
-
1341
- context.drawImage( image, 0, 0, canvas.width, canvas.height );
1342
-
1343
- // Currently only used to apply opacity scale since QuickLook and usdview don't support that yet
1344
- if ( color !== undefined ) {
1345
-
1346
- const r = color.x;
1347
- const g = color.y;
1348
- const b = color.z;
1349
- const a = color.w;
1350
-
1351
- const imagedata = context.getImageData( 0, 0, canvas.width, canvas.height );
1352
- const data = imagedata.data;
1353
-
1354
- for ( let i = 0; i < data.length; i += 4 ) {
1355
-
1356
- data[ i + 0 ] = data[ i + 0 ] * r;
1357
- data[ i + 1 ] = data[ i + 1 ] * g;
1358
- data[ i + 2 ] = data[ i + 2 ] * b;
1359
- data[ i + 3 ] = data[ i + 3 ] * a;
1360
-
1361
- }
1362
-
1363
- context.putImageData( imagedata, 0, 0 );
1364
-
1365
- }
1366
-
1367
- return canvas as OffscreenCanvasExt;
1368
-
1369
- } else {
1370
-
1371
- throw new Error( 'NeedleUSDZExporter: No valid image data found. Unable to process texture.' );
1372
-
1373
- }
1374
-
1375
- }
1376
-
1377
- //
1378
-
1379
- const PRECISION = 7;
1380
-
1381
- function buildHeader() {
1382
-
1383
- return `#usda 1.0
1384
- (
1385
- customLayerData = {
1386
- string creator = "Needle Engine USDZExporter"
1387
- }
1388
- metersPerUnit = 1
1389
- upAxis = "Y"
1390
- )
1391
- `;
1392
-
1393
- }
1394
-
1395
- function buildUSDFileAsString( dataToInsert, _context: USDZExporterContext ) {
1396
-
1397
- let output = buildHeader();
1398
- output += dataToInsert;
1399
- return fflate.strToU8( output );
1400
-
1401
- }
1402
-
1403
- function getObjectId( object ) {
1404
-
1405
- return object.name.replace( /[-<>\(\)\[\]§$%&\/\\\=\?\,\;]/g, '' ) + '_' + object.id;
1406
-
1407
- }
1408
-
1409
- function getBoneName(bone: Object3D) {
1410
- return makeNameSafe(bone.name || 'bone_' + bone.uuid);
1411
- }
1412
-
1413
- function getGeometryName(geometry: BufferGeometry, _fallbackName: string) {
1414
- // Using object names here breaks instancing...
1415
- // So we removed fallbackName again. Downside: geometries don't have nice names...
1416
- // A better workaround would be that we actually name them on glTF import (so the geometry has a name, not the object)
1417
- return makeNameSafe(geometry.name || 'Geometry') + "_" + geometry.id;
1418
- }
1419
-
1420
- function getMaterialName(material: Material) {
1421
- return makeNameSafe(material.name || 'Material') + "_" + material.id;
1422
- }
1423
-
1424
- function getPathToSkeleton(bone: Object3D, assumedRoot: Object3D) {
1425
- let path = getBoneName(bone);
1426
- let current = bone.parent;
1427
- while ( current && current !== assumedRoot ) {
1428
- path = getBoneName(current) + '/' + path;
1429
- current = current.parent;
1430
- }
1431
- return path;
1432
- }
1433
-
1434
- // Xform
1435
-
1436
- export function buildXform( model: USDObject | null, writer: USDWriter, context: USDZExporterContext ) {
1437
-
1438
- if ( model == null)
1439
- return;
1440
-
1441
- Progress.report("export-usdz-xforms", { message: "buildXform " + model.displayName || model.name, autoStep: true });
1442
-
1443
- // const matrix = model.matrix;
1444
- const transform = model.transform;
1445
- const geometry = model.geometry;
1446
- const material = model.material;
1447
- const camera = model.camera;
1448
- const name = model.name;
1449
-
1450
- if ( model.animations ) {
1451
- for ( const animation of model.animations ) {
1452
- context.animations.push( animation )
1453
- }
1454
- }
1455
-
1456
- // const transform = buildMatrix( matrix );
1457
-
1458
- /*
1459
- if ( matrix.determinant() < 0 ) {
1460
-
1461
- console.warn( 'NeedleUSDZExporter: USDZ does not support negative scales', name );
1462
-
1463
- }
1464
- */
1465
-
1466
- const isSkinnedMesh = geometry && geometry.isBufferGeometry && geometry.attributes.skinIndex !== undefined && geometry.attributes.skinIndex.count > 0;
1467
- const objType = isSkinnedMesh ? 'SkelRoot' : 'Xform';
1468
- const _apiSchemas = new Array<string>();
1469
-
1470
- // Specific case: the material is white unlit, the mesh has vertex colors, so we can
1471
- // export as displayColor directly
1472
- const isUnlitDisplayColor =
1473
- material && material instanceof MeshBasicMaterial &&
1474
- material.color && material.color.r === 1 && material.color.g === 1 && material.color.b === 1 &&
1475
- !material.map && material.opacity === 1 &&
1476
- geometry?.attributes.color;
1477
-
1478
- if (geometry?.attributes.color && !isUnlitDisplayColor) {
1479
- console.warn("NeedleUSDZExporter: Geometry has vertex colors. Vertex colors will only be shown in QuickLook for unlit materials with white color and no texture. Otherwise, they will be ignored.", model.displayName);
1480
- }
1481
-
1482
- writer.appendLine();
1483
- if ( geometry ) {
1484
- writer.beginBlock( `def ${objType} "${name}"`, "(", false );
1485
- // NE-4084: To use the doubleSided workaround with skeletal meshes we'd have to
1486
- // also emit extra data for jointIndices etc., so we're skipping skinned meshes here.
1487
- if (context.quickLookCompatible && material && material.side === DoubleSide && !isSkinnedMesh)
1488
- writer.appendLine(`prepend references = @./geometries/${getGeometryName(geometry, name)}.usda@</Geometry_doubleSided>`);
1489
- else
1490
- writer.appendLine(`prepend references = @./geometries/${getGeometryName(geometry, name)}.usda@</Geometry>`);
1491
- if (!isUnlitDisplayColor)
1492
- _apiSchemas.push("MaterialBindingAPI");
1493
- if (isSkinnedMesh)
1494
- _apiSchemas.push("SkelBindingAPI");
1495
- }
1496
- else if ( camera && !context.quickLookCompatible)
1497
- writer.beginBlock( `def Camera "${name}"`, "(", false );
1498
- else if ( model.type !== undefined)
1499
- writer.beginBlock( `def ${model.type} "${name}"` );
1500
- else // if (model.type === undefined)
1501
- writer.beginBlock( `def Xform "${name}"`, "(", false);
1502
-
1503
- if (model.type === undefined) {
1504
- if (model.extraSchemas?.length)
1505
- _apiSchemas.push(...model.extraSchemas);
1506
- if (_apiSchemas.length)
1507
- writer.appendLine(`prepend apiSchemas = [${_apiSchemas.map(s => `"${s}"`).join(', ')}]`);
1508
- }
1509
-
1510
- if (model.displayName)
1511
- writer.appendLine(`displayName = "${makeDisplayNameSafe(model.displayName)}"`);
1512
-
1513
- if ( camera || model.type === undefined) {
1514
- writer.closeBlock( ")" );
1515
- writer.beginBlock();
1516
- }
1517
-
1518
- if ( geometry && material ) {
1519
- if (!isUnlitDisplayColor) {
1520
- const materialName = getMaterialName(material);
1521
- writer.appendLine( `rel material:binding = </StageRoot/Materials/${materialName}>` );
1522
- }
1523
-
1524
- // Turns out QuickLook / RealityKit doesn't support the doubleSided attribute, so we
1525
- // work around that by emitting additional indices above, and then we shouldn't emit the attribute either as geometry is
1526
- // already doubleSided then.
1527
- if (!context.quickLookCompatible && material.side === DoubleSide ) {
1528
- // double-sided is a mesh property in USD, we can apply it as `over` here
1529
- writer.beginBlock( `over "Geometry" `);
1530
- writer.appendLine( `uniform bool doubleSided = 1` );
1531
- writer.closeBlock();
1532
- }
1533
- }
1534
- let haveWrittenAnyXformOps = false;
1535
- if ( isSkinnedMesh ) {
1536
- writer.appendLine( `rel skel:skeleton = <Rig>` );
1537
- writer.appendLine( `rel skel:animationSource = <Rig/_anim>`);
1538
- haveWrittenAnyXformOps = false;
1539
- // writer.appendLine( `matrix4d xformOp:transform = ${buildMatrix(new Matrix4())}` ); // always identity / in world space
1540
- }
1541
- else if (model.type === undefined) {
1542
- if (transform) {
1543
- haveWrittenAnyXformOps = haveWrittenAnyXformOps || (transform.position !== null || transform.quaternion !== null || transform.scale !== null);
1544
- if (transform.position) {
1545
- model.needsTranslate = true;
1546
- writer.appendLine( `double3 xformOp:translate = (${fn(transform.position.x)}, ${fn(transform.position.y)}, ${fn(transform.position.z)})` );
1547
- }
1548
- if (transform.quaternion) {
1549
- model.needsOrient = true;
1550
- writer.appendLine( `quatf xformOp:orient = (${fn(transform.quaternion.w)}, ${fn(transform.quaternion.x)}, ${fn(transform.quaternion.y)}, ${fn(transform.quaternion.z)})` );
1551
- }
1552
- if (transform.scale) {
1553
- model.needsScale = true;
1554
- writer.appendLine( `double3 xformOp:scale = (${fn(transform.scale.x)}, ${fn(transform.scale.y)}, ${fn(transform.scale.z)})` );
1555
- }
1556
- }
1557
- }
1558
-
1559
- if (model.visibility !== undefined)
1560
- writer.appendLine(`token visibility = "${model.visibility}"`);
1561
-
1562
- if ( camera && !context.quickLookCompatible) {
1563
-
1564
- if ( 'isOrthographicCamera' in camera && camera.isOrthographicCamera ) {
1565
-
1566
- writer.appendLine( `float2 clippingRange = (${camera.near}, ${camera.far})` );
1567
- writer.appendLine( `float horizontalAperture = ${( ( Math.abs( camera.left ) + Math.abs( camera.right ) ) * 10 ).toPrecision( PRECISION )}` );
1568
- writer.appendLine( `float verticalAperture = ${( ( Math.abs( camera.top ) + Math.abs( camera.bottom ) ) * 10 ).toPrecision( PRECISION )}` );
1569
- writer.appendLine( 'token projection = "orthographic"' );
1570
-
1571
- } else if ( 'isPerspectiveCamera' in camera && camera.isPerspectiveCamera) {
1572
-
1573
- writer.appendLine( `float2 clippingRange = (${camera.near.toPrecision( PRECISION )}, ${camera.far.toPrecision( PRECISION )})` );
1574
- writer.appendLine( `float focalLength = ${camera.getFocalLength().toPrecision( PRECISION )}` );
1575
- writer.appendLine( `float focusDistance = ${camera.focus.toPrecision( PRECISION )}` );
1576
- writer.appendLine( `float horizontalAperture = ${camera.getFilmWidth().toPrecision( PRECISION )}` );
1577
- writer.appendLine( 'token projection = "perspective"' );
1578
- writer.appendLine( `float verticalAperture = ${camera.getFilmHeight().toPrecision( PRECISION )}` );
1579
- }
1580
-
1581
- }
1582
-
1583
- if ( model.onSerialize ) {
1584
-
1585
- model.onSerialize( writer, context );
1586
-
1587
- }
1588
-
1589
- // after serialization, we know which xformops to actually define here:
1590
- if (model.type === undefined) {
1591
- // TODO only write the necessary ones – this isn't trivial though because we need to know
1592
- // if some of them are animated, and then we need to include those.
1593
- // Best approach would likely be to write xformOpOrder _after_ onSerialize
1594
- // and keep track of what was written in onSerialize (e.g. model.needsTranslate = true)
1595
- const ops = new Array<string>();
1596
- if (model.needsTranslate) ops.push('"xformOp:translate"');
1597
- if (model.needsOrient) ops.push('"xformOp:orient"');
1598
- if (model.needsScale) ops.push('"xformOp:scale"');
1599
- if (ops.length)
1600
- writer.appendLine( `uniform token[] xformOpOrder = [${ops.join(', ')}]` );
1601
- }
1602
-
1603
- if ( model.children ) {
1604
-
1605
- writer.appendLine();
1606
- for ( const ch of model.children ) {
1607
-
1608
- buildXform( ch, writer, context );
1609
-
1610
- }
1611
-
1612
- }
1613
-
1614
- writer.closeBlock();
1615
-
1616
- }
1617
-
1618
- function fn( num:number ): string {
1619
-
1620
- return Number.isInteger(num) ? num.toString() : num.toFixed( 10 );
1621
-
1622
- }
1623
-
1624
- function buildMatrix( matrix ) {
1625
-
1626
- const array = matrix.elements;
1627
-
1628
- return `( ${buildMatrixRow( array, 0 )}, ${buildMatrixRow( array, 4 )}, ${buildMatrixRow( array, 8 )}, ${buildMatrixRow( array, 12 )} )`;
1629
-
1630
- }
1631
-
1632
- function buildMatrixRow( array, offset ) {
1633
-
1634
- return `(${fn( array[ offset + 0 ] )}, ${fn( array[ offset + 1 ] )}, ${fn( array[ offset + 2 ] )}, ${fn( array[ offset + 3 ] )})`;
1635
-
1636
- }
1637
-
1638
- // Mesh
1639
-
1640
- function buildMeshObject( geometry: BufferGeometry, bonesArray: Bone[] = [], quickLookCompatible: boolean = true) {
1641
-
1642
- const mesh = buildMesh( geometry, bonesArray, quickLookCompatible );
1643
- return `
1644
- def "Geometry"
1645
- ${mesh}
1646
- `;
1647
-
1648
- }
1649
-
1650
- function buildMesh( geometry: BufferGeometry, bones: Bone[] = [], quickLookCompatible: boolean = true) {
1651
-
1652
- const name = 'Geometry';
1653
- const attributes = geometry.attributes;
1654
- const count = attributes.position.count;
1655
-
1656
- const hasBones = bones && bones.length > 0;
1657
-
1658
- // We need to sort bones and all skinning data by path –
1659
- // Neither glTF nor three.js care, but in USD they must be sorted
1660
- // since the array defines the virtual hierarchy and is evaluated in that order
1661
- const sortedBones: Array<{bone: Object3D, index: number}> = [];
1662
- const indexMapping: number[] = [];
1663
- let sortedSkinIndex = new Array<number>();
1664
- let sortedSkinIndexAttribute: BufferAttribute | InterleavedBufferAttribute | null = attributes.skinIndex;
1665
- let bonesArray = "";
1666
- if (hasBones) {
1667
- const uuidsFound:string[] = [];
1668
- for (const bone of bones ) {
1669
- // if (bone.parent!.type !== 'Bone')
1670
- {
1671
- sortedBones.push({bone: bone, index: bones.indexOf(bone)});
1672
- uuidsFound.push(bone.uuid);
1673
- }
1674
- }
1675
-
1676
- let maxSteps = 10_000;
1677
- while (uuidsFound.length < bones.length && maxSteps-- > 0) {
1678
- for (const sortedBone of sortedBones) {
1679
- const children = sortedBone.bone.children as Bone[];
1680
- for (const childBone of children){
1681
- if (uuidsFound.indexOf(childBone.uuid) === -1 && bones.indexOf(childBone) !== -1){
1682
- sortedBones.push({bone: childBone, index: bones.indexOf(childBone)});
1683
- uuidsFound.push(childBone.uuid);
1684
- }
1685
- }
1686
- }
1687
- }
1688
-
1689
- if (maxSteps <= 0) console.error("Failed to sort bones in skinned mesh", sortedBones, bones, uuidsFound);
1690
-
1691
- // add structural nodes to the list of bones
1692
- for (const structuralNode of findStructuralNodesInBoneHierarchy(bones) ) {
1693
- sortedBones.push( { bone: structuralNode, index: sortedBones.length } );
1694
- }
1695
-
1696
- // sort bones by path – need to be sorted in the same order as during mesh export
1697
- const assumedRoot = sortedBones[0].bone.parent!;
1698
- sortedBones.sort((a, b) => getPathToSkeleton(a.bone, assumedRoot) > getPathToSkeleton(b.bone, assumedRoot) ? 1 : -1);
1699
- bonesArray = sortedBones.map( x => "\"" + getPathToSkeleton(x.bone, assumedRoot) + "\"" ).join( ', ' );
1700
-
1701
- // TODO we can probably skip the expensive attribute re-ordering if the bones were already in a correct order.
1702
- // That doesn't mean that they are strictly sorted by path – just that all parents strictly need to come first.
1703
-
1704
- // build index mapping
1705
- for (const i in sortedBones) {
1706
- indexMapping[sortedBones[i].index] = parseInt(i);
1707
- }
1708
-
1709
- // remap skin index attributes
1710
- const skinIndex = attributes.skinIndex;
1711
- sortedSkinIndex = new Array<number>();
1712
- for ( let i = 0; i < skinIndex.count; i ++ ) {
1713
-
1714
- const x = skinIndex.getX( i );
1715
- const y = skinIndex.getY( i );
1716
- const z = skinIndex.getZ( i );
1717
- const w = skinIndex.getW( i );
1718
-
1719
- sortedSkinIndex.push( indexMapping[x], indexMapping[y], indexMapping[z], indexMapping[w] );
1720
- }
1721
-
1722
- // turn it back into an attribute so the rest of the code doesn't need to learn a new thing
1723
- sortedSkinIndexAttribute = new BufferAttribute( new Uint16Array( sortedSkinIndex ), 4 );
1724
- }
1725
-
1726
- const isSkinnedMesh = attributes.skinWeight && attributes.skinIndex;
1727
-
1728
- return `
1729
- {
1730
- def Mesh "${name}" ${isSkinnedMesh ? `(
1731
- prepend apiSchemas = ["SkelBindingAPI"]
1732
- )` : ''}
1733
- {
1734
- int[] faceVertexCounts = [${buildMeshVertexCount( geometry )}]
1735
- int[] faceVertexIndices = [${buildMeshVertexIndices( geometry )}]
1736
- ${attributes.normal || quickLookCompatible ? // in QuickLook, normals are required, otherwise double-sided rendering doesn't work.
1737
- `normal3f[] normals = [${buildVector3Array( attributes.normal, count )}] (
1738
- interpolation = "vertex"
1739
- )` : '' }
1740
- point3f[] points = [${buildVector3Array( attributes.position, count )}]
1741
- ${attributes.uv ?
1742
- `texCoord2f[] primvars:st = [${buildVector2Array( attributes.uv, count, true )}] (
1743
- interpolation = "vertex"
1744
- )` : '' }
1745
- ${attributes.uv1 ? buildCustomAttributeAccessor('st1', attributes.uv1) : '' }
1746
- ${attributes.uv2 ? buildCustomAttributeAccessor('st2', attributes.uv2) : '' }
1747
- ${attributes.uv3 ? buildCustomAttributeAccessor('st3', attributes.uv3) : '' }
1748
- ${isSkinnedMesh ?
1749
- `matrix4d primvars:skel:geomBindTransform = ( (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1) ) (
1750
- elementSize = 1
1751
- interpolation = "constant"
1752
- )` : '' }
1753
- ${attributes.skinIndex ?
1754
- `int[] primvars:skel:jointIndices = [${buildVector4Array( sortedSkinIndexAttribute, true )}] (
1755
- elementSize = 4
1756
- interpolation = "vertex"
1757
- )` : '' }
1758
- ${attributes.skinWeight ?
1759
- `float[] primvars:skel:jointWeights = [${buildVector4Array( attributes.skinWeight )}] (
1760
- elementSize = 4
1761
- interpolation = "vertex"
1762
- )` : '' }
1763
- ${attributes.color ?
1764
- `color3f[] primvars:displayColor = [${buildVector3Array( attributes.color, count )}] (
1765
- interpolation = "vertex"
1766
- )` : '' }
1767
- uniform token subdivisionScheme = "none"
1768
- }
1769
- }
1770
- ${quickLookCompatible ? `
1771
- # This is a workaround for QuickLook/RealityKit not supporting the doubleSided attribute. We're adding a second
1772
- # geometry definition here, that uses the same mesh data but appends extra faces with reversed winding order.
1773
- def "${name}_doubleSided" (
1774
- prepend references = </Geometry>
1775
- )
1776
- {
1777
- over "Geometry"
1778
- {
1779
- int[] faceVertexCounts = [${buildMeshVertexCount( geometry ) + ", " + buildMeshVertexCount( geometry )}]
1780
- int[] faceVertexIndices = [${buildMeshVertexIndices( geometry ) + ", " + buildMeshVertexIndices( geometry, true )}]
1781
- }
1782
- }
1783
- ` : '' }
1784
- `;
1785
- }
1786
-
1787
- function buildMeshVertexCount( geometry: BufferGeometry ) {
1788
-
1789
- const count = geometry.index !== null ? geometry.index.count : geometry.attributes.position.count;
1790
-
1791
- return Array( Math.floor(count / 3) ).fill( 3 ).join( ', ' );
1792
-
1793
- }
1794
-
1795
- function buildMeshVertexIndices( geometry: BufferGeometry, reverseWinding: boolean = false ) {
1796
-
1797
- const index = geometry.index;
1798
- const array: Array<number> = [];
1799
-
1800
- if ( index !== null ) {
1801
-
1802
- for ( let i = 0; i < index.count; i ++ ) {
1803
-
1804
- let val = i;
1805
- if ( reverseWinding )
1806
- val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1807
- array.push( index.getX( val ) );
1808
-
1809
- }
1810
-
1811
- } else {
1812
-
1813
- const length = geometry.attributes.position.count;
1814
-
1815
- for ( let i = 0; i < length; i ++ ) {
1816
-
1817
- let val = i;
1818
- if ( reverseWinding )
1819
- val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1820
- array.push( val );
1821
-
1822
- }
1823
-
1824
- }
1825
-
1826
- return array.join( ', ' );
1827
-
1828
- }
1829
-
1830
- /** Returns a string with the correct attribute declaration for the given attribute. Could have 2,3,4 components. */
1831
- function buildCustomAttributeAccessor( primvarName: string, attribute: BufferAttribute | InterleavedBufferAttribute ) {
1832
- const count = attribute.itemSize;
1833
- switch (count) {
1834
- case 2:
1835
- // TODO: Check if we want to flip Y here as well. We do that for texcoords, but the data in UV1..N could be intended for other purposes.
1836
- return `texCoord2f[] primvars:${primvarName} = [${buildVector2Array( attribute, count, true )}] (
1837
- interpolation = "vertex"
1838
- )`;
1839
- case 3:
1840
- return `texCoord3f[] primvars:${primvarName} = [${buildVector3Array( attribute, count )}] (
1841
- interpolation = "vertex"
1842
- )`;
1843
- case 4:
1844
- return `double4[] primvars:${primvarName} = [${buildVector4Array2( attribute, count )}] (
1845
- interpolation = "vertex"
1846
- )`;
1847
- default:
1848
- console.warn('USDZExporter: Attribute with ' + count + ' components are currently not supported. Results may be undefined for ' + primvarName + '.');
1849
- return '';
1850
- }
1851
- }
1852
-
1853
- function buildVector3Array( attribute, count ) {
1854
-
1855
- if ( attribute === undefined ) {
1856
-
1857
- console.warn( 'USDZExporter: A mesh attribute is missing and will be set with placeholder data. The result may look incorrect.' );
1858
- return Array( count ).fill( '(0, 0, 1)' ).join( ', ' );
1859
-
1860
- }
1861
-
1862
- const array: Array<string> = [];
1863
-
1864
- for ( let i = 0; i < attribute.count; i ++ ) {
1865
-
1866
- const x = attribute.getX( i );
1867
- const y = attribute.getY( i );
1868
- const z = attribute.getZ( i );
1869
-
1870
- array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )}, ${z.toPrecision( PRECISION )})` );
1871
-
1872
- }
1873
-
1874
- return array.join( ', ' );
1875
-
1876
- }
1877
-
1878
-
1879
- function buildVector4Array2( attribute, count ) {
1880
-
1881
- if ( attribute === undefined ) {
1882
-
1883
- console.warn( 'USDZExporter: Attribute is missing. Results may be undefined.' );
1884
- return Array( count ).fill( '(0, 0, 0, 0)' ).join( ', ' );
1885
-
1886
- }
1887
-
1888
- const array: Array<string> = [];
1889
-
1890
- for ( let i = 0; i < attribute.count; i ++ ) {
1891
-
1892
- const x = attribute.getX( i );
1893
- const y = attribute.getY( i );
1894
- const z = attribute.getZ( i ) || 0;
1895
- const w = attribute.getW( i ) || 0;
1896
-
1897
- array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )}, ${z.toPrecision( PRECISION )}, ${w.toPrecision( PRECISION )})` );
1898
-
1899
- }
1900
-
1901
- return array.join( ', ' );
1902
-
1903
- }
1904
-
1905
- function buildVector4Array( attribute, ints = false ) {
1906
- const array: Array<string> = [];
1907
-
1908
- for ( let i = 0; i < attribute.count; i ++ ) {
1909
-
1910
- const x = attribute.getX( i );
1911
- const y = attribute.getY( i );
1912
- const z = attribute.getZ( i );
1913
- const w = attribute.getW( i );
1914
-
1915
- array.push( `${ints ? x : x.toPrecision( PRECISION )}` );
1916
- array.push( `${ints ? y : y.toPrecision( PRECISION )}` );
1917
- array.push( `${ints ? z : z.toPrecision( PRECISION )}` );
1918
- array.push( `${ints ? w : w.toPrecision( PRECISION )}` );
1919
-
1920
- }
1921
-
1922
- return array.join( ', ' );
1923
-
1924
- }
1925
-
1926
- function buildVector2Array( attribute: BufferAttribute | InterleavedBufferAttribute, count: number, flipY: boolean = false ) {
1927
-
1928
- if ( attribute === undefined ) {
1929
-
1930
- console.warn( 'USDZExporter: UVs missing.' );
1931
- return Array( count ).fill( '(0, 0)' ).join( ', ' );
1932
-
1933
- }
1934
-
1935
- const array: Array<string> = [];
1936
-
1937
- for ( let i = 0; i < attribute.count; i ++ ) {
1938
-
1939
- const x = attribute.getX( i );
1940
- let y = attribute.getY( i );
1941
- if (flipY)
1942
- y = 1 - y;
1943
- array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )})` );
1944
-
1945
- }
1946
-
1947
- return array.join( ', ' );
1948
-
1949
- }
1950
-
1951
- // Materials
1952
-
1953
- function buildMaterials( materials: Map<string, Material>, textures: TextureMap, quickLookCompatible = false ) {
1954
-
1955
- const array: Array<string> = [];
1956
-
1957
- for ( const uuid in materials ) {
1958
-
1959
- const material = materials[ uuid ];
1960
-
1961
- array.push( buildMaterial( material, textures, quickLookCompatible ) );
1962
-
1963
- }
1964
-
1965
- return `
1966
- def "Materials"
1967
- {
1968
- ${array.join( '' )}
1969
- }`;
1970
- }
1971
-
1972
- /** Slot of the exported texture. Some slots (e.g. normal, opacity) require additional processing. */
1973
- declare type MapType = 'diffuse' | 'normal' | 'occlusion' | 'opacity' | 'roughness' | 'emissive' | 'metallic' | 'transmission';
1974
-
1975
- function texName(tex: Texture) {
1976
- // If we have a source, we only want to use the source's id, not the texture's id
1977
- // to avoid exporting the same underlying data multiple times.
1978
- return makeNameSafe(tex.name) + '_' + (tex.source?.id ?? tex.id);
1979
- }
1980
-
1981
- function buildTexture( texture: Texture, mapType: MapType, quickLookCompatible: boolean, textures: TextureMap, material, usedUVChannels, color: Color | undefined = undefined, opacity: number | undefined = undefined ) {
1982
-
1983
- const name = texName(texture);
1984
- // File names need to be unique; so when we're baking color and/or opacity into a texture we need to keep track of that.
1985
- // TODO This currently results in textures potentially being exported multiple times that are actually identical!
1986
- // const colorHex = color ? color.getHexString() : undefined;
1987
- // const colorId = ( color && colorHex != 'ffffff' ? '_' + color.getHexString() : '' );
1988
- const id = name + ( opacity !== undefined && opacity !== 1.0 ? '_' + opacity : '' );
1989
-
1990
- // Seems neither QuickLook nor usdview support scale/bias on .a values, so we need to bake opacity multipliers into
1991
- // the texture. This is not ideal.
1992
- const opacityIsAppliedToTextureAndNotAsScale = quickLookCompatible && opacity !== undefined && opacity !== 1.0;
1993
- const scaleToApply = opacityIsAppliedToTextureAndNotAsScale ? new Vector4(1, 1, 1, opacity) : undefined;
1994
-
1995
- // Treat undefined opacity as 1
1996
- if (opacity === undefined)
1997
- opacity = 1.0;
1998
-
1999
- // When we're baking opacity into the texture, we shouldn't apply it as scale as well – so we set it to 1.
2000
- if (opacityIsAppliedToTextureAndNotAsScale)
2001
- opacity = 1.0;
2002
-
2003
- // sanitize scaleToApply.w === 0 - this isn't working right now, seems the PNG can't be read back properly.
2004
- if (scaleToApply && scaleToApply.w <= 0.05) scaleToApply.w = 0.05;
2005
-
2006
- textures[ id ] = { texture, scale: scaleToApply };
2007
-
2008
- const uv = texture.channel > 0 ? 'st' + texture.channel : 'st';
2009
- usedUVChannels.add(texture.channel);
2010
-
2011
- const isRGBA = formatsWithAlphaChannel.includes( texture.format );
2012
-
2013
- const WRAPPINGS = {
2014
- 1000: 'repeat', // RepeatWrapping
2015
- 1001: 'clamp', // ClampToEdgeWrapping
2016
- 1002: 'mirror' // MirroredRepeatWrapping
2017
- };
2018
-
2019
- const repeat = texture.repeat.clone();
2020
- const offset = texture.offset.clone();
2021
- const rotation = texture.rotation;
2022
-
2023
- // rotation is around the wrong point. after rotation we need to shift offset again so that we're rotating around the right spot
2024
- const xRotationOffset = Math.sin(rotation);
2025
- const yRotationOffset = Math.cos(rotation);
2026
-
2027
- // texture coordinates start in the opposite corner, need to correct
2028
- offset.y = 1 - offset.y - repeat.y;
2029
-
2030
- // turns out QuickLook is buggy and interprets texture repeat inverted.
2031
- // Apple Feedback: FB10036297 and FB11442287
2032
- if ( quickLookCompatible ) {
2033
-
2034
- // This is NOT correct yet in QuickLook, but comes close for a range of models.
2035
- // It becomes more incorrect the bigger the offset is
2036
-
2037
- // sanitize repeat values to avoid NaNs
2038
- if (repeat.x === 0) repeat.x = 0.0001;
2039
- if (repeat.y === 0) repeat.y = 0.0001;
2040
-
2041
- offset.x = offset.x / repeat.x;
2042
- offset.y = offset.y / repeat.y;
2043
-
2044
- offset.x += xRotationOffset / repeat.x;
2045
- offset.y += yRotationOffset - 1;
2046
- }
2047
-
2048
- else {
2049
-
2050
- // results match glTF results exactly. verified correct in usdview.
2051
- offset.x += xRotationOffset * repeat.x;
2052
- offset.y += (1 - yRotationOffset) * repeat.y;
2053
-
2054
- }
2055
-
2056
- const materialName = getMaterialName(material);
2057
-
2058
- const needsTextureTransform = ( repeat.x != 1 || repeat.y != 1 || offset.x != 0 || offset.y != 0 || rotation != 0 );
2059
- const textureTransformInput = `${materialRoot}/${materialName}/${'uvReader_' + uv}.outputs:result>`; const textureTransformOutput = `${materialRoot}/${materialName}/Transform2d_${mapType}.outputs:result>`;
2060
- const needsTextureScale = mapType !== 'normal' && (color && (color.r !== 1 || color.g !== 1 || color.b !== 1 || opacity !== 1)) || false;
2061
-
2062
- const needsNormalScaleAndBias = mapType === 'normal';
2063
- const normalScale = material instanceof MeshStandardMaterial ? (material.normalScale ? material.normalScale.x * 2 : 2) : 2;
2064
- const normalScaleValueString = normalScale.toFixed( PRECISION );
2065
- const normalBiasString = (-1 * (normalScale / 2)).toFixed( PRECISION );
2066
- const normalBiasZString = (1 - normalScale).toFixed( PRECISION );
2067
-
2068
- return `
2069
- ${needsTextureTransform ? `def Shader "Transform2d_${mapType}" (
2070
- sdrMetadata = {
2071
- string role = "math"
2072
- }
2073
- )
2074
- {
2075
- uniform token info:id = "UsdTransform2d"
2076
- float2 inputs:in.connect = ${textureTransformInput}
2077
- float2 inputs:scale = ${buildVector2( repeat )}
2078
- float2 inputs:translation = ${buildVector2( offset )}
2079
- float inputs:rotation = ${(rotation / Math.PI * 180).toFixed( PRECISION )}
2080
- float2 outputs:result
2081
- }
2082
- ` : '' }
2083
- def Shader "${name}_${mapType}"
2084
- {
2085
- uniform token info:id = "UsdUVTexture"
2086
- asset inputs:file = @textures/${id}.${isRGBA ? 'png' : 'jpg'}@
2087
- token inputs:sourceColorSpace = "${ texture.colorSpace === 'srgb' ? 'sRGB' : 'raw' }"
2088
- float2 inputs:st.connect = ${needsTextureTransform ? textureTransformOutput : textureTransformInput}
2089
- ${needsTextureScale ? `
2090
- float4 inputs:scale = (${color ? color.r + ', ' + color.g + ', ' + color.b : '1, 1, 1'}, ${opacity})
2091
- ` : `` }
2092
- ${needsNormalScaleAndBias ? `
2093
- float4 inputs:scale = (${normalScaleValueString}, ${normalScaleValueString}, ${normalScaleValueString}, 1)
2094
- float4 inputs:bias = (${normalBiasString}, ${normalBiasString}, ${normalBiasZString}, 0)
2095
- ` : `` }
2096
- token inputs:wrapS = "${ WRAPPINGS[ texture.wrapS ] }"
2097
- token inputs:wrapT = "${ WRAPPINGS[ texture.wrapT ] }"
2098
- float outputs:r
2099
- float outputs:g
2100
- float outputs:b
2101
- float3 outputs:rgb
2102
- ${material.transparent || material.alphaTest > 0.0 ? 'float outputs:a' : ''}
2103
- }`;
2104
- }
2105
-
2106
- function buildMaterial( material: MeshBasicMaterial, textures: TextureMap, quickLookCompatible = false ) {
2107
-
2108
- const materialName = getMaterialName(material);
2109
-
2110
- // Special case: occluder material
2111
- // Supported on iOS 18+ and visionOS 1+
2112
- const isShadowCatcherMaterial =
2113
- material.colorWrite === false ||
2114
- (material.userData?.isShadowCatcherMaterial || material.userData?.isLightBlendMaterial);
2115
- if (isShadowCatcherMaterial) {
2116
-
2117
- // Two options here:
2118
- // - ND_realitykit_occlusion_surfaceshader (non-shadow receiving)
2119
- // - ND_realitykit_shadowreceiver_surfaceshader (shadow receiving)
2120
-
2121
- const mode = (material.userData.isLightBlendMaterial || material.userData.isShadowCatcherMaterial)
2122
- ? "ND_realitykit_shadowreceiver_surfaceshader"
2123
- : "ND_realitykit_occlusion_surfaceshader";
2124
-
2125
- return `
2126
-
2127
- def Material "${materialName}" ${material.name ?`(
2128
- displayName = "${material.name}"
2129
- )` : ''}
2130
- {
2131
- token outputs:mtlx:surface.connect = ${materialRoot}/${materialName}/Occlusion.outputs:out>
2132
-
2133
- def Shader "Occlusion"
2134
- {
2135
- uniform token info:id = "${mode}"
2136
- token outputs:out
2137
- }
2138
- }`;
2139
- }
2140
-
2141
- // https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
2142
-
2143
- const pad = ' ';
2144
- const inputs: Array<string> = [];
2145
- const samplers: Array<string> = [];
2146
- const usedUVChannels: Set<number> = new Set();
2147
-
2148
- if ((material as any).isMeshPhysicalNodeMaterial === true) {
2149
- return buildNodeMaterial(material as unknown as MeshPhysicalNodeMaterial, materialName, textures);
2150
- }
2151
-
2152
- let effectiveOpacity = ( material.transparent || material.alphaTest ) ? material.opacity : 1;
2153
- let haveConnectedOpacity = false;
2154
- let haveConnectedOpacityThreshold = false;
2155
-
2156
- if ( material instanceof MeshPhysicalMaterial && material.transmission !== undefined) {
2157
-
2158
- // TODO does not help when a roughnessMap is used
2159
- effectiveOpacity *= (1 - material.transmission * (1 - (material.roughness * 0.5)));
2160
-
2161
- }
2162
-
2163
- if ( material.map ) {
2164
-
2165
- inputs.push( `${pad}color3f inputs:diffuseColor.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:rgb>` );
2166
-
2167
- // Enforce alpha hashing in QuickLook for unlit materials
2168
- if (material instanceof MeshBasicMaterial && material.transparent && material.alphaTest == 0.0 && quickLookCompatible) {
2169
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:a>` );
2170
- haveConnectedOpacity = true;
2171
- // see below – QuickLook applies alpha hashing instead of pure blending when
2172
- // both opacity and opacityThreshold are connected
2173
- inputs.push( `${pad}float inputs:opacityThreshold = ${0.0000000001}` );
2174
- haveConnectedOpacityThreshold = true;
2175
- }
2176
- else if ( material.transparent ) {
2177
-
2178
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:a>` );
2179
- haveConnectedOpacity = true;
2180
-
2181
- } else if ( material.alphaTest > 0.0 ) {
2182
-
2183
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:a>` );
2184
- haveConnectedOpacity = true;
2185
- inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
2186
- haveConnectedOpacityThreshold = true;
2187
-
2188
- }
2189
-
2190
- samplers.push( buildTexture( material.map, 'diffuse', quickLookCompatible, textures, material, usedUVChannels, material.color, effectiveOpacity ) );
2191
-
2192
- } else {
2193
-
2194
- inputs.push( `${pad}color3f inputs:diffuseColor = ${buildColor( material.color )}` );
2195
-
2196
- }
2197
-
2198
- if ( material.alphaHash && quickLookCompatible) {
2199
-
2200
- // Seems we can do this to basically enforce alpha hashing / dithered transparency in QuickLook –
2201
- // works completely different in usdview though...
2202
- if (haveConnectedOpacityThreshold) {
2203
- console.warn('Opacity threshold for ' + material.name + ' was already connected. Skipping alphaHash opacity threshold.');
2204
- }
2205
- else {
2206
- inputs.push( `${pad}float inputs:opacityThreshold = 0.0000000001` );
2207
- haveConnectedOpacityThreshold = true;
2208
- }
2209
- }
2210
-
2211
- if ( material.aoMap ) {
2212
-
2213
- inputs.push( `${pad}float inputs:occlusion.connect = ${materialRoot}/${materialName}/${texName(material.aoMap)}_occlusion.outputs:r>` );
2214
-
2215
- samplers.push( buildTexture( material.aoMap, 'occlusion', quickLookCompatible, textures, material, usedUVChannels ) );
2216
-
2217
- }
2218
-
2219
- if ( material.alphaMap ) {
2220
-
2221
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.alphaMap)}_opacity.outputs:r>` );
2222
- // TODO this is likely not correct and will prevent blending from working correctly – masking will be used instead
2223
- inputs.push( `${pad}float inputs:opacityThreshold = 0.0000000001` );
2224
- haveConnectedOpacity = true;
2225
- haveConnectedOpacityThreshold = true;
2226
-
2227
- samplers.push( buildTexture( material.alphaMap, 'opacity', quickLookCompatible, textures, material, usedUVChannels, new Color( 1, 1, 1 ), effectiveOpacity ) );
2228
-
2229
- } else { // opacity will always be connected if we have a map
2230
-
2231
- if (haveConnectedOpacity) {
2232
- console.warn('Opacity for ' + material.name + ' was already connected. Skipping default opacity.');
2233
- } else {
2234
- inputs.push( `${pad}float inputs:opacity = ${effectiveOpacity}` );
2235
- haveConnectedOpacity = true;
2236
- }
2237
-
2238
- if ( material.alphaTest > 0.0 ) {
2239
-
2240
- if (haveConnectedOpacityThreshold) {
2241
- console.warn('Opacity threshold for ' + material.name + ' was already connected. Skipping default opacity threshold.');
2242
- }
2243
- else {
2244
- inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
2245
- haveConnectedOpacityThreshold = true;
2246
- }
2247
- }
2248
-
2249
- }
2250
-
2251
- if ( material instanceof MeshStandardMaterial ) {
2252
-
2253
- if ( material.emissiveMap ) {
2254
-
2255
- inputs.push( `${pad}color3f inputs:emissiveColor.connect = ${materialRoot}/${materialName}/${texName(material.emissiveMap)}_emissive.outputs:rgb>` );
2256
- const color = material.emissive.clone();
2257
- color.multiplyScalar( material.emissiveIntensity );
2258
- samplers.push( buildTexture( material.emissiveMap, 'emissive', quickLookCompatible, textures, material, usedUVChannels, color ) );
2259
-
2260
- } else if ( material.emissive?.getHex() > 0 ) {
2261
-
2262
- const color = material.emissive.clone();
2263
- color.multiplyScalar( material.emissiveIntensity );
2264
- inputs.push( `${pad}color3f inputs:emissiveColor = ${buildColor( color )}` );
2265
-
2266
- } else {
2267
-
2268
- // We don't need to export (0,0,0) as emissive color
2269
- // inputs.push( `${pad}color3f inputs:emissiveColor = (0, 0, 0)` );
2270
-
2271
- }
2272
-
2273
- if ( material.normalMap ) {
2274
-
2275
- inputs.push( `${pad}normal3f inputs:normal.connect = ${materialRoot}/${materialName}/${texName(material.normalMap)}_normal.outputs:rgb>` );
2276
-
2277
- samplers.push( buildTexture( material.normalMap, 'normal', quickLookCompatible, textures, material, usedUVChannels ) );
2278
-
2279
- }
2280
-
2281
- if ( material.roughnessMap && material.roughness === 1 ) {
2282
-
2283
- inputs.push( `${pad}float inputs:roughness.connect = ${materialRoot}/${materialName}/${texName(material.roughnessMap)}_roughness.outputs:g>` );
2284
-
2285
- samplers.push( buildTexture( material.roughnessMap, 'roughness', quickLookCompatible, textures, material, usedUVChannels ) );
2286
-
2287
- } else {
2288
-
2289
- inputs.push( `${pad}float inputs:roughness = ${material.roughness !== undefined ? material.roughness : 1 }` );
2290
-
2291
- }
2292
-
2293
- if ( material.metalnessMap && material.metalness === 1 ) {
2294
-
2295
- inputs.push( `${pad}float inputs:metallic.connect = ${materialRoot}/${materialName}/${texName(material.metalnessMap)}_metallic.outputs:b>` );
2296
-
2297
- samplers.push( buildTexture( material.metalnessMap, 'metallic', quickLookCompatible, textures, material, usedUVChannels ) );
2298
-
2299
- } else {
2300
-
2301
- inputs.push( `${pad}float inputs:metallic = ${material.metalness !== undefined ? material.metalness : 0 }` );
2302
-
2303
- }
2304
-
2305
- }
2306
-
2307
- if ( material instanceof MeshPhysicalMaterial ) {
2308
-
2309
- inputs.push( `${pad}float inputs:clearcoat = ${material.clearcoat}` );
2310
- inputs.push( `${pad}float inputs:clearcoatRoughness = ${material.clearcoatRoughness}` );
2311
- inputs.push( `${pad}float inputs:ior = ${material.ior}` );
2312
-
2313
- if ( !material.transparent && ! (material.alphaTest > 0.0) && material.transmissionMap) {
2314
-
2315
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.transmissionMap)}_transmission.outputs:r>` );
2316
-
2317
- samplers.push( buildTexture( material.transmissionMap, 'transmission', quickLookCompatible, textures, material, usedUVChannels ) );
2318
- }
2319
-
2320
- }
2321
-
2322
- // check if usedUVChannels contains any data besides exactly 0 or 1
2323
- if (usedUVChannels.size > 2) {
2324
- console.warn('USDZExporter: Material ' + material.name + ' uses more than 2 UV channels. Currently, only UV0 and UV1 are supported.');
2325
- }
2326
- else if (usedUVChannels.size === 2) {
2327
- // check if it's exactly 0 and 1
2328
- if (!usedUVChannels.has(0) || !usedUVChannels.has(1)) {
2329
- console.warn('USDZExporter: Material ' + material.name + ' uses UV channels other than 0 and 1. Currently, only UV0 and UV1 are supported.');
2330
- }
2331
- }
2332
-
2333
- return `
2334
-
2335
- def Material "${materialName}" ${material.name ?`(
2336
- displayName = "${makeDisplayNameSafe(material.name)}"
2337
- )` : ''}
2338
- {
2339
- token outputs:surface.connect = ${materialRoot}/${materialName}/PreviewSurface.outputs:surface>
2340
-
2341
- def Shader "PreviewSurface"
2342
- {
2343
- uniform token info:id = "UsdPreviewSurface"
2344
- ${inputs.join( '\n' )}
2345
- int inputs:useSpecularWorkflow = ${material instanceof MeshBasicMaterial ? '1' : '0'}
2346
- token outputs:surface
2347
- }
2348
- ${samplers.length > 0 ? `
2349
- ${usedUVChannels.has(0) ? `
2350
- def Shader "uvReader_st"
2351
- {
2352
- uniform token info:id = "UsdPrimvarReader_float2"
2353
- token inputs:varname = "st"
2354
- float2 inputs:fallback = (0.0, 0.0)
2355
- float2 outputs:result
2356
- }
2357
- ` : ''}
2358
- ${usedUVChannels.has(1) ? `
2359
- def Shader "uvReader_st1"
2360
- {
2361
- uniform token info:id = "UsdPrimvarReader_float2"
2362
- token inputs:varname = "st1"
2363
- float2 inputs:fallback = (0.0, 0.0)
2364
- float2 outputs:result
2365
- }
2366
- ` : ''}
2367
- ${samplers.join( '\n' )}` : ''}
2368
- }`;
2369
- }
2370
-
2371
- function buildColor( color ) {
2372
-
2373
- return `(${color.r}, ${color.g}, ${color.b})`;
2374
-
2375
- }
2376
-
2377
- function buildVector2( vector ) {
2378
-
2379
- return `(${ vector.x }, ${ vector.y })`;
2380
-
2381
- }
2382
-
2383
- const formatsWithAlphaChannel = [
2384
- // uncompressed formats with alpha channel
2385
- 1023, // RGBAFormat
2386
- // compressed formats with alpha channel
2387
- 33777, // RGBA_S3TC_DXT1_Format
2388
- 33778, // RGBA_S3TC_DXT3_Format
2389
- 33779, // RGBA_S3TC_DXT5_Format
2390
- 35842, // RGBA_PVRTC_4BPPV1_Format
2391
- 35843, // RGBA_PVRTC_2BPPV1_Format
2392
- 37496, // RGBA_ETC2_EAC_Format
2393
- 37808, // RGBA_ASTC_4x4_Format
2394
- 37809, // RGBA_ASTC_5x4_Format
2395
- 37810, // RGBA_ASTC_5x5_Format
2396
- 37811, // RGBA_ASTC_6x5_Format
2397
- 37812, // RGBA_ASTC_6x6_Format
2398
- 37813, // RGBA_ASTC_8x5_Format
2399
- 37814, // RGBA_ASTC_8x6_Format
2400
- 37815, // RGBA_ASTC_8x8_Format
2401
- 37816, // RGBA_ASTC_10x5_Format
2402
- 37817, // RGBA_ASTC_10x6_Format
2403
- 37818, // RGBA_ASTC_10x8_Format
2404
- 37819, // RGBA_ASTC_10x10_Format
2405
- 37820, // RGBA_ASTC_12x10_Format
2406
- 37821, // RGBA_ASTC_12x12_Format
2407
- 36492, // RGBA_BPTC_Format
2408
- ];
2409
-
2410
- export {
2411
- buildMatrix,
2412
- decompressGpuTexture,
2413
- findStructuralNodesInBoneHierarchy,
2414
- formatsWithAlphaChannel,
2415
- getBoneName,
2416
- getMaterialName as getMaterialNameForUSD,
2417
- getPathToSkeleton,
2418
- imageToCanvas,
2419
- imageToCanvasUnpremultiplied,
2420
- makeNameSafe as makeNameSafeForUSD,
2421
- USDDocument,
2422
- fn as usdNumberFormatting,
2423
- USDObject,
2424
- USDWriter,
2425
- USDZExporter,
2426
- USDZExporterContext,
1
+ import '../../../engine/engine_shims.js';
2
+
3
+ import {
4
+ AnimationClip,
5
+ Bone,
6
+ BufferAttribute,
7
+ BufferGeometry,
8
+ Color,
9
+ DoubleSide,
10
+ InterleavedBufferAttribute,
11
+ LinearFilter,
12
+ Material,
13
+ MathUtils,
14
+ Matrix4,
15
+ Mesh,
16
+ MeshBasicMaterial,
17
+ MeshPhysicalMaterial,
18
+ MeshStandardMaterial,
19
+ Object3D,
20
+ OrthographicCamera,
21
+ PerspectiveCamera,
22
+ PlaneGeometry,
23
+ Quaternion,
24
+ RGBAFormat,
25
+ Scene,
26
+ ShaderMaterial,
27
+ SkinnedMesh,
28
+ SRGBColorSpace,
29
+ Texture,
30
+ Uniform,
31
+ UnsignedByteType,
32
+ Vector3,
33
+ Vector4,
34
+ WebGLRenderer,
35
+ WebGLRenderTarget
36
+ } from 'three';
37
+ import * as fflate from 'three/examples/jsm/libs/fflate.module.js';
38
+
39
+ import { VERSION } from "../../../engine/engine_constants.js";
40
+ import type { OffscreenCanvasExt } from '../../../engine/engine_shims.js';
41
+ import { Progress } from '../../../engine/engine_time_utils.js';
42
+ import { BehaviorExtension } from '../../api.js';
43
+ import type { IUSDExporterExtension } from './Extension.js';
44
+ import type { AnimationExtension } from './extensions/Animation.js';
45
+ import type { PhysicsExtension } from './extensions/behavior/PhysicsExtension.js';
46
+ import {buildNodeMaterial} from './extensions/NodeMaterialConverter.js';
47
+
48
+ type MeshPhysicalNodeMaterial = import("three/src/materials/nodes/MeshPhysicalNodeMaterial.js").default;
49
+
50
+ function makeNameSafe( str ) {
51
+ // Remove characters that are not allowed in USD ASCII identifiers
52
+ str = str.replace( /[^a-zA-Z0-9_]/g, '' );
53
+
54
+ // If name doesn't start with a-zA-Z_, add _ to the beginning – required by USD
55
+ if ( !str.match( /^[a-zA-Z_]/ ) )
56
+ str = '_' + str;
57
+
58
+ return str;
59
+ }
60
+
61
+ function makeDisplayNameSafe(str) {
62
+ str = str.replace("\"", "\\\"");
63
+ return str;
64
+ }
65
+
66
+ // TODO check if this works when bones in the skeleton are completely unordered
67
+ function findCommonAncestor(objects: Object3D[]): Object3D | null {
68
+ if (objects.length === 0) return null;
69
+
70
+ const ancestors = objects.map((obj) => {
71
+ const objAncestors = new Array<Object3D>();
72
+ while (obj.parent) {
73
+ objAncestors.unshift(obj.parent);
74
+ obj = obj.parent;
75
+ }
76
+ return objAncestors;
77
+ });
78
+
79
+ //@ts-ignore – findLast seems to be missing in TypeScript types pre-5.x
80
+ const commonAncestor = ancestors[0].findLast((ancestor) => {
81
+ return ancestors.every((a) => a.includes(ancestor));
82
+ });
83
+
84
+ return commonAncestor || null;
85
+ }
86
+
87
+ function findStructuralNodesInBoneHierarchy(bones: Array<Object3D>) {
88
+
89
+ const commonAncestor = findCommonAncestor(bones);
90
+ // find all structural nodes – parents of bones that are not bones themselves
91
+ const structuralNodes = new Set<Object3D>();
92
+ for ( const bone of bones ) {
93
+ let current = bone.parent;
94
+ while ( current && current !== commonAncestor ) {
95
+ if ( !bones.includes(current) ) {
96
+ structuralNodes.add(current);
97
+ }
98
+ current = current.parent;
99
+ }
100
+ }
101
+
102
+ return structuralNodes;
103
+ }
104
+
105
+ declare type USDObjectTransform = {
106
+ position: Vector3 | null;
107
+ quaternion: Quaternion | null;
108
+ scale: Vector3 | null;
109
+ }
110
+
111
+ const PositionIdentity = new Vector3();
112
+ const QuaternionIdentity = new Quaternion();
113
+ const ScaleIdentity = new Vector3(1,1,1);
114
+
115
+ class USDObject {
116
+
117
+ static USDObject_export_id = 0;
118
+
119
+ uuid: string;
120
+ name: string;
121
+ /** If no type is provided, type is chosen automatically (Xform or Mesh) */
122
+ type?: string;
123
+ /** MaterialBindingAPI and SkelBindingAPI are handled automatically, extra schemas can be added here */
124
+ extraSchemas: string[] = [];
125
+ displayName?: string;
126
+ visibility?: "inherited" | "invisible"; // defaults to "inherited" in USD
127
+ getMatrix(): Matrix4 {
128
+ if (!this.transform) return new Matrix4();
129
+ const { position, quaternion, scale } = this.transform;
130
+ const matrix = new Matrix4();
131
+ matrix.compose(position || PositionIdentity, quaternion || QuaternionIdentity, scale || ScaleIdentity);
132
+ return matrix;
133
+ }
134
+ setMatrix( value ) {
135
+ if (!value || !(value instanceof Matrix4)) {
136
+ this.transform = null;
137
+ return;
138
+ }
139
+ const position = new Vector3();
140
+ const quaternion = new Quaternion();
141
+ const scale = new Vector3();
142
+ value.decompose(position, quaternion, scale);
143
+ this.transform = { position, quaternion, scale };
144
+ }
145
+ /** @deprecated Use `transform`, or `getMatrix()` if you really need the matrix */
146
+ get matrix() { return this.getMatrix(); }
147
+ /** @deprecated Use `transform`, or `setMatrix()` if you really need the matrix */
148
+ set matrix( value ) { this.setMatrix( value ); }
149
+
150
+ transform: USDObjectTransform | null = null;
151
+ private _isDynamic: boolean;
152
+ get isDynamic() { return this._isDynamic; }
153
+ private set isDynamic( value ) { this._isDynamic = value; }
154
+ geometry: BufferGeometry | null;
155
+ material: MeshStandardMaterial | MeshBasicMaterial | Material | MeshPhysicalNodeMaterial | null;
156
+ camera: PerspectiveCamera | OrthographicCamera | null;
157
+ parent: USDObject | null;
158
+ skinnedMesh: SkinnedMesh | null;
159
+ children: Array<USDObject | null> = [];
160
+ animations: AnimationClip[] | null;
161
+ _eventListeners: {};
162
+
163
+ // these are for tracking which xformops are needed
164
+ needsTranslate: boolean = false;
165
+ needsOrient: boolean = false;
166
+ needsScale: boolean = false;
167
+
168
+ static createEmptyParent( object: USDObject ) {
169
+
170
+ const emptyParent = new USDObject( MathUtils.generateUUID(), object.name + '_empty_' + ( USDObject.USDObject_export_id ++ ), object.transform );
171
+ const parent = object.parent;
172
+ if (parent) parent.add( emptyParent );
173
+ emptyParent.add( object );
174
+ emptyParent.isDynamic = true;
175
+ object.transform = null;
176
+ return emptyParent;
177
+
178
+ }
179
+
180
+ static createEmpty() {
181
+
182
+ const empty = new USDObject( MathUtils.generateUUID(), 'Empty_' + ( USDObject.USDObject_export_id ++ ) );
183
+ empty.isDynamic = true;
184
+ return empty;
185
+ }
186
+
187
+ constructor( id, name, transform: USDObjectTransform | null = null, mesh: BufferGeometry | null = null, material: MeshStandardMaterial | MeshBasicMaterial | MeshPhysicalNodeMaterial | Material | null = null, camera: PerspectiveCamera | OrthographicCamera | null = null, skinnedMesh: SkinnedMesh | null = null, animations: AnimationClip[] | null = null ) {
188
+
189
+ this.uuid = id;
190
+ this.name = makeNameSafe( name );
191
+ this.displayName = name;
192
+
193
+ if (!transform) this.transform = null;
194
+ else this.transform = {
195
+ position: transform.position?.clone() || null,
196
+ quaternion: transform.quaternion?.clone() || null,
197
+ scale: transform.scale?.clone() || null
198
+ };
199
+ this.geometry = mesh;
200
+ this.material = material;
201
+ this.camera = camera;
202
+ this.parent = null;
203
+ this.children = [];
204
+ this._eventListeners = {};
205
+ this._isDynamic = false;
206
+ this.skinnedMesh = skinnedMesh;
207
+ this.animations = animations;
208
+
209
+ }
210
+
211
+ is( obj ) {
212
+
213
+ if ( ! obj ) return false;
214
+ return this.uuid === obj.uuid;
215
+
216
+ }
217
+
218
+ isEmpty() {
219
+
220
+ return ! this.geometry;
221
+
222
+ }
223
+
224
+ clone() {
225
+
226
+ const clone = new USDObject( MathUtils.generateUUID(), this.name, this.transform, this.geometry, this.material );
227
+ clone.isDynamic = this.isDynamic;
228
+ return clone;
229
+
230
+ }
231
+
232
+ deepClone() {
233
+
234
+ const clone = this.clone();
235
+ for ( const child of this.children ) {
236
+
237
+ if ( !child ) continue;
238
+ clone.add( child.deepClone() );
239
+
240
+ }
241
+
242
+ return clone;
243
+
244
+ }
245
+
246
+ getPath() {
247
+
248
+ let current = this.parent;
249
+ let path = this.name;
250
+ while ( current ) {
251
+
252
+ // StageRoot has a special path right now since there's additional Xforms for encapsulation.
253
+ // Better would be to actually model them as part of our object graph, but they're written separately,
254
+ // so currently we don't and instead work around that here.
255
+ const currentName = current.parent ? current.name : (current.name + "/Scenes/Scene");
256
+ path = currentName + '/' + path;
257
+ current = current.parent;
258
+
259
+ }
260
+
261
+ return '</' + path + '>';
262
+
263
+ }
264
+
265
+ add( child ) {
266
+
267
+ if ( child.parent ) {
268
+
269
+ child.parent.remove( child );
270
+
271
+ }
272
+
273
+ child.parent = this;
274
+ this.children.push( child );
275
+
276
+ }
277
+
278
+ remove( child ) {
279
+
280
+ const index = this.children.indexOf( child );
281
+ if ( index >= 0 ) {
282
+
283
+ if ( child.parent === this ) child.parent = null;
284
+ this.children.splice( index, 1 );
285
+
286
+ }
287
+
288
+ }
289
+
290
+ addEventListener( evt, listener: ( writer: USDWriter, context: USDZExporterContext ) => void ) {
291
+
292
+ if ( ! this._eventListeners[ evt ] ) this._eventListeners[ evt ] = [];
293
+ this._eventListeners[ evt ].push( listener );
294
+
295
+ }
296
+
297
+ removeEventListener( evt, listener: ( writer: USDWriter, context: USDZExporterContext ) => void ) {
298
+
299
+ if ( ! this._eventListeners[ evt ] ) return;
300
+ const index = this._eventListeners[ evt ].indexOf( listener );
301
+ if ( index >= 0 ) {
302
+
303
+ this._eventListeners[ evt ].splice( index, 1 );
304
+
305
+ }
306
+
307
+ }
308
+
309
+ onSerialize( writer, context ) {
310
+
311
+ const listeners = this._eventListeners[ 'serialize' ];
312
+ if ( listeners ) listeners.forEach( listener => listener( writer, context ) );
313
+
314
+ }
315
+
316
+ }
317
+
318
+
319
+ class USDDocument extends USDObject {
320
+
321
+ stageLength: number;
322
+
323
+ get isDocumentRoot() {
324
+
325
+ return true;
326
+
327
+ }
328
+ get isDynamic() {
329
+
330
+ return false;
331
+
332
+ }
333
+
334
+ constructor() {
335
+
336
+ super(undefined, 'StageRoot', null, null, null, null);
337
+ this.children = [];
338
+ this.stageLength = 200;
339
+
340
+ }
341
+
342
+ add( child: USDObject ) {
343
+
344
+ child.parent = this;
345
+ this.children.push( child );
346
+
347
+ }
348
+
349
+ remove( child: USDObject ) {
350
+
351
+ const index = this.children.indexOf( child );
352
+ if ( index >= 0 ) {
353
+
354
+ if ( child.parent === this ) child.parent = null;
355
+ this.children.splice( index, 1 );
356
+
357
+ }
358
+
359
+ }
360
+
361
+ traverse( callback: ( object: USDObject ) => void, current: USDObject | null = null ) {
362
+
363
+ if ( current !== null ) callback( current );
364
+ else current = this;
365
+ if ( current.children ) {
366
+
367
+ for ( const child of current.children ) {
368
+
369
+ this.traverse( callback, child );
370
+
371
+ }
372
+
373
+ }
374
+
375
+ }
376
+
377
+ findById( uuid: string ) {
378
+
379
+ let found = false;
380
+ function search( current: USDObject ): USDObject | undefined {
381
+
382
+ if ( found ) return undefined;
383
+ if ( current.uuid === uuid ) {
384
+
385
+ found = true;
386
+ return current;
387
+
388
+ }
389
+
390
+ if ( current.children ) {
391
+
392
+ for ( const child of current.children ) {
393
+
394
+ if (!child) continue;
395
+ const res = search( child );
396
+ if ( res ) return res;
397
+
398
+ }
399
+
400
+ }
401
+ return undefined;
402
+ }
403
+
404
+ return search( this );
405
+
406
+ }
407
+
408
+ buildHeader( _context: USDZExporterContext ) {
409
+ const animationExtension = _context.extensions?.find( ext => ext?.extensionName === 'animation' ) as AnimationExtension | undefined;
410
+ const behaviorExtension = _context.extensions?.find( ext => ext?.extensionName === 'Behaviour' ) as BehaviorExtension | undefined;
411
+ const physicsExtension = _context.extensions?.find( ext => ext?.extensionName === 'Physics' ) as PhysicsExtension | undefined;
412
+ const startTimeCode = animationExtension?.getStartTimeCode() ?? 0;
413
+ const endTimeCode = animationExtension?.getEndTimeCode() ?? 0;
414
+
415
+ let comment = "";
416
+ const registeredClips = animationExtension?.registeredClips;
417
+ if (registeredClips) {
418
+ for ( const clip of registeredClips ) {
419
+ comment += `\t# Animation: ${clip.name}, start=${animationExtension.getStartTimeByClip(clip) * 60}, length=${clip.duration * 60}\n`;
420
+ }
421
+ }
422
+ const comments = comment;
423
+
424
+ return `#usda 1.0
425
+ (
426
+ customLayerData = {
427
+ string creator = "Needle Engine ${VERSION}"
428
+ dictionary Needle = {
429
+ bool animations = ${animationExtension ? 1 : 0}
430
+ bool interactive = ${behaviorExtension ? 1 : 0}
431
+ bool physics = ${physicsExtension ? 1 : 0}
432
+ bool quickLookCompatible = ${_context.quickLookCompatible ? 1 : 0}
433
+ }
434
+ }
435
+ defaultPrim = "${makeNameSafe( this.name )}"
436
+ metersPerUnit = 1
437
+ upAxis = "Y"
438
+ startTimeCode = ${startTimeCode}
439
+ endTimeCode = ${endTimeCode}
440
+ timeCodesPerSecond = 60
441
+ framesPerSecond = 60
442
+ doc = """Generated by Needle Engine USDZ Exporter ${VERSION}"""
443
+ ${comments}
444
+ )
445
+ `;
446
+
447
+ }
448
+
449
+ }
450
+
451
+ const newLine = '\n';
452
+ const materialRoot = '</StageRoot/Materials';
453
+
454
+ class USDWriter {
455
+ str: string;
456
+ indent: number;
457
+
458
+ constructor() {
459
+
460
+ this.str = '';
461
+ this.indent = 0;
462
+
463
+ }
464
+
465
+ clear() {
466
+
467
+ this.str = '';
468
+ this.indent = 0;
469
+
470
+ }
471
+
472
+ beginBlock( str: string | undefined = undefined, char = '{', createNewLine = true ) {
473
+
474
+ if ( str !== undefined ) {
475
+ str = this.applyIndent( str );
476
+ this.str += str;
477
+ if ( createNewLine ) {
478
+ this.str += newLine;
479
+ this.str += this.applyIndent( char );
480
+ }
481
+ else {
482
+ this.str += " " + char;
483
+ }
484
+ }
485
+ else {
486
+ this.str += this.applyIndent( char );
487
+ }
488
+
489
+ this.str += newLine;
490
+ this.indent += 1;
491
+
492
+ }
493
+
494
+ closeBlock( char = '}' ) {
495
+
496
+ this.indent -= 1;
497
+ this.str += this.applyIndent( char ) + newLine;
498
+
499
+ }
500
+
501
+ beginArray( str ) {
502
+
503
+ str = this.applyIndent( str + ' = [' );
504
+ this.str += str;
505
+ this.str += newLine;
506
+ this.indent += 1;
507
+
508
+ }
509
+
510
+ closeArray() {
511
+
512
+ this.indent -= 1;
513
+ this.str += this.applyIndent( ']' ) + newLine;
514
+
515
+ }
516
+
517
+ appendLine( str = '' ) {
518
+
519
+ str = this.applyIndent( str );
520
+ this.str += str;
521
+ this.str += newLine;
522
+
523
+ }
524
+
525
+ toString() {
526
+
527
+ return this.str;
528
+
529
+ }
530
+
531
+ applyIndent( str ) {
532
+
533
+ let indents = '';
534
+ for ( let i = 0; i < this.indent; i ++ ) indents += '\t';
535
+ return indents + str;
536
+
537
+ }
538
+
539
+ }
540
+
541
+ declare type TextureMap = {[name: string]: {texture: Texture, scale?: Vector4}};
542
+
543
+ class USDZExporterContext {
544
+ root?: Object3D;
545
+ exporter: USDZExporter;
546
+ extensions: Array<IUSDExporterExtension> = [];
547
+ quickLookCompatible: boolean;
548
+ exportInvisible: boolean;
549
+ materials: Map<string, Material>;
550
+ textures: TextureMap;
551
+ files: { [path: string]: Uint8Array | [Uint8Array, fflate.ZipOptions] | null | any }
552
+ document: USDDocument;
553
+ output: string;
554
+ animations: AnimationClip[];
555
+
556
+ constructor( root: Object3D | undefined, exporter: USDZExporter, options: {
557
+ extensions?: Array<IUSDExporterExtension>,
558
+ quickLookCompatible: boolean,
559
+ exportInvisible: boolean,
560
+ } ) {
561
+
562
+ this.root = root;
563
+ this.exporter = exporter;
564
+ this.quickLookCompatible = options.quickLookCompatible;
565
+ this.exportInvisible = options.exportInvisible;
566
+
567
+ if ( options.extensions )
568
+ this.extensions = options.extensions;
569
+
570
+ this.materials = new Map();
571
+ this.textures = {};
572
+ this.files = {};
573
+ this.document = new USDDocument();
574
+ this.output = '';
575
+ this.animations = [];
576
+
577
+ }
578
+
579
+ }
580
+
581
+ /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_anchoring_type) */
582
+ export type Anchoring = "plane" | "image" | "face" | "none"
583
+ /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_planeanchoring_alignment) */
584
+ export type Alignment = "horizontal" | "vertical" | "any";
585
+
586
+ class USDZExporterOptions {
587
+ ar: {
588
+ anchoring: { type: Anchoring },
589
+ planeAnchoring: { alignment: Alignment },
590
+ } = {
591
+ anchoring: { type: 'plane' },
592
+ planeAnchoring: { alignment: 'horizontal' }
593
+ };
594
+ quickLookCompatible: boolean = false;
595
+ extensions: any[] = [];
596
+ maxTextureSize: number = 4096;
597
+ exportInvisible: boolean = false;
598
+ }
599
+
600
+ class USDZExporter {
601
+ debug: boolean;
602
+ pruneUnusedNodes: boolean;
603
+ sceneAnchoringOptions: USDZExporterOptions = new USDZExporterOptions();
604
+ extensions: Array<IUSDExporterExtension> = [];
605
+ keepObject?: (object: Object3D) => boolean;
606
+ beforeWritingDocument?: () => void;
607
+
608
+ constructor() {
609
+
610
+ this.debug = false;
611
+ this.pruneUnusedNodes = true;
612
+
613
+ }
614
+
615
+ async parse(scene: Object3D | undefined, options: USDZExporterOptions = new USDZExporterOptions()) {
616
+
617
+ options = Object.assign( new USDZExporterOptions(), options );
618
+
619
+ this.sceneAnchoringOptions = options;
620
+ const context = new USDZExporterContext( scene, this, options );
621
+ this.extensions = context.extensions;
622
+
623
+ const files = context.files;
624
+ const modelFileName = 'model.usda';
625
+
626
+ // model file should be first in USDZ archive so we init it here
627
+ files[ modelFileName ] = null;
628
+
629
+ const materials = context.materials;
630
+ const textures = context.textures;
631
+
632
+ Progress.report('export-usdz', "Invoking onBeforeBuildDocument");
633
+ await invokeAll( context, 'onBeforeBuildDocument' );
634
+ Progress.report('export-usdz', "Done onBeforeBuildDocument");
635
+
636
+ Progress.report('export-usdz', "Reparent bones to common ancestor");
637
+
638
+ // Find all skeletons and reparent them to their skelroot / armature / uppermost bone parent.
639
+ // This may not be correct in all cases.
640
+ const reparentings: Array<{ object: Object3D, originalParent: Object3D | null, newParent: Object3D }> = [];
641
+ const allReparentingObjects = new Set<string>();
642
+ scene?.traverse(object => {
643
+ if (!options.exportInvisible && !object.visible) return;
644
+
645
+ if (object instanceof SkinnedMesh) {
646
+ const bones = object.skeleton.bones as Bone[];
647
+
648
+ const commonAncestor = findCommonAncestor(bones);
649
+ if (commonAncestor) {
650
+ const newReparenting = { object, originalParent: object.parent, newParent: commonAncestor };
651
+ reparentings.push( newReparenting );
652
+
653
+ // keep track of which nodes are important for skeletal export consistency
654
+ allReparentingObjects.add(newReparenting.object.uuid);
655
+ if (newReparenting.newParent) allReparentingObjects.add(newReparenting.newParent.uuid);
656
+ if (newReparenting.originalParent) allReparentingObjects.add(newReparenting.originalParent.uuid);
657
+ }
658
+ }
659
+ });
660
+
661
+ for ( const reparenting of reparentings ) {
662
+ const { object, originalParent, newParent } = reparenting;
663
+ newParent.add( object );
664
+ }
665
+
666
+ Progress.report('export-usdz', "Traversing hierarchy");
667
+ if (scene) traverse( scene, context.document, context, this.keepObject);
668
+
669
+ // Root object should have identity matrix
670
+ // so that root transformations don't end up in the resulting file.
671
+ // if (context.document.children?.length > 0)
672
+ // context.document.children[0]?.matrix.identity(); //.multiply(new Matrix4().makeRotationY(Math.PI));
673
+
674
+ Progress.report('export-usdz', "Invoking onAfterBuildDocument");
675
+ await invokeAll( context, 'onAfterBuildDocument' );
676
+
677
+ // At this point, we know all animated objects, all skinned mesh objects, and all objects targeted by behaviors.
678
+ // We can prune all empty nodes (no geometry or material) depth-first.
679
+ // This avoids unnecessary export of e.g. animated bones as nodes when they have no children
680
+ // (for example, a sword attached to a hand still needs that entire hierarchy exported)
681
+ const behaviorExt = context.extensions.find( ext => ext.extensionName === 'Behaviour' ) as BehaviorExtension | undefined;
682
+ const allBehaviorTargets = behaviorExt?.getAllTargetUuids() ?? new Set<string>();
683
+
684
+ // Prune pass. Depth-first removal of nodes that don't affect the outcome of the scene.
685
+ if (this.pruneUnusedNodes) {
686
+ const options = {
687
+ allBehaviorTargets,
688
+ debug: false,
689
+ boneReparentings: allReparentingObjects,
690
+ quickLookCompatible: context.quickLookCompatible,
691
+ };
692
+ if (this.debug) logUsdHierarchy(context.document, "Hierarchy BEFORE pruning", options);
693
+ prune( context.document, options );
694
+ if (this.debug) logUsdHierarchy(context.document, "Hierarchy AFTER pruning");
695
+ }
696
+ else if (this.debug) {
697
+ console.log("Pruning of empty nodes is disabled. This may result in a larger USDZ file.");
698
+ }
699
+
700
+ Progress.report('export-usdz', { message: "Parsing document", autoStep: 10 });
701
+ await parseDocument( context, () => {
702
+ // injected after stageRoot.
703
+ // TODO property use context/writer instead of string concat
704
+ Progress.report('export-usdz', "Building materials");
705
+ const result = buildMaterials( materials, textures, options.quickLookCompatible );
706
+ return result;
707
+ } );
708
+
709
+ Progress.report("export-usdz", "Invoking onAfterSerialize");
710
+ await invokeAll( context, 'onAfterSerialize' );
711
+
712
+ // repair the parenting again
713
+ for ( const reparenting of reparentings ) {
714
+ const { object, originalParent, newParent } = reparenting;
715
+ if (originalParent)
716
+ originalParent.add( object );
717
+ }
718
+
719
+ // Moved into parseDocument callback for proper defaultPrim encapsulation
720
+ // context.output += buildMaterials( materials, textures, options.quickLookCompatible );
721
+
722
+ // callback for validating after all export has been done
723
+ context.exporter?.beforeWritingDocument?.();
724
+
725
+ const header = context.document.buildHeader( context );
726
+ const final = header + '\n' + context.output;
727
+
728
+ // full output file
729
+ if ( this.debug ) console.log( final );
730
+
731
+ files[ modelFileName ] = fflate.strToU8( final );
732
+ context.output = '';
733
+
734
+ Progress.report("export-usdz", { message: "Exporting textures", autoStep: 10 });
735
+ Progress.start("export-usdz-textures", { parentScope: "export-usdz", logTimings: false });
736
+ const decompressionRenderer = new WebGLRenderer( {
737
+ antialias: false,
738
+ alpha: true,
739
+ premultipliedAlpha: false,
740
+ preserveDrawingBuffer: true
741
+ } );
742
+
743
+ const textureCount = Object.keys(textures).length;
744
+ Progress.report("export-usdz-textures", { totalSteps: textureCount * 3, currentStep: 0 });
745
+ const convertTexture = async (id: string) => {
746
+
747
+ const textureData = textures[ id ];
748
+ const texture = textureData.texture;
749
+
750
+ const isRGBA = formatsWithAlphaChannel.includes( texture.format );
751
+
752
+ // Change: we need to always read back the texture now, otherwise the unpremultiplied workflow doesn't work.
753
+ let img: ImageReadbackResult = {
754
+ imageData: texture.image
755
+ };
756
+
757
+ Progress.report("export-usdz-textures", { message: "read back texture", autoStep: true });
758
+ const anyColorScale = textureData.scale !== undefined && textureData.scale.x !== 1 && textureData.scale.y !== 1 && textureData.scale.z !== 1 && textureData.scale.w !== 1;
759
+ // @ts-ignore
760
+ if ( texture.isCompressedTexture || texture.isRenderTargetTexture || anyColorScale ) {
761
+ img = await decompressGpuTexture( texture, options.maxTextureSize, decompressionRenderer, textureData.scale );
762
+ }
763
+
764
+ Progress.report("export-usdz-textures", { message: "convert texture to canvas", autoStep: true });
765
+ const canvas = await imageToCanvasUnpremultiplied( img.imageBitmap || img.imageData, options.maxTextureSize ).catch( err => {
766
+ console.error("Error converting texture to canvas", texture, err);
767
+ });
768
+
769
+ if ( canvas ) {
770
+
771
+ Progress.report("export-usdz-textures", { message: "convert canvas to blob", autoStep: true });
772
+ const blob = await canvas.convertToBlob( {type: isRGBA ? 'image/png' : 'image/jpeg', quality: 0.95 } );
773
+ files[ `textures/${id}.${isRGBA ? 'png' : 'jpg'}` ] = new Uint8Array( await blob.arrayBuffer() );
774
+
775
+ } else {
776
+
777
+ console.warn( 'Can`t export texture: ', texture );
778
+
779
+ }
780
+ };
781
+
782
+ for ( const id in textures ) {
783
+
784
+ await convertTexture( id );
785
+ }
786
+
787
+ decompressionRenderer.dispose();
788
+
789
+ // 64 byte alignment
790
+ // https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109
791
+
792
+ Progress.end("export-usdz-textures");
793
+
794
+ let offset = 0;
795
+
796
+ for ( const filename in files ) {
797
+
798
+ const file = files[ filename ];
799
+ const headerSize = 34 + filename.length;
800
+
801
+ offset += headerSize;
802
+
803
+ const offsetMod64 = offset & 63;
804
+
805
+ if ( offsetMod64 !== 4 ) {
806
+
807
+ const padLength = 64 - offsetMod64;
808
+ const padding = new Uint8Array( padLength );
809
+
810
+ files[ filename ] = [ file, { extra: { 12345: padding } } ];
811
+
812
+ }
813
+
814
+ offset = file.length;
815
+
816
+ }
817
+
818
+ Progress.report("export-usdz", "zip archive");
819
+ return fflate.zipSync( files, { level: 0 } );
820
+
821
+ }
822
+
823
+ }
824
+
825
+ function traverse( object: Object3D, parentModel: USDObject, context: USDZExporterContext, keepObject?: (object: Object3D) => boolean ) {
826
+
827
+ if (!context.exportInvisible && !object.visible) return;
828
+
829
+ let model: USDObject | undefined = undefined;
830
+ let geometry: BufferGeometry | undefined = undefined;
831
+ let material: Material | Material[] | undefined = undefined;
832
+
833
+ const transform: USDObjectTransform = { position: object.position, quaternion: object.quaternion, scale: object.scale };
834
+ if (object.position.x === 0 && object.position.y === 0 && object.position.z === 0)
835
+ transform.position = null;
836
+ if (object.quaternion.x === 0 && object.quaternion.y === 0 && object.quaternion.z === 0 && object.quaternion.w === 1)
837
+ transform.quaternion = null;
838
+ if (object.scale.x === 1 && object.scale.y === 1 && object.scale.z === 1)
839
+ transform.scale = null;
840
+
841
+ if (object instanceof Mesh || object instanceof SkinnedMesh) {
842
+ geometry = object.geometry;
843
+ material = object.material;
844
+ }
845
+
846
+ // API for an explicit choice to discard this object – for example, a geometry that should not be exported,
847
+ // but childs should still be exported.
848
+ if (keepObject && !keepObject(object)) {
849
+ geometry = undefined;
850
+ material = undefined;
851
+ }
852
+
853
+ if ( (object instanceof Mesh || object instanceof SkinnedMesh) &&
854
+ material && typeof material === 'object' &&
855
+ (material instanceof MeshStandardMaterial ||
856
+ material instanceof MeshBasicMaterial ||
857
+ // material instanceof MeshPhysicalNodeMaterial ||
858
+ (material as any).isMeshPhysicalNodeMaterial ||
859
+ (material instanceof Material && material.type === "MeshLineMaterial"))) {
860
+
861
+ const name = getObjectId( object );
862
+ const skinnedMeshObject = object instanceof SkinnedMesh ? object : null;
863
+ model = new USDObject( object.uuid, name, transform, geometry, material as any, undefined, skinnedMeshObject, object.animations );
864
+
865
+ } else if ( object instanceof PerspectiveCamera || object instanceof OrthographicCamera ) {
866
+
867
+ const name = getObjectId( object );
868
+ model = new USDObject( object.uuid, name, transform, undefined, undefined, object );
869
+
870
+ } else {
871
+
872
+ const name = getObjectId( object );
873
+ model = new USDObject( object.uuid, name, transform, undefined, undefined, undefined, undefined, object.animations );
874
+
875
+ }
876
+
877
+ if ( model ) {
878
+
879
+ model.displayName = object.userData?.name || object.name;
880
+ model.visibility = object.visible ? undefined : "invisible";
881
+
882
+ if ( parentModel ) {
883
+
884
+ parentModel.add( model );
885
+
886
+ }
887
+
888
+ parentModel = model;
889
+
890
+ if ( context.extensions ) {
891
+
892
+ for ( const ext of context.extensions ) {
893
+
894
+ if ( ext.onExportObject ) ext.onExportObject.call( ext, object, model, context );
895
+
896
+ }
897
+
898
+ }
899
+
900
+ } else {
901
+
902
+ const name = getObjectId( object );
903
+ const empty = new USDObject( object.uuid, name, { position: object.position, quaternion: object.quaternion, scale: object.scale } );
904
+ if ( parentModel ) {
905
+
906
+ parentModel.add( empty );
907
+
908
+ }
909
+
910
+ parentModel = empty;
911
+
912
+ }
913
+
914
+ for ( const ch of object.children ) {
915
+
916
+ traverse( ch, parentModel, context, keepObject );
917
+
918
+ }
919
+
920
+ }
921
+
922
+ function logUsdHierarchy( object: USDObject, prefix: string, ...extraLogObjects: any[] ) {
923
+
924
+ const item = {};
925
+ let itemCount = 0;
926
+
927
+ function collectItem( object: USDObject, current) {
928
+ itemCount++;
929
+ let name = object.displayName || object.name;
930
+ name += " (" + object.uuid + ")";
931
+ const hasAny = object.geometry || object.material || object.camera || object.skinnedMesh;
932
+ if (hasAny) {
933
+ name += " (" + (object.geometry ? "geo, " : "") + (object.material ? "mat, " : "") + (object.camera ? "cam, " : "") + (object.skinnedMesh ? "skin, " : "") + ")";
934
+ }
935
+ current[name] = {};
936
+ const props = { object };
937
+ if (object.material) props['mat'] = true;
938
+ if (object.geometry) props['geo'] = true;
939
+ if (object.camera) props['cam'] = true;
940
+ if (object.skinnedMesh) props['skin'] = true;
941
+
942
+ current[name]._self = props;
943
+ for ( const child of object.children ) {
944
+ if (child) {
945
+ collectItem(child, current[name]);
946
+ }
947
+ }
948
+ }
949
+
950
+ collectItem(object, item);
951
+
952
+ console.log(prefix + " (" + itemCount + " nodes)", item, ...extraLogObjects);
953
+ }
954
+
955
+ function prune ( object: USDObject, options : {
956
+ allBehaviorTargets: Set<string>,
957
+ debug: boolean,
958
+ boneReparentings: Set<string>,
959
+ quickLookCompatible: boolean,
960
+ } ) {
961
+
962
+ let allChildsWerePruned = true;
963
+
964
+ const prunedChilds = new Array<USDObject>();
965
+ const keptChilds = new Array<USDObject>();
966
+
967
+ if (object.children.length === 0) {
968
+ allChildsWerePruned = true;
969
+ }
970
+ else {
971
+ const childs = [...object.children];
972
+ for ( const child of childs ) {
973
+ if (child) {
974
+ const childWasPruned = prune(child, options);
975
+ if (options.debug) {
976
+ if (childWasPruned) prunedChilds.push(child);
977
+ else keptChilds.push(child);
978
+ }
979
+ allChildsWerePruned = allChildsWerePruned && childWasPruned;
980
+ }
981
+ }
982
+ }
983
+
984
+ // check if this object is referenced by any behavior
985
+ const isBehaviorSourceOrTarget = options.allBehaviorTargets.has(object.uuid);
986
+
987
+ // check if this object has any material or geometry
988
+ const isVisible = object.geometry || object.material || (object.camera && !options.quickLookCompatible) || object.skinnedMesh || false;
989
+
990
+ // check if this object is part of any reparenting
991
+ const isBoneReparenting = options.boneReparentings.has(object.uuid);
992
+
993
+ const canBePruned = allChildsWerePruned && !isBehaviorSourceOrTarget && !isVisible && !isBoneReparenting;
994
+
995
+ if (canBePruned) {
996
+ if (options.debug) console.log("Pruned object:", (object.displayName || object.name) + " (" + object.uuid + ")", {
997
+ isVisible,
998
+ isBehaviorSourceOrTarget,
999
+ allChildsWerePruned,
1000
+ isBoneReparenting,
1001
+ object,
1002
+ prunedChilds,
1003
+ keptChilds
1004
+ });
1005
+ object.parent?.remove(object);
1006
+ }
1007
+ else {
1008
+ if (options.debug) console.log("Kept object:", (object.displayName || object.name) + " (" + object.uuid + ")", {
1009
+ isVisible,
1010
+ isBehaviorSourceOrTarget,
1011
+ allChildsWerePruned,
1012
+ isBoneReparenting,
1013
+ object,
1014
+ prunedChilds,
1015
+ keptChilds
1016
+ });
1017
+ }
1018
+
1019
+ // if it has no children and is not a behavior source or target, and is not visible, prune it
1020
+ return canBePruned;
1021
+ }
1022
+
1023
+ async function parseDocument( context: USDZExporterContext, afterStageRoot: () => string ) {
1024
+
1025
+ Progress.start("export-usdz-resources", "export-usdz");
1026
+ const resources: Array<() => void> = [];
1027
+ for ( const child of context.document.children ) {
1028
+ addResources( child, context, resources );
1029
+ }
1030
+ // addResources now only collects promises for better progress reporting.
1031
+ // We are resolving them here and reporting progress on that:
1032
+ const total = resources.length;
1033
+ for (let i = 0; i < total; i++) {
1034
+ Progress.report("export-usdz-resources", { totalSteps: total, currentStep: i });
1035
+ await new Promise<void>((resolve, _reject) => {
1036
+ resources[i]();
1037
+ resolve();
1038
+ });
1039
+ }
1040
+ Progress.end("export-usdz-resources");
1041
+
1042
+ const writer = new USDWriter();
1043
+ const arAnchoringOptions = context.exporter.sceneAnchoringOptions.ar;
1044
+
1045
+ writer.beginBlock( `def Xform "${context.document.name}"` );
1046
+
1047
+ writer.beginBlock( `def Scope "Scenes" (
1048
+ kind = "sceneLibrary"
1049
+ )` );
1050
+
1051
+ writer.beginBlock( `def Xform "Scene"`, '(', false);
1052
+ writer.appendLine( `apiSchemas = ["Preliminary_AnchoringAPI"]` );
1053
+ writer.appendLine( `customData = {`);
1054
+ writer.appendLine( ` bool preliminary_collidesWithEnvironment = 0` );
1055
+ writer.appendLine( ` string sceneName = "Scene"`);
1056
+ writer.appendLine( `}` );
1057
+ writer.appendLine( `sceneName = "Scene"` );
1058
+ writer.closeBlock( ')' );
1059
+ writer.beginBlock();
1060
+
1061
+ writer.appendLine( `token preliminary:anchoring:type = "${arAnchoringOptions.anchoring.type}"` );
1062
+ if (arAnchoringOptions.anchoring.type === 'plane')
1063
+ writer.appendLine( `token preliminary:planeAnchoring:alignment = "${arAnchoringOptions.planeAnchoring.alignment}"` );
1064
+ // bit hacky as we don't have a callback here yet. Relies on the fact that the image is named identical in the ImageTracking extension.
1065
+ if (arAnchoringOptions.anchoring.type === 'image')
1066
+ writer.appendLine( `rel preliminary:imageAnchoring:referenceImage = </${context.document.name}/Scenes/Scene/AnchoringReferenceImage>` );
1067
+ writer.appendLine();
1068
+
1069
+ const count = (object: USDObject | null) => {
1070
+ if (!object) return 0;
1071
+ let total = 1;
1072
+ for ( const child of object.children ) total += count( child );
1073
+ return total;
1074
+ }
1075
+ const totalXformCount = count(context.document);
1076
+ Progress.start("export-usdz-xforms", "export-usdz");
1077
+ Progress.report("export-usdz-xforms", { totalSteps: totalXformCount, currentStep: 1 });
1078
+
1079
+ for ( const child of context.document.children ) {
1080
+ buildXform( child, writer, context );
1081
+ }
1082
+ Progress.end("export-usdz-xforms");
1083
+
1084
+ Progress.report("export-usdz", "invoke onAfterHierarchy");
1085
+ invokeAll( context, 'onAfterHierarchy', writer );
1086
+
1087
+ writer.closeBlock();
1088
+ writer.closeBlock();
1089
+ writer.appendLine(afterStageRoot());
1090
+ writer.closeBlock();
1091
+
1092
+ Progress.report("export-usdz", "write to string")
1093
+ context.output += writer.toString();
1094
+
1095
+ }
1096
+
1097
+ function addResources( object: USDObject | null, context: USDZExporterContext, resources: Array<() => void>) {
1098
+
1099
+ if ( !object ) return;
1100
+
1101
+ const geometry = object.geometry;
1102
+ const material = object.material;
1103
+
1104
+ if ( geometry ) {
1105
+
1106
+ if ( material && (
1107
+ 'isMeshStandardMaterial' in material && material.isMeshStandardMaterial ||
1108
+ 'isMeshBasicMaterial' in material && material.isMeshBasicMaterial ||
1109
+ material.type === "MeshLineMaterial"
1110
+ ) ) { // TODO convert unlit to lit+emissive
1111
+
1112
+ const geometryFileName = 'geometries/' + getGeometryName(geometry, object.name) + '.usda';
1113
+
1114
+ if ( ! ( geometryFileName in context.files ) ) {
1115
+
1116
+ const action = () => {
1117
+ const meshObject = buildMeshObject( geometry, object.skinnedMesh?.skeleton?.bones, context.quickLookCompatible );
1118
+ context.files[ geometryFileName ] = buildUSDFileAsString( meshObject, context);
1119
+ };
1120
+
1121
+ resources.push(action);
1122
+
1123
+ }
1124
+
1125
+ } else {
1126
+
1127
+ console.warn( 'NeedleUSDZExporter: Unsupported material type (USDZ only supports MeshStandardMaterial)', material?.name );
1128
+
1129
+ }
1130
+
1131
+ }
1132
+
1133
+ if ( material ) {
1134
+
1135
+ if ( ! ( material.uuid in context.materials ) ) {
1136
+
1137
+ context.materials[ material.uuid ] = material;
1138
+
1139
+ }
1140
+ }
1141
+
1142
+ for ( const ch of object.children ) {
1143
+
1144
+ addResources( ch, context, resources );
1145
+
1146
+ }
1147
+
1148
+ }
1149
+
1150
+ async function invokeAll( context: USDZExporterContext, name: string, writer: USDWriter | null = null ) {
1151
+
1152
+ if ( context.extensions ) {
1153
+
1154
+ for ( const ext of context.extensions ) {
1155
+
1156
+ if ( !ext ) continue;
1157
+
1158
+ if ( typeof ext[ name ] === 'function' ) {
1159
+
1160
+ const method = ext[ name ];
1161
+ const res = method.call( ext, context, writer );
1162
+ if(res instanceof Promise) {
1163
+ await res;
1164
+ }
1165
+ }
1166
+
1167
+ }
1168
+
1169
+ }
1170
+
1171
+ }
1172
+ let _renderer: WebGLRenderer | null = null;
1173
+ let renderTarget: WebGLRenderTarget | null = null;
1174
+ let fullscreenQuadGeometry: PlaneGeometry | null;
1175
+ let fullscreenQuadMaterial: ShaderMaterial | null;
1176
+ let fullscreenQuad: Mesh | null;
1177
+
1178
+ declare type ImageReadbackResult = {
1179
+ imageData: ImageData;
1180
+ imageBitmap?: ImageBitmap;
1181
+ }
1182
+
1183
+ /** Reads back a texture from the GPU (can be compressed, a render texture, or anything), optionally applies RGBA colorScale to it, and returns CPU data for further usage.
1184
+ * Note that there are WebGL / WebGPU rules preventing some use of data between WebGL contexts.
1185
+ */
1186
+ async function decompressGpuTexture( texture, maxTextureSize = Infinity, renderer: WebGLRenderer | null = null, colorScale: Vector4 | undefined = undefined): Promise<ImageReadbackResult> {
1187
+
1188
+ if ( ! fullscreenQuadGeometry ) fullscreenQuadGeometry = new PlaneGeometry( 2, 2, 1, 1 );
1189
+ if ( ! fullscreenQuadMaterial ) fullscreenQuadMaterial = new ShaderMaterial( {
1190
+ uniforms: {
1191
+ blitTexture: new Uniform( texture ),
1192
+ flipY: new Uniform( false ),
1193
+ scale: new Uniform( new Vector4( 1, 1, 1, 1 ) ),
1194
+ },
1195
+ vertexShader: `
1196
+ varying vec2 vUv;
1197
+ uniform bool flipY;
1198
+ void main(){
1199
+ vUv = uv;
1200
+ if (flipY)
1201
+ vUv.y = 1. - vUv.y;
1202
+ gl_Position = vec4(position.xy * 1.0,0.,.999999);
1203
+ }`,
1204
+ fragmentShader: `
1205
+ uniform sampler2D blitTexture;
1206
+ uniform vec4 scale;
1207
+ varying vec2 vUv;
1208
+
1209
+ void main(){
1210
+ gl_FragColor = vec4(vUv.xy, 0, 1);
1211
+
1212
+ #ifdef IS_SRGB
1213
+ gl_FragColor = sRGBTransferOETF( texture2D( blitTexture, vUv) );
1214
+ #else
1215
+ gl_FragColor = texture2D( blitTexture, vUv);
1216
+ #endif
1217
+
1218
+ gl_FragColor.rgba *= scale.rgba;
1219
+ }`
1220
+ } );
1221
+
1222
+ // update uniforms
1223
+ const uniforms = fullscreenQuadMaterial.uniforms;
1224
+ uniforms.blitTexture.value = texture;
1225
+ uniforms.flipY.value = false;
1226
+ uniforms.scale.value = new Vector4( 1, 1, 1, 1 );
1227
+ if ( colorScale !== undefined ) uniforms.scale.value.copy( colorScale );
1228
+
1229
+ fullscreenQuadMaterial.defines.IS_SRGB = texture.colorSpace == SRGBColorSpace;
1230
+ fullscreenQuadMaterial.needsUpdate = true;
1231
+
1232
+ if ( ! fullscreenQuad ) {
1233
+
1234
+ fullscreenQuad = new Mesh( fullscreenQuadGeometry, fullscreenQuadMaterial );
1235
+ fullscreenQuad.frustumCulled = false;
1236
+
1237
+ }
1238
+
1239
+ const _camera = new PerspectiveCamera();
1240
+ const _scene = new Scene();
1241
+ _scene.add( fullscreenQuad );
1242
+
1243
+ if ( ! renderer ) {
1244
+
1245
+ renderer = _renderer = new WebGLRenderer( { antialias: false, alpha: true, premultipliedAlpha: false, preserveDrawingBuffer: true } );
1246
+
1247
+ }
1248
+
1249
+ const width = Math.min( texture.image.width, maxTextureSize );
1250
+ const height = Math.min( texture.image.height, maxTextureSize );
1251
+
1252
+ // dispose render target if the size is wrong
1253
+ if ( renderTarget && ( renderTarget.width !== width || renderTarget.height !== height ) ) {
1254
+
1255
+ renderTarget.dispose();
1256
+ renderTarget = null;
1257
+
1258
+ }
1259
+
1260
+ if ( ! renderTarget ) {
1261
+
1262
+ renderTarget = new WebGLRenderTarget( width, height, { format: RGBAFormat, type: UnsignedByteType, minFilter: LinearFilter, magFilter: LinearFilter } );
1263
+ }
1264
+
1265
+ renderer.setRenderTarget( renderTarget );
1266
+ renderer.setSize( width, height );
1267
+ renderer.clear();
1268
+ renderer.render( _scene, _camera );
1269
+
1270
+ if ( _renderer ) {
1271
+
1272
+ _renderer.dispose();
1273
+ _renderer = null;
1274
+
1275
+ }
1276
+
1277
+ const buffer = new Uint8ClampedArray( renderTarget.width * renderTarget.height * 4 );
1278
+ renderer.readRenderTargetPixels( renderTarget, 0, 0, renderTarget.width, renderTarget.height, buffer );
1279
+ const imageData = new ImageData( buffer, renderTarget.width, renderTarget.height, undefined );
1280
+ const bmp = await createImageBitmap( imageData, { premultiplyAlpha: "none" } );
1281
+ return {
1282
+ imageData,
1283
+ imageBitmap: bmp
1284
+ };
1285
+
1286
+ }
1287
+
1288
+ /** Checks if the given image is of a type with readable data and width/height */
1289
+ function isImageBitmap( image ) {
1290
+
1291
+ return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
1292
+ ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
1293
+ ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
1294
+ ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap );
1295
+
1296
+ }
1297
+
1298
+ /** This method uses a 'bitmaprenderer' context and doesn't do any pixel manipulation.
1299
+ * This way, we can keep the alpha channel as it was, but we're losing the ability to do pixel manipulations or resize operations. */
1300
+ async function imageToCanvasUnpremultiplied( image: ImageBitmapSource & { width: number, height: number }, maxTextureSize = 4096) {
1301
+
1302
+ const scale = maxTextureSize / Math.max( image.width, image.height );
1303
+ const width = image.width * Math.min( 1, scale );
1304
+ const height = image.height * Math.min( 1, scale );
1305
+
1306
+ const canvas = new OffscreenCanvas( width, height );
1307
+ const settings: ImageBitmapOptions = { premultiplyAlpha: "none" };
1308
+ if (image.width !== width) settings.resizeWidth = width;
1309
+ if (image.height !== height) settings.resizeHeight = height;
1310
+
1311
+ const imageBitmap = await createImageBitmap(image, settings);
1312
+ const ctx = canvas.getContext("bitmaprenderer") as ImageBitmapRenderingContext | null;
1313
+ if (ctx) {
1314
+ ctx.transferFromImageBitmap(imageBitmap);
1315
+ }
1316
+ return canvas as OffscreenCanvasExt;
1317
+ }
1318
+
1319
+ /** This method uses a '2d' canvas context for pixel manipulation, and can apply a color scale or Y flip to the given image.
1320
+ * Unfortunately, canvas always uses premultiplied data, and thus images with low alpha values (or multiplying by a=0) will result in black pixels.
1321
+ */
1322
+ async function imageToCanvas( image: HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap, color: Vector4 | undefined = undefined, flipY = false, maxTextureSize = 4096 ) {
1323
+
1324
+ if ( isImageBitmap( image ) ) {
1325
+
1326
+ // max. canvas size on Safari is still 4096x4096
1327
+ const scale = maxTextureSize / Math.max( image.width, image.height );
1328
+
1329
+ const canvas = new OffscreenCanvas( image.width * Math.min( 1, scale ), image.height * Math.min( 1, scale ) );
1330
+
1331
+ const context = canvas.getContext( '2d', { alpha: true, premultipliedAlpha: false } ) as OffscreenCanvasRenderingContext2D;
1332
+ if (!context) throw new Error('Could not get canvas 2D context');
1333
+
1334
+ if ( flipY === true ) {
1335
+
1336
+ context.translate( 0, canvas.height );
1337
+ context.scale( 1, - 1 );
1338
+
1339
+ }
1340
+
1341
+ context.drawImage( image, 0, 0, canvas.width, canvas.height );
1342
+
1343
+ // Currently only used to apply opacity scale since QuickLook and usdview don't support that yet
1344
+ if ( color !== undefined ) {
1345
+
1346
+ const r = color.x;
1347
+ const g = color.y;
1348
+ const b = color.z;
1349
+ const a = color.w;
1350
+
1351
+ const imagedata = context.getImageData( 0, 0, canvas.width, canvas.height );
1352
+ const data = imagedata.data;
1353
+
1354
+ for ( let i = 0; i < data.length; i += 4 ) {
1355
+
1356
+ data[ i + 0 ] = data[ i + 0 ] * r;
1357
+ data[ i + 1 ] = data[ i + 1 ] * g;
1358
+ data[ i + 2 ] = data[ i + 2 ] * b;
1359
+ data[ i + 3 ] = data[ i + 3 ] * a;
1360
+
1361
+ }
1362
+
1363
+ context.putImageData( imagedata, 0, 0 );
1364
+
1365
+ }
1366
+
1367
+ return canvas as OffscreenCanvasExt;
1368
+
1369
+ } else {
1370
+
1371
+ throw new Error( 'NeedleUSDZExporter: No valid image data found. Unable to process texture.' );
1372
+
1373
+ }
1374
+
1375
+ }
1376
+
1377
+ //
1378
+
1379
+ const PRECISION = 7;
1380
+
1381
+ function buildHeader() {
1382
+
1383
+ return `#usda 1.0
1384
+ (
1385
+ customLayerData = {
1386
+ string creator = "Needle Engine USDZExporter"
1387
+ }
1388
+ metersPerUnit = 1
1389
+ upAxis = "Y"
1390
+ )
1391
+ `;
1392
+
1393
+ }
1394
+
1395
+ function buildUSDFileAsString( dataToInsert, _context: USDZExporterContext ) {
1396
+
1397
+ let output = buildHeader();
1398
+ output += dataToInsert;
1399
+ return fflate.strToU8( output );
1400
+
1401
+ }
1402
+
1403
+ function getObjectId( object ) {
1404
+
1405
+ return object.name.replace( /[-<>\(\)\[\]§$%&\/\\\=\?\,\;]/g, '' ) + '_' + object.id;
1406
+
1407
+ }
1408
+
1409
+ function getBoneName(bone: Object3D) {
1410
+ return makeNameSafe(bone.name || 'bone_' + bone.uuid);
1411
+ }
1412
+
1413
+ function getGeometryName(geometry: BufferGeometry, _fallbackName: string) {
1414
+ // Using object names here breaks instancing...
1415
+ // So we removed fallbackName again. Downside: geometries don't have nice names...
1416
+ // A better workaround would be that we actually name them on glTF import (so the geometry has a name, not the object)
1417
+ return makeNameSafe(geometry.name || 'Geometry') + "_" + geometry.id;
1418
+ }
1419
+
1420
+ function getMaterialName(material: Material) {
1421
+ return makeNameSafe(material.name || 'Material') + "_" + material.id;
1422
+ }
1423
+
1424
+ function getPathToSkeleton(bone: Object3D, assumedRoot: Object3D) {
1425
+ let path = getBoneName(bone);
1426
+ let current = bone.parent;
1427
+ while ( current && current !== assumedRoot ) {
1428
+ path = getBoneName(current) + '/' + path;
1429
+ current = current.parent;
1430
+ }
1431
+ return path;
1432
+ }
1433
+
1434
+ // Xform
1435
+
1436
+ export function buildXform( model: USDObject | null, writer: USDWriter, context: USDZExporterContext ) {
1437
+
1438
+ if ( model == null)
1439
+ return;
1440
+
1441
+ Progress.report("export-usdz-xforms", { message: "buildXform " + model.displayName || model.name, autoStep: true });
1442
+
1443
+ // const matrix = model.matrix;
1444
+ const transform = model.transform;
1445
+ const geometry = model.geometry;
1446
+ const material = model.material;
1447
+ const camera = model.camera;
1448
+ const name = model.name;
1449
+
1450
+ if ( model.animations ) {
1451
+ for ( const animation of model.animations ) {
1452
+ context.animations.push( animation )
1453
+ }
1454
+ }
1455
+
1456
+ // const transform = buildMatrix( matrix );
1457
+
1458
+ /*
1459
+ if ( matrix.determinant() < 0 ) {
1460
+
1461
+ console.warn( 'NeedleUSDZExporter: USDZ does not support negative scales', name );
1462
+
1463
+ }
1464
+ */
1465
+
1466
+ const isSkinnedMesh = geometry && geometry.isBufferGeometry && geometry.attributes.skinIndex !== undefined && geometry.attributes.skinIndex.count > 0;
1467
+ const objType = isSkinnedMesh ? 'SkelRoot' : 'Xform';
1468
+ const _apiSchemas = new Array<string>();
1469
+
1470
+ // Specific case: the material is white unlit, the mesh has vertex colors, so we can
1471
+ // export as displayColor directly
1472
+ const isUnlitDisplayColor =
1473
+ material && material instanceof MeshBasicMaterial &&
1474
+ material.color && material.color.r === 1 && material.color.g === 1 && material.color.b === 1 &&
1475
+ !material.map && material.opacity === 1 &&
1476
+ geometry?.attributes.color;
1477
+
1478
+ if (geometry?.attributes.color && !isUnlitDisplayColor) {
1479
+ console.warn("NeedleUSDZExporter: Geometry has vertex colors. Vertex colors will only be shown in QuickLook for unlit materials with white color and no texture. Otherwise, they will be ignored.", model.displayName);
1480
+ }
1481
+
1482
+ writer.appendLine();
1483
+ if ( geometry ) {
1484
+ writer.beginBlock( `def ${objType} "${name}"`, "(", false );
1485
+ // NE-4084: To use the doubleSided workaround with skeletal meshes we'd have to
1486
+ // also emit extra data for jointIndices etc., so we're skipping skinned meshes here.
1487
+ if (context.quickLookCompatible && material && material.side === DoubleSide && !isSkinnedMesh)
1488
+ writer.appendLine(`prepend references = @./geometries/${getGeometryName(geometry, name)}.usda@</Geometry_doubleSided>`);
1489
+ else
1490
+ writer.appendLine(`prepend references = @./geometries/${getGeometryName(geometry, name)}.usda@</Geometry>`);
1491
+ if (!isUnlitDisplayColor)
1492
+ _apiSchemas.push("MaterialBindingAPI");
1493
+ if (isSkinnedMesh)
1494
+ _apiSchemas.push("SkelBindingAPI");
1495
+ }
1496
+ else if ( camera && !context.quickLookCompatible)
1497
+ writer.beginBlock( `def Camera "${name}"`, "(", false );
1498
+ else if ( model.type !== undefined)
1499
+ writer.beginBlock( `def ${model.type} "${name}"` );
1500
+ else // if (model.type === undefined)
1501
+ writer.beginBlock( `def Xform "${name}"`, "(", false);
1502
+
1503
+ if (model.type === undefined) {
1504
+ if (model.extraSchemas?.length)
1505
+ _apiSchemas.push(...model.extraSchemas);
1506
+ if (_apiSchemas.length)
1507
+ writer.appendLine(`prepend apiSchemas = [${_apiSchemas.map(s => `"${s}"`).join(', ')}]`);
1508
+ }
1509
+
1510
+ if (model.displayName)
1511
+ writer.appendLine(`displayName = "${makeDisplayNameSafe(model.displayName)}"`);
1512
+
1513
+ if ( camera || model.type === undefined) {
1514
+ writer.closeBlock( ")" );
1515
+ writer.beginBlock();
1516
+ }
1517
+
1518
+ if ( geometry && material ) {
1519
+ if (!isUnlitDisplayColor) {
1520
+ const materialName = getMaterialName(material);
1521
+ writer.appendLine( `rel material:binding = </StageRoot/Materials/${materialName}>` );
1522
+ }
1523
+
1524
+ // Turns out QuickLook / RealityKit doesn't support the doubleSided attribute, so we
1525
+ // work around that by emitting additional indices above, and then we shouldn't emit the attribute either as geometry is
1526
+ // already doubleSided then.
1527
+ if (!context.quickLookCompatible && material.side === DoubleSide ) {
1528
+ // double-sided is a mesh property in USD, we can apply it as `over` here
1529
+ writer.beginBlock( `over "Geometry" `);
1530
+ writer.appendLine( `uniform bool doubleSided = 1` );
1531
+ writer.closeBlock();
1532
+ }
1533
+ }
1534
+ let haveWrittenAnyXformOps = false;
1535
+ if ( isSkinnedMesh ) {
1536
+ writer.appendLine( `rel skel:skeleton = <Rig>` );
1537
+ writer.appendLine( `rel skel:animationSource = <Rig/_anim>`);
1538
+ haveWrittenAnyXformOps = false;
1539
+ // writer.appendLine( `matrix4d xformOp:transform = ${buildMatrix(new Matrix4())}` ); // always identity / in world space
1540
+ }
1541
+ else if (model.type === undefined) {
1542
+ if (transform) {
1543
+ haveWrittenAnyXformOps = haveWrittenAnyXformOps || (transform.position !== null || transform.quaternion !== null || transform.scale !== null);
1544
+ if (transform.position) {
1545
+ model.needsTranslate = true;
1546
+ writer.appendLine( `double3 xformOp:translate = (${fn(transform.position.x)}, ${fn(transform.position.y)}, ${fn(transform.position.z)})` );
1547
+ }
1548
+ if (transform.quaternion) {
1549
+ model.needsOrient = true;
1550
+ writer.appendLine( `quatf xformOp:orient = (${fn(transform.quaternion.w)}, ${fn(transform.quaternion.x)}, ${fn(transform.quaternion.y)}, ${fn(transform.quaternion.z)})` );
1551
+ }
1552
+ if (transform.scale) {
1553
+ model.needsScale = true;
1554
+ writer.appendLine( `double3 xformOp:scale = (${fn(transform.scale.x)}, ${fn(transform.scale.y)}, ${fn(transform.scale.z)})` );
1555
+ }
1556
+ }
1557
+ }
1558
+
1559
+ if (model.visibility !== undefined)
1560
+ writer.appendLine(`token visibility = "${model.visibility}"`);
1561
+
1562
+ if ( camera && !context.quickLookCompatible) {
1563
+
1564
+ if ( 'isOrthographicCamera' in camera && camera.isOrthographicCamera ) {
1565
+
1566
+ writer.appendLine( `float2 clippingRange = (${camera.near}, ${camera.far})` );
1567
+ writer.appendLine( `float horizontalAperture = ${( ( Math.abs( camera.left ) + Math.abs( camera.right ) ) * 10 ).toPrecision( PRECISION )}` );
1568
+ writer.appendLine( `float verticalAperture = ${( ( Math.abs( camera.top ) + Math.abs( camera.bottom ) ) * 10 ).toPrecision( PRECISION )}` );
1569
+ writer.appendLine( 'token projection = "orthographic"' );
1570
+
1571
+ } else if ( 'isPerspectiveCamera' in camera && camera.isPerspectiveCamera) {
1572
+
1573
+ writer.appendLine( `float2 clippingRange = (${camera.near.toPrecision( PRECISION )}, ${camera.far.toPrecision( PRECISION )})` );
1574
+ writer.appendLine( `float focalLength = ${camera.getFocalLength().toPrecision( PRECISION )}` );
1575
+ writer.appendLine( `float focusDistance = ${camera.focus.toPrecision( PRECISION )}` );
1576
+ writer.appendLine( `float horizontalAperture = ${camera.getFilmWidth().toPrecision( PRECISION )}` );
1577
+ writer.appendLine( 'token projection = "perspective"' );
1578
+ writer.appendLine( `float verticalAperture = ${camera.getFilmHeight().toPrecision( PRECISION )}` );
1579
+ }
1580
+
1581
+ }
1582
+
1583
+ if ( model.onSerialize ) {
1584
+
1585
+ model.onSerialize( writer, context );
1586
+
1587
+ }
1588
+
1589
+ // after serialization, we know which xformops to actually define here:
1590
+ if (model.type === undefined) {
1591
+ // TODO only write the necessary ones – this isn't trivial though because we need to know
1592
+ // if some of them are animated, and then we need to include those.
1593
+ // Best approach would likely be to write xformOpOrder _after_ onSerialize
1594
+ // and keep track of what was written in onSerialize (e.g. model.needsTranslate = true)
1595
+ const ops = new Array<string>();
1596
+ if (model.needsTranslate) ops.push('"xformOp:translate"');
1597
+ if (model.needsOrient) ops.push('"xformOp:orient"');
1598
+ if (model.needsScale) ops.push('"xformOp:scale"');
1599
+ if (ops.length)
1600
+ writer.appendLine( `uniform token[] xformOpOrder = [${ops.join(', ')}]` );
1601
+ }
1602
+
1603
+ if ( model.children ) {
1604
+
1605
+ writer.appendLine();
1606
+ for ( const ch of model.children ) {
1607
+
1608
+ buildXform( ch, writer, context );
1609
+
1610
+ }
1611
+
1612
+ }
1613
+
1614
+ writer.closeBlock();
1615
+
1616
+ }
1617
+
1618
+ function fn( num:number ): string {
1619
+
1620
+ return Number.isInteger(num) ? num.toString() : num.toFixed( 10 );
1621
+
1622
+ }
1623
+
1624
+ function buildMatrix( matrix ) {
1625
+
1626
+ const array = matrix.elements;
1627
+
1628
+ return `( ${buildMatrixRow( array, 0 )}, ${buildMatrixRow( array, 4 )}, ${buildMatrixRow( array, 8 )}, ${buildMatrixRow( array, 12 )} )`;
1629
+
1630
+ }
1631
+
1632
+ function buildMatrixRow( array, offset ) {
1633
+
1634
+ return `(${fn( array[ offset + 0 ] )}, ${fn( array[ offset + 1 ] )}, ${fn( array[ offset + 2 ] )}, ${fn( array[ offset + 3 ] )})`;
1635
+
1636
+ }
1637
+
1638
+ // Mesh
1639
+
1640
+ function buildMeshObject( geometry: BufferGeometry, bonesArray: Bone[] = [], quickLookCompatible: boolean = true) {
1641
+
1642
+ const mesh = buildMesh( geometry, bonesArray, quickLookCompatible );
1643
+ return `
1644
+ def "Geometry"
1645
+ ${mesh}
1646
+ `;
1647
+
1648
+ }
1649
+
1650
+ function buildMesh( geometry: BufferGeometry, bones: Bone[] = [], quickLookCompatible: boolean = true) {
1651
+
1652
+ const name = 'Geometry';
1653
+ const attributes = geometry.attributes;
1654
+ const count = attributes.position.count;
1655
+
1656
+ const hasBones = bones && bones.length > 0;
1657
+
1658
+ // We need to sort bones and all skinning data by path –
1659
+ // Neither glTF nor three.js care, but in USD they must be sorted
1660
+ // since the array defines the virtual hierarchy and is evaluated in that order
1661
+ const sortedBones: Array<{bone: Object3D, index: number}> = [];
1662
+ const indexMapping: number[] = [];
1663
+ let sortedSkinIndex = new Array<number>();
1664
+ let sortedSkinIndexAttribute: BufferAttribute | InterleavedBufferAttribute | null = attributes.skinIndex;
1665
+ let bonesArray = "";
1666
+ if (hasBones) {
1667
+ const uuidsFound:string[] = [];
1668
+ for (const bone of bones ) {
1669
+ // if (bone.parent!.type !== 'Bone')
1670
+ {
1671
+ sortedBones.push({bone: bone, index: bones.indexOf(bone)});
1672
+ uuidsFound.push(bone.uuid);
1673
+ }
1674
+ }
1675
+
1676
+ let maxSteps = 10_000;
1677
+ while (uuidsFound.length < bones.length && maxSteps-- > 0) {
1678
+ for (const sortedBone of sortedBones) {
1679
+ const children = sortedBone.bone.children as Bone[];
1680
+ for (const childBone of children){
1681
+ if (uuidsFound.indexOf(childBone.uuid) === -1 && bones.indexOf(childBone) !== -1){
1682
+ sortedBones.push({bone: childBone, index: bones.indexOf(childBone)});
1683
+ uuidsFound.push(childBone.uuid);
1684
+ }
1685
+ }
1686
+ }
1687
+ }
1688
+
1689
+ if (maxSteps <= 0) console.error("Failed to sort bones in skinned mesh", sortedBones, bones, uuidsFound);
1690
+
1691
+ // add structural nodes to the list of bones
1692
+ for (const structuralNode of findStructuralNodesInBoneHierarchy(bones) ) {
1693
+ sortedBones.push( { bone: structuralNode, index: sortedBones.length } );
1694
+ }
1695
+
1696
+ // sort bones by path – need to be sorted in the same order as during mesh export
1697
+ const assumedRoot = sortedBones[0].bone.parent!;
1698
+ sortedBones.sort((a, b) => getPathToSkeleton(a.bone, assumedRoot) > getPathToSkeleton(b.bone, assumedRoot) ? 1 : -1);
1699
+ bonesArray = sortedBones.map( x => "\"" + getPathToSkeleton(x.bone, assumedRoot) + "\"" ).join( ', ' );
1700
+
1701
+ // TODO we can probably skip the expensive attribute re-ordering if the bones were already in a correct order.
1702
+ // That doesn't mean that they are strictly sorted by path – just that all parents strictly need to come first.
1703
+
1704
+ // build index mapping
1705
+ for (const i in sortedBones) {
1706
+ indexMapping[sortedBones[i].index] = parseInt(i);
1707
+ }
1708
+
1709
+ // remap skin index attributes
1710
+ const skinIndex = attributes.skinIndex;
1711
+ sortedSkinIndex = new Array<number>();
1712
+ for ( let i = 0; i < skinIndex.count; i ++ ) {
1713
+
1714
+ const x = skinIndex.getX( i );
1715
+ const y = skinIndex.getY( i );
1716
+ const z = skinIndex.getZ( i );
1717
+ const w = skinIndex.getW( i );
1718
+
1719
+ sortedSkinIndex.push( indexMapping[x], indexMapping[y], indexMapping[z], indexMapping[w] );
1720
+ }
1721
+
1722
+ // turn it back into an attribute so the rest of the code doesn't need to learn a new thing
1723
+ sortedSkinIndexAttribute = new BufferAttribute( new Uint16Array( sortedSkinIndex ), 4 );
1724
+ }
1725
+
1726
+ const isSkinnedMesh = attributes.skinWeight && attributes.skinIndex;
1727
+
1728
+ return `
1729
+ {
1730
+ def Mesh "${name}" ${isSkinnedMesh ? `(
1731
+ prepend apiSchemas = ["SkelBindingAPI"]
1732
+ )` : ''}
1733
+ {
1734
+ int[] faceVertexCounts = [${buildMeshVertexCount( geometry )}]
1735
+ int[] faceVertexIndices = [${buildMeshVertexIndices( geometry )}]
1736
+ ${attributes.normal || quickLookCompatible ? // in QuickLook, normals are required, otherwise double-sided rendering doesn't work.
1737
+ `normal3f[] normals = [${buildVector3Array( attributes.normal, count )}] (
1738
+ interpolation = "vertex"
1739
+ )` : '' }
1740
+ point3f[] points = [${buildVector3Array( attributes.position, count )}]
1741
+ ${attributes.uv ?
1742
+ `texCoord2f[] primvars:st = [${buildVector2Array( attributes.uv, count, true )}] (
1743
+ interpolation = "vertex"
1744
+ )` : '' }
1745
+ ${attributes.uv1 ? buildCustomAttributeAccessor('st1', attributes.uv1) : '' }
1746
+ ${attributes.uv2 ? buildCustomAttributeAccessor('st2', attributes.uv2) : '' }
1747
+ ${attributes.uv3 ? buildCustomAttributeAccessor('st3', attributes.uv3) : '' }
1748
+ ${isSkinnedMesh ?
1749
+ `matrix4d primvars:skel:geomBindTransform = ( (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1) ) (
1750
+ elementSize = 1
1751
+ interpolation = "constant"
1752
+ )` : '' }
1753
+ ${attributes.skinIndex ?
1754
+ `int[] primvars:skel:jointIndices = [${buildVector4Array( sortedSkinIndexAttribute, true )}] (
1755
+ elementSize = 4
1756
+ interpolation = "vertex"
1757
+ )` : '' }
1758
+ ${attributes.skinWeight ?
1759
+ `float[] primvars:skel:jointWeights = [${buildVector4Array( attributes.skinWeight )}] (
1760
+ elementSize = 4
1761
+ interpolation = "vertex"
1762
+ )` : '' }
1763
+ ${attributes.color ?
1764
+ `color3f[] primvars:displayColor = [${buildVector3Array( attributes.color, count )}] (
1765
+ interpolation = "vertex"
1766
+ )` : '' }
1767
+ uniform token subdivisionScheme = "none"
1768
+ }
1769
+ }
1770
+ ${quickLookCompatible ? `
1771
+ # This is a workaround for QuickLook/RealityKit not supporting the doubleSided attribute. We're adding a second
1772
+ # geometry definition here, that uses the same mesh data but appends extra faces with reversed winding order.
1773
+ def "${name}_doubleSided" (
1774
+ prepend references = </Geometry>
1775
+ )
1776
+ {
1777
+ over "Geometry"
1778
+ {
1779
+ int[] faceVertexCounts = [${buildMeshVertexCount( geometry ) + ", " + buildMeshVertexCount( geometry )}]
1780
+ int[] faceVertexIndices = [${buildMeshVertexIndices( geometry ) + ", " + buildMeshVertexIndices( geometry, true )}]
1781
+ }
1782
+ }
1783
+ ` : '' }
1784
+ `;
1785
+ }
1786
+
1787
+ function buildMeshVertexCount( geometry: BufferGeometry ) {
1788
+
1789
+ const count = geometry.index !== null ? geometry.index.count : geometry.attributes.position.count;
1790
+
1791
+ return Array( Math.floor(count / 3) ).fill( 3 ).join( ', ' );
1792
+
1793
+ }
1794
+
1795
+ function buildMeshVertexIndices( geometry: BufferGeometry, reverseWinding: boolean = false ) {
1796
+
1797
+ const index = geometry.index;
1798
+ const array: Array<number> = [];
1799
+
1800
+ if ( index !== null ) {
1801
+
1802
+ for ( let i = 0; i < index.count; i ++ ) {
1803
+
1804
+ let val = i;
1805
+ if ( reverseWinding )
1806
+ val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1807
+ array.push( index.getX( val ) );
1808
+
1809
+ }
1810
+
1811
+ } else {
1812
+
1813
+ const length = geometry.attributes.position.count;
1814
+
1815
+ for ( let i = 0; i < length; i ++ ) {
1816
+
1817
+ let val = i;
1818
+ if ( reverseWinding )
1819
+ val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1820
+ array.push( val );
1821
+
1822
+ }
1823
+
1824
+ }
1825
+
1826
+ return array.join( ', ' );
1827
+
1828
+ }
1829
+
1830
+ /** Returns a string with the correct attribute declaration for the given attribute. Could have 2,3,4 components. */
1831
+ function buildCustomAttributeAccessor( primvarName: string, attribute: BufferAttribute | InterleavedBufferAttribute ) {
1832
+ const count = attribute.itemSize;
1833
+ switch (count) {
1834
+ case 2:
1835
+ // TODO: Check if we want to flip Y here as well. We do that for texcoords, but the data in UV1..N could be intended for other purposes.
1836
+ return `texCoord2f[] primvars:${primvarName} = [${buildVector2Array( attribute, count, true )}] (
1837
+ interpolation = "vertex"
1838
+ )`;
1839
+ case 3:
1840
+ return `texCoord3f[] primvars:${primvarName} = [${buildVector3Array( attribute, count )}] (
1841
+ interpolation = "vertex"
1842
+ )`;
1843
+ case 4:
1844
+ return `double4[] primvars:${primvarName} = [${buildVector4Array2( attribute, count )}] (
1845
+ interpolation = "vertex"
1846
+ )`;
1847
+ default:
1848
+ console.warn('USDZExporter: Attribute with ' + count + ' components are currently not supported. Results may be undefined for ' + primvarName + '.');
1849
+ return '';
1850
+ }
1851
+ }
1852
+
1853
+ function buildVector3Array( attribute, count ) {
1854
+
1855
+ if ( attribute === undefined ) {
1856
+
1857
+ console.warn( 'USDZExporter: A mesh attribute is missing and will be set with placeholder data. The result may look incorrect.' );
1858
+ return Array( count ).fill( '(0, 0, 1)' ).join( ', ' );
1859
+
1860
+ }
1861
+
1862
+ const array: Array<string> = [];
1863
+
1864
+ for ( let i = 0; i < attribute.count; i ++ ) {
1865
+
1866
+ const x = attribute.getX( i );
1867
+ const y = attribute.getY( i );
1868
+ const z = attribute.getZ( i );
1869
+
1870
+ array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )}, ${z.toPrecision( PRECISION )})` );
1871
+
1872
+ }
1873
+
1874
+ return array.join( ', ' );
1875
+
1876
+ }
1877
+
1878
+
1879
+ function buildVector4Array2( attribute, count ) {
1880
+
1881
+ if ( attribute === undefined ) {
1882
+
1883
+ console.warn( 'USDZExporter: Attribute is missing. Results may be undefined.' );
1884
+ return Array( count ).fill( '(0, 0, 0, 0)' ).join( ', ' );
1885
+
1886
+ }
1887
+
1888
+ const array: Array<string> = [];
1889
+
1890
+ for ( let i = 0; i < attribute.count; i ++ ) {
1891
+
1892
+ const x = attribute.getX( i );
1893
+ const y = attribute.getY( i );
1894
+ const z = attribute.getZ( i ) || 0;
1895
+ const w = attribute.getW( i ) || 0;
1896
+
1897
+ array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )}, ${z.toPrecision( PRECISION )}, ${w.toPrecision( PRECISION )})` );
1898
+
1899
+ }
1900
+
1901
+ return array.join( ', ' );
1902
+
1903
+ }
1904
+
1905
+ function buildVector4Array( attribute, ints = false ) {
1906
+ const array: Array<string> = [];
1907
+
1908
+ for ( let i = 0; i < attribute.count; i ++ ) {
1909
+
1910
+ const x = attribute.getX( i );
1911
+ const y = attribute.getY( i );
1912
+ const z = attribute.getZ( i );
1913
+ const w = attribute.getW( i );
1914
+
1915
+ array.push( `${ints ? x : x.toPrecision( PRECISION )}` );
1916
+ array.push( `${ints ? y : y.toPrecision( PRECISION )}` );
1917
+ array.push( `${ints ? z : z.toPrecision( PRECISION )}` );
1918
+ array.push( `${ints ? w : w.toPrecision( PRECISION )}` );
1919
+
1920
+ }
1921
+
1922
+ return array.join( ', ' );
1923
+
1924
+ }
1925
+
1926
+ function buildVector2Array( attribute: BufferAttribute | InterleavedBufferAttribute, count: number, flipY: boolean = false ) {
1927
+
1928
+ if ( attribute === undefined ) {
1929
+
1930
+ console.warn( 'USDZExporter: UVs missing.' );
1931
+ return Array( count ).fill( '(0, 0)' ).join( ', ' );
1932
+
1933
+ }
1934
+
1935
+ const array: Array<string> = [];
1936
+
1937
+ for ( let i = 0; i < attribute.count; i ++ ) {
1938
+
1939
+ const x = attribute.getX( i );
1940
+ let y = attribute.getY( i );
1941
+ if (flipY)
1942
+ y = 1 - y;
1943
+ array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )})` );
1944
+
1945
+ }
1946
+
1947
+ return array.join( ', ' );
1948
+
1949
+ }
1950
+
1951
+ // Materials
1952
+
1953
+ function buildMaterials( materials: Map<string, Material>, textures: TextureMap, quickLookCompatible = false ) {
1954
+
1955
+ const array: Array<string> = [];
1956
+
1957
+ for ( const uuid in materials ) {
1958
+
1959
+ const material = materials[ uuid ];
1960
+
1961
+ array.push( buildMaterial( material, textures, quickLookCompatible ) );
1962
+
1963
+ }
1964
+
1965
+ return `
1966
+ def "Materials"
1967
+ {
1968
+ ${array.join( '' )}
1969
+ }`;
1970
+ }
1971
+
1972
+ /** Slot of the exported texture. Some slots (e.g. normal, opacity) require additional processing. */
1973
+ declare type MapType = 'diffuse' | 'normal' | 'occlusion' | 'opacity' | 'roughness' | 'emissive' | 'metallic' | 'transmission';
1974
+
1975
+ function texName(tex: Texture) {
1976
+ // If we have a source, we only want to use the source's id, not the texture's id
1977
+ // to avoid exporting the same underlying data multiple times.
1978
+ return makeNameSafe(tex.name) + '_' + (tex.source?.id ?? tex.id);
1979
+ }
1980
+
1981
+ function buildTexture( texture: Texture, mapType: MapType, quickLookCompatible: boolean, textures: TextureMap, material, usedUVChannels, color: Color | undefined = undefined, opacity: number | undefined = undefined ) {
1982
+
1983
+ const name = texName(texture);
1984
+ // File names need to be unique; so when we're baking color and/or opacity into a texture we need to keep track of that.
1985
+ // TODO This currently results in textures potentially being exported multiple times that are actually identical!
1986
+ // const colorHex = color ? color.getHexString() : undefined;
1987
+ // const colorId = ( color && colorHex != 'ffffff' ? '_' + color.getHexString() : '' );
1988
+ const id = name + ( opacity !== undefined && opacity !== 1.0 ? '_' + opacity : '' );
1989
+
1990
+ // Seems neither QuickLook nor usdview support scale/bias on .a values, so we need to bake opacity multipliers into
1991
+ // the texture. This is not ideal.
1992
+ const opacityIsAppliedToTextureAndNotAsScale = quickLookCompatible && opacity !== undefined && opacity !== 1.0;
1993
+ const scaleToApply = opacityIsAppliedToTextureAndNotAsScale ? new Vector4(1, 1, 1, opacity) : undefined;
1994
+
1995
+ // Treat undefined opacity as 1
1996
+ if (opacity === undefined)
1997
+ opacity = 1.0;
1998
+
1999
+ // When we're baking opacity into the texture, we shouldn't apply it as scale as well – so we set it to 1.
2000
+ if (opacityIsAppliedToTextureAndNotAsScale)
2001
+ opacity = 1.0;
2002
+
2003
+ // sanitize scaleToApply.w === 0 - this isn't working right now, seems the PNG can't be read back properly.
2004
+ if (scaleToApply && scaleToApply.w <= 0.05) scaleToApply.w = 0.05;
2005
+
2006
+ textures[ id ] = { texture, scale: scaleToApply };
2007
+
2008
+ const uv = texture.channel > 0 ? 'st' + texture.channel : 'st';
2009
+ usedUVChannels.add(texture.channel);
2010
+
2011
+ const isRGBA = formatsWithAlphaChannel.includes( texture.format );
2012
+
2013
+ const WRAPPINGS = {
2014
+ 1000: 'repeat', // RepeatWrapping
2015
+ 1001: 'clamp', // ClampToEdgeWrapping
2016
+ 1002: 'mirror' // MirroredRepeatWrapping
2017
+ };
2018
+
2019
+ const repeat = texture.repeat.clone();
2020
+ const offset = texture.offset.clone();
2021
+ const rotation = texture.rotation;
2022
+
2023
+ // rotation is around the wrong point. after rotation we need to shift offset again so that we're rotating around the right spot
2024
+ const xRotationOffset = Math.sin(rotation);
2025
+ const yRotationOffset = Math.cos(rotation);
2026
+
2027
+ // texture coordinates start in the opposite corner, need to correct
2028
+ offset.y = 1 - offset.y - repeat.y;
2029
+
2030
+ // turns out QuickLook is buggy and interprets texture repeat inverted.
2031
+ // Apple Feedback: FB10036297 and FB11442287
2032
+ if ( quickLookCompatible ) {
2033
+
2034
+ // This is NOT correct yet in QuickLook, but comes close for a range of models.
2035
+ // It becomes more incorrect the bigger the offset is
2036
+
2037
+ // sanitize repeat values to avoid NaNs
2038
+ if (repeat.x === 0) repeat.x = 0.0001;
2039
+ if (repeat.y === 0) repeat.y = 0.0001;
2040
+
2041
+ offset.x = offset.x / repeat.x;
2042
+ offset.y = offset.y / repeat.y;
2043
+
2044
+ offset.x += xRotationOffset / repeat.x;
2045
+ offset.y += yRotationOffset - 1;
2046
+ }
2047
+
2048
+ else {
2049
+
2050
+ // results match glTF results exactly. verified correct in usdview.
2051
+ offset.x += xRotationOffset * repeat.x;
2052
+ offset.y += (1 - yRotationOffset) * repeat.y;
2053
+
2054
+ }
2055
+
2056
+ const materialName = getMaterialName(material);
2057
+
2058
+ const needsTextureTransform = ( repeat.x != 1 || repeat.y != 1 || offset.x != 0 || offset.y != 0 || rotation != 0 );
2059
+ const textureTransformInput = `${materialRoot}/${materialName}/${'uvReader_' + uv}.outputs:result>`; const textureTransformOutput = `${materialRoot}/${materialName}/Transform2d_${mapType}.outputs:result>`;
2060
+ const needsTextureScale = mapType !== 'normal' && (color && (color.r !== 1 || color.g !== 1 || color.b !== 1 || opacity !== 1)) || false;
2061
+
2062
+ const needsNormalScaleAndBias = mapType === 'normal';
2063
+ const normalScale = material instanceof MeshStandardMaterial ? (material.normalScale ? material.normalScale.x * 2 : 2) : 2;
2064
+ const normalScaleValueString = normalScale.toFixed( PRECISION );
2065
+ const normalBiasString = (-1 * (normalScale / 2)).toFixed( PRECISION );
2066
+ const normalBiasZString = (1 - normalScale).toFixed( PRECISION );
2067
+
2068
+ return `
2069
+ ${needsTextureTransform ? `def Shader "Transform2d_${mapType}" (
2070
+ sdrMetadata = {
2071
+ string role = "math"
2072
+ }
2073
+ )
2074
+ {
2075
+ uniform token info:id = "UsdTransform2d"
2076
+ float2 inputs:in.connect = ${textureTransformInput}
2077
+ float2 inputs:scale = ${buildVector2( repeat )}
2078
+ float2 inputs:translation = ${buildVector2( offset )}
2079
+ float inputs:rotation = ${(rotation / Math.PI * 180).toFixed( PRECISION )}
2080
+ float2 outputs:result
2081
+ }
2082
+ ` : '' }
2083
+ def Shader "${name}_${mapType}"
2084
+ {
2085
+ uniform token info:id = "UsdUVTexture"
2086
+ asset inputs:file = @textures/${id}.${isRGBA ? 'png' : 'jpg'}@
2087
+ token inputs:sourceColorSpace = "${ texture.colorSpace === 'srgb' ? 'sRGB' : 'raw' }"
2088
+ float2 inputs:st.connect = ${needsTextureTransform ? textureTransformOutput : textureTransformInput}
2089
+ ${needsTextureScale ? `
2090
+ float4 inputs:scale = (${color ? color.r + ', ' + color.g + ', ' + color.b : '1, 1, 1'}, ${opacity})
2091
+ ` : `` }
2092
+ ${needsNormalScaleAndBias ? `
2093
+ float4 inputs:scale = (${normalScaleValueString}, ${normalScaleValueString}, ${normalScaleValueString}, 1)
2094
+ float4 inputs:bias = (${normalBiasString}, ${normalBiasString}, ${normalBiasZString}, 0)
2095
+ ` : `` }
2096
+ token inputs:wrapS = "${ WRAPPINGS[ texture.wrapS ] }"
2097
+ token inputs:wrapT = "${ WRAPPINGS[ texture.wrapT ] }"
2098
+ float outputs:r
2099
+ float outputs:g
2100
+ float outputs:b
2101
+ float3 outputs:rgb
2102
+ ${material.transparent || material.alphaTest > 0.0 ? 'float outputs:a' : ''}
2103
+ }`;
2104
+ }
2105
+
2106
+ function buildMaterial( material: MeshBasicMaterial, textures: TextureMap, quickLookCompatible = false ) {
2107
+
2108
+ const materialName = getMaterialName(material);
2109
+
2110
+ // Special case: occluder material
2111
+ // Supported on iOS 18+ and visionOS 1+
2112
+ const isShadowCatcherMaterial =
2113
+ material.colorWrite === false ||
2114
+ (material.userData?.isShadowCatcherMaterial || material.userData?.isLightBlendMaterial);
2115
+ if (isShadowCatcherMaterial) {
2116
+
2117
+ // Two options here:
2118
+ // - ND_realitykit_occlusion_surfaceshader (non-shadow receiving)
2119
+ // - ND_realitykit_shadowreceiver_surfaceshader (shadow receiving)
2120
+
2121
+ const mode = (material.userData.isLightBlendMaterial || material.userData.isShadowCatcherMaterial)
2122
+ ? "ND_realitykit_shadowreceiver_surfaceshader"
2123
+ : "ND_realitykit_occlusion_surfaceshader";
2124
+
2125
+ return `
2126
+
2127
+ def Material "${materialName}" ${material.name ?`(
2128
+ displayName = "${material.name}"
2129
+ )` : ''}
2130
+ {
2131
+ token outputs:mtlx:surface.connect = ${materialRoot}/${materialName}/Occlusion.outputs:out>
2132
+
2133
+ def Shader "Occlusion"
2134
+ {
2135
+ uniform token info:id = "${mode}"
2136
+ token outputs:out
2137
+ }
2138
+ }`;
2139
+ }
2140
+
2141
+ // https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
2142
+
2143
+ const pad = ' ';
2144
+ const inputs: Array<string> = [];
2145
+ const samplers: Array<string> = [];
2146
+ const usedUVChannels: Set<number> = new Set();
2147
+
2148
+ if ((material as any).isMeshPhysicalNodeMaterial === true) {
2149
+ return buildNodeMaterial(material as unknown as MeshPhysicalNodeMaterial, materialName, textures);
2150
+ }
2151
+
2152
+ let effectiveOpacity = ( material.transparent || material.alphaTest ) ? material.opacity : 1;
2153
+ let haveConnectedOpacity = false;
2154
+ let haveConnectedOpacityThreshold = false;
2155
+
2156
+ if ( material instanceof MeshPhysicalMaterial && material.transmission !== undefined) {
2157
+
2158
+ // TODO does not help when a roughnessMap is used
2159
+ effectiveOpacity *= (1 - material.transmission * (1 - (material.roughness * 0.5)));
2160
+
2161
+ }
2162
+
2163
+ if ( material.map ) {
2164
+
2165
+ inputs.push( `${pad}color3f inputs:diffuseColor.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:rgb>` );
2166
+
2167
+ // Enforce alpha hashing in QuickLook for unlit materials
2168
+ if (material instanceof MeshBasicMaterial && material.transparent && material.alphaTest == 0.0 && quickLookCompatible) {
2169
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:a>` );
2170
+ haveConnectedOpacity = true;
2171
+ // see below – QuickLook applies alpha hashing instead of pure blending when
2172
+ // both opacity and opacityThreshold are connected
2173
+ inputs.push( `${pad}float inputs:opacityThreshold = ${0.0000000001}` );
2174
+ haveConnectedOpacityThreshold = true;
2175
+ }
2176
+ else if ( material.transparent ) {
2177
+
2178
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:a>` );
2179
+ haveConnectedOpacity = true;
2180
+
2181
+ } else if ( material.alphaTest > 0.0 ) {
2182
+
2183
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.map)}_diffuse.outputs:a>` );
2184
+ haveConnectedOpacity = true;
2185
+ inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
2186
+ haveConnectedOpacityThreshold = true;
2187
+
2188
+ }
2189
+
2190
+ samplers.push( buildTexture( material.map, 'diffuse', quickLookCompatible, textures, material, usedUVChannels, material.color, effectiveOpacity ) );
2191
+
2192
+ } else {
2193
+
2194
+ inputs.push( `${pad}color3f inputs:diffuseColor = ${buildColor( material.color )}` );
2195
+
2196
+ }
2197
+
2198
+ if ( material.alphaHash && quickLookCompatible) {
2199
+
2200
+ // Seems we can do this to basically enforce alpha hashing / dithered transparency in QuickLook –
2201
+ // works completely different in usdview though...
2202
+ if (haveConnectedOpacityThreshold) {
2203
+ console.warn('Opacity threshold for ' + material.name + ' was already connected. Skipping alphaHash opacity threshold.');
2204
+ }
2205
+ else {
2206
+ inputs.push( `${pad}float inputs:opacityThreshold = 0.0000000001` );
2207
+ haveConnectedOpacityThreshold = true;
2208
+ }
2209
+ }
2210
+
2211
+ if ( material.aoMap ) {
2212
+
2213
+ inputs.push( `${pad}float inputs:occlusion.connect = ${materialRoot}/${materialName}/${texName(material.aoMap)}_occlusion.outputs:r>` );
2214
+
2215
+ samplers.push( buildTexture( material.aoMap, 'occlusion', quickLookCompatible, textures, material, usedUVChannels ) );
2216
+
2217
+ }
2218
+
2219
+ if ( material.alphaMap ) {
2220
+
2221
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.alphaMap)}_opacity.outputs:r>` );
2222
+ // TODO this is likely not correct and will prevent blending from working correctly – masking will be used instead
2223
+ inputs.push( `${pad}float inputs:opacityThreshold = 0.0000000001` );
2224
+ haveConnectedOpacity = true;
2225
+ haveConnectedOpacityThreshold = true;
2226
+
2227
+ samplers.push( buildTexture( material.alphaMap, 'opacity', quickLookCompatible, textures, material, usedUVChannels, new Color( 1, 1, 1 ), effectiveOpacity ) );
2228
+
2229
+ } else { // opacity will always be connected if we have a map
2230
+
2231
+ if (haveConnectedOpacity) {
2232
+ console.warn('Opacity for ' + material.name + ' was already connected. Skipping default opacity.');
2233
+ } else {
2234
+ inputs.push( `${pad}float inputs:opacity = ${effectiveOpacity}` );
2235
+ haveConnectedOpacity = true;
2236
+ }
2237
+
2238
+ if ( material.alphaTest > 0.0 ) {
2239
+
2240
+ if (haveConnectedOpacityThreshold) {
2241
+ console.warn('Opacity threshold for ' + material.name + ' was already connected. Skipping default opacity threshold.');
2242
+ }
2243
+ else {
2244
+ inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
2245
+ haveConnectedOpacityThreshold = true;
2246
+ }
2247
+ }
2248
+
2249
+ }
2250
+
2251
+ if ( material instanceof MeshStandardMaterial ) {
2252
+
2253
+ if ( material.emissiveMap ) {
2254
+
2255
+ inputs.push( `${pad}color3f inputs:emissiveColor.connect = ${materialRoot}/${materialName}/${texName(material.emissiveMap)}_emissive.outputs:rgb>` );
2256
+ const color = material.emissive.clone();
2257
+ color.multiplyScalar( material.emissiveIntensity );
2258
+ samplers.push( buildTexture( material.emissiveMap, 'emissive', quickLookCompatible, textures, material, usedUVChannels, color ) );
2259
+
2260
+ } else if ( material.emissive?.getHex() > 0 ) {
2261
+
2262
+ const color = material.emissive.clone();
2263
+ color.multiplyScalar( material.emissiveIntensity );
2264
+ inputs.push( `${pad}color3f inputs:emissiveColor = ${buildColor( color )}` );
2265
+
2266
+ } else {
2267
+
2268
+ // We don't need to export (0,0,0) as emissive color
2269
+ // inputs.push( `${pad}color3f inputs:emissiveColor = (0, 0, 0)` );
2270
+
2271
+ }
2272
+
2273
+ if ( material.normalMap ) {
2274
+
2275
+ inputs.push( `${pad}normal3f inputs:normal.connect = ${materialRoot}/${materialName}/${texName(material.normalMap)}_normal.outputs:rgb>` );
2276
+
2277
+ samplers.push( buildTexture( material.normalMap, 'normal', quickLookCompatible, textures, material, usedUVChannels ) );
2278
+
2279
+ }
2280
+
2281
+ if ( material.roughnessMap && material.roughness === 1 ) {
2282
+
2283
+ inputs.push( `${pad}float inputs:roughness.connect = ${materialRoot}/${materialName}/${texName(material.roughnessMap)}_roughness.outputs:g>` );
2284
+
2285
+ samplers.push( buildTexture( material.roughnessMap, 'roughness', quickLookCompatible, textures, material, usedUVChannels ) );
2286
+
2287
+ } else {
2288
+
2289
+ inputs.push( `${pad}float inputs:roughness = ${material.roughness !== undefined ? material.roughness : 1 }` );
2290
+
2291
+ }
2292
+
2293
+ if ( material.metalnessMap && material.metalness === 1 ) {
2294
+
2295
+ inputs.push( `${pad}float inputs:metallic.connect = ${materialRoot}/${materialName}/${texName(material.metalnessMap)}_metallic.outputs:b>` );
2296
+
2297
+ samplers.push( buildTexture( material.metalnessMap, 'metallic', quickLookCompatible, textures, material, usedUVChannels ) );
2298
+
2299
+ } else {
2300
+
2301
+ inputs.push( `${pad}float inputs:metallic = ${material.metalness !== undefined ? material.metalness : 0 }` );
2302
+
2303
+ }
2304
+
2305
+ }
2306
+
2307
+ if ( material instanceof MeshPhysicalMaterial ) {
2308
+
2309
+ inputs.push( `${pad}float inputs:clearcoat = ${material.clearcoat}` );
2310
+ inputs.push( `${pad}float inputs:clearcoatRoughness = ${material.clearcoatRoughness}` );
2311
+ inputs.push( `${pad}float inputs:ior = ${material.ior}` );
2312
+
2313
+ if ( !material.transparent && ! (material.alphaTest > 0.0) && material.transmissionMap) {
2314
+
2315
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/${materialName}/${texName(material.transmissionMap)}_transmission.outputs:r>` );
2316
+
2317
+ samplers.push( buildTexture( material.transmissionMap, 'transmission', quickLookCompatible, textures, material, usedUVChannels ) );
2318
+ }
2319
+
2320
+ }
2321
+
2322
+ // check if usedUVChannels contains any data besides exactly 0 or 1
2323
+ if (usedUVChannels.size > 2) {
2324
+ console.warn('USDZExporter: Material ' + material.name + ' uses more than 2 UV channels. Currently, only UV0 and UV1 are supported.');
2325
+ }
2326
+ else if (usedUVChannels.size === 2) {
2327
+ // check if it's exactly 0 and 1
2328
+ if (!usedUVChannels.has(0) || !usedUVChannels.has(1)) {
2329
+ console.warn('USDZExporter: Material ' + material.name + ' uses UV channels other than 0 and 1. Currently, only UV0 and UV1 are supported.');
2330
+ }
2331
+ }
2332
+
2333
+ return `
2334
+
2335
+ def Material "${materialName}" ${material.name ?`(
2336
+ displayName = "${makeDisplayNameSafe(material.name)}"
2337
+ )` : ''}
2338
+ {
2339
+ token outputs:surface.connect = ${materialRoot}/${materialName}/PreviewSurface.outputs:surface>
2340
+
2341
+ def Shader "PreviewSurface"
2342
+ {
2343
+ uniform token info:id = "UsdPreviewSurface"
2344
+ ${inputs.join( '\n' )}
2345
+ int inputs:useSpecularWorkflow = ${material instanceof MeshBasicMaterial ? '1' : '0'}
2346
+ token outputs:surface
2347
+ }
2348
+ ${samplers.length > 0 ? `
2349
+ ${usedUVChannels.has(0) ? `
2350
+ def Shader "uvReader_st"
2351
+ {
2352
+ uniform token info:id = "UsdPrimvarReader_float2"
2353
+ token inputs:varname = "st"
2354
+ float2 inputs:fallback = (0.0, 0.0)
2355
+ float2 outputs:result
2356
+ }
2357
+ ` : ''}
2358
+ ${usedUVChannels.has(1) ? `
2359
+ def Shader "uvReader_st1"
2360
+ {
2361
+ uniform token info:id = "UsdPrimvarReader_float2"
2362
+ token inputs:varname = "st1"
2363
+ float2 inputs:fallback = (0.0, 0.0)
2364
+ float2 outputs:result
2365
+ }
2366
+ ` : ''}
2367
+ ${samplers.join( '\n' )}` : ''}
2368
+ }`;
2369
+ }
2370
+
2371
+ function buildColor( color ) {
2372
+
2373
+ return `(${color.r}, ${color.g}, ${color.b})`;
2374
+
2375
+ }
2376
+
2377
+ function buildVector2( vector ) {
2378
+
2379
+ return `(${ vector.x }, ${ vector.y })`;
2380
+
2381
+ }
2382
+
2383
+ const formatsWithAlphaChannel = [
2384
+ // uncompressed formats with alpha channel
2385
+ 1023, // RGBAFormat
2386
+ // compressed formats with alpha channel
2387
+ 33777, // RGBA_S3TC_DXT1_Format
2388
+ 33778, // RGBA_S3TC_DXT3_Format
2389
+ 33779, // RGBA_S3TC_DXT5_Format
2390
+ 35842, // RGBA_PVRTC_4BPPV1_Format
2391
+ 35843, // RGBA_PVRTC_2BPPV1_Format
2392
+ 37496, // RGBA_ETC2_EAC_Format
2393
+ 37808, // RGBA_ASTC_4x4_Format
2394
+ 37809, // RGBA_ASTC_5x4_Format
2395
+ 37810, // RGBA_ASTC_5x5_Format
2396
+ 37811, // RGBA_ASTC_6x5_Format
2397
+ 37812, // RGBA_ASTC_6x6_Format
2398
+ 37813, // RGBA_ASTC_8x5_Format
2399
+ 37814, // RGBA_ASTC_8x6_Format
2400
+ 37815, // RGBA_ASTC_8x8_Format
2401
+ 37816, // RGBA_ASTC_10x5_Format
2402
+ 37817, // RGBA_ASTC_10x6_Format
2403
+ 37818, // RGBA_ASTC_10x8_Format
2404
+ 37819, // RGBA_ASTC_10x10_Format
2405
+ 37820, // RGBA_ASTC_12x10_Format
2406
+ 37821, // RGBA_ASTC_12x12_Format
2407
+ 36492, // RGBA_BPTC_Format
2408
+ ];
2409
+
2410
+ export {
2411
+ buildMatrix,
2412
+ decompressGpuTexture,
2413
+ findStructuralNodesInBoneHierarchy,
2414
+ formatsWithAlphaChannel,
2415
+ getBoneName,
2416
+ getMaterialName as getMaterialNameForUSD,
2417
+ getPathToSkeleton,
2418
+ imageToCanvas,
2419
+ imageToCanvasUnpremultiplied,
2420
+ makeNameSafe as makeNameSafeForUSD,
2421
+ USDDocument,
2422
+ fn as usdNumberFormatting,
2423
+ USDObject,
2424
+ USDWriter,
2425
+ USDZExporter,
2426
+ USDZExporterContext,
2427
2427
  };