@needle-tools/engine 4.4.1 → 4.4.3-beta

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