@needle-tools/engine 3.28.6-beta → 3.28.7-beta.1

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 (848) hide show
  1. package/CHANGELOG.md +2259 -2253
  2. package/LICENSE.md +10 -10
  3. package/README.md +52 -52
  4. package/dist/needle-engine.js +7 -10
  5. package/dist/needle-engine.light.js +7 -10
  6. package/dist/needle-engine.light.min.js +7 -10
  7. package/dist/needle-engine.light.umd.cjs +7 -10
  8. package/dist/needle-engine.min.js +7 -10
  9. package/dist/needle-engine.umd.cjs +7 -10
  10. package/lib/engine/api.d.ts +52 -52
  11. package/lib/engine/api.js +51 -51
  12. package/lib/engine/assets/index.d.ts +1 -1
  13. package/lib/engine/assets/index.js +4 -4
  14. package/lib/engine/codegen/register_types.d.ts +1 -1
  15. package/lib/engine/codegen/register_types.js +439 -439
  16. package/lib/engine/debug/debug.d.ts +12 -12
  17. package/lib/engine/debug/debug.js +26 -26
  18. package/lib/engine/debug/debug_console.d.ts +2 -2
  19. package/lib/engine/debug/debug_console.js +204 -204
  20. package/lib/engine/debug/debug_overlay.d.ts +10 -10
  21. package/lib/engine/debug/debug_overlay.js +277 -277
  22. package/lib/engine/debug/index.d.ts +1 -1
  23. package/lib/engine/debug/index.js +1 -1
  24. package/lib/engine/engine_addressables.d.ts +75 -75
  25. package/lib/engine/engine_addressables.js +441 -441
  26. package/lib/engine/engine_application.d.ts +19 -19
  27. package/lib/engine/engine_application.js +45 -45
  28. package/lib/engine/engine_assetdatabase.d.ts +25 -25
  29. package/lib/engine/engine_assetdatabase.js +341 -341
  30. package/lib/engine/engine_camera.d.ts +6 -6
  31. package/lib/engine/engine_camera.js +23 -23
  32. package/lib/engine/engine_components.d.ts +17 -17
  33. package/lib/engine/engine_components.js +273 -273
  34. package/lib/engine/engine_components_internal.d.ts +11 -11
  35. package/lib/engine/engine_components_internal.js +41 -41
  36. package/lib/engine/engine_constants.d.ts +5 -5
  37. package/lib/engine/engine_constants.js +32 -32
  38. package/lib/engine/engine_context.d.ts +269 -269
  39. package/lib/engine/engine_context.js +1242 -1242
  40. package/lib/engine/engine_context_registry.d.ts +50 -50
  41. package/lib/engine/engine_context_registry.js +89 -89
  42. package/lib/engine/engine_coroutine.d.ts +4 -4
  43. package/lib/engine/engine_coroutine.js +21 -21
  44. package/lib/engine/engine_create_objects.d.ts +13 -13
  45. package/lib/engine/engine_create_objects.js +33 -33
  46. package/lib/engine/engine_default_parameters.d.ts +2 -2
  47. package/lib/engine/engine_default_parameters.js +3 -3
  48. package/lib/engine/engine_editor-sync.d.ts +17 -17
  49. package/lib/engine/engine_editor-sync.js +7 -7
  50. package/lib/engine/engine_element.d.ts +55 -55
  51. package/lib/engine/engine_element.js +559 -559
  52. package/lib/engine/engine_element_attributes.d.ts +49 -49
  53. package/lib/engine/engine_element_attributes.js +1 -1
  54. package/lib/engine/engine_element_extras.d.ts +6 -6
  55. package/lib/engine/engine_element_extras.js +13 -13
  56. package/lib/engine/engine_element_loading.d.ts +40 -40
  57. package/lib/engine/engine_element_loading.js +312 -312
  58. package/lib/engine/engine_element_overlay.d.ts +19 -19
  59. package/lib/engine/engine_element_overlay.js +143 -143
  60. package/lib/engine/engine_fileloader.d.ts +3 -3
  61. package/lib/engine/engine_fileloader.js +7 -7
  62. package/lib/engine/engine_gameobject.d.ts +39 -39
  63. package/lib/engine/engine_gameobject.js +559 -559
  64. package/lib/engine/engine_generic_utils.d.ts +1 -1
  65. package/lib/engine/engine_generic_utils.js +13 -13
  66. package/lib/engine/engine_gizmos.d.ts +26 -26
  67. package/lib/engine/engine_gizmos.js +282 -282
  68. package/lib/engine/engine_gltf.d.ts +13 -13
  69. package/lib/engine/engine_gltf.js +15 -15
  70. package/lib/engine/engine_gltf_builtin_components.d.ts +7 -7
  71. package/lib/engine/engine_gltf_builtin_components.js +298 -298
  72. package/lib/engine/engine_hot_reload.d.ts +5 -5
  73. package/lib/engine/engine_hot_reload.js +182 -182
  74. package/lib/engine/engine_input.d.ts +129 -129
  75. package/lib/engine/engine_input.js +799 -799
  76. package/lib/engine/engine_input_utils.d.ts +2 -2
  77. package/lib/engine/engine_input_utils.js +22 -22
  78. package/lib/engine/engine_instancing.d.ts +16 -16
  79. package/lib/engine/engine_instancing.js +36 -36
  80. package/lib/engine/engine_license.d.ts +4 -4
  81. package/lib/engine/engine_license.js +398 -398
  82. package/lib/engine/engine_lifecycle_api.d.ts +14 -14
  83. package/lib/engine/engine_lifecycle_api.js +24 -24
  84. package/lib/engine/engine_lifecycle_functions_internal.d.ts +6 -6
  85. package/lib/engine/engine_lifecycle_functions_internal.js +28 -28
  86. package/lib/engine/engine_lightdata.d.ts +23 -23
  87. package/lib/engine/engine_lightdata.js +86 -86
  88. package/lib/engine/engine_loaders.d.ts +7 -7
  89. package/lib/engine/engine_loaders.js +69 -69
  90. package/lib/engine/engine_mainloop_utils.d.ts +13 -13
  91. package/lib/engine/engine_mainloop_utils.js +426 -426
  92. package/lib/engine/engine_math.d.ts +43 -43
  93. package/lib/engine/engine_math.js +147 -147
  94. package/lib/engine/engine_networking.d.ts +176 -176
  95. package/lib/engine/engine_networking.js +649 -649
  96. package/lib/engine/engine_networking_auto.d.ts +24 -24
  97. package/lib/engine/engine_networking_auto.js +324 -324
  98. package/lib/engine/engine_networking_files.d.ts +23 -23
  99. package/lib/engine/engine_networking_files.js +176 -176
  100. package/lib/engine/engine_networking_files_default_components.d.ts +3 -3
  101. package/lib/engine/engine_networking_files_default_components.js +39 -39
  102. package/lib/engine/engine_networking_instantiate.d.ts +39 -39
  103. package/lib/engine/engine_networking_instantiate.js +302 -302
  104. package/lib/engine/engine_networking_peer.d.ts +15 -15
  105. package/lib/engine/engine_networking_peer.js +132 -132
  106. package/lib/engine/engine_networking_streams.d.ts +90 -90
  107. package/lib/engine/engine_networking_streams.js +428 -428
  108. package/lib/engine/engine_networking_types.d.ts +14 -14
  109. package/lib/engine/engine_networking_types.js +7 -7
  110. package/lib/engine/engine_networking_utils.d.ts +2 -2
  111. package/lib/engine/engine_networking_utils.js +20 -20
  112. package/lib/engine/engine_patcher.d.ts +10 -10
  113. package/lib/engine/engine_patcher.js +142 -142
  114. package/lib/engine/engine_physics.d.ts +115 -115
  115. package/lib/engine/engine_physics.js +228 -228
  116. package/lib/engine/engine_physics.types.d.ts +37 -37
  117. package/lib/engine/engine_physics.types.js +33 -33
  118. package/lib/engine/engine_physics_rapier.d.ts +112 -112
  119. package/lib/engine/engine_physics_rapier.js +1266 -1266
  120. package/lib/engine/engine_playerview.d.ts +26 -26
  121. package/lib/engine/engine_playerview.js +64 -64
  122. package/lib/engine/engine_scenelighting.d.ts +74 -74
  123. package/lib/engine/engine_scenelighting.js +285 -285
  124. package/lib/engine/engine_scenetools.d.ts +35 -35
  125. package/lib/engine/engine_scenetools.js +212 -212
  126. package/lib/engine/engine_serialization.d.ts +4 -4
  127. package/lib/engine/engine_serialization.js +4 -4
  128. package/lib/engine/engine_serialization_builtin_serializer.d.ts +62 -62
  129. package/lib/engine/engine_serialization_builtin_serializer.js +369 -369
  130. package/lib/engine/engine_serialization_core.d.ts +84 -84
  131. package/lib/engine/engine_serialization_core.js +576 -576
  132. package/lib/engine/engine_serialization_decorator.d.ts +15 -15
  133. package/lib/engine/engine_serialization_decorator.js +54 -54
  134. package/lib/engine/engine_setup.d.ts +1 -1
  135. package/lib/engine/engine_setup.js +2 -2
  136. package/lib/engine/engine_shaders.d.ts +31 -31
  137. package/lib/engine/engine_shaders.js +229 -229
  138. package/lib/engine/engine_shims.d.ts +3 -3
  139. package/lib/engine/engine_shims.js +22 -22
  140. package/lib/engine/engine_texture.d.ts +20 -20
  141. package/lib/engine/engine_texture.js +57 -57
  142. package/lib/engine/engine_three_utils.d.ts +51 -51
  143. package/lib/engine/engine_three_utils.js +342 -342
  144. package/lib/engine/engine_time.d.ts +19 -19
  145. package/lib/engine/engine_time.js +47 -47
  146. package/lib/engine/engine_types.d.ts +358 -358
  147. package/lib/engine/engine_types.js +72 -72
  148. package/lib/engine/engine_typestore.d.ts +16 -16
  149. package/lib/engine/engine_typestore.js +35 -35
  150. package/lib/engine/engine_util_decorator.d.ts +12 -12
  151. package/lib/engine/engine_util_decorator.js +115 -115
  152. package/lib/engine/engine_utils.d.ts +104 -104
  153. package/lib/engine/engine_utils.js +518 -518
  154. package/lib/engine/engine_utils_screenshot.d.ts +10 -10
  155. package/lib/engine/engine_utils_screenshot.js +70 -70
  156. package/lib/engine/engine_web_api.d.ts +12 -12
  157. package/lib/engine/engine_web_api.js +112 -112
  158. package/lib/engine/extensions/EXT_texture_exr.d.ts +8 -8
  159. package/lib/engine/extensions/EXT_texture_exr.js +32 -32
  160. package/lib/engine/extensions/NEEDLE_animator_controller_model.d.ts +116 -116
  161. package/lib/engine/extensions/NEEDLE_animator_controller_model.js +91 -91
  162. package/lib/engine/extensions/NEEDLE_components.d.ts +33 -33
  163. package/lib/engine/extensions/NEEDLE_components.js +206 -206
  164. package/lib/engine/extensions/NEEDLE_gameobject_data.d.ts +10 -10
  165. package/lib/engine/extensions/NEEDLE_gameobject_data.js +57 -57
  166. package/lib/engine/extensions/NEEDLE_lighting_settings.d.ts +38 -38
  167. package/lib/engine/extensions/NEEDLE_lighting_settings.js +183 -183
  168. package/lib/engine/extensions/NEEDLE_lightmaps.d.ts +18 -18
  169. package/lib/engine/extensions/NEEDLE_lightmaps.js +108 -108
  170. package/lib/engine/extensions/NEEDLE_persistent_assets.d.ts +11 -11
  171. package/lib/engine/extensions/NEEDLE_persistent_assets.js +63 -63
  172. package/lib/engine/extensions/NEEDLE_progressive.d.ts +41 -41
  173. package/lib/engine/extensions/NEEDLE_progressive.js +366 -366
  174. package/lib/engine/extensions/NEEDLE_render_objects.d.ts +13 -13
  175. package/lib/engine/extensions/NEEDLE_render_objects.js +159 -159
  176. package/lib/engine/extensions/NEEDLE_techniques_webgl.d.ts +39 -39
  177. package/lib/engine/extensions/NEEDLE_techniques_webgl.js +544 -544
  178. package/lib/engine/extensions/extension_resolver.d.ts +4 -4
  179. package/lib/engine/extensions/extension_resolver.js +1 -1
  180. package/lib/engine/extensions/extension_utils.d.ts +2 -2
  181. package/lib/engine/extensions/extension_utils.js +140 -140
  182. package/lib/engine/extensions/extensions.d.ts +21 -21
  183. package/lib/engine/extensions/extensions.js +94 -94
  184. package/lib/engine/extensions/index.d.ts +5 -5
  185. package/lib/engine/extensions/index.js +5 -5
  186. package/lib/engine/extensions/usage_tracker.d.ts +13 -13
  187. package/lib/engine/extensions/usage_tracker.js +61 -61
  188. package/lib/engine/js-extensions/Camera.d.ts +1 -1
  189. package/lib/engine/js-extensions/Camera.js +36 -36
  190. package/lib/engine/js-extensions/Layers.d.ts +3 -3
  191. package/lib/engine/js-extensions/Layers.js +19 -19
  192. package/lib/engine/js-extensions/index.d.ts +2 -2
  193. package/lib/engine/js-extensions/index.js +2 -2
  194. package/lib/engine/shaders/shaderData.d.ts +55 -55
  195. package/lib/engine/shaders/shaderData.js +58 -58
  196. package/lib/engine/tests/test_utils.d.ts +2 -2
  197. package/lib/engine/tests/test_utils.js +53 -53
  198. package/lib/engine-components/AlignmentConstraint.d.ts +10 -10
  199. package/lib/engine-components/AlignmentConstraint.js +39 -39
  200. package/lib/engine-components/Animation.d.ts +53 -53
  201. package/lib/engine-components/Animation.js +333 -333
  202. package/lib/engine-components/AnimationCurve.d.ts +16 -16
  203. package/lib/engine-components/AnimationCurve.js +97 -97
  204. package/lib/engine-components/AnimationUtils.d.ts +8 -8
  205. package/lib/engine-components/AnimationUtils.js +110 -110
  206. package/lib/engine-components/Animator.d.ts +81 -81
  207. package/lib/engine-components/Animator.js +229 -229
  208. package/lib/engine-components/AnimatorController.d.ts +57 -57
  209. package/lib/engine-components/AnimatorController.js +887 -887
  210. package/lib/engine-components/AudioListener.d.ts +7 -7
  211. package/lib/engine-components/AudioListener.js +30 -30
  212. package/lib/engine-components/AudioSource.d.ts +61 -61
  213. package/lib/engine-components/AudioSource.js +422 -422
  214. package/lib/engine-components/AvatarLoader.d.ts +19 -19
  215. package/lib/engine-components/AvatarLoader.js +173 -173
  216. package/lib/engine-components/AxesHelper.d.ts +9 -9
  217. package/lib/engine-components/AxesHelper.js +44 -44
  218. package/lib/engine-components/BasicIKConstraint.d.ts +9 -9
  219. package/lib/engine-components/BasicIKConstraint.js +43 -43
  220. package/lib/engine-components/BoxHelperComponent.d.ts +16 -16
  221. package/lib/engine-components/BoxHelperComponent.js +89 -89
  222. package/lib/engine-components/Camera.d.ts +70 -70
  223. package/lib/engine-components/Camera.js +450 -450
  224. package/lib/engine-components/CameraUtils.d.ts +1 -1
  225. package/lib/engine-components/CameraUtils.js +77 -77
  226. package/lib/engine-components/CharacterController.d.ts +46 -46
  227. package/lib/engine-components/CharacterController.js +227 -227
  228. package/lib/engine-components/Collider.d.ts +46 -46
  229. package/lib/engine-components/Collider.js +153 -153
  230. package/lib/engine-components/Component.d.ts +228 -228
  231. package/lib/engine-components/Component.js +541 -541
  232. package/lib/engine-components/ContactShadows.d.ts +23 -23
  233. package/lib/engine-components/ContactShadows.js +233 -233
  234. package/lib/engine-components/DeleteBox.d.ts +9 -9
  235. package/lib/engine-components/DeleteBox.js +30 -30
  236. package/lib/engine-components/DeviceFlag.d.ts +12 -12
  237. package/lib/engine-components/DeviceFlag.js +43 -43
  238. package/lib/engine-components/DragControls.d.ts +51 -51
  239. package/lib/engine-components/DragControls.js +516 -516
  240. package/lib/engine-components/DropListener.d.ts +15 -15
  241. package/lib/engine-components/DropListener.js +120 -120
  242. package/lib/engine-components/Duplicatable.d.ts +16 -16
  243. package/lib/engine-components/Duplicatable.js +150 -150
  244. package/lib/engine-components/EventList.d.ts +28 -28
  245. package/lib/engine-components/EventList.js +105 -105
  246. package/lib/engine-components/EventTrigger.d.ts +12 -12
  247. package/lib/engine-components/EventTrigger.js +50 -50
  248. package/lib/engine-components/EventType.d.ts +19 -19
  249. package/lib/engine-components/EventType.js +71 -71
  250. package/lib/engine-components/FlyControls.d.ts +7 -7
  251. package/lib/engine-components/FlyControls.js +25 -25
  252. package/lib/engine-components/Fog.d.ts +20 -20
  253. package/lib/engine-components/Fog.js +60 -60
  254. package/lib/engine-components/Gizmos.d.ts +12 -12
  255. package/lib/engine-components/Gizmos.js +60 -60
  256. package/lib/engine-components/GridHelper.d.ts +12 -12
  257. package/lib/engine-components/GridHelper.js +47 -47
  258. package/lib/engine-components/GroundProjection.d.ts +21 -21
  259. package/lib/engine-components/GroundProjection.js +97 -97
  260. package/lib/engine-components/Interactable.d.ts +10 -10
  261. package/lib/engine-components/Interactable.js +11 -11
  262. package/lib/engine-components/Joints.d.ts +19 -19
  263. package/lib/engine-components/Joints.js +51 -51
  264. package/lib/engine-components/LODGroup.d.ts +30 -30
  265. package/lib/engine-components/LODGroup.js +145 -145
  266. package/lib/engine-components/Light.d.ts +75 -75
  267. package/lib/engine-components/Light.js +475 -475
  268. package/lib/engine-components/LookAtConstraint.d.ts +7 -7
  269. package/lib/engine-components/LookAtConstraint.js +17 -17
  270. package/lib/engine-components/NestedGltf.d.ts +11 -11
  271. package/lib/engine-components/NestedGltf.js +74 -74
  272. package/lib/engine-components/Networking.d.ts +11 -11
  273. package/lib/engine-components/Networking.js +70 -70
  274. package/lib/engine-components/OffsetConstraint.d.ts +14 -14
  275. package/lib/engine-components/OffsetConstraint.js +65 -65
  276. package/lib/engine-components/OrbitControls.d.ts +111 -111
  277. package/lib/engine-components/OrbitControls.js +646 -646
  278. package/lib/engine-components/ParticleSystem.d.ts +145 -145
  279. package/lib/engine-components/ParticleSystem.js +1077 -1077
  280. package/lib/engine-components/ParticleSystemModules.d.ts +489 -489
  281. package/lib/engine-components/ParticleSystemModules.js +1667 -1667
  282. package/lib/engine-components/ParticleSystemSubEmitter.d.ts +25 -25
  283. package/lib/engine-components/ParticleSystemSubEmitter.js +86 -86
  284. package/lib/engine-components/PlayerColor.d.ts +13 -13
  285. package/lib/engine-components/PlayerColor.js +83 -83
  286. package/lib/engine-components/ReflectionProbe.d.ts +22 -22
  287. package/lib/engine-components/ReflectionProbe.js +181 -181
  288. package/lib/engine-components/Renderer.d.ts +112 -112
  289. package/lib/engine-components/Renderer.js +1029 -1029
  290. package/lib/engine-components/RendererLightmap.d.ts +19 -19
  291. package/lib/engine-components/RendererLightmap.js +127 -127
  292. package/lib/engine-components/RigidBody.d.ts +120 -120
  293. package/lib/engine-components/RigidBody.js +452 -452
  294. package/lib/engine-components/SceneSwitcher.d.ts +72 -72
  295. package/lib/engine-components/SceneSwitcher.js +583 -583
  296. package/lib/engine-components/ScreenCapture.d.ts +64 -64
  297. package/lib/engine-components/ScreenCapture.js +405 -405
  298. package/lib/engine-components/ShadowCatcher.d.ts +18 -18
  299. package/lib/engine-components/ShadowCatcher.js +144 -144
  300. package/lib/engine-components/Skybox.d.ts +23 -23
  301. package/lib/engine-components/Skybox.js +287 -287
  302. package/lib/engine-components/SmoothFollow.d.ts +14 -14
  303. package/lib/engine-components/SmoothFollow.js +63 -63
  304. package/lib/engine-components/SpatialTrigger.d.ts +27 -27
  305. package/lib/engine-components/SpatialTrigger.js +144 -144
  306. package/lib/engine-components/SpectatorCamera.d.ts +45 -45
  307. package/lib/engine-components/SpectatorCamera.js +593 -593
  308. package/lib/engine-components/SpriteRenderer.d.ts +48 -48
  309. package/lib/engine-components/SpriteRenderer.js +257 -257
  310. package/lib/engine-components/SyncedCamera.d.ts +27 -27
  311. package/lib/engine-components/SyncedCamera.js +187 -187
  312. package/lib/engine-components/SyncedRoom.d.ts +24 -24
  313. package/lib/engine-components/SyncedRoom.js +162 -162
  314. package/lib/engine-components/SyncedTransform.d.ts +35 -35
  315. package/lib/engine-components/SyncedTransform.js +265 -265
  316. package/lib/engine-components/TestRunner.d.ts +13 -13
  317. package/lib/engine-components/TestRunner.js +99 -99
  318. package/lib/engine-components/TransformGizmo.d.ts +16 -16
  319. package/lib/engine-components/TransformGizmo.js +148 -148
  320. package/lib/engine-components/VideoPlayer.d.ts +86 -86
  321. package/lib/engine-components/VideoPlayer.js +792 -792
  322. package/lib/engine-components/Voip.d.ts +29 -29
  323. package/lib/engine-components/Voip.js +203 -203
  324. package/lib/engine-components/XRFlag.d.ts +33 -33
  325. package/lib/engine-components/XRFlag.js +128 -128
  326. package/lib/engine-components/api.d.ts +15 -15
  327. package/lib/engine-components/api.js +15 -15
  328. package/lib/engine-components/avatar/AvatarBlink_Simple.d.ts +10 -10
  329. package/lib/engine-components/avatar/AvatarBlink_Simple.js +75 -75
  330. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.d.ts +13 -13
  331. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js +74 -74
  332. package/lib/engine-components/avatar/Avatar_Brain_LookAt.d.ts +27 -27
  333. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js +119 -119
  334. package/lib/engine-components/avatar/Avatar_MouthShapes.d.ts +13 -13
  335. package/lib/engine-components/avatar/Avatar_MouthShapes.js +78 -78
  336. package/lib/engine-components/avatar/Avatar_MustacheShake.d.ts +8 -8
  337. package/lib/engine-components/avatar/Avatar_MustacheShake.js +28 -28
  338. package/lib/engine-components/codegen/components.d.ts +216 -216
  339. package/lib/engine-components/codegen/components.js +217 -217
  340. package/lib/engine-components/debug/LogStats.d.ts +5 -5
  341. package/lib/engine-components/debug/LogStats.js +18 -18
  342. package/lib/engine-components/export/gltf/GltfExport.d.ts +25 -25
  343. package/lib/engine-components/export/gltf/GltfExport.js +215 -215
  344. package/lib/engine-components/export/index.d.ts +1 -1
  345. package/lib/engine-components/export/index.js +1 -1
  346. package/lib/engine-components/export/usdz/Extension.d.ts +10 -10
  347. package/lib/engine-components/export/usdz/Extension.js +1 -1
  348. package/lib/engine-components/export/usdz/ThreeUSDZExporter.d.ts +114 -114
  349. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +1211 -1211
  350. package/lib/engine-components/export/usdz/USDZExporter.d.ts +59 -59
  351. package/lib/engine-components/export/usdz/USDZExporter.js +450 -450
  352. package/lib/engine-components/export/usdz/extensions/Animation.d.ts +69 -69
  353. package/lib/engine-components/export/usdz/extensions/Animation.js +650 -650
  354. package/lib/engine-components/export/usdz/extensions/DocumentExtension.d.ts +5 -5
  355. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js +6 -6
  356. package/lib/engine-components/export/usdz/extensions/USDZText.d.ts +55 -55
  357. package/lib/engine-components/export/usdz/extensions/USDZText.js +246 -246
  358. package/lib/engine-components/export/usdz/extensions/USDZUI.d.ts +8 -8
  359. package/lib/engine-components/export/usdz/extensions/USDZUI.js +100 -100
  360. package/lib/engine-components/export/usdz/extensions/behavior/Actions.d.ts +30 -30
  361. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js +88 -88
  362. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.d.ts +9 -9
  363. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.js +52 -52
  364. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.d.ts +22 -22
  365. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js +134 -134
  366. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.d.ts +126 -126
  367. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js +824 -824
  368. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.d.ts +133 -133
  369. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js +464 -464
  370. package/lib/engine-components/export/usdz/index.d.ts +3 -3
  371. package/lib/engine-components/export/usdz/index.js +2 -2
  372. package/lib/engine-components/export/usdz/utils/animationutils.d.ts +3 -3
  373. package/lib/engine-components/export/usdz/utils/animationutils.js +85 -85
  374. package/lib/engine-components/export/usdz/utils/quicklook.d.ts +2 -2
  375. package/lib/engine-components/export/usdz/utils/quicklook.js +35 -35
  376. package/lib/engine-components/export/usdz/utils/timeutils.d.ts +1 -1
  377. package/lib/engine-components/export/usdz/utils/timeutils.js +14 -14
  378. package/lib/engine-components/js-extensions/ExtensionUtils.d.ts +6 -6
  379. package/lib/engine-components/js-extensions/ExtensionUtils.js +65 -65
  380. package/lib/engine-components/js-extensions/Object3D.d.ts +2 -2
  381. package/lib/engine-components/js-extensions/Object3D.js +140 -140
  382. package/lib/engine-components/js-extensions/RGBAColor.d.ts +14 -14
  383. package/lib/engine-components/js-extensions/RGBAColor.js +49 -49
  384. package/lib/engine-components/js-extensions/index.d.ts +3 -3
  385. package/lib/engine-components/js-extensions/index.js +3 -3
  386. package/lib/engine-components/postprocessing/Effects/Antialiasing.d.ts +13 -13
  387. package/lib/engine-components/postprocessing/Effects/Antialiasing.js +45 -45
  388. package/lib/engine-components/postprocessing/Effects/Bloom.d.ts +12 -12
  389. package/lib/engine-components/postprocessing/Effects/Bloom.js +77 -77
  390. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.d.ts +8 -8
  391. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.js +38 -38
  392. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.d.ts +12 -12
  393. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.js +81 -81
  394. package/lib/engine-components/postprocessing/Effects/DepthOfField.d.ts +21 -21
  395. package/lib/engine-components/postprocessing/Effects/DepthOfField.js +97 -97
  396. package/lib/engine-components/postprocessing/Effects/Pixelation.d.ts +7 -7
  397. package/lib/engine-components/postprocessing/Effects/Pixelation.js +28 -28
  398. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.d.ts +13 -13
  399. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.js +86 -86
  400. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.d.ts +24 -24
  401. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.js +94 -94
  402. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.d.ts +13 -13
  403. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.js +62 -62
  404. package/lib/engine-components/postprocessing/Effects/Tonemapping.d.ts +16 -16
  405. package/lib/engine-components/postprocessing/Effects/Tonemapping.js +51 -51
  406. package/lib/engine-components/postprocessing/Effects/Vignette.d.ts +11 -11
  407. package/lib/engine-components/postprocessing/Effects/Vignette.js +56 -56
  408. package/lib/engine-components/postprocessing/PostProcessingEffect.d.ts +33 -33
  409. package/lib/engine-components/postprocessing/PostProcessingEffect.js +126 -126
  410. package/lib/engine-components/postprocessing/PostProcessingHandler.d.ts +22 -22
  411. package/lib/engine-components/postprocessing/PostProcessingHandler.js +201 -201
  412. package/lib/engine-components/postprocessing/Volume.d.ts +25 -25
  413. package/lib/engine-components/postprocessing/Volume.js +193 -193
  414. package/lib/engine-components/postprocessing/VolumeParameter.d.ts +22 -22
  415. package/lib/engine-components/postprocessing/VolumeParameter.js +80 -80
  416. package/lib/engine-components/postprocessing/VolumeProfile.d.ts +7 -7
  417. package/lib/engine-components/postprocessing/VolumeProfile.js +41 -41
  418. package/lib/engine-components/postprocessing/index.d.ts +4 -4
  419. package/lib/engine-components/postprocessing/index.js +4 -4
  420. package/lib/engine-components/timeline/PlayableDirector.d.ts +107 -107
  421. package/lib/engine-components/timeline/PlayableDirector.js +624 -624
  422. package/lib/engine-components/timeline/SignalAsset.d.ts +18 -18
  423. package/lib/engine-components/timeline/SignalAsset.js +124 -124
  424. package/lib/engine-components/timeline/TimelineModels.d.ts +88 -88
  425. package/lib/engine-components/timeline/TimelineModels.js +22 -22
  426. package/lib/engine-components/timeline/TimelineTracks.d.ts +90 -90
  427. package/lib/engine-components/timeline/TimelineTracks.js +825 -825
  428. package/lib/engine-components/timeline/index.d.ts +4 -4
  429. package/lib/engine-components/timeline/index.js +3 -3
  430. package/lib/engine-components/ui/BaseUIComponent.d.ts +31 -31
  431. package/lib/engine-components/ui/BaseUIComponent.js +161 -161
  432. package/lib/engine-components/ui/Button.d.ts +56 -56
  433. package/lib/engine-components/ui/Button.js +282 -282
  434. package/lib/engine-components/ui/Canvas.d.ts +67 -67
  435. package/lib/engine-components/ui/Canvas.js +382 -382
  436. package/lib/engine-components/ui/CanvasGroup.d.ts +15 -15
  437. package/lib/engine-components/ui/CanvasGroup.js +53 -53
  438. package/lib/engine-components/ui/EventSystem.d.ts +102 -102
  439. package/lib/engine-components/ui/EventSystem.js +641 -641
  440. package/lib/engine-components/ui/Graphic.d.ts +45 -45
  441. package/lib/engine-components/ui/Graphic.js +236 -236
  442. package/lib/engine-components/ui/Image.d.ts +27 -27
  443. package/lib/engine-components/ui/Image.js +107 -107
  444. package/lib/engine-components/ui/InputField.d.ts +34 -34
  445. package/lib/engine-components/ui/InputField.js +234 -234
  446. package/lib/engine-components/ui/Interfaces.d.ts +38 -38
  447. package/lib/engine-components/ui/Interfaces.js +12 -12
  448. package/lib/engine-components/ui/Layout.d.ts +72 -72
  449. package/lib/engine-components/ui/Layout.js +318 -318
  450. package/lib/engine-components/ui/Outline.d.ts +7 -7
  451. package/lib/engine-components/ui/Outline.js +20 -20
  452. package/lib/engine-components/ui/PointerEvents.d.ts +64 -64
  453. package/lib/engine-components/ui/PointerEvents.js +68 -68
  454. package/lib/engine-components/ui/RaycastUtils.d.ts +11 -11
  455. package/lib/engine-components/ui/RaycastUtils.js +67 -67
  456. package/lib/engine-components/ui/Raycaster.d.ts +18 -18
  457. package/lib/engine-components/ui/Raycaster.js +69 -69
  458. package/lib/engine-components/ui/RectTransform.d.ts +61 -61
  459. package/lib/engine-components/ui/RectTransform.js +343 -343
  460. package/lib/engine-components/ui/SpatialHtml.d.ts +6 -6
  461. package/lib/engine-components/ui/SpatialHtml.js +57 -57
  462. package/lib/engine-components/ui/Text.d.ts +74 -74
  463. package/lib/engine-components/ui/Text.js +534 -534
  464. package/lib/engine-components/ui/Utils.d.ts +23 -23
  465. package/lib/engine-components/ui/Utils.js +90 -90
  466. package/lib/engine-components/ui/index.d.ts +1 -1
  467. package/lib/engine-components/ui/index.js +1 -1
  468. package/lib/engine-components/utils/LookAt.d.ts +13 -13
  469. package/lib/engine-components/utils/LookAt.js +59 -59
  470. package/lib/engine-components/utils/OpenURL.d.ts +21 -21
  471. package/lib/engine-components/utils/OpenURL.js +124 -124
  472. package/lib/engine-components/webxr/WebARCameraBackground.d.ts +19 -19
  473. package/lib/engine-components/webxr/WebARCameraBackground.js +193 -193
  474. package/lib/engine-components/webxr/WebARSessionRoot.d.ts +38 -38
  475. package/lib/engine-components/webxr/WebARSessionRoot.js +407 -407
  476. package/lib/engine-components/webxr/WebXR.d.ts +110 -110
  477. package/lib/engine-components/webxr/WebXR.js +672 -672
  478. package/lib/engine-components/webxr/WebXRAvatar.d.ts +61 -61
  479. package/lib/engine-components/webxr/WebXRAvatar.js +289 -289
  480. package/lib/engine-components/webxr/WebXRController.d.ts +154 -154
  481. package/lib/engine-components/webxr/WebXRController.js +1028 -1028
  482. package/lib/engine-components/webxr/WebXRGrabRendering.d.ts +42 -42
  483. package/lib/engine-components/webxr/WebXRGrabRendering.js +137 -137
  484. package/lib/engine-components/webxr/WebXRImageTracking.d.ts +49 -49
  485. package/lib/engine-components/webxr/WebXRImageTracking.js +336 -336
  486. package/lib/engine-components/webxr/WebXRPlaneTracking.d.ts +49 -49
  487. package/lib/engine-components/webxr/WebXRPlaneTracking.js +372 -372
  488. package/lib/engine-components/webxr/WebXRRig.d.ts +4 -4
  489. package/lib/engine-components/webxr/WebXRRig.js +19 -19
  490. package/lib/engine-components/webxr/WebXRSync.d.ts +54 -54
  491. package/lib/engine-components/webxr/WebXRSync.js +410 -410
  492. package/lib/engine-components/webxr/index.d.ts +4 -4
  493. package/lib/engine-components/webxr/index.js +4 -4
  494. package/lib/engine-components-experimental/Presentation.d.ts +6 -6
  495. package/lib/engine-components-experimental/Presentation.js +9 -9
  496. package/lib/engine-components-experimental/api.d.ts +1 -1
  497. package/lib/engine-components-experimental/api.js +1 -1
  498. package/lib/engine-components-experimental/networking/PlayerSync.d.ts +50 -50
  499. package/lib/engine-components-experimental/networking/PlayerSync.js +200 -200
  500. package/lib/engine-schemes/api.d.ts +1 -1
  501. package/lib/engine-schemes/api.js +1 -1
  502. package/lib/engine-schemes/schemes.d.ts +7 -7
  503. package/lib/engine-schemes/schemes.js +19 -19
  504. package/lib/engine-schemes/synced-camera-model.d.ts +25 -25
  505. package/lib/engine-schemes/synced-camera-model.js +67 -67
  506. package/lib/engine-schemes/synced-transform-model.d.ts +31 -31
  507. package/lib/engine-schemes/synced-transform-model.js +66 -66
  508. package/lib/engine-schemes/transform.d.ts +12 -12
  509. package/lib/engine-schemes/transform.js +39 -39
  510. package/lib/engine-schemes/vec3.d.ts +11 -11
  511. package/lib/engine-schemes/vec3.js +29 -29
  512. package/lib/engine-schemes/vec4.d.ts +12 -12
  513. package/lib/engine-schemes/vec4.js +33 -33
  514. package/lib/engine-schemes/vr-user-state-buffer.d.ts +36 -36
  515. package/lib/engine-schemes/vr-user-state-buffer.js +103 -103
  516. package/lib/include/three/ARButton.d.ts +3 -3
  517. package/lib/include/three/ARButton.js +151 -151
  518. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.d.ts +6 -6
  519. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.js +45 -45
  520. package/lib/include/three/VRButton.d.ts +5 -5
  521. package/lib/include/three/VRButton.js +118 -118
  522. package/lib/needle-engine.d.ts +6 -6
  523. package/lib/needle-engine.js +49 -49
  524. package/package.json +1 -1
  525. package/plugins/common/config.cjs +14 -14
  526. package/plugins/common/config.js +19 -19
  527. package/plugins/common/generator.js +10 -10
  528. package/plugins/common/license.cjs +30 -30
  529. package/plugins/common/version.js +11 -11
  530. package/plugins/next/license.cjs +4 -4
  531. package/plugins/next/next.js +70 -70
  532. package/plugins/types/index.d.ts +1 -1
  533. package/plugins/types/needleConfig.d.ts +21 -21
  534. package/plugins/types/userconfig.d.ts +42 -42
  535. package/plugins/vite/alias.js +70 -70
  536. package/plugins/vite/build.js +19 -19
  537. package/plugins/vite/config.js +73 -73
  538. package/plugins/vite/copyfiles.js +134 -134
  539. package/plugins/vite/defines.js +45 -45
  540. package/plugins/vite/dependency-watcher.js +224 -224
  541. package/plugins/vite/drop-client.js +76 -76
  542. package/plugins/vite/drop.js +82 -82
  543. package/plugins/vite/editor-connection.js +121 -121
  544. package/plugins/vite/facebook-instant-games.js +99 -99
  545. package/plugins/vite/gzip.js +5 -5
  546. package/plugins/vite/imports-logger.js +143 -143
  547. package/plugins/vite/index.js +81 -81
  548. package/plugins/vite/license.js +42 -42
  549. package/plugins/vite/meta.js +149 -149
  550. package/plugins/vite/peer.js +31 -31
  551. package/plugins/vite/poster-client.js +59 -59
  552. package/plugins/vite/poster.js +73 -73
  553. package/plugins/vite/reload-client.js +15 -15
  554. package/plugins/vite/reload.js +363 -363
  555. package/plugins/vite/transform-codegen.js +55 -55
  556. package/plugins/vite/vite-4.4-hack.js +31 -31
  557. package/src/engine/api.ts +54 -54
  558. package/src/engine/assets/index.ts +4 -4
  559. package/src/engine/codegen/register_types.ts +441 -441
  560. package/src/engine/debug/debug.ts +29 -29
  561. package/src/engine/debug/debug_console.ts +213 -213
  562. package/src/engine/debug/debug_overlay.ts +283 -283
  563. package/src/engine/engine.ts +13 -13
  564. package/src/engine/engine_addressables.ts +494 -494
  565. package/src/engine/engine_application.ts +53 -53
  566. package/src/engine/engine_assetdatabase.ts +383 -383
  567. package/src/engine/engine_camera.ts +32 -32
  568. package/src/engine/engine_components.ts +266 -266
  569. package/src/engine/engine_components_internal.ts +42 -42
  570. package/src/engine/engine_constants.ts +42 -42
  571. package/src/engine/engine_context.ts +1386 -1386
  572. package/src/engine/engine_context_registry.ts +103 -103
  573. package/src/engine/engine_coroutine.ts +24 -24
  574. package/src/engine/engine_create_objects.ts +39 -39
  575. package/src/engine/engine_default_parameters.ts +3 -3
  576. package/src/engine/engine_editor-sync.ts +29 -29
  577. package/src/engine/engine_element.ts +592 -592
  578. package/src/engine/engine_element_attributes.ts +61 -61
  579. package/src/engine/engine_element_extras.ts +16 -16
  580. package/src/engine/engine_element_loading.ts +341 -341
  581. package/src/engine/engine_element_overlay.ts +160 -160
  582. package/src/engine/engine_fileloader.js +8 -8
  583. package/src/engine/engine_gameobject.ts +621 -621
  584. package/src/engine/engine_generic_utils.js +13 -13
  585. package/src/engine/engine_gizmos.ts +321 -321
  586. package/src/engine/engine_gltf.ts +30 -30
  587. package/src/engine/engine_gltf_builtin_components.ts +350 -350
  588. package/src/engine/engine_hot_reload.ts +196 -196
  589. package/src/engine/engine_input.ts +879 -879
  590. package/src/engine/engine_input_utils.ts +23 -23
  591. package/src/engine/engine_instancing.ts +42 -42
  592. package/src/engine/engine_license.ts +413 -413
  593. package/src/engine/engine_lifecycle_api.ts +29 -29
  594. package/src/engine/engine_lifecycle_functions_internal.ts +36 -36
  595. package/src/engine/engine_lightdata.ts +113 -113
  596. package/src/engine/engine_loaders.ts +77 -77
  597. package/src/engine/engine_mainloop_utils.ts +431 -431
  598. package/src/engine/engine_math.ts +174 -174
  599. package/src/engine/engine_networking.ts +742 -742
  600. package/src/engine/engine_networking_auto.ts +373 -373
  601. package/src/engine/engine_networking_files.ts +206 -206
  602. package/src/engine/engine_networking_files_default_components.ts +54 -54
  603. package/src/engine/engine_networking_instantiate.ts +362 -362
  604. package/src/engine/engine_networking_peer.ts +158 -158
  605. package/src/engine/engine_networking_streams.ts +489 -489
  606. package/src/engine/engine_networking_types.ts +18 -18
  607. package/src/engine/engine_networking_utils.ts +23 -23
  608. package/src/engine/engine_networking_websocket.ts +2 -2
  609. package/src/engine/engine_patcher.ts +199 -199
  610. package/src/engine/engine_physics.ts +287 -287
  611. package/src/engine/engine_physics.types.ts +43 -43
  612. package/src/engine/engine_physics_rapier.ts +1385 -1385
  613. package/src/engine/engine_playerview.ts +79 -79
  614. package/src/engine/engine_scenelighting.ts +313 -313
  615. package/src/engine/engine_scenetools.ts +242 -242
  616. package/src/engine/engine_serialization.ts +6 -6
  617. package/src/engine/engine_serialization_builtin_serializer.ts +415 -415
  618. package/src/engine/engine_serialization_core.ts +680 -680
  619. package/src/engine/engine_serialization_decorator.ts +68 -68
  620. package/src/engine/engine_setup.ts +1 -1
  621. package/src/engine/engine_shaders.ts +242 -242
  622. package/src/engine/engine_shims.ts +28 -28
  623. package/src/engine/engine_texture.ts +70 -70
  624. package/src/engine/engine_three_utils.ts +382 -382
  625. package/src/engine/engine_time.ts +55 -55
  626. package/src/engine/engine_types.ts +489 -489
  627. package/src/engine/engine_typestore.ts +41 -41
  628. package/src/engine/engine_util_decorator.ts +134 -134
  629. package/src/engine/engine_utils.ts +605 -605
  630. package/src/engine/engine_utils_screenshot.ts +84 -84
  631. package/src/engine/engine_web_api.ts +119 -119
  632. package/src/engine/extensions/EXT_texture_exr.ts +49 -49
  633. package/src/engine/extensions/NEEDLE_animator_controller_model.ts +193 -193
  634. package/src/engine/extensions/NEEDLE_components.ts +250 -250
  635. package/src/engine/extensions/NEEDLE_gameobject_data.ts +82 -82
  636. package/src/engine/extensions/NEEDLE_lighting_settings.ts +210 -210
  637. package/src/engine/extensions/NEEDLE_lightmaps.ts +130 -130
  638. package/src/engine/extensions/NEEDLE_persistent_assets.ts +75 -75
  639. package/src/engine/extensions/NEEDLE_progressive.ts +412 -412
  640. package/src/engine/extensions/NEEDLE_render_objects.ts +209 -209
  641. package/src/engine/extensions/NEEDLE_techniques_webgl.ts +618 -618
  642. package/src/engine/extensions/extension_resolver.ts +4 -4
  643. package/src/engine/extensions/extension_utils.ts +149 -149
  644. package/src/engine/extensions/extensions.ts +118 -118
  645. package/src/engine/extensions/index.ts +4 -4
  646. package/src/engine/extensions/usage_tracker.ts +95 -95
  647. package/src/engine/js-extensions/Camera.ts +34 -34
  648. package/src/engine/js-extensions/Layers.ts +19 -19
  649. package/src/engine/js-extensions/index.ts +1 -1
  650. package/src/engine/shaders/shaderData.ts +67 -67
  651. package/src/engine/tests/test_utils.ts +63 -63
  652. package/src/engine-components/AlignmentConstraint.ts +35 -35
  653. package/src/engine-components/Animation.ts +345 -345
  654. package/src/engine-components/AnimationCurve.ts +83 -83
  655. package/src/engine-components/AnimationUtils.ts +117 -117
  656. package/src/engine-components/Animator.ts +243 -243
  657. package/src/engine-components/AnimatorController.ts +1020 -1020
  658. package/src/engine-components/AudioListener.ts +32 -32
  659. package/src/engine-components/AudioSource.ts +419 -419
  660. package/src/engine-components/AvatarLoader.ts +204 -204
  661. package/src/engine-components/AxesHelper.ts +33 -33
  662. package/src/engine-components/BasicIKConstraint.ts +53 -53
  663. package/src/engine-components/BoxCollider.ts +1 -1
  664. package/src/engine-components/BoxHelperComponent.ts +100 -100
  665. package/src/engine-components/Camera.ts +454 -454
  666. package/src/engine-components/CameraUtils.ts +89 -89
  667. package/src/engine-components/CharacterController.ts +243 -243
  668. package/src/engine-components/Collider.ts +160 -160
  669. package/src/engine-components/Component.ts +670 -670
  670. package/src/engine-components/ContactShadows.ts +265 -265
  671. package/src/engine-components/DeleteBox.ts +35 -35
  672. package/src/engine-components/DeviceFlag.ts +42 -42
  673. package/src/engine-components/DragControls.ts +574 -574
  674. package/src/engine-components/DropListener.ts +112 -112
  675. package/src/engine-components/Duplicatable.ts +146 -146
  676. package/src/engine-components/EventList.ts +125 -125
  677. package/src/engine-components/EventTrigger.ts +47 -47
  678. package/src/engine-components/EventType.ts +87 -87
  679. package/src/engine-components/FlyControls.ts +31 -31
  680. package/src/engine-components/Fog.ts +59 -59
  681. package/src/engine-components/Gizmos.ts +52 -52
  682. package/src/engine-components/GridHelper.ts +40 -40
  683. package/src/engine-components/GroundProjection.ts +97 -97
  684. package/src/engine-components/Interactable.ts +18 -18
  685. package/src/engine-components/Joints.ts +51 -51
  686. package/src/engine-components/LODGroup.ts +145 -145
  687. package/src/engine-components/Light.ts +493 -493
  688. package/src/engine-components/LookAtConstraint.ts +11 -11
  689. package/src/engine-components/NestedGltf.ts +70 -70
  690. package/src/engine-components/Networking.ts +72 -72
  691. package/src/engine-components/OffsetConstraint.ts +59 -59
  692. package/src/engine-components/OrbitControls.ts +653 -653
  693. package/src/engine-components/ParticleSystem.ts +1192 -1192
  694. package/src/engine-components/ParticleSystemModules.ts +1481 -1481
  695. package/src/engine-components/ParticleSystemSubEmitter.ts +110 -110
  696. package/src/engine-components/PlayerColor.ts +93 -93
  697. package/src/engine-components/ReflectionProbe.ts +192 -192
  698. package/src/engine-components/Renderer.ts +1125 -1125
  699. package/src/engine-components/RendererLightmap.ts +145 -145
  700. package/src/engine-components/RigidBody.ts +453 -453
  701. package/src/engine-components/SceneSwitcher.ts +594 -594
  702. package/src/engine-components/ScreenCapture.ts +437 -437
  703. package/src/engine-components/ShadowCatcher.ts +149 -149
  704. package/src/engine-components/Skybox.ts +281 -281
  705. package/src/engine-components/SmoothFollow.ts +57 -57
  706. package/src/engine-components/SpatialTrigger.ts +142 -142
  707. package/src/engine-components/SpectatorCamera.ts +675 -675
  708. package/src/engine-components/SphereCollider.ts +1 -1
  709. package/src/engine-components/SpriteRenderer.ts +244 -244
  710. package/src/engine-components/SyncedCamera.ts +208 -208
  711. package/src/engine-components/SyncedRoom.ts +166 -166
  712. package/src/engine-components/SyncedTransform.ts +336 -336
  713. package/src/engine-components/TestRunner.ts +114 -114
  714. package/src/engine-components/TransformGizmo.ts +157 -157
  715. package/src/engine-components/VideoPlayer.ts +831 -831
  716. package/src/engine-components/Voip.ts +214 -214
  717. package/src/engine-components/XRFlag.ts +138 -138
  718. package/src/engine-components/api.ts +22 -22
  719. package/src/engine-components/avatar/AvatarBlink_Simple.ts +67 -67
  720. package/src/engine-components/avatar/AvatarEyeLook_Rotation.ts +68 -68
  721. package/src/engine-components/avatar/Avatar_Brain_LookAt.ts +136 -136
  722. package/src/engine-components/avatar/Avatar_MouthShapes.ts +81 -81
  723. package/src/engine-components/avatar/Avatar_MustacheShake.ts +28 -28
  724. package/src/engine-components/codegen/components.ts +216 -216
  725. package/src/engine-components/debug/LogStats.ts +21 -21
  726. package/src/engine-components/export/gltf/GltfExport.ts +231 -231
  727. package/src/engine-components/export/usdz/Extension.ts +11 -11
  728. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +1773 -1773
  729. package/src/engine-components/export/usdz/USDZExporter.ts +477 -477
  730. package/src/engine-components/export/usdz/extensions/Animation.ts +774 -774
  731. package/src/engine-components/export/usdz/extensions/DocumentExtension.ts +9 -9
  732. package/src/engine-components/export/usdz/extensions/USDZText.ts +287 -287
  733. package/src/engine-components/export/usdz/extensions/USDZUI.ts +119 -119
  734. package/src/engine-components/export/usdz/extensions/behavior/Actions.ts +98 -98
  735. package/src/engine-components/export/usdz/extensions/behavior/AudioExtension.ts +67 -67
  736. package/src/engine-components/export/usdz/extensions/behavior/Behaviour.ts +202 -202
  737. package/src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +963 -963
  738. package/src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts +517 -517
  739. package/src/engine-components/export/usdz/index.ts +2 -2
  740. package/src/engine-components/export/usdz/utils/animationutils.ts +100 -100
  741. package/src/engine-components/export/usdz/utils/quicklook.ts +42 -42
  742. package/src/engine-components/export/usdz/utils/timeutils.ts +19 -19
  743. package/src/engine-components/js-extensions/ExtensionUtils.ts +81 -81
  744. package/src/engine-components/js-extensions/Object3D.ts +181 -181
  745. package/src/engine-components/js-extensions/RGBAColor.ts +54 -54
  746. package/src/engine-components/js-extensions/Vector.ts +16 -16
  747. package/src/engine-components/js-extensions/index.ts +2 -2
  748. package/src/engine-components/postprocessing/Effects/Antialiasing.ts +51 -51
  749. package/src/engine-components/postprocessing/Effects/Bloom.ts +76 -76
  750. package/src/engine-components/postprocessing/Effects/ChromaticAberration.ts +35 -35
  751. package/src/engine-components/postprocessing/Effects/ColorAdjustments.ts +96 -96
  752. package/src/engine-components/postprocessing/Effects/DepthOfField.ts +93 -93
  753. package/src/engine-components/postprocessing/Effects/Pixelation.ts +26 -26
  754. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.ts +84 -84
  755. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.ts +98 -98
  756. package/src/engine-components/postprocessing/Effects/TiltShiftEffect.ts +55 -55
  757. package/src/engine-components/postprocessing/Effects/Tonemapping.ts +54 -54
  758. package/src/engine-components/postprocessing/Effects/Vignette.ts +54 -54
  759. package/src/engine-components/postprocessing/PostProcessingEffect.ts +148 -148
  760. package/src/engine-components/postprocessing/PostProcessingHandler.ts +232 -232
  761. package/src/engine-components/postprocessing/Volume.ts +216 -216
  762. package/src/engine-components/postprocessing/VolumeParameter.ts +92 -92
  763. package/src/engine-components/postprocessing/VolumeProfile.ts +40 -40
  764. package/src/engine-components/postprocessing/index.ts +3 -3
  765. package/src/engine-components/timeline/PlayableDirector.ts +666 -666
  766. package/src/engine-components/timeline/SignalAsset.ts +138 -138
  767. package/src/engine-components/timeline/TimelineModels.ts +93 -93
  768. package/src/engine-components/timeline/TimelineTracks.ts +906 -906
  769. package/src/engine-components/timeline/index.ts +3 -3
  770. package/src/engine-components/ui/BaseUIComponent.ts +195 -195
  771. package/src/engine-components/ui/Button.ts +283 -283
  772. package/src/engine-components/ui/Canvas.ts +390 -390
  773. package/src/engine-components/ui/CanvasGroup.ts +49 -49
  774. package/src/engine-components/ui/EventSystem.ts +736 -736
  775. package/src/engine-components/ui/Graphic.ts +255 -255
  776. package/src/engine-components/ui/Image.ts +102 -102
  777. package/src/engine-components/ui/InputField.ts +290 -290
  778. package/src/engine-components/ui/Interfaces.ts +57 -57
  779. package/src/engine-components/ui/Layout.ts +322 -322
  780. package/src/engine-components/ui/Outline.ts +12 -12
  781. package/src/engine-components/ui/PointerEvents.ts +118 -118
  782. package/src/engine-components/ui/RaycastUtils.ts +68 -68
  783. package/src/engine-components/ui/Raycaster.ts +73 -73
  784. package/src/engine-components/ui/RectTransform.ts +364 -364
  785. package/src/engine-components/ui/SpatialHtml.ts +63 -63
  786. package/src/engine-components/ui/Text.ts +572 -572
  787. package/src/engine-components/ui/Utils.ts +110 -110
  788. package/src/engine-components/utils/LookAt.ts +65 -65
  789. package/src/engine-components/utils/OpenURL.ts +118 -118
  790. package/src/engine-components/webxr/WebARCameraBackground.ts +224 -224
  791. package/src/engine-components/webxr/WebARSessionRoot.ts +446 -446
  792. package/src/engine-components/webxr/WebXR.ts +761 -761
  793. package/src/engine-components/webxr/WebXRAvatar.ts +356 -356
  794. package/src/engine-components/webxr/WebXRController.ts +1168 -1168
  795. package/src/engine-components/webxr/WebXRGrabRendering.ts +150 -150
  796. package/src/engine-components/webxr/WebXRImageTracking.ts +371 -371
  797. package/src/engine-components/webxr/WebXRPlaneTracking.ts +429 -429
  798. package/src/engine-components/webxr/WebXRRig.ts +21 -21
  799. package/src/engine-components/webxr/WebXRSync.ts +463 -463
  800. package/src/engine-components/webxr/index.ts +3 -3
  801. package/src/engine-components-experimental/Presentation.ts +12 -12
  802. package/src/engine-components-experimental/networking/PlayerSync.ts +217 -217
  803. package/src/engine-schemes/COMPILE_SCHEMES.bat +3 -3
  804. package/src/engine-schemes/COMPILE_TS.bat +11 -11
  805. package/src/engine-schemes/schemes.ts +27 -27
  806. package/src/engine-schemes/synced-camera-model.ts +92 -92
  807. package/src/engine-schemes/synced-transform-model.ts +90 -90
  808. package/src/engine-schemes/syncedCamera.fbs +10 -10
  809. package/src/engine-schemes/transform.ts +50 -50
  810. package/src/engine-schemes/transforms.fbs +25 -25
  811. package/src/engine-schemes/vec.fbs +19 -19
  812. package/src/engine-schemes/vec2.ts +33 -33
  813. package/src/engine-schemes/vec3.ts +38 -38
  814. package/src/engine-schemes/vec4.ts +43 -43
  815. package/src/engine-schemes/vr-user-state-buffer.ts +138 -138
  816. package/src/engine-schemes/vrUserStateBuffer.fbs +16 -16
  817. package/src/include/draco/draco_decoder.js +34 -34
  818. package/src/include/draco/draco_wasm_wrapper.js +117 -117
  819. package/src/include/ktx2/basis_transcoder.js +21 -21
  820. package/src/include/needle/arial-msdf.json +1471 -1471
  821. package/src/include/three/ARButton.js +231 -231
  822. package/src/include/three/DragControls.js +231 -231
  823. package/src/include/three/EXT_mesh_gpu_instancing_exporter.js +66 -66
  824. package/src/include/three/VRButton.js +194 -194
  825. package/src/needle-engine.ts +55 -55
  826. package/src/engine/dist/api.js +0 -73
  827. package/src/engine/dist/api.js.meta +0 -7
  828. package/src/engine/dist/engine_networking_streams.js +0 -474
  829. package/src/engine/dist/engine_networking_streams.js.meta +0 -7
  830. package/src/engine-schemes/dist/api.js +0 -17
  831. package/src/engine-schemes/dist/api.js.meta +0 -7
  832. package/src/engine-schemes/dist/schemes.js +0 -25
  833. package/src/engine-schemes/dist/schemes.js.meta +0 -7
  834. package/src/engine-schemes/dist/synced-camera-model.js +0 -74
  835. package/src/engine-schemes/dist/synced-camera-model.js.meta +0 -7
  836. package/src/engine-schemes/dist/synced-transform-model.js +0 -73
  837. package/src/engine-schemes/dist/synced-transform-model.js.meta +0 -7
  838. package/src/engine-schemes/dist/transform.js +0 -46
  839. package/src/engine-schemes/dist/transform.js.meta +0 -7
  840. package/src/engine-schemes/dist/vec2.js +0 -32
  841. package/src/engine-schemes/dist/vec2.js.meta +0 -7
  842. package/src/engine-schemes/dist/vec3.js +0 -36
  843. package/src/engine-schemes/dist/vec3.js.meta +0 -7
  844. package/src/engine-schemes/dist/vec4.js +0 -40
  845. package/src/engine-schemes/dist/vec4.js.meta +0 -7
  846. package/src/engine-schemes/dist/vr-user-state-buffer.js +0 -110
  847. package/src/engine-schemes/dist/vr-user-state-buffer.js.meta +0 -7
  848. package/src/engine-schemes/flatc.exe +0 -0
@@ -1,1774 +1,1774 @@
1
- import { Renderer } from '../../Renderer.js';
2
- import { GameObject } from '../../Component.js';
3
- import type { OffscreenCanvasExt } from '../../../engine/engine_shims.js';
4
- import '../../../engine/engine_shims.js';
5
- import {
6
- PlaneGeometry,
7
- Texture,
8
- Uniform,
9
- PerspectiveCamera,
10
- Scene,
11
- Mesh,
12
- ShaderMaterial,
13
- WebGLRenderer,
14
- MathUtils,
15
- Matrix4,
16
- DoubleSide,
17
- BufferGeometry,
18
- Material,
19
- Color,
20
- MeshStandardMaterial,
21
- MeshPhysicalMaterial,
22
- Object3D,
23
- MeshBasicMaterial,
24
- Bone,
25
- SkinnedMesh,
26
- SRGBColorSpace,
27
- AnimationClip,
28
- OrthographicCamera,
29
- BufferAttribute,
30
- Vector4
31
- } from 'three';
32
- import * as fflate from 'three/examples/jsm/libs/fflate.module.js';
33
-
34
- function makeNameSafe( str ) {
35
- str = str.replace( /[^a-zA-Z0-9_]/g, '' );
36
-
37
- // if str does not start with a-zA-Z_ add _ to the beginning
38
- if ( !str.match( /^[a-zA-Z_]/ ) )
39
- str = '_' + str;
40
-
41
- return str;
42
- }
43
-
44
- // TODO check if this works when bones in the skeleton are completely unordered
45
- function findCommonAncestor(objects: Object3D[]): Object3D | null {
46
- if (objects.length === 0) return null;
47
-
48
- const ancestors = objects.map((obj) => {
49
- const objAncestors = new Array<Object3D>();
50
- while (obj.parent) {
51
- objAncestors.unshift(obj.parent);
52
- obj = obj.parent;
53
- }
54
- return objAncestors;
55
- });
56
-
57
- //@ts-ignore – findLast seems to be missing in TypeScript types pre-5.x
58
- const commonAncestor = ancestors[0].findLast((ancestor) => {
59
- return ancestors.every((a) => a.includes(ancestor));
60
- });
61
-
62
- return commonAncestor || null;
63
- }
64
-
65
- function findStructuralNodesInBoneHierarchy(bones: Array<Object3D>) {
66
-
67
- const commonAncestor = findCommonAncestor(bones);
68
- // find all structural nodes – parents of bones that are not bones themselves
69
- const structuralNodes = new Set<Object3D>();
70
- for ( const bone of bones ) {
71
- let current = bone.parent;
72
- while ( current && current !== commonAncestor ) {
73
- if ( !bones.includes(current) ) {
74
- structuralNodes.add(current);
75
- }
76
- current = current.parent;
77
- }
78
- }
79
-
80
- return structuralNodes;
81
- }
82
-
83
- class USDObject {
84
-
85
- static USDObject_export_id = 0;
86
-
87
- uuid: string;
88
- name: string;
89
- matrix: Matrix4;
90
- private _isDynamic: boolean;
91
- get isDynamic() { return this._isDynamic; }
92
- private set isDynamic( value ) { this._isDynamic = value; }
93
- geometry: BufferGeometry | null;
94
- material: MeshStandardMaterial | MeshBasicMaterial | Material | null;
95
- camera: PerspectiveCamera | OrthographicCamera | null;
96
- parent: USDObject | null;
97
- skinnedMesh: SkinnedMesh | null;
98
- children: Array<USDObject | null> = [];
99
- animations: AnimationClip[] | null;
100
- _eventListeners: {};
101
-
102
- static createEmptyParent( object ) {
103
-
104
- const emptyParent = new USDObject( MathUtils.generateUUID(), object.name + '_empty_' + ( USDObject.USDObject_export_id ++ ), object.matrix );
105
- const parent = object.parent;
106
- parent.add( emptyParent );
107
- emptyParent.add( object );
108
- emptyParent.isDynamic = true;
109
- object.matrix = new Matrix4().identity();
110
- return emptyParent;
111
-
112
- }
113
-
114
- static createEmpty() {
115
-
116
- const empty = new USDObject( MathUtils.generateUUID(), 'Empty_' + ( USDObject.USDObject_export_id ++ ), new Matrix4() );
117
- empty.isDynamic = true;
118
- return empty;
119
- }
120
-
121
- constructor( id, name, matrix, mesh: BufferGeometry | null = null, material: MeshStandardMaterial | MeshBasicMaterial | Material | null = null, camera: PerspectiveCamera | OrthographicCamera | null = null, skinnedMesh: SkinnedMesh | null = null, animations: AnimationClip[] | null = null ) {
122
-
123
- this.uuid = id;
124
- this.name = makeNameSafe( name );
125
- this.matrix = matrix.clone();
126
- this.geometry = mesh;
127
- this.material = material;
128
- this.camera = camera;
129
- this.parent = null;
130
- this.children = [];
131
- this._eventListeners = {};
132
- this._isDynamic = false;
133
- this.skinnedMesh = skinnedMesh;
134
- this.animations = animations;
135
-
136
- }
137
-
138
- is( obj ) {
139
-
140
- if ( ! obj ) return false;
141
- return this.uuid === obj.uuid;
142
-
143
- }
144
-
145
- isEmpty() {
146
-
147
- return ! this.geometry;
148
-
149
- }
150
-
151
- clone() {
152
-
153
- const clone = new USDObject( MathUtils.generateUUID(), this.name, this.matrix, this.geometry, this.material );
154
- clone.isDynamic = this.isDynamic;
155
- return clone;
156
-
157
- }
158
-
159
- deepClone() {
160
-
161
- const clone = this.clone();
162
- for ( const child of this.children ) {
163
-
164
- if ( !child ) continue;
165
- clone.add( child.deepClone() );
166
-
167
- }
168
-
169
- return clone;
170
-
171
- }
172
-
173
- getPath() {
174
-
175
- let current = this.parent;
176
- let path = this.name;
177
- while ( current ) {
178
-
179
- // StageRoot has a special path right now since there's additional Xforms for encapsulation.
180
- // Better would be to actually model them as part of our object graph, but they're written separately,
181
- // so currently we don't and instead work around that here.
182
- const currentName = current.parent ? current.name : (current.name + "/Scenes/Scene");
183
- path = currentName + '/' + path;
184
- current = current.parent;
185
-
186
- }
187
-
188
- return '</' + path + '>';
189
-
190
- }
191
-
192
- add( child ) {
193
-
194
- if ( child.parent ) {
195
-
196
- child.parent.remove( child );
197
-
198
- }
199
-
200
- child.parent = this;
201
- this.children.push( child );
202
-
203
- }
204
-
205
- remove( child ) {
206
-
207
- const index = this.children.indexOf( child );
208
- if ( index >= 0 ) {
209
-
210
- if ( child.parent === this ) child.parent = null;
211
- this.children.splice( index, 1 );
212
-
213
- }
214
-
215
- }
216
-
217
- addEventListener( evt, listener ) {
218
-
219
- if ( ! this._eventListeners[ evt ] ) this._eventListeners[ evt ] = [];
220
- this._eventListeners[ evt ].push( listener );
221
-
222
- }
223
-
224
- removeEventListener( evt, listener ) {
225
-
226
- if ( ! this._eventListeners[ evt ] ) return;
227
- const index = this._eventListeners[ evt ].indexOf( listener );
228
- if ( index >= 0 ) {
229
-
230
- this._eventListeners[ evt ].splice( index, 1 );
231
-
232
- }
233
-
234
- }
235
-
236
- onSerialize( writer, context ) {
237
-
238
- const listeners = this._eventListeners[ 'serialize' ];
239
- if ( listeners ) listeners.forEach( listener => listener( writer, context ) );
240
-
241
- }
242
-
243
- }
244
-
245
-
246
- class USDDocument extends USDObject {
247
-
248
- stageLength: number;
249
-
250
- get isDocumentRoot() {
251
-
252
- return true;
253
-
254
- }
255
- get isDynamic() {
256
-
257
- return false;
258
-
259
- }
260
-
261
- constructor() {
262
-
263
- super(undefined, 'StageRoot', new Matrix4(), null, null, null);
264
- this.children = [];
265
- this.stageLength = 200;
266
-
267
- }
268
-
269
- add( child: USDObject ) {
270
-
271
- child.parent = this;
272
- this.children.push( child );
273
-
274
- }
275
-
276
- remove( child: USDObject ) {
277
-
278
- const index = this.children.indexOf( child );
279
- if ( index >= 0 ) {
280
-
281
- if ( child.parent === this ) child.parent = null;
282
- this.children.splice( index, 1 );
283
-
284
- }
285
-
286
- }
287
-
288
- traverse( callback: ( object: USDObject ) => void, current: USDObject | null = null ) {
289
-
290
- if ( current !== null ) callback( current );
291
- else current = this;
292
- if ( current.children ) {
293
-
294
- for ( const child of current.children ) {
295
-
296
- this.traverse( callback, child );
297
-
298
- }
299
-
300
- }
301
-
302
- }
303
-
304
- findById( uuid: string ) {
305
-
306
- let found = false;
307
- function search( current ) {
308
-
309
- if ( found ) return;
310
- if ( current.uuid === uuid ) {
311
-
312
- found = true;
313
- return current;
314
-
315
- }
316
-
317
- if ( current.children ) {
318
-
319
- for ( const child of current.children ) {
320
-
321
- const res = search( child );
322
- if ( res ) return res;
323
-
324
- }
325
-
326
- }
327
-
328
- }
329
-
330
- return search( this );
331
-
332
- }
333
-
334
-
335
- buildHeader( endTimeCode ) {
336
-
337
- return `#usda 1.0
338
- (
339
- customLayerData = {
340
- string creator = "Needle Engine USDZExporter"
341
- }
342
- defaultPrim = "${makeNameSafe( this.name )}"
343
- metersPerUnit = 1
344
- upAxis = "Y"
345
- startTimeCode = 0
346
- endTimeCode = ${endTimeCode}
347
- timeCodesPerSecond = 60
348
- framesPerSecond = 60
349
- )
350
- `;
351
-
352
- }
353
-
354
- }
355
-
356
- const newLine = '\n';
357
- const materialRoot = '</StageRoot/Materials';
358
-
359
- class USDWriter {
360
- str: string;
361
- indent: number;
362
-
363
- constructor() {
364
-
365
- this.str = '';
366
- this.indent = 0;
367
-
368
- }
369
-
370
- clear() {
371
-
372
- this.str = '';
373
- this.indent = 0;
374
-
375
- }
376
-
377
- beginBlock( str: string | undefined = undefined, char = '{', createNewLine = true ) {
378
-
379
- if ( str !== undefined ) {
380
- str = this.applyIndent( str );
381
- this.str += str;
382
- if ( createNewLine ) {
383
- this.str += newLine;
384
- this.str += this.applyIndent( char );
385
- }
386
- else {
387
- this.str += " " + char;
388
- }
389
- }
390
- else {
391
- this.str += this.applyIndent( char );
392
- }
393
-
394
- this.str += newLine;
395
- this.indent += 1;
396
-
397
- }
398
-
399
- closeBlock( char = '}' ) {
400
-
401
- this.indent -= 1;
402
- this.str += this.applyIndent( char ) + newLine;
403
-
404
- }
405
-
406
- beginArray( str ) {
407
-
408
- str = this.applyIndent( str + ' = [' );
409
- this.str += str;
410
- this.str += newLine;
411
- this.indent += 1;
412
-
413
- }
414
-
415
- closeArray() {
416
-
417
- this.indent -= 1;
418
- this.str += this.applyIndent( ']' ) + newLine;
419
-
420
- }
421
-
422
- appendLine( str = '' ) {
423
-
424
- str = this.applyIndent( str );
425
- this.str += str;
426
- this.str += newLine;
427
-
428
- }
429
-
430
- toString() {
431
-
432
- return this.str;
433
-
434
- }
435
-
436
- applyIndent( str ) {
437
-
438
- let indents = '';
439
- for ( let i = 0; i < this.indent; i ++ ) indents += '\t';
440
- return indents + str;
441
-
442
- }
443
-
444
- }
445
-
446
- declare type TextureMap = {[name: string]: {texture: Texture, scale?: Vector4}};
447
-
448
- class USDZExporterContext {
449
- root: any;
450
- exporter: any;
451
- extensions: any;
452
- quickLookCompatible: boolean;
453
- materials: Map<string, Material>;
454
- textures: TextureMap;
455
- files: { [path: string]: Uint8Array | [Uint8Array, fflate.ZipOptions] | null | any }
456
- document: USDDocument;
457
- output: string;
458
- animations: AnimationClip[];
459
-
460
- constructor( root, exporter: USDZExporter, extensions, quickLookCompatible ) {
461
-
462
- this.root = root;
463
- this.exporter = exporter;
464
- this.quickLookCompatible = quickLookCompatible;
465
-
466
- if ( extensions )
467
- this.extensions = extensions;
468
-
469
- this.materials = new Map();
470
- this.textures = {};
471
- this.files = {};
472
- this.document = new USDDocument();
473
- this.output = '';
474
- this.animations = [];
475
-
476
- }
477
-
478
- }
479
-
480
- /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_anchoring_type) */
481
- export type Anchoring = "plane" | "image" | "face" | "none"
482
- /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_planeanchoring_alignment) */
483
- export type Alignment = "horizontal" | "vertical" | "any";
484
-
485
- class USDZExporterOptions {
486
- ar: {
487
- anchoring: { type: Anchoring },
488
- planeAnchoring: { alignment: Alignment },
489
- } = {
490
- anchoring: { type: 'plane' },
491
- planeAnchoring: { alignment: 'horizontal' }
492
- };
493
- quickLookCompatible: boolean = false;
494
- extensions: any[] = [];
495
- maxTextureSize: number = 4096;
496
- }
497
-
498
- class USDZExporter {
499
- debug: boolean;
500
- sceneAnchoringOptions: {} = {};
501
- extensions: any;
502
-
503
- constructor() {
504
-
505
- this.debug = false;
506
-
507
- }
508
-
509
- getEndTimeCode( animations ) {
510
- let endTimeCode = 0;
511
-
512
- for( const animation of animations ) {
513
- const currentEndTimeCode = animation.duration * 60;
514
-
515
- if ( endTimeCode < currentEndTimeCode ) {
516
- endTimeCode = currentEndTimeCode;
517
- }
518
- }
519
-
520
- return endTimeCode;
521
- }
522
-
523
- async parse( scene, options: USDZExporterOptions = new USDZExporterOptions() ) {
524
-
525
- options = Object.assign( new USDZExporterOptions(), options );
526
-
527
- this.sceneAnchoringOptions = options;
528
- const context = new USDZExporterContext( scene, this, options.extensions, options.quickLookCompatible );
529
- this.extensions = context.extensions;
530
-
531
- const files = context.files;
532
- const modelFileName = 'model.usda';
533
-
534
- // model file should be first in USDZ archive so we init it here
535
- files[ modelFileName ] = null;
536
-
537
- const materials = context.materials;
538
- const textures = context.textures;
539
-
540
- await invokeAll( context, 'onBeforeBuildDocument' );
541
-
542
- // HACK let's find all skeletons and reparent them to their skelroot / armature / uppermost bone parent
543
- const reparentings: Array<any> = [];
544
- scene.traverseVisible(object => {
545
-
546
- if (object.isSkinnedMesh) {
547
- const bones = object.skeleton.bones as Bone[];
548
-
549
- const commonAncestor = findCommonAncestor(bones);
550
- if (commonAncestor) {
551
- reparentings.push( { object, originalParent: object.parent, newParent: commonAncestor } );
552
- }
553
- }
554
- });
555
-
556
- for ( const reparenting of reparentings ) {
557
- const { object, originalParent, newParent } = reparenting;
558
- if (this.debug) console.log("REPARENTING", object, "from", originalParent, "to", newParent);
559
- newParent.add( object );
560
- }
561
-
562
- traverseVisible( scene, context.document, context );
563
-
564
- await invokeAll( context, 'onAfterBuildDocument' );
565
-
566
- parseDocument( context, () => {
567
- // injected after stageRoot.
568
- // TODO property use context/writer instead of string concat
569
- return buildMaterials( materials, textures, options.quickLookCompatible );
570
- } );
571
-
572
- await invokeAll( context, 'onAfterSerialize' );
573
-
574
- // repair the parenting again
575
- for ( const reparenting of reparentings ) {
576
- const { object, originalParent, newParent } = reparenting;
577
- originalParent.add( object );
578
- }
579
-
580
- // Moved into parseDocument callback for proper defaultPrim encapsulation
581
- // context.output += buildMaterials( materials, textures, options.quickLookCompatible );
582
-
583
- const endTimeCode = this.getEndTimeCode( context.animations );
584
-
585
- const header = context.document.buildHeader( endTimeCode );
586
- const final = header + '\n' + context.output;
587
-
588
- // full output file
589
- if ( this.debug )
590
- console.log( final );
591
-
592
- files[ modelFileName ] = fflate.strToU8( final );
593
- context.output = '';
594
-
595
- const decompressionRenderer = new WebGLRenderer( { antialias: false, alpha: true } );
596
-
597
- for ( const id in textures ) {
598
-
599
- const textureData = textures[ id ];
600
- let texture = textureData.texture;
601
-
602
- const isRGBA = formatsWithAlphaChannel.includes( texture.format );
603
-
604
- //@ts-ignore
605
- if ( texture.isCompressedTexture || texture.isRenderTargetTexture ) {
606
-
607
- texture = decompressGpuTexture( texture, options.maxTextureSize, decompressionRenderer );
608
-
609
- }
610
-
611
- const canvas = await imageToCanvas( texture.image, textureData.scale, false, options.maxTextureSize ).catch( err => {
612
- console.error("Error converting texture to canvas", texture, err);
613
- });
614
-
615
- if ( canvas ) {
616
-
617
- const blob = await canvas.convertToBlob( {type: isRGBA ? 'image/png' : 'image/jpeg', quality: 0.95 } );
618
- files[ `textures/Texture_${id}.${isRGBA ? 'png' : 'jpg'}` ] = new Uint8Array( await blob.arrayBuffer() );
619
-
620
- } else {
621
-
622
- console.warn( 'Can`t export texture: ', texture );
623
-
624
- }
625
-
626
- }
627
-
628
- decompressionRenderer.dispose();
629
-
630
- // 64 byte alignment
631
- // https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109
632
-
633
- let offset = 0;
634
-
635
- for ( const filename in files ) {
636
-
637
- const file = files[ filename ];
638
- const headerSize = 34 + filename.length;
639
-
640
- offset += headerSize;
641
-
642
- const offsetMod64 = offset & 63;
643
-
644
- if ( offsetMod64 !== 4 ) {
645
-
646
- const padLength = 64 - offsetMod64;
647
- const padding = new Uint8Array( padLength );
648
-
649
- files[ filename ] = [ file, { extra: { 12345: padding } } ];
650
-
651
- }
652
-
653
- offset = file.length;
654
-
655
- }
656
-
657
- return fflate.zipSync( files, { level: 0 } );
658
-
659
- }
660
-
661
- }
662
-
663
- function traverseVisible( object: Object3D, parentModel: USDObject, context: USDZExporterContext ) {
664
-
665
- if ( ! object.visible ) return;
666
-
667
- let model: USDObject | undefined = undefined;
668
- let geometry: BufferGeometry | undefined = undefined;
669
- let material: Material | Material[] | undefined = undefined;
670
-
671
- if (object instanceof Mesh || object instanceof SkinnedMesh) {
672
- geometry = object.geometry;
673
- material = object.material;
674
- }
675
-
676
- // TODO what should be do with disabled renderers?
677
- // Here we just assume they're off, and don't export them
678
- const renderer = GameObject.getComponent( object, Renderer )
679
- if (renderer && !renderer.enabled) {
680
- geometry = undefined;
681
- material = undefined;
682
- }
683
-
684
- if ( (object instanceof Mesh || object instanceof SkinnedMesh) && material && (material instanceof MeshStandardMaterial || material instanceof MeshBasicMaterial)) {
685
-
686
- const name = getObjectId( object );
687
- const skinnedMeshObject = object instanceof SkinnedMesh ? object : null;
688
- model = new USDObject( object.uuid, name, object.matrix, geometry, material, undefined, skinnedMeshObject, object.animations );
689
-
690
- } else if ( object instanceof PerspectiveCamera || object instanceof OrthographicCamera ) {
691
-
692
- const name = getObjectId( object );
693
- model = new USDObject( object.uuid, name, object.matrix, undefined, undefined, object );
694
-
695
- } else {
696
-
697
- const name = getObjectId( object );
698
- model = new USDObject( object.uuid, name, object.matrix, undefined, undefined, undefined, undefined, object.animations );
699
-
700
- }
701
-
702
- if ( model ) {
703
-
704
- if ( parentModel ) {
705
-
706
- parentModel.add( model );
707
-
708
- }
709
-
710
- parentModel = model;
711
-
712
- if ( context.extensions ) {
713
-
714
- for ( const ext of context.extensions ) {
715
-
716
- if ( ext.onExportObject ) ext.onExportObject.call( ext, object, model, context );
717
-
718
- }
719
-
720
- }
721
-
722
- } else {
723
-
724
- const name = getObjectId( object );
725
- const empty = new USDObject( object.uuid, name, object.matrix );
726
- if ( parentModel ) {
727
-
728
- parentModel.add( empty );
729
-
730
- }
731
-
732
- parentModel = empty;
733
-
734
- }
735
-
736
- for ( const ch of object.children ) {
737
-
738
- traverseVisible( ch, parentModel, context );
739
-
740
- }
741
-
742
- }
743
-
744
- async function parseDocument( context: USDZExporterContext, afterStageRoot: () => string ) {
745
-
746
- for ( const child of context.document.children ) {
747
-
748
- addResources( child, context );
749
-
750
- }
751
-
752
- const writer = new USDWriter();
753
-
754
- writer.beginBlock( `def Xform "${context.document.name}"` );
755
-
756
- writer.beginBlock( `def Scope "Scenes" (
757
- kind = "sceneLibrary"
758
- )` );
759
-
760
- writer.beginBlock( `def Xform "Scene" (
761
- apiSchemas = ["Preliminary_AnchoringAPI"]
762
- customData = {
763
- bool preliminary_collidesWithEnvironment = 0
764
- string sceneName = "Scene"
765
- }
766
- sceneName = "Scene"
767
- )` );
768
-
769
- writer.appendLine( `token preliminary:anchoring:type = "${context.exporter.sceneAnchoringOptions.ar.anchoring.type}"` );
770
- if (context.exporter.sceneAnchoringOptions.ar.anchoring.type === 'plane')
771
- writer.appendLine( `token preliminary:planeAnchoring:alignment = "${context.exporter.sceneAnchoringOptions.ar.planeAnchoring.alignment}"` );
772
- // 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.
773
- if (context.exporter.sceneAnchoringOptions.ar.anchoring.type === 'image')
774
- writer.appendLine( `rel preliminary:imageAnchoring:referenceImage = </${context.document.name}/Scenes/Scene/AnchoringReferenceImage>` );
775
- writer.appendLine();
776
-
777
- for ( const child of context.document.children ) {
778
-
779
- buildXform( child, writer, context );
780
-
781
- }
782
-
783
- invokeAll( context, 'onAfterHierarchy', writer );
784
-
785
- writer.closeBlock();
786
- writer.closeBlock();
787
- writer.appendLine(afterStageRoot());
788
- writer.closeBlock();
789
-
790
- context.output += writer.toString();
791
-
792
- }
793
-
794
- function addResources( object: USDObject | null, context: USDZExporterContext ) {
795
-
796
- if ( object == null ) {
797
- return;
798
- }
799
- const geometry = object.geometry;
800
- const material = object.material;
801
-
802
- if ( geometry ) {
803
-
804
- if ( material && ( 'isMeshStandardMaterial' in material && material.isMeshStandardMaterial || 'isMeshBasicMaterial' in material && material.isMeshBasicMaterial ) ) { // TODO convert unlit to lit+emissive
805
-
806
- const geometryFileName = 'geometries/Geometry_' + geometry.id + '.usd';
807
-
808
- if ( ! ( geometryFileName in context.files ) ) {
809
-
810
- const meshObject = buildMeshObject( geometry, object.skinnedMesh?.skeleton?.bones );
811
- context.files[ geometryFileName ] = buildUSDFileAsString( meshObject, context );
812
-
813
- }
814
-
815
- } else {
816
-
817
- console.warn( 'THREE.USDZExporter: Unsupported material type (USDZ only supports MeshStandardMaterial)', name );
818
-
819
- }
820
-
821
- }
822
-
823
- if( material ) {
824
-
825
- if ( ! ( material.uuid in context.materials ) ) {
826
-
827
- context.materials[ material.uuid ] = material;
828
-
829
- }
830
- }
831
-
832
- for ( const ch of object.children ) {
833
-
834
- addResources( ch, context );
835
-
836
- }
837
-
838
- }
839
-
840
- async function invokeAll( context: USDZExporterContext, name: string, writer: USDWriter | null = null ) {
841
-
842
- if ( context.extensions ) {
843
-
844
- for ( const ext of context.extensions ) {
845
-
846
- if ( !ext ) continue;
847
-
848
- if ( typeof ext[ name ] === 'function' ) {
849
-
850
- const method = ext[ name ];
851
- const res = method.call( ext, context, writer );
852
- if(res instanceof Promise) {
853
- await res;
854
- }
855
- }
856
-
857
- }
858
-
859
- }
860
-
861
- }
862
- let _renderer: WebGLRenderer | null = null;
863
- let fullscreenQuadGeometry: PlaneGeometry | null;
864
- let fullscreenQuadMaterial: ShaderMaterial | null;
865
- let fullscreenQuad: Mesh | null;
866
-
867
- function decompressGpuTexture( texture, maxTextureSize = Infinity, renderer: WebGLRenderer | null = null ) {
868
-
869
- if ( ! fullscreenQuadGeometry ) fullscreenQuadGeometry = new PlaneGeometry( 2, 2, 1, 1 );
870
- if ( ! fullscreenQuadMaterial ) fullscreenQuadMaterial = new ShaderMaterial( {
871
- uniforms: { blitTexture: new Uniform( texture ) },
872
- vertexShader: `
873
- varying vec2 vUv;
874
- void main(){
875
- vUv = uv;
876
- vUv.y = 1. - vUv.y;
877
- gl_Position = vec4(position.xy * 1.0,0.,.999999);
878
- }`,
879
- fragmentShader: `
880
- uniform sampler2D blitTexture;
881
- varying vec2 vUv;
882
-
883
- void main(){
884
- gl_FragColor = vec4(vUv.xy, 0, 1);
885
-
886
- #ifdef IS_SRGB
887
- gl_FragColor = LinearTosRGB( texture2D( blitTexture, vUv) );
888
- #else
889
- gl_FragColor = texture2D( blitTexture, vUv);
890
- #endif
891
- gl_FragColor.rgb *= gl_FragColor.a;
892
- }`
893
- } );
894
-
895
- fullscreenQuadMaterial.uniforms.blitTexture.value = texture;
896
- fullscreenQuadMaterial.defines.IS_SRGB = texture.colorSpace == SRGBColorSpace;
897
- fullscreenQuadMaterial.needsUpdate = true;
898
-
899
- if ( ! fullscreenQuad ) {
900
-
901
- fullscreenQuad = new Mesh( fullscreenQuadGeometry, fullscreenQuadMaterial );
902
- fullscreenQuad.frustumCulled = false;
903
-
904
- }
905
-
906
- const _camera = new PerspectiveCamera();
907
- const _scene = new Scene();
908
- _scene.add( fullscreenQuad );
909
-
910
- if ( ! renderer ) {
911
-
912
- renderer = _renderer = new WebGLRenderer( { antialias: false, alpha: true } );
913
-
914
- }
915
-
916
- renderer.setSize( Math.min( texture.image.width, maxTextureSize ), Math.min( texture.image.height, maxTextureSize ) );
917
- renderer.clear();
918
- renderer.render( _scene, _camera );
919
-
920
- const readableTexture = new Texture( renderer.domElement );
921
-
922
- readableTexture.minFilter = texture.minFilter;
923
- readableTexture.magFilter = texture.magFilter;
924
- readableTexture.wrapS = texture.wrapS;
925
- readableTexture.wrapT = texture.wrapT;
926
- readableTexture.name = texture.name;
927
-
928
- if ( _renderer ) {
929
-
930
- _renderer.dispose();
931
- _renderer = null;
932
-
933
- }
934
-
935
- return readableTexture;
936
-
937
- }
938
-
939
-
940
- function isImageBitmap( image ) {
941
-
942
- return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
943
- ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
944
- ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
945
- ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap );
946
-
947
- }
948
-
949
- async function imageToCanvas( image, color: Vector4 | undefined = undefined, flipY = false, maxTextureSize = 4096 ) {
950
-
951
- if ( isImageBitmap( image ) ) {
952
-
953
- // max. canvas size on Safari is still 4096x4096
954
- const scale = maxTextureSize / Math.max( image.width, image.height );
955
-
956
- const canvas = new OffscreenCanvas( image.width * Math.min( 1, scale ), image.height * Math.min( 1, scale ) );
957
-
958
- const context = canvas.getContext( '2d' ) as OffscreenCanvasRenderingContext2D;
959
- if (!context) throw new Error('Could not get canvas 2D context');
960
-
961
- if ( flipY === true ) {
962
-
963
- context.translate( 0, canvas.height );
964
- context.scale( 1, - 1 );
965
-
966
- }
967
-
968
- context.drawImage( image, 0, 0, canvas.width, canvas.height );
969
-
970
- // Currently only used to apply opacity scale since QuickLook and usdview don't support that yet
971
- if ( color !== undefined ) {
972
-
973
- const r = color.x;
974
- const g = color.y;
975
- const b = color.z;
976
- const a = color.w;
977
-
978
- const imagedata = context.getImageData( 0, 0, canvas.width, canvas.height );
979
- const data = imagedata.data;
980
-
981
- for ( let i = 0; i < data.length; i += 4 ) {
982
-
983
- data[ i + 0 ] = data[ i + 0 ] * r;
984
- data[ i + 1 ] = data[ i + 1 ] * g;
985
- data[ i + 2 ] = data[ i + 2 ] * b;
986
- data[ i + 3 ] = data[ i + 3 ] * a;
987
-
988
- }
989
-
990
- context.putImageData( imagedata, 0, 0 );
991
-
992
- }
993
-
994
- return canvas as OffscreenCanvasExt;
995
-
996
- } else {
997
-
998
- throw new Error( 'THREE.USDZExporter: No valid image data found. Unable to process texture.' );
999
-
1000
- }
1001
-
1002
- }
1003
-
1004
- //
1005
-
1006
- const PRECISION = 7;
1007
-
1008
- function buildHeader() {
1009
-
1010
- return `#usda 1.0
1011
- (
1012
- customLayerData = {
1013
- string creator = "Needle Engine USDZExporter"
1014
- }
1015
- metersPerUnit = 1
1016
- upAxis = "Y"
1017
- )
1018
- `;
1019
-
1020
- }
1021
-
1022
- function buildUSDFileAsString( dataToInsert, _context: USDZExporterContext ) {
1023
-
1024
- let output = buildHeader();
1025
- output += dataToInsert;
1026
- return fflate.strToU8( output );
1027
-
1028
- }
1029
-
1030
- function getObjectId( object ) {
1031
-
1032
- return object.name.replace( /[-<>\(\)\[\]§$%&\/\\\=\?\,\;]/g, '' ) + '_' + object.id;
1033
-
1034
- }
1035
-
1036
- function getBoneName(bone) {
1037
- return makeNameSafe(bone.name || 'bone_' + bone.uuid);
1038
- }
1039
-
1040
- function getPathToSkeleton(bone: Object3D, assumedRoot: Object3D) {
1041
- let path = getBoneName(bone);
1042
- let current = bone.parent;
1043
- while ( current && current !== assumedRoot ) {
1044
- path = getBoneName(current) + '/' + path;
1045
- current = current.parent;
1046
- }
1047
- return path;
1048
- }
1049
-
1050
- // Xform
1051
-
1052
- export function buildXform( model: USDObject | null, writer: USDWriter, context: USDZExporterContext ) {
1053
-
1054
- if ( model == null)
1055
- return;
1056
-
1057
- const matrix = model.matrix;
1058
- const geometry = model.geometry;
1059
- const material = model.material;
1060
- const camera = model.camera;
1061
- const name = model.name;
1062
-
1063
- if ( model.animations ) {
1064
- for ( const animation of model.animations ) {
1065
- context.animations.push( animation )
1066
- }
1067
- }
1068
-
1069
- const transform = buildMatrix( matrix );
1070
-
1071
- if ( matrix.determinant() < 0 ) {
1072
-
1073
- console.warn( 'THREE.USDZExporter: USDZ does not support negative scales', name );
1074
-
1075
- }
1076
-
1077
- const isSkinnedMesh = geometry && geometry.isBufferGeometry && geometry.attributes.skinIndex !== undefined && geometry.attributes.skinIndex.count > 0;
1078
- const objType = isSkinnedMesh ? 'SkelRoot' : 'Xform';
1079
- const apiSchemas = isSkinnedMesh ? '"MaterialBindingAPI", "SkelBindingAPI"' : '"MaterialBindingAPI"';
1080
-
1081
- if ( geometry ) {
1082
- writer.beginBlock( `def ${objType} "${name}"`, "(", false );
1083
- if (context.quickLookCompatible && material && material.side === DoubleSide)
1084
- writer.appendLine(`prepend references = @./geometries/Geometry_${geometry.id}.usd@</Geometry_doubleSided>`);
1085
- else
1086
- writer.appendLine(`prepend references = @./geometries/Geometry_${geometry.id}.usd@</Geometry>`);
1087
- writer.appendLine(`prepend apiSchemas = [${apiSchemas}]`);
1088
- writer.closeBlock( ")" );
1089
- writer.beginBlock();
1090
- }
1091
- else if ( camera )
1092
- writer.beginBlock( `def Camera "${name}"` );
1093
- else
1094
- writer.beginBlock( `def Xform "${name}"` );
1095
-
1096
- if ( geometry && material ) {
1097
- writer.appendLine( `rel material:binding = </StageRoot/Materials/Material_${material.id}>` );
1098
-
1099
- // Turns out QuickLook / RealityKit doesn't support the doubleSided attribute, so we
1100
- // work around that by emitting additional indices above, and then we shouldn't emit the attribute either as geometry is
1101
- // already doubleSided then.
1102
- if (!context.quickLookCompatible && material.side === DoubleSide ) {
1103
- // double-sided is a mesh property in USD, we can apply it as `over` here
1104
- writer.beginBlock( `over "Geometry" `);
1105
- writer.appendLine( `uniform bool doubleSided = 1` );
1106
- writer.closeBlock();
1107
- }
1108
- }
1109
- if ( isSkinnedMesh ) {
1110
- writer.appendLine( `rel skel:skeleton = <Rig>` );
1111
- writer.appendLine( `rel skel:animationSource = <Rig/_anim>`);
1112
- writer.appendLine( `matrix4d xformOp:transform = ${buildMatrix(new Matrix4())}` ); // always identity / in world space
1113
- }
1114
- else {
1115
- writer.appendLine( `matrix4d xformOp:transform = ${transform}` );
1116
- }
1117
- writer.appendLine( 'uniform token[] xformOpOrder = ["xformOp:transform"]' );
1118
-
1119
- if ( camera ) {
1120
-
1121
- if ( 'isOrthographicCamera' in camera && camera.isOrthographicCamera ) {
1122
-
1123
- writer.appendLine( `float2 clippingRange = (${camera.near}, ${camera.far})` );
1124
- writer.appendLine( `float horizontalAperture = ${( ( Math.abs( camera.left ) + Math.abs( camera.right ) ) * 10 ).toPrecision( PRECISION )}` );
1125
- writer.appendLine( `float verticalAperture = ${( ( Math.abs( camera.top ) + Math.abs( camera.bottom ) ) * 10 ).toPrecision( PRECISION )}` );
1126
- writer.appendLine( 'token projection = "orthographic"' );
1127
-
1128
- } else if ( 'isPerspectiveCamera' in camera && camera.isPerspectiveCamera) {
1129
-
1130
- writer.appendLine( `float2 clippingRange = (${camera.near.toPrecision( PRECISION )}, ${camera.far.toPrecision( PRECISION )})` );
1131
- writer.appendLine( `float focalLength = ${camera.getFocalLength().toPrecision( PRECISION )}` );
1132
- writer.appendLine( `float focusDistance = ${camera.focus.toPrecision( PRECISION )}` );
1133
- writer.appendLine( `float horizontalAperture = ${camera.getFilmWidth().toPrecision( PRECISION )}` );
1134
- writer.appendLine( 'token projection = "perspective"' );
1135
- writer.appendLine( `float verticalAperture = ${camera.getFilmHeight().toPrecision( PRECISION )}` );
1136
- }
1137
-
1138
- }
1139
-
1140
- if ( model.onSerialize ) {
1141
-
1142
- model.onSerialize( writer, context );
1143
-
1144
- }
1145
-
1146
- if ( model.children ) {
1147
-
1148
- writer.appendLine();
1149
- for ( const ch of model.children ) {
1150
-
1151
- buildXform( ch, writer, context );
1152
-
1153
- }
1154
-
1155
- }
1156
-
1157
- writer.closeBlock();
1158
-
1159
- }
1160
-
1161
- function fn( num:number ): string {
1162
-
1163
- return Number.isInteger(num) ? num.toString() : num.toFixed( 10 );
1164
-
1165
- }
1166
-
1167
- function buildMatrix( matrix ) {
1168
-
1169
- const array = matrix.elements;
1170
-
1171
- return `( ${buildMatrixRow( array, 0 )}, ${buildMatrixRow( array, 4 )}, ${buildMatrixRow( array, 8 )}, ${buildMatrixRow( array, 12 )} )`;
1172
-
1173
- }
1174
-
1175
- function buildMatrixRow( array, offset ) {
1176
-
1177
- return `(${fn( array[ offset + 0 ] )}, ${fn( array[ offset + 1 ] )}, ${fn( array[ offset + 2 ] )}, ${fn( array[ offset + 3 ] )})`;
1178
-
1179
- }
1180
-
1181
- // Mesh
1182
-
1183
- function buildMeshObject( geometry, bonesArray: Bone[] = [] ) {
1184
-
1185
- const mesh = buildMesh( geometry, bonesArray );
1186
- return `
1187
- def "Geometry"
1188
- ${mesh}
1189
- `;
1190
-
1191
- }
1192
-
1193
- function buildMesh( geometry, bones: Bone[] = [] ) {
1194
-
1195
- const name = 'Geometry';
1196
- const attributes = geometry.attributes;
1197
- const count = attributes.position.count;
1198
-
1199
- const hasBones = bones && bones.length > 0;
1200
-
1201
- // We need to sort bones and all skinning data by path –
1202
- // Neither glTF nor three.js care, but in USD they must be sorted
1203
- // since the array defines the virtual hierarchy and is evaluated in that order
1204
- const sortedBones: Array<{bone: Object3D, index: number}> = [];
1205
- const indexMapping: number[] = [];
1206
- let sortedSkinIndex = new Array<number>();
1207
- let sortedSkinIndexAttribute: BufferAttribute | null = attributes.skinIndex;
1208
- let bonesArray = "";
1209
- if (hasBones) {
1210
-
1211
- for ( const index in bones ) {
1212
- sortedBones.push( { bone: bones[index], index: parseInt(index) } );
1213
- }
1214
-
1215
- // add structural nodes to the list of bones
1216
- for ( const structuralNode of findStructuralNodesInBoneHierarchy(bones) ) {
1217
- sortedBones.push( { bone: structuralNode, index: sortedBones.length } );
1218
- }
1219
-
1220
- // sort bones by path – need to be sorted in the same order as during mesh export
1221
- const assumedRoot = bones[0].parent!;
1222
- sortedBones.sort((a, b) => getPathToSkeleton(a.bone, assumedRoot) > getPathToSkeleton(b.bone, assumedRoot) ? 1 : -1);
1223
- bonesArray = sortedBones.map( x => "\"" + getPathToSkeleton(x.bone, assumedRoot) + "\"" ).join( ', ' );
1224
-
1225
- // TODO we can probably skip the expensive attribute re-ordering if the bones were already in a correct order.
1226
- // That doesn't mean that they are strictly sorted by path – just that all parents strictly need to come first.
1227
-
1228
- // build index mapping
1229
- for (const i in sortedBones) {
1230
- indexMapping[sortedBones[i].index] = parseInt(i);
1231
- }
1232
-
1233
- // remap skin index attributes
1234
- const skinIndex = attributes.skinIndex;
1235
- sortedSkinIndex = new Array<number>();
1236
- for ( let i = 0; i < skinIndex.count; i ++ ) {
1237
-
1238
- const x = skinIndex.getX( i );
1239
- const y = skinIndex.getY( i );
1240
- const z = skinIndex.getZ( i );
1241
- const w = skinIndex.getW( i );
1242
-
1243
- sortedSkinIndex.push( indexMapping[x], indexMapping[y], indexMapping[z], indexMapping[w] );
1244
- }
1245
-
1246
- // turn it back into an attribute so the rest of the code doesn't need to learn a new thing
1247
- sortedSkinIndexAttribute = new BufferAttribute( new Uint16Array( sortedSkinIndex ), 4 );
1248
- }
1249
-
1250
- const isSkinnedMesh = attributes.skinWeight && attributes.skinIndex;
1251
-
1252
- return `
1253
- {
1254
- def Mesh "${name}" ${isSkinnedMesh ? `(
1255
- prepend apiSchemas = ["SkelBindingAPI"]
1256
- )` : ''}
1257
- {
1258
- int[] faceVertexCounts = [${buildMeshVertexCount( geometry )}]
1259
- int[] faceVertexIndices = [${buildMeshVertexIndices( geometry )}]
1260
- normal3f[] normals = [${buildVector3Array( attributes.normal, count )}] (
1261
- interpolation = "vertex"
1262
- )
1263
- point3f[] points = [${buildVector3Array( attributes.position, count )}]
1264
- ${attributes.uv ?
1265
- `texCoord2f[] primvars:st = [${buildVector2Array( attributes.uv, count )}] (
1266
- interpolation = "vertex"
1267
- )` : '' }
1268
- ${attributes.uv2 ?
1269
- `texCoord2f[] primvars:st2 = [${buildVector2Array( attributes.uv2, count )}] (
1270
- interpolation = "vertex"
1271
- )` : '' }
1272
- ${isSkinnedMesh ?
1273
- `matrix4d primvars:skel:geomBindTransform = ( (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1) ) (
1274
- elementSize = 1
1275
- interpolation = "constant"
1276
- )` : '' }
1277
- ${attributes.skinIndex ?
1278
- `int[] primvars:skel:jointIndices = [${buildVector4Array( sortedSkinIndexAttribute, true )}] (
1279
- elementSize = 4
1280
- interpolation = "vertex"
1281
- )` : '' }
1282
- ${attributes.skinWeight ?
1283
- `float[] primvars:skel:jointWeights = [${buildVector4Array( attributes.skinWeight )}] (
1284
- elementSize = 4
1285
- interpolation = "vertex"
1286
- )` : '' }
1287
- ${hasBones ?
1288
- //`uniform token[] skel:blendShapes
1289
- `uniform token[] skel:joints = [${bonesArray}]` : '' }
1290
- uniform token subdivisionScheme = "none"
1291
- }
1292
- }
1293
-
1294
- # This is a workaround for QuickLook/RealityKit not supporting the doubleSided attribute. We're adding a second
1295
- # geometry definition here, that uses the same mesh data but appends extra faces with reversed winding order.
1296
- def "${name}_doubleSided" (
1297
- prepend references = </Geometry>
1298
- )
1299
- {
1300
- over "Geometry"
1301
- {
1302
- int[] faceVertexCounts = [${buildMeshVertexCount( geometry ) + ", " + buildMeshVertexCount( geometry )}]
1303
- int[] faceVertexIndices = [${buildMeshVertexIndices( geometry ) + ", " + buildMeshVertexIndices( geometry, true )}]
1304
- }
1305
- }
1306
- `;
1307
-
1308
- }
1309
-
1310
- function buildMeshVertexCount( geometry ) {
1311
-
1312
- const count = geometry.index !== null ? geometry.index.count : geometry.attributes.position.count;
1313
-
1314
- return Array( count / 3 ).fill( 3 ).join( ', ' );
1315
-
1316
- }
1317
-
1318
- function buildMeshVertexIndices( geometry: BufferGeometry, reverseWinding: boolean = false ) {
1319
-
1320
- const index = geometry.index;
1321
- const array: Array<number> = [];
1322
-
1323
- if ( index !== null ) {
1324
-
1325
- for ( let i = 0; i < index.count; i ++ ) {
1326
-
1327
- let val = i;
1328
- if ( reverseWinding )
1329
- val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1330
- array.push( index.getX( val ) );
1331
-
1332
- }
1333
-
1334
- } else {
1335
-
1336
- const length = geometry.attributes.position.count;
1337
-
1338
- for ( let i = 0; i < length; i ++ ) {
1339
-
1340
- let val = i;
1341
- if ( reverseWinding )
1342
- val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1343
- array.push( val );
1344
-
1345
- }
1346
-
1347
- }
1348
-
1349
- return array.join( ', ' );
1350
-
1351
- }
1352
-
1353
- function buildVector3Array( attribute, count ) {
1354
-
1355
- if ( attribute === undefined ) {
1356
-
1357
- console.warn( 'USDZExporter: Normals missing.' );
1358
- return Array( count ).fill( '(0, 0, 0)' ).join( ', ' );
1359
-
1360
- }
1361
-
1362
- const array: Array<string> = [];
1363
-
1364
- for ( let i = 0; i < attribute.count; i ++ ) {
1365
-
1366
- const x = attribute.getX( i );
1367
- const y = attribute.getY( i );
1368
- const z = attribute.getZ( i );
1369
-
1370
- array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )}, ${z.toPrecision( PRECISION )})` );
1371
-
1372
- }
1373
-
1374
- return array.join( ', ' );
1375
-
1376
- }
1377
-
1378
- function buildVector4Array( attribute, ints = false ) {
1379
- const array: Array<string> = [];
1380
-
1381
- for ( let i = 0; i < attribute.count; i ++ ) {
1382
-
1383
- const x = attribute.getX( i );
1384
- const y = attribute.getY( i );
1385
- const z = attribute.getZ( i );
1386
- const w = attribute.getW( i );
1387
-
1388
- array.push( `${ints ? x : x.toPrecision( PRECISION )}` );
1389
- array.push( `${ints ? y : y.toPrecision( PRECISION )}` );
1390
- array.push( `${ints ? z : z.toPrecision( PRECISION )}` );
1391
- array.push( `${ints ? w : w.toPrecision( PRECISION )}` );
1392
-
1393
- }
1394
-
1395
- return array.join( ', ' );
1396
-
1397
- }
1398
-
1399
- function buildVector2Array( attribute, count ) {
1400
-
1401
- if ( attribute === undefined ) {
1402
-
1403
- console.warn( 'USDZExporter: UVs missing.' );
1404
- return Array( count ).fill( '(0, 0)' ).join( ', ' );
1405
-
1406
- }
1407
-
1408
- const array: Array<string> = [];
1409
-
1410
- for ( let i = 0; i < attribute.count; i ++ ) {
1411
-
1412
- const x = attribute.getX( i );
1413
- const y = attribute.getY( i );
1414
-
1415
- array.push( `(${x.toPrecision( PRECISION )}, ${1 - y.toPrecision( PRECISION )})` );
1416
-
1417
- }
1418
-
1419
- return array.join( ', ' );
1420
-
1421
- }
1422
-
1423
- // Materials
1424
-
1425
- function buildMaterials( materials: Map<string, Material>, textures: TextureMap, quickLookCompatible = false ) {
1426
-
1427
- const array: Array<string> = [];
1428
-
1429
- for ( const uuid in materials ) {
1430
-
1431
- const material = materials[ uuid ];
1432
-
1433
- array.push( buildMaterial( material, textures, quickLookCompatible ) );
1434
-
1435
- }
1436
-
1437
- return `def "Materials"
1438
- {
1439
- ${array.join( '' )}
1440
- }
1441
-
1442
- `;
1443
-
1444
- }
1445
-
1446
- function buildMaterial( material: MeshBasicMaterial, textures: TextureMap, quickLookCompatible = false ) {
1447
-
1448
- // https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
1449
-
1450
- const pad = ' ';
1451
- const inputs: Array<string> = [];
1452
- const samplers: Array<string> = [];
1453
-
1454
- function buildTexture( texture, mapType, color: Color | undefined = undefined, opacity: number | undefined = undefined ) {
1455
-
1456
- const id = texture.id + ( color ? '_' + color.getHexString() : '' ) + ( opacity !== undefined ? '_' + opacity : '' );
1457
-
1458
- // Seems neither QuickLook nor usdview support scale/bias on .a values, so we need to bake opacity multipliers into
1459
- // the texture. This is not ideal.
1460
- const opacityIsAppliedToTextureAndNotAsScale = quickLookCompatible && opacity !== undefined;
1461
- const scaleToApply = opacityIsAppliedToTextureAndNotAsScale ? new Vector4(1, 1, 1, opacity) : undefined;
1462
- textures[ id ] = { texture, scale: scaleToApply };
1463
-
1464
- const uv = texture.channel > 0 ? 'st' + texture.channel : 'st';
1465
-
1466
- const isRGBA = formatsWithAlphaChannel.includes( texture.format );
1467
-
1468
- const WRAPPINGS = {
1469
- 1000: 'repeat', // RepeatWrapping
1470
- 1001: 'clamp', // ClampToEdgeWrapping
1471
- 1002: 'mirror' // MirroredRepeatWrapping
1472
- };
1473
-
1474
- const repeat = texture.repeat.clone();
1475
- const offset = texture.offset.clone();
1476
- const rotation = texture.rotation;
1477
-
1478
- // rotation is around the wrong point. after rotation we need to shift offset again so that we're rotating around the right spot
1479
- const xRotationOffset = Math.sin(rotation);
1480
- const yRotationOffset = Math.cos(rotation);
1481
-
1482
- // texture coordinates start in the opposite corner, need to correct
1483
- offset.y = 1 - offset.y - repeat.y;
1484
-
1485
- // turns out QuickLook is buggy and interprets texture repeat inverted.
1486
- // Apple Feedback: FB10036297 and FB11442287
1487
- if ( quickLookCompatible ) {
1488
-
1489
- // This is NOT correct yet in QuickLook, but comes close for a range of models.
1490
- // It becomes more incorrect the bigger the offset is
1491
-
1492
- offset.x = offset.x / repeat.x;
1493
- offset.y = offset.y / repeat.y;
1494
-
1495
- offset.x += xRotationOffset / repeat.x;
1496
- offset.y += yRotationOffset - 1;
1497
- }
1498
-
1499
- else {
1500
-
1501
- // results match glTF results exactly. verified correct in usdview.
1502
- offset.x += xRotationOffset * repeat.x;
1503
- offset.y += (1 - yRotationOffset) * repeat.y;
1504
-
1505
- }
1506
-
1507
- const needsTextureTransform = ( repeat.x != 1 || repeat.y != 1 || offset.x != 0 || offset.y != 0 || rotation != 0 );
1508
- const textureTransformInput = `${materialRoot}/Material_${material.id}/${'uvReader_' + uv}.outputs:result>`; const textureTransformOutput = `${materialRoot}/Material_${material.id}/Transform2d_${mapType}.outputs:result>`;
1509
- const needsTextureScale = mapType !== 'normal' && (color && (color.r !== 1 || color.g !== 1 || color.b !== 1 || opacity !== 1)) || false;
1510
-
1511
- const needsNormalScaleAndBias = mapType === 'normal';
1512
- const normalScale = material instanceof MeshStandardMaterial ? (material.normalScale ? material.normalScale.x * 2 : 2) : 2;
1513
- const normalScaleValueString = normalScale.toFixed( PRECISION );
1514
- const normalBiasString = (-1 * (normalScale / 2)).toFixed( PRECISION );
1515
- const normalBiasZString = (1 - normalScale).toFixed( PRECISION );
1516
-
1517
- return `
1518
- ${needsTextureTransform ? `def Shader "Transform2d_${mapType}" (
1519
- sdrMetadata = {
1520
- string role = "math"
1521
- }
1522
- )
1523
- {
1524
- uniform token info:id = "UsdTransform2d"
1525
- float2 inputs:in.connect = ${textureTransformInput}
1526
- float2 inputs:scale = ${buildVector2( repeat )}
1527
- float2 inputs:translation = ${buildVector2( offset )}
1528
- float inputs:rotation = ${(rotation / Math.PI * 180).toFixed( PRECISION )}
1529
- float2 outputs:result
1530
- }
1531
- ` : '' }
1532
- def Shader "Texture_${texture.id}_${mapType}"
1533
- {
1534
- uniform token info:id = "UsdUVTexture"
1535
- asset inputs:file = @textures/Texture_${id}.${isRGBA ? 'png' : 'jpg'}@
1536
- token inputs:sourceColorSpace = "${ texture.colorSpace === 'srgb' ? 'sRGB' : 'raw' }"
1537
- float2 inputs:st.connect = ${needsTextureTransform ? textureTransformOutput : textureTransformInput}
1538
- ${needsTextureScale ? `
1539
- float4 inputs:scale = (${color ? color.r + ', ' + color.g + ', ' + color.b : '1, 1, 1'}, ${(opacity !== undefined && !opacityIsAppliedToTextureAndNotAsScale) ? opacity : '1'})
1540
- ` : `` }
1541
- ${needsNormalScaleAndBias ? `
1542
- float4 inputs:scale = (${normalScaleValueString}, ${normalScaleValueString}, ${normalScaleValueString}, 1)
1543
- float4 inputs:bias = (${normalBiasString}, ${normalBiasString}, ${normalBiasZString}, 0)
1544
- ` : `` }
1545
- token inputs:wrapS = "${ WRAPPINGS[ texture.wrapS ] }"
1546
- token inputs:wrapT = "${ WRAPPINGS[ texture.wrapT ] }"
1547
- float outputs:r
1548
- float outputs:g
1549
- float outputs:b
1550
- float3 outputs:rgb
1551
- ${material.transparent || material.alphaTest > 0.0 ? 'float outputs:a' : ''}
1552
- }`;
1553
-
1554
- }
1555
-
1556
- let effectiveOpacity = ( material.transparent || material.alphaTest ) ? material.opacity : 1;
1557
-
1558
- if ( material instanceof MeshPhysicalMaterial && material.transmission !== undefined) {
1559
-
1560
- // TODO does not help when a roughnessMap is used
1561
- effectiveOpacity *= (1 - material.transmission * (1 - (material.roughness * 0.5)));
1562
-
1563
- }
1564
-
1565
- if ( material.map !== null ) {
1566
-
1567
- inputs.push( `${pad}color3f inputs:diffuseColor.connect = ${materialRoot}/Material_${material.id}/Texture_${material.map.id}_diffuse.outputs:rgb>` );
1568
-
1569
- if ( material.transparent ) {
1570
-
1571
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.map.id}_diffuse.outputs:a>` );
1572
-
1573
- } else if ( material.alphaTest > 0.0 ) {
1574
-
1575
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.map.id}_diffuse.outputs:a>` );
1576
- inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
1577
-
1578
- }
1579
-
1580
- samplers.push( buildTexture( material.map, 'diffuse', material.color, effectiveOpacity ) );
1581
-
1582
- } else {
1583
-
1584
- inputs.push( `${pad}color3f inputs:diffuseColor = ${buildColor( material.color )}` );
1585
-
1586
- }
1587
-
1588
- if ( material.aoMap ) {
1589
-
1590
- inputs.push( `${pad}float inputs:occlusion.connect = ${materialRoot}/Material_${material.id}/Texture_${material.aoMap.id}_occlusion.outputs:r>` );
1591
-
1592
- samplers.push( buildTexture( material.aoMap, 'occlusion' ) );
1593
-
1594
- }
1595
-
1596
- if ( material.alphaMap ) {
1597
-
1598
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.alphaMap.id}_opacity.outputs:r>` );
1599
- inputs.push( `${pad}float inputs:opacityThreshold = 0.0001` );
1600
-
1601
- samplers.push( buildTexture( material.alphaMap, 'opacity', new Color( 1, 1, 1 ), effectiveOpacity ) );
1602
-
1603
- } else {
1604
-
1605
- inputs.push( `${pad}float inputs:opacity = ${effectiveOpacity}` );
1606
-
1607
- if ( material.alphaTest > 0.0 ) {
1608
-
1609
- inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
1610
-
1611
- }
1612
-
1613
- }
1614
-
1615
- if ( material instanceof MeshStandardMaterial ) {
1616
-
1617
- if ( material.emissiveMap ) {
1618
-
1619
- inputs.push( `${pad}color3f inputs:emissiveColor.connect = ${materialRoot}/Material_${material.id}/Texture_${material.emissiveMap.id}_emissive.outputs:rgb>` );
1620
- const color = material.emissive.clone();
1621
- color.multiplyScalar( material.emissiveIntensity );
1622
- samplers.push( buildTexture( material.emissiveMap, 'emissive', color ) );
1623
-
1624
- } else if ( material.emissive?.getHex() > 0 ) {
1625
-
1626
- const color = material.emissive.clone();
1627
- color.multiplyScalar( material.emissiveIntensity );
1628
- inputs.push( `${pad}color3f inputs:emissiveColor = ${buildColor( color )}` );
1629
-
1630
- } else {
1631
-
1632
- inputs.push( `${pad}color3f inputs:emissiveColor = (0, 0, 0)` );
1633
-
1634
- }
1635
-
1636
- if ( material.normalMap ) {
1637
-
1638
- inputs.push( `${pad}normal3f inputs:normal.connect = ${materialRoot}/Material_${material.id}/Texture_${material.normalMap.id}_normal.outputs:rgb>` );
1639
-
1640
- samplers.push( buildTexture( material.normalMap, 'normal' ) );
1641
-
1642
- }
1643
-
1644
- if ( material.roughnessMap && material.roughness === 1 ) {
1645
-
1646
- inputs.push( `${pad}float inputs:roughness.connect = ${materialRoot}/Material_${material.id}/Texture_${material.roughnessMap.id}_roughness.outputs:g>` );
1647
-
1648
- samplers.push( buildTexture( material.roughnessMap, 'roughness' ) );
1649
-
1650
- } else {
1651
-
1652
- inputs.push( `${pad}float inputs:roughness = ${material.roughness !== undefined ? material.roughness : 1 }` );
1653
-
1654
- }
1655
-
1656
- if ( material.metalnessMap && material.metalness === 1 ) {
1657
-
1658
- inputs.push( `${pad}float inputs:metallic.connect = ${materialRoot}/Material_${material.id}/Texture_${material.metalnessMap.id}_metallic.outputs:b>` );
1659
-
1660
- samplers.push( buildTexture( material.metalnessMap, 'metallic' ) );
1661
-
1662
- } else {
1663
-
1664
- inputs.push( `${pad}float inputs:metallic = ${material.metalness !== undefined ? material.metalness : 0 }` );
1665
-
1666
- }
1667
-
1668
- }
1669
-
1670
- if ( material instanceof MeshPhysicalMaterial ) {
1671
-
1672
- inputs.push( `${pad}float inputs:clearcoat = ${material.clearcoat}` );
1673
- inputs.push( `${pad}float inputs:clearcoatRoughness = ${material.clearcoatRoughness}` );
1674
- inputs.push( `${pad}float inputs:ior = ${material.ior}` );
1675
-
1676
- if ( !material.transparent && ! (material.alphaTest > 0.0) && material.transmissionMap) {
1677
-
1678
- inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.transmissionMap.id}_transmission.outputs:r>` );
1679
-
1680
- samplers.push( buildTexture( material.transmissionMap, 'transmission' ) );
1681
- }
1682
-
1683
- }
1684
-
1685
- return `
1686
- def Material "Material_${material.id}"
1687
- {
1688
- def Shader "PreviewSurface"
1689
- {
1690
- uniform token info:id = "UsdPreviewSurface"
1691
- ${inputs.join( '\n' )}
1692
- int inputs:useSpecularWorkflow = 0
1693
- token outputs:surface
1694
- }
1695
-
1696
- token outputs:surface.connect = ${materialRoot}/Material_${material.id}/PreviewSurface.outputs:surface>
1697
-
1698
- def Shader "uvReader_st"
1699
- {
1700
- uniform token info:id = "UsdPrimvarReader_float2"
1701
- token inputs:varname = "st"
1702
- float2 inputs:fallback = (0.0, 0.0)
1703
- float2 outputs:result
1704
- }
1705
-
1706
- def Shader "uvReader_st2"
1707
- {
1708
- uniform token info:id = "UsdPrimvarReader_float2"
1709
- token inputs:varname = "st2"
1710
- float2 inputs:fallback = (0.0, 0.0)
1711
- float2 outputs:result
1712
- }
1713
-
1714
- ${samplers.join( '\n' )}
1715
-
1716
- }
1717
- `;
1718
-
1719
- }
1720
-
1721
- function buildColor( color ) {
1722
-
1723
- return `(${color.r}, ${color.g}, ${color.b})`;
1724
-
1725
- }
1726
-
1727
- function buildVector2( vector ) {
1728
-
1729
- return `(${ vector.x }, ${ vector.y })`;
1730
-
1731
- }
1732
-
1733
- const formatsWithAlphaChannel = [
1734
- // uncompressed formats with alpha channel
1735
- 1023, // RGBAFormat
1736
- // compressed formats with alpha channel
1737
- 33777, // RGBA_S3TC_DXT1_Format
1738
- 33778, // RGBA_S3TC_DXT3_Format
1739
- 33779, // RGBA_S3TC_DXT5_Format
1740
- 35842, // RGBA_PVRTC_4BPPV1_Format
1741
- 35843, // RGBA_PVRTC_2BPPV1_Format
1742
- 37496, // RGBA_ETC2_EAC_Format
1743
- 37808, // RGBA_ASTC_4x4_Format
1744
- 37809, // RGBA_ASTC_5x4_Format
1745
- 37810, // RGBA_ASTC_5x5_Format
1746
- 37811, // RGBA_ASTC_6x5_Format
1747
- 37812, // RGBA_ASTC_6x6_Format
1748
- 37813, // RGBA_ASTC_8x5_Format
1749
- 37814, // RGBA_ASTC_8x6_Format
1750
- 37815, // RGBA_ASTC_8x8_Format
1751
- 37816, // RGBA_ASTC_10x5_Format
1752
- 37817, // RGBA_ASTC_10x6_Format
1753
- 37818, // RGBA_ASTC_10x8_Format
1754
- 37819, // RGBA_ASTC_10x10_Format
1755
- 37820, // RGBA_ASTC_12x10_Format
1756
- 37821, // RGBA_ASTC_12x12_Format
1757
- 36492, // RGBA_BPTC_Format
1758
- ];
1759
-
1760
- export {
1761
- USDZExporter,
1762
- USDZExporterContext,
1763
- USDWriter,
1764
- USDObject,
1765
- buildMatrix,
1766
- getBoneName,
1767
- getPathToSkeleton,
1768
- fn as usdNumberFormatting,
1769
- USDDocument,
1770
- makeNameSafe as makeNameSafeForUSD,
1771
- imageToCanvas,
1772
- decompressGpuTexture,
1773
- findStructuralNodesInBoneHierarchy,
1
+ import { Renderer } from '../../Renderer.js';
2
+ import { GameObject } from '../../Component.js';
3
+ import type { OffscreenCanvasExt } from '../../../engine/engine_shims.js';
4
+ import '../../../engine/engine_shims.js';
5
+ import {
6
+ PlaneGeometry,
7
+ Texture,
8
+ Uniform,
9
+ PerspectiveCamera,
10
+ Scene,
11
+ Mesh,
12
+ ShaderMaterial,
13
+ WebGLRenderer,
14
+ MathUtils,
15
+ Matrix4,
16
+ DoubleSide,
17
+ BufferGeometry,
18
+ Material,
19
+ Color,
20
+ MeshStandardMaterial,
21
+ MeshPhysicalMaterial,
22
+ Object3D,
23
+ MeshBasicMaterial,
24
+ Bone,
25
+ SkinnedMesh,
26
+ SRGBColorSpace,
27
+ AnimationClip,
28
+ OrthographicCamera,
29
+ BufferAttribute,
30
+ Vector4
31
+ } from 'three';
32
+ import * as fflate from 'three/examples/jsm/libs/fflate.module.js';
33
+
34
+ function makeNameSafe( str ) {
35
+ str = str.replace( /[^a-zA-Z0-9_]/g, '' );
36
+
37
+ // if str does not start with a-zA-Z_ add _ to the beginning
38
+ if ( !str.match( /^[a-zA-Z_]/ ) )
39
+ str = '_' + str;
40
+
41
+ return str;
42
+ }
43
+
44
+ // TODO check if this works when bones in the skeleton are completely unordered
45
+ function findCommonAncestor(objects: Object3D[]): Object3D | null {
46
+ if (objects.length === 0) return null;
47
+
48
+ const ancestors = objects.map((obj) => {
49
+ const objAncestors = new Array<Object3D>();
50
+ while (obj.parent) {
51
+ objAncestors.unshift(obj.parent);
52
+ obj = obj.parent;
53
+ }
54
+ return objAncestors;
55
+ });
56
+
57
+ //@ts-ignore – findLast seems to be missing in TypeScript types pre-5.x
58
+ const commonAncestor = ancestors[0].findLast((ancestor) => {
59
+ return ancestors.every((a) => a.includes(ancestor));
60
+ });
61
+
62
+ return commonAncestor || null;
63
+ }
64
+
65
+ function findStructuralNodesInBoneHierarchy(bones: Array<Object3D>) {
66
+
67
+ const commonAncestor = findCommonAncestor(bones);
68
+ // find all structural nodes – parents of bones that are not bones themselves
69
+ const structuralNodes = new Set<Object3D>();
70
+ for ( const bone of bones ) {
71
+ let current = bone.parent;
72
+ while ( current && current !== commonAncestor ) {
73
+ if ( !bones.includes(current) ) {
74
+ structuralNodes.add(current);
75
+ }
76
+ current = current.parent;
77
+ }
78
+ }
79
+
80
+ return structuralNodes;
81
+ }
82
+
83
+ class USDObject {
84
+
85
+ static USDObject_export_id = 0;
86
+
87
+ uuid: string;
88
+ name: string;
89
+ matrix: Matrix4;
90
+ private _isDynamic: boolean;
91
+ get isDynamic() { return this._isDynamic; }
92
+ private set isDynamic( value ) { this._isDynamic = value; }
93
+ geometry: BufferGeometry | null;
94
+ material: MeshStandardMaterial | MeshBasicMaterial | Material | null;
95
+ camera: PerspectiveCamera | OrthographicCamera | null;
96
+ parent: USDObject | null;
97
+ skinnedMesh: SkinnedMesh | null;
98
+ children: Array<USDObject | null> = [];
99
+ animations: AnimationClip[] | null;
100
+ _eventListeners: {};
101
+
102
+ static createEmptyParent( object ) {
103
+
104
+ const emptyParent = new USDObject( MathUtils.generateUUID(), object.name + '_empty_' + ( USDObject.USDObject_export_id ++ ), object.matrix );
105
+ const parent = object.parent;
106
+ parent.add( emptyParent );
107
+ emptyParent.add( object );
108
+ emptyParent.isDynamic = true;
109
+ object.matrix = new Matrix4().identity();
110
+ return emptyParent;
111
+
112
+ }
113
+
114
+ static createEmpty() {
115
+
116
+ const empty = new USDObject( MathUtils.generateUUID(), 'Empty_' + ( USDObject.USDObject_export_id ++ ), new Matrix4() );
117
+ empty.isDynamic = true;
118
+ return empty;
119
+ }
120
+
121
+ constructor( id, name, matrix, mesh: BufferGeometry | null = null, material: MeshStandardMaterial | MeshBasicMaterial | Material | null = null, camera: PerspectiveCamera | OrthographicCamera | null = null, skinnedMesh: SkinnedMesh | null = null, animations: AnimationClip[] | null = null ) {
122
+
123
+ this.uuid = id;
124
+ this.name = makeNameSafe( name );
125
+ this.matrix = matrix.clone();
126
+ this.geometry = mesh;
127
+ this.material = material;
128
+ this.camera = camera;
129
+ this.parent = null;
130
+ this.children = [];
131
+ this._eventListeners = {};
132
+ this._isDynamic = false;
133
+ this.skinnedMesh = skinnedMesh;
134
+ this.animations = animations;
135
+
136
+ }
137
+
138
+ is( obj ) {
139
+
140
+ if ( ! obj ) return false;
141
+ return this.uuid === obj.uuid;
142
+
143
+ }
144
+
145
+ isEmpty() {
146
+
147
+ return ! this.geometry;
148
+
149
+ }
150
+
151
+ clone() {
152
+
153
+ const clone = new USDObject( MathUtils.generateUUID(), this.name, this.matrix, this.geometry, this.material );
154
+ clone.isDynamic = this.isDynamic;
155
+ return clone;
156
+
157
+ }
158
+
159
+ deepClone() {
160
+
161
+ const clone = this.clone();
162
+ for ( const child of this.children ) {
163
+
164
+ if ( !child ) continue;
165
+ clone.add( child.deepClone() );
166
+
167
+ }
168
+
169
+ return clone;
170
+
171
+ }
172
+
173
+ getPath() {
174
+
175
+ let current = this.parent;
176
+ let path = this.name;
177
+ while ( current ) {
178
+
179
+ // StageRoot has a special path right now since there's additional Xforms for encapsulation.
180
+ // Better would be to actually model them as part of our object graph, but they're written separately,
181
+ // so currently we don't and instead work around that here.
182
+ const currentName = current.parent ? current.name : (current.name + "/Scenes/Scene");
183
+ path = currentName + '/' + path;
184
+ current = current.parent;
185
+
186
+ }
187
+
188
+ return '</' + path + '>';
189
+
190
+ }
191
+
192
+ add( child ) {
193
+
194
+ if ( child.parent ) {
195
+
196
+ child.parent.remove( child );
197
+
198
+ }
199
+
200
+ child.parent = this;
201
+ this.children.push( child );
202
+
203
+ }
204
+
205
+ remove( child ) {
206
+
207
+ const index = this.children.indexOf( child );
208
+ if ( index >= 0 ) {
209
+
210
+ if ( child.parent === this ) child.parent = null;
211
+ this.children.splice( index, 1 );
212
+
213
+ }
214
+
215
+ }
216
+
217
+ addEventListener( evt, listener ) {
218
+
219
+ if ( ! this._eventListeners[ evt ] ) this._eventListeners[ evt ] = [];
220
+ this._eventListeners[ evt ].push( listener );
221
+
222
+ }
223
+
224
+ removeEventListener( evt, listener ) {
225
+
226
+ if ( ! this._eventListeners[ evt ] ) return;
227
+ const index = this._eventListeners[ evt ].indexOf( listener );
228
+ if ( index >= 0 ) {
229
+
230
+ this._eventListeners[ evt ].splice( index, 1 );
231
+
232
+ }
233
+
234
+ }
235
+
236
+ onSerialize( writer, context ) {
237
+
238
+ const listeners = this._eventListeners[ 'serialize' ];
239
+ if ( listeners ) listeners.forEach( listener => listener( writer, context ) );
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+ class USDDocument extends USDObject {
247
+
248
+ stageLength: number;
249
+
250
+ get isDocumentRoot() {
251
+
252
+ return true;
253
+
254
+ }
255
+ get isDynamic() {
256
+
257
+ return false;
258
+
259
+ }
260
+
261
+ constructor() {
262
+
263
+ super(undefined, 'StageRoot', new Matrix4(), null, null, null);
264
+ this.children = [];
265
+ this.stageLength = 200;
266
+
267
+ }
268
+
269
+ add( child: USDObject ) {
270
+
271
+ child.parent = this;
272
+ this.children.push( child );
273
+
274
+ }
275
+
276
+ remove( child: USDObject ) {
277
+
278
+ const index = this.children.indexOf( child );
279
+ if ( index >= 0 ) {
280
+
281
+ if ( child.parent === this ) child.parent = null;
282
+ this.children.splice( index, 1 );
283
+
284
+ }
285
+
286
+ }
287
+
288
+ traverse( callback: ( object: USDObject ) => void, current: USDObject | null = null ) {
289
+
290
+ if ( current !== null ) callback( current );
291
+ else current = this;
292
+ if ( current.children ) {
293
+
294
+ for ( const child of current.children ) {
295
+
296
+ this.traverse( callback, child );
297
+
298
+ }
299
+
300
+ }
301
+
302
+ }
303
+
304
+ findById( uuid: string ) {
305
+
306
+ let found = false;
307
+ function search( current ) {
308
+
309
+ if ( found ) return;
310
+ if ( current.uuid === uuid ) {
311
+
312
+ found = true;
313
+ return current;
314
+
315
+ }
316
+
317
+ if ( current.children ) {
318
+
319
+ for ( const child of current.children ) {
320
+
321
+ const res = search( child );
322
+ if ( res ) return res;
323
+
324
+ }
325
+
326
+ }
327
+
328
+ }
329
+
330
+ return search( this );
331
+
332
+ }
333
+
334
+
335
+ buildHeader( endTimeCode ) {
336
+
337
+ return `#usda 1.0
338
+ (
339
+ customLayerData = {
340
+ string creator = "Needle Engine USDZExporter"
341
+ }
342
+ defaultPrim = "${makeNameSafe( this.name )}"
343
+ metersPerUnit = 1
344
+ upAxis = "Y"
345
+ startTimeCode = 0
346
+ endTimeCode = ${endTimeCode}
347
+ timeCodesPerSecond = 60
348
+ framesPerSecond = 60
349
+ )
350
+ `;
351
+
352
+ }
353
+
354
+ }
355
+
356
+ const newLine = '\n';
357
+ const materialRoot = '</StageRoot/Materials';
358
+
359
+ class USDWriter {
360
+ str: string;
361
+ indent: number;
362
+
363
+ constructor() {
364
+
365
+ this.str = '';
366
+ this.indent = 0;
367
+
368
+ }
369
+
370
+ clear() {
371
+
372
+ this.str = '';
373
+ this.indent = 0;
374
+
375
+ }
376
+
377
+ beginBlock( str: string | undefined = undefined, char = '{', createNewLine = true ) {
378
+
379
+ if ( str !== undefined ) {
380
+ str = this.applyIndent( str );
381
+ this.str += str;
382
+ if ( createNewLine ) {
383
+ this.str += newLine;
384
+ this.str += this.applyIndent( char );
385
+ }
386
+ else {
387
+ this.str += " " + char;
388
+ }
389
+ }
390
+ else {
391
+ this.str += this.applyIndent( char );
392
+ }
393
+
394
+ this.str += newLine;
395
+ this.indent += 1;
396
+
397
+ }
398
+
399
+ closeBlock( char = '}' ) {
400
+
401
+ this.indent -= 1;
402
+ this.str += this.applyIndent( char ) + newLine;
403
+
404
+ }
405
+
406
+ beginArray( str ) {
407
+
408
+ str = this.applyIndent( str + ' = [' );
409
+ this.str += str;
410
+ this.str += newLine;
411
+ this.indent += 1;
412
+
413
+ }
414
+
415
+ closeArray() {
416
+
417
+ this.indent -= 1;
418
+ this.str += this.applyIndent( ']' ) + newLine;
419
+
420
+ }
421
+
422
+ appendLine( str = '' ) {
423
+
424
+ str = this.applyIndent( str );
425
+ this.str += str;
426
+ this.str += newLine;
427
+
428
+ }
429
+
430
+ toString() {
431
+
432
+ return this.str;
433
+
434
+ }
435
+
436
+ applyIndent( str ) {
437
+
438
+ let indents = '';
439
+ for ( let i = 0; i < this.indent; i ++ ) indents += '\t';
440
+ return indents + str;
441
+
442
+ }
443
+
444
+ }
445
+
446
+ declare type TextureMap = {[name: string]: {texture: Texture, scale?: Vector4}};
447
+
448
+ class USDZExporterContext {
449
+ root: any;
450
+ exporter: any;
451
+ extensions: any;
452
+ quickLookCompatible: boolean;
453
+ materials: Map<string, Material>;
454
+ textures: TextureMap;
455
+ files: { [path: string]: Uint8Array | [Uint8Array, fflate.ZipOptions] | null | any }
456
+ document: USDDocument;
457
+ output: string;
458
+ animations: AnimationClip[];
459
+
460
+ constructor( root, exporter: USDZExporter, extensions, quickLookCompatible ) {
461
+
462
+ this.root = root;
463
+ this.exporter = exporter;
464
+ this.quickLookCompatible = quickLookCompatible;
465
+
466
+ if ( extensions )
467
+ this.extensions = extensions;
468
+
469
+ this.materials = new Map();
470
+ this.textures = {};
471
+ this.files = {};
472
+ this.document = new USDDocument();
473
+ this.output = '';
474
+ this.animations = [];
475
+
476
+ }
477
+
478
+ }
479
+
480
+ /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_anchoring_type) */
481
+ export type Anchoring = "plane" | "image" | "face" | "none"
482
+ /**[documentation](https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/preliminary_anchoringapi/preliminary_planeanchoring_alignment) */
483
+ export type Alignment = "horizontal" | "vertical" | "any";
484
+
485
+ class USDZExporterOptions {
486
+ ar: {
487
+ anchoring: { type: Anchoring },
488
+ planeAnchoring: { alignment: Alignment },
489
+ } = {
490
+ anchoring: { type: 'plane' },
491
+ planeAnchoring: { alignment: 'horizontal' }
492
+ };
493
+ quickLookCompatible: boolean = false;
494
+ extensions: any[] = [];
495
+ maxTextureSize: number = 4096;
496
+ }
497
+
498
+ class USDZExporter {
499
+ debug: boolean;
500
+ sceneAnchoringOptions: {} = {};
501
+ extensions: any;
502
+
503
+ constructor() {
504
+
505
+ this.debug = false;
506
+
507
+ }
508
+
509
+ getEndTimeCode( animations ) {
510
+ let endTimeCode = 0;
511
+
512
+ for( const animation of animations ) {
513
+ const currentEndTimeCode = animation.duration * 60;
514
+
515
+ if ( endTimeCode < currentEndTimeCode ) {
516
+ endTimeCode = currentEndTimeCode;
517
+ }
518
+ }
519
+
520
+ return endTimeCode;
521
+ }
522
+
523
+ async parse( scene, options: USDZExporterOptions = new USDZExporterOptions() ) {
524
+
525
+ options = Object.assign( new USDZExporterOptions(), options );
526
+
527
+ this.sceneAnchoringOptions = options;
528
+ const context = new USDZExporterContext( scene, this, options.extensions, options.quickLookCompatible );
529
+ this.extensions = context.extensions;
530
+
531
+ const files = context.files;
532
+ const modelFileName = 'model.usda';
533
+
534
+ // model file should be first in USDZ archive so we init it here
535
+ files[ modelFileName ] = null;
536
+
537
+ const materials = context.materials;
538
+ const textures = context.textures;
539
+
540
+ await invokeAll( context, 'onBeforeBuildDocument' );
541
+
542
+ // HACK let's find all skeletons and reparent them to their skelroot / armature / uppermost bone parent
543
+ const reparentings: Array<any> = [];
544
+ scene.traverseVisible(object => {
545
+
546
+ if (object.isSkinnedMesh) {
547
+ const bones = object.skeleton.bones as Bone[];
548
+
549
+ const commonAncestor = findCommonAncestor(bones);
550
+ if (commonAncestor) {
551
+ reparentings.push( { object, originalParent: object.parent, newParent: commonAncestor } );
552
+ }
553
+ }
554
+ });
555
+
556
+ for ( const reparenting of reparentings ) {
557
+ const { object, originalParent, newParent } = reparenting;
558
+ if (this.debug) console.log("REPARENTING", object, "from", originalParent, "to", newParent);
559
+ newParent.add( object );
560
+ }
561
+
562
+ traverseVisible( scene, context.document, context );
563
+
564
+ await invokeAll( context, 'onAfterBuildDocument' );
565
+
566
+ parseDocument( context, () => {
567
+ // injected after stageRoot.
568
+ // TODO property use context/writer instead of string concat
569
+ return buildMaterials( materials, textures, options.quickLookCompatible );
570
+ } );
571
+
572
+ await invokeAll( context, 'onAfterSerialize' );
573
+
574
+ // repair the parenting again
575
+ for ( const reparenting of reparentings ) {
576
+ const { object, originalParent, newParent } = reparenting;
577
+ originalParent.add( object );
578
+ }
579
+
580
+ // Moved into parseDocument callback for proper defaultPrim encapsulation
581
+ // context.output += buildMaterials( materials, textures, options.quickLookCompatible );
582
+
583
+ const endTimeCode = this.getEndTimeCode( context.animations );
584
+
585
+ const header = context.document.buildHeader( endTimeCode );
586
+ const final = header + '\n' + context.output;
587
+
588
+ // full output file
589
+ if ( this.debug )
590
+ console.log( final );
591
+
592
+ files[ modelFileName ] = fflate.strToU8( final );
593
+ context.output = '';
594
+
595
+ const decompressionRenderer = new WebGLRenderer( { antialias: false, alpha: true } );
596
+
597
+ for ( const id in textures ) {
598
+
599
+ const textureData = textures[ id ];
600
+ let texture = textureData.texture;
601
+
602
+ const isRGBA = formatsWithAlphaChannel.includes( texture.format );
603
+
604
+ //@ts-ignore
605
+ if ( texture.isCompressedTexture || texture.isRenderTargetTexture ) {
606
+
607
+ texture = decompressGpuTexture( texture, options.maxTextureSize, decompressionRenderer );
608
+
609
+ }
610
+
611
+ const canvas = await imageToCanvas( texture.image, textureData.scale, false, options.maxTextureSize ).catch( err => {
612
+ console.error("Error converting texture to canvas", texture, err);
613
+ });
614
+
615
+ if ( canvas ) {
616
+
617
+ const blob = await canvas.convertToBlob( {type: isRGBA ? 'image/png' : 'image/jpeg', quality: 0.95 } );
618
+ files[ `textures/Texture_${id}.${isRGBA ? 'png' : 'jpg'}` ] = new Uint8Array( await blob.arrayBuffer() );
619
+
620
+ } else {
621
+
622
+ console.warn( 'Can`t export texture: ', texture );
623
+
624
+ }
625
+
626
+ }
627
+
628
+ decompressionRenderer.dispose();
629
+
630
+ // 64 byte alignment
631
+ // https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109
632
+
633
+ let offset = 0;
634
+
635
+ for ( const filename in files ) {
636
+
637
+ const file = files[ filename ];
638
+ const headerSize = 34 + filename.length;
639
+
640
+ offset += headerSize;
641
+
642
+ const offsetMod64 = offset & 63;
643
+
644
+ if ( offsetMod64 !== 4 ) {
645
+
646
+ const padLength = 64 - offsetMod64;
647
+ const padding = new Uint8Array( padLength );
648
+
649
+ files[ filename ] = [ file, { extra: { 12345: padding } } ];
650
+
651
+ }
652
+
653
+ offset = file.length;
654
+
655
+ }
656
+
657
+ return fflate.zipSync( files, { level: 0 } );
658
+
659
+ }
660
+
661
+ }
662
+
663
+ function traverseVisible( object: Object3D, parentModel: USDObject, context: USDZExporterContext ) {
664
+
665
+ if ( ! object.visible ) return;
666
+
667
+ let model: USDObject | undefined = undefined;
668
+ let geometry: BufferGeometry | undefined = undefined;
669
+ let material: Material | Material[] | undefined = undefined;
670
+
671
+ if (object instanceof Mesh || object instanceof SkinnedMesh) {
672
+ geometry = object.geometry;
673
+ material = object.material;
674
+ }
675
+
676
+ // TODO what should be do with disabled renderers?
677
+ // Here we just assume they're off, and don't export them
678
+ const renderer = GameObject.getComponent( object, Renderer )
679
+ if (renderer && !renderer.enabled) {
680
+ geometry = undefined;
681
+ material = undefined;
682
+ }
683
+
684
+ if ( (object instanceof Mesh || object instanceof SkinnedMesh) && material && (material instanceof MeshStandardMaterial || material instanceof MeshBasicMaterial)) {
685
+
686
+ const name = getObjectId( object );
687
+ const skinnedMeshObject = object instanceof SkinnedMesh ? object : null;
688
+ model = new USDObject( object.uuid, name, object.matrix, geometry, material, undefined, skinnedMeshObject, object.animations );
689
+
690
+ } else if ( object instanceof PerspectiveCamera || object instanceof OrthographicCamera ) {
691
+
692
+ const name = getObjectId( object );
693
+ model = new USDObject( object.uuid, name, object.matrix, undefined, undefined, object );
694
+
695
+ } else {
696
+
697
+ const name = getObjectId( object );
698
+ model = new USDObject( object.uuid, name, object.matrix, undefined, undefined, undefined, undefined, object.animations );
699
+
700
+ }
701
+
702
+ if ( model ) {
703
+
704
+ if ( parentModel ) {
705
+
706
+ parentModel.add( model );
707
+
708
+ }
709
+
710
+ parentModel = model;
711
+
712
+ if ( context.extensions ) {
713
+
714
+ for ( const ext of context.extensions ) {
715
+
716
+ if ( ext.onExportObject ) ext.onExportObject.call( ext, object, model, context );
717
+
718
+ }
719
+
720
+ }
721
+
722
+ } else {
723
+
724
+ const name = getObjectId( object );
725
+ const empty = new USDObject( object.uuid, name, object.matrix );
726
+ if ( parentModel ) {
727
+
728
+ parentModel.add( empty );
729
+
730
+ }
731
+
732
+ parentModel = empty;
733
+
734
+ }
735
+
736
+ for ( const ch of object.children ) {
737
+
738
+ traverseVisible( ch, parentModel, context );
739
+
740
+ }
741
+
742
+ }
743
+
744
+ async function parseDocument( context: USDZExporterContext, afterStageRoot: () => string ) {
745
+
746
+ for ( const child of context.document.children ) {
747
+
748
+ addResources( child, context );
749
+
750
+ }
751
+
752
+ const writer = new USDWriter();
753
+
754
+ writer.beginBlock( `def Xform "${context.document.name}"` );
755
+
756
+ writer.beginBlock( `def Scope "Scenes" (
757
+ kind = "sceneLibrary"
758
+ )` );
759
+
760
+ writer.beginBlock( `def Xform "Scene" (
761
+ apiSchemas = ["Preliminary_AnchoringAPI"]
762
+ customData = {
763
+ bool preliminary_collidesWithEnvironment = 0
764
+ string sceneName = "Scene"
765
+ }
766
+ sceneName = "Scene"
767
+ )` );
768
+
769
+ writer.appendLine( `token preliminary:anchoring:type = "${context.exporter.sceneAnchoringOptions.ar.anchoring.type}"` );
770
+ if (context.exporter.sceneAnchoringOptions.ar.anchoring.type === 'plane')
771
+ writer.appendLine( `token preliminary:planeAnchoring:alignment = "${context.exporter.sceneAnchoringOptions.ar.planeAnchoring.alignment}"` );
772
+ // 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.
773
+ if (context.exporter.sceneAnchoringOptions.ar.anchoring.type === 'image')
774
+ writer.appendLine( `rel preliminary:imageAnchoring:referenceImage = </${context.document.name}/Scenes/Scene/AnchoringReferenceImage>` );
775
+ writer.appendLine();
776
+
777
+ for ( const child of context.document.children ) {
778
+
779
+ buildXform( child, writer, context );
780
+
781
+ }
782
+
783
+ invokeAll( context, 'onAfterHierarchy', writer );
784
+
785
+ writer.closeBlock();
786
+ writer.closeBlock();
787
+ writer.appendLine(afterStageRoot());
788
+ writer.closeBlock();
789
+
790
+ context.output += writer.toString();
791
+
792
+ }
793
+
794
+ function addResources( object: USDObject | null, context: USDZExporterContext ) {
795
+
796
+ if ( object == null ) {
797
+ return;
798
+ }
799
+ const geometry = object.geometry;
800
+ const material = object.material;
801
+
802
+ if ( geometry ) {
803
+
804
+ if ( material && ( 'isMeshStandardMaterial' in material && material.isMeshStandardMaterial || 'isMeshBasicMaterial' in material && material.isMeshBasicMaterial ) ) { // TODO convert unlit to lit+emissive
805
+
806
+ const geometryFileName = 'geometries/Geometry_' + geometry.id + '.usd';
807
+
808
+ if ( ! ( geometryFileName in context.files ) ) {
809
+
810
+ const meshObject = buildMeshObject( geometry, object.skinnedMesh?.skeleton?.bones );
811
+ context.files[ geometryFileName ] = buildUSDFileAsString( meshObject, context );
812
+
813
+ }
814
+
815
+ } else {
816
+
817
+ console.warn( 'THREE.USDZExporter: Unsupported material type (USDZ only supports MeshStandardMaterial)', name );
818
+
819
+ }
820
+
821
+ }
822
+
823
+ if( material ) {
824
+
825
+ if ( ! ( material.uuid in context.materials ) ) {
826
+
827
+ context.materials[ material.uuid ] = material;
828
+
829
+ }
830
+ }
831
+
832
+ for ( const ch of object.children ) {
833
+
834
+ addResources( ch, context );
835
+
836
+ }
837
+
838
+ }
839
+
840
+ async function invokeAll( context: USDZExporterContext, name: string, writer: USDWriter | null = null ) {
841
+
842
+ if ( context.extensions ) {
843
+
844
+ for ( const ext of context.extensions ) {
845
+
846
+ if ( !ext ) continue;
847
+
848
+ if ( typeof ext[ name ] === 'function' ) {
849
+
850
+ const method = ext[ name ];
851
+ const res = method.call( ext, context, writer );
852
+ if(res instanceof Promise) {
853
+ await res;
854
+ }
855
+ }
856
+
857
+ }
858
+
859
+ }
860
+
861
+ }
862
+ let _renderer: WebGLRenderer | null = null;
863
+ let fullscreenQuadGeometry: PlaneGeometry | null;
864
+ let fullscreenQuadMaterial: ShaderMaterial | null;
865
+ let fullscreenQuad: Mesh | null;
866
+
867
+ function decompressGpuTexture( texture, maxTextureSize = Infinity, renderer: WebGLRenderer | null = null ) {
868
+
869
+ if ( ! fullscreenQuadGeometry ) fullscreenQuadGeometry = new PlaneGeometry( 2, 2, 1, 1 );
870
+ if ( ! fullscreenQuadMaterial ) fullscreenQuadMaterial = new ShaderMaterial( {
871
+ uniforms: { blitTexture: new Uniform( texture ) },
872
+ vertexShader: `
873
+ varying vec2 vUv;
874
+ void main(){
875
+ vUv = uv;
876
+ vUv.y = 1. - vUv.y;
877
+ gl_Position = vec4(position.xy * 1.0,0.,.999999);
878
+ }`,
879
+ fragmentShader: `
880
+ uniform sampler2D blitTexture;
881
+ varying vec2 vUv;
882
+
883
+ void main(){
884
+ gl_FragColor = vec4(vUv.xy, 0, 1);
885
+
886
+ #ifdef IS_SRGB
887
+ gl_FragColor = LinearTosRGB( texture2D( blitTexture, vUv) );
888
+ #else
889
+ gl_FragColor = texture2D( blitTexture, vUv);
890
+ #endif
891
+ gl_FragColor.rgb *= gl_FragColor.a;
892
+ }`
893
+ } );
894
+
895
+ fullscreenQuadMaterial.uniforms.blitTexture.value = texture;
896
+ fullscreenQuadMaterial.defines.IS_SRGB = texture.colorSpace == SRGBColorSpace;
897
+ fullscreenQuadMaterial.needsUpdate = true;
898
+
899
+ if ( ! fullscreenQuad ) {
900
+
901
+ fullscreenQuad = new Mesh( fullscreenQuadGeometry, fullscreenQuadMaterial );
902
+ fullscreenQuad.frustumCulled = false;
903
+
904
+ }
905
+
906
+ const _camera = new PerspectiveCamera();
907
+ const _scene = new Scene();
908
+ _scene.add( fullscreenQuad );
909
+
910
+ if ( ! renderer ) {
911
+
912
+ renderer = _renderer = new WebGLRenderer( { antialias: false, alpha: true } );
913
+
914
+ }
915
+
916
+ renderer.setSize( Math.min( texture.image.width, maxTextureSize ), Math.min( texture.image.height, maxTextureSize ) );
917
+ renderer.clear();
918
+ renderer.render( _scene, _camera );
919
+
920
+ const readableTexture = new Texture( renderer.domElement );
921
+
922
+ readableTexture.minFilter = texture.minFilter;
923
+ readableTexture.magFilter = texture.magFilter;
924
+ readableTexture.wrapS = texture.wrapS;
925
+ readableTexture.wrapT = texture.wrapT;
926
+ readableTexture.name = texture.name;
927
+
928
+ if ( _renderer ) {
929
+
930
+ _renderer.dispose();
931
+ _renderer = null;
932
+
933
+ }
934
+
935
+ return readableTexture;
936
+
937
+ }
938
+
939
+
940
+ function isImageBitmap( image ) {
941
+
942
+ return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
943
+ ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
944
+ ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
945
+ ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap );
946
+
947
+ }
948
+
949
+ async function imageToCanvas( image, color: Vector4 | undefined = undefined, flipY = false, maxTextureSize = 4096 ) {
950
+
951
+ if ( isImageBitmap( image ) ) {
952
+
953
+ // max. canvas size on Safari is still 4096x4096
954
+ const scale = maxTextureSize / Math.max( image.width, image.height );
955
+
956
+ const canvas = new OffscreenCanvas( image.width * Math.min( 1, scale ), image.height * Math.min( 1, scale ) );
957
+
958
+ const context = canvas.getContext( '2d' ) as OffscreenCanvasRenderingContext2D;
959
+ if (!context) throw new Error('Could not get canvas 2D context');
960
+
961
+ if ( flipY === true ) {
962
+
963
+ context.translate( 0, canvas.height );
964
+ context.scale( 1, - 1 );
965
+
966
+ }
967
+
968
+ context.drawImage( image, 0, 0, canvas.width, canvas.height );
969
+
970
+ // Currently only used to apply opacity scale since QuickLook and usdview don't support that yet
971
+ if ( color !== undefined ) {
972
+
973
+ const r = color.x;
974
+ const g = color.y;
975
+ const b = color.z;
976
+ const a = color.w;
977
+
978
+ const imagedata = context.getImageData( 0, 0, canvas.width, canvas.height );
979
+ const data = imagedata.data;
980
+
981
+ for ( let i = 0; i < data.length; i += 4 ) {
982
+
983
+ data[ i + 0 ] = data[ i + 0 ] * r;
984
+ data[ i + 1 ] = data[ i + 1 ] * g;
985
+ data[ i + 2 ] = data[ i + 2 ] * b;
986
+ data[ i + 3 ] = data[ i + 3 ] * a;
987
+
988
+ }
989
+
990
+ context.putImageData( imagedata, 0, 0 );
991
+
992
+ }
993
+
994
+ return canvas as OffscreenCanvasExt;
995
+
996
+ } else {
997
+
998
+ throw new Error( 'THREE.USDZExporter: No valid image data found. Unable to process texture.' );
999
+
1000
+ }
1001
+
1002
+ }
1003
+
1004
+ //
1005
+
1006
+ const PRECISION = 7;
1007
+
1008
+ function buildHeader() {
1009
+
1010
+ return `#usda 1.0
1011
+ (
1012
+ customLayerData = {
1013
+ string creator = "Needle Engine USDZExporter"
1014
+ }
1015
+ metersPerUnit = 1
1016
+ upAxis = "Y"
1017
+ )
1018
+ `;
1019
+
1020
+ }
1021
+
1022
+ function buildUSDFileAsString( dataToInsert, _context: USDZExporterContext ) {
1023
+
1024
+ let output = buildHeader();
1025
+ output += dataToInsert;
1026
+ return fflate.strToU8( output );
1027
+
1028
+ }
1029
+
1030
+ function getObjectId( object ) {
1031
+
1032
+ return object.name.replace( /[-<>\(\)\[\]§$%&\/\\\=\?\,\;]/g, '' ) + '_' + object.id;
1033
+
1034
+ }
1035
+
1036
+ function getBoneName(bone) {
1037
+ return makeNameSafe(bone.name || 'bone_' + bone.uuid);
1038
+ }
1039
+
1040
+ function getPathToSkeleton(bone: Object3D, assumedRoot: Object3D) {
1041
+ let path = getBoneName(bone);
1042
+ let current = bone.parent;
1043
+ while ( current && current !== assumedRoot ) {
1044
+ path = getBoneName(current) + '/' + path;
1045
+ current = current.parent;
1046
+ }
1047
+ return path;
1048
+ }
1049
+
1050
+ // Xform
1051
+
1052
+ export function buildXform( model: USDObject | null, writer: USDWriter, context: USDZExporterContext ) {
1053
+
1054
+ if ( model == null)
1055
+ return;
1056
+
1057
+ const matrix = model.matrix;
1058
+ const geometry = model.geometry;
1059
+ const material = model.material;
1060
+ const camera = model.camera;
1061
+ const name = model.name;
1062
+
1063
+ if ( model.animations ) {
1064
+ for ( const animation of model.animations ) {
1065
+ context.animations.push( animation )
1066
+ }
1067
+ }
1068
+
1069
+ const transform = buildMatrix( matrix );
1070
+
1071
+ if ( matrix.determinant() < 0 ) {
1072
+
1073
+ console.warn( 'THREE.USDZExporter: USDZ does not support negative scales', name );
1074
+
1075
+ }
1076
+
1077
+ const isSkinnedMesh = geometry && geometry.isBufferGeometry && geometry.attributes.skinIndex !== undefined && geometry.attributes.skinIndex.count > 0;
1078
+ const objType = isSkinnedMesh ? 'SkelRoot' : 'Xform';
1079
+ const apiSchemas = isSkinnedMesh ? '"MaterialBindingAPI", "SkelBindingAPI"' : '"MaterialBindingAPI"';
1080
+
1081
+ if ( geometry ) {
1082
+ writer.beginBlock( `def ${objType} "${name}"`, "(", false );
1083
+ if (context.quickLookCompatible && material && material.side === DoubleSide)
1084
+ writer.appendLine(`prepend references = @./geometries/Geometry_${geometry.id}.usd@</Geometry_doubleSided>`);
1085
+ else
1086
+ writer.appendLine(`prepend references = @./geometries/Geometry_${geometry.id}.usd@</Geometry>`);
1087
+ writer.appendLine(`prepend apiSchemas = [${apiSchemas}]`);
1088
+ writer.closeBlock( ")" );
1089
+ writer.beginBlock();
1090
+ }
1091
+ else if ( camera )
1092
+ writer.beginBlock( `def Camera "${name}"` );
1093
+ else
1094
+ writer.beginBlock( `def Xform "${name}"` );
1095
+
1096
+ if ( geometry && material ) {
1097
+ writer.appendLine( `rel material:binding = </StageRoot/Materials/Material_${material.id}>` );
1098
+
1099
+ // Turns out QuickLook / RealityKit doesn't support the doubleSided attribute, so we
1100
+ // work around that by emitting additional indices above, and then we shouldn't emit the attribute either as geometry is
1101
+ // already doubleSided then.
1102
+ if (!context.quickLookCompatible && material.side === DoubleSide ) {
1103
+ // double-sided is a mesh property in USD, we can apply it as `over` here
1104
+ writer.beginBlock( `over "Geometry" `);
1105
+ writer.appendLine( `uniform bool doubleSided = 1` );
1106
+ writer.closeBlock();
1107
+ }
1108
+ }
1109
+ if ( isSkinnedMesh ) {
1110
+ writer.appendLine( `rel skel:skeleton = <Rig>` );
1111
+ writer.appendLine( `rel skel:animationSource = <Rig/_anim>`);
1112
+ writer.appendLine( `matrix4d xformOp:transform = ${buildMatrix(new Matrix4())}` ); // always identity / in world space
1113
+ }
1114
+ else {
1115
+ writer.appendLine( `matrix4d xformOp:transform = ${transform}` );
1116
+ }
1117
+ writer.appendLine( 'uniform token[] xformOpOrder = ["xformOp:transform"]' );
1118
+
1119
+ if ( camera ) {
1120
+
1121
+ if ( 'isOrthographicCamera' in camera && camera.isOrthographicCamera ) {
1122
+
1123
+ writer.appendLine( `float2 clippingRange = (${camera.near}, ${camera.far})` );
1124
+ writer.appendLine( `float horizontalAperture = ${( ( Math.abs( camera.left ) + Math.abs( camera.right ) ) * 10 ).toPrecision( PRECISION )}` );
1125
+ writer.appendLine( `float verticalAperture = ${( ( Math.abs( camera.top ) + Math.abs( camera.bottom ) ) * 10 ).toPrecision( PRECISION )}` );
1126
+ writer.appendLine( 'token projection = "orthographic"' );
1127
+
1128
+ } else if ( 'isPerspectiveCamera' in camera && camera.isPerspectiveCamera) {
1129
+
1130
+ writer.appendLine( `float2 clippingRange = (${camera.near.toPrecision( PRECISION )}, ${camera.far.toPrecision( PRECISION )})` );
1131
+ writer.appendLine( `float focalLength = ${camera.getFocalLength().toPrecision( PRECISION )}` );
1132
+ writer.appendLine( `float focusDistance = ${camera.focus.toPrecision( PRECISION )}` );
1133
+ writer.appendLine( `float horizontalAperture = ${camera.getFilmWidth().toPrecision( PRECISION )}` );
1134
+ writer.appendLine( 'token projection = "perspective"' );
1135
+ writer.appendLine( `float verticalAperture = ${camera.getFilmHeight().toPrecision( PRECISION )}` );
1136
+ }
1137
+
1138
+ }
1139
+
1140
+ if ( model.onSerialize ) {
1141
+
1142
+ model.onSerialize( writer, context );
1143
+
1144
+ }
1145
+
1146
+ if ( model.children ) {
1147
+
1148
+ writer.appendLine();
1149
+ for ( const ch of model.children ) {
1150
+
1151
+ buildXform( ch, writer, context );
1152
+
1153
+ }
1154
+
1155
+ }
1156
+
1157
+ writer.closeBlock();
1158
+
1159
+ }
1160
+
1161
+ function fn( num:number ): string {
1162
+
1163
+ return Number.isInteger(num) ? num.toString() : num.toFixed( 10 );
1164
+
1165
+ }
1166
+
1167
+ function buildMatrix( matrix ) {
1168
+
1169
+ const array = matrix.elements;
1170
+
1171
+ return `( ${buildMatrixRow( array, 0 )}, ${buildMatrixRow( array, 4 )}, ${buildMatrixRow( array, 8 )}, ${buildMatrixRow( array, 12 )} )`;
1172
+
1173
+ }
1174
+
1175
+ function buildMatrixRow( array, offset ) {
1176
+
1177
+ return `(${fn( array[ offset + 0 ] )}, ${fn( array[ offset + 1 ] )}, ${fn( array[ offset + 2 ] )}, ${fn( array[ offset + 3 ] )})`;
1178
+
1179
+ }
1180
+
1181
+ // Mesh
1182
+
1183
+ function buildMeshObject( geometry, bonesArray: Bone[] = [] ) {
1184
+
1185
+ const mesh = buildMesh( geometry, bonesArray );
1186
+ return `
1187
+ def "Geometry"
1188
+ ${mesh}
1189
+ `;
1190
+
1191
+ }
1192
+
1193
+ function buildMesh( geometry, bones: Bone[] = [] ) {
1194
+
1195
+ const name = 'Geometry';
1196
+ const attributes = geometry.attributes;
1197
+ const count = attributes.position.count;
1198
+
1199
+ const hasBones = bones && bones.length > 0;
1200
+
1201
+ // We need to sort bones and all skinning data by path –
1202
+ // Neither glTF nor three.js care, but in USD they must be sorted
1203
+ // since the array defines the virtual hierarchy and is evaluated in that order
1204
+ const sortedBones: Array<{bone: Object3D, index: number}> = [];
1205
+ const indexMapping: number[] = [];
1206
+ let sortedSkinIndex = new Array<number>();
1207
+ let sortedSkinIndexAttribute: BufferAttribute | null = attributes.skinIndex;
1208
+ let bonesArray = "";
1209
+ if (hasBones) {
1210
+
1211
+ for ( const index in bones ) {
1212
+ sortedBones.push( { bone: bones[index], index: parseInt(index) } );
1213
+ }
1214
+
1215
+ // add structural nodes to the list of bones
1216
+ for ( const structuralNode of findStructuralNodesInBoneHierarchy(bones) ) {
1217
+ sortedBones.push( { bone: structuralNode, index: sortedBones.length } );
1218
+ }
1219
+
1220
+ // sort bones by path – need to be sorted in the same order as during mesh export
1221
+ const assumedRoot = bones[0].parent!;
1222
+ sortedBones.sort((a, b) => getPathToSkeleton(a.bone, assumedRoot) > getPathToSkeleton(b.bone, assumedRoot) ? 1 : -1);
1223
+ bonesArray = sortedBones.map( x => "\"" + getPathToSkeleton(x.bone, assumedRoot) + "\"" ).join( ', ' );
1224
+
1225
+ // TODO we can probably skip the expensive attribute re-ordering if the bones were already in a correct order.
1226
+ // That doesn't mean that they are strictly sorted by path – just that all parents strictly need to come first.
1227
+
1228
+ // build index mapping
1229
+ for (const i in sortedBones) {
1230
+ indexMapping[sortedBones[i].index] = parseInt(i);
1231
+ }
1232
+
1233
+ // remap skin index attributes
1234
+ const skinIndex = attributes.skinIndex;
1235
+ sortedSkinIndex = new Array<number>();
1236
+ for ( let i = 0; i < skinIndex.count; i ++ ) {
1237
+
1238
+ const x = skinIndex.getX( i );
1239
+ const y = skinIndex.getY( i );
1240
+ const z = skinIndex.getZ( i );
1241
+ const w = skinIndex.getW( i );
1242
+
1243
+ sortedSkinIndex.push( indexMapping[x], indexMapping[y], indexMapping[z], indexMapping[w] );
1244
+ }
1245
+
1246
+ // turn it back into an attribute so the rest of the code doesn't need to learn a new thing
1247
+ sortedSkinIndexAttribute = new BufferAttribute( new Uint16Array( sortedSkinIndex ), 4 );
1248
+ }
1249
+
1250
+ const isSkinnedMesh = attributes.skinWeight && attributes.skinIndex;
1251
+
1252
+ return `
1253
+ {
1254
+ def Mesh "${name}" ${isSkinnedMesh ? `(
1255
+ prepend apiSchemas = ["SkelBindingAPI"]
1256
+ )` : ''}
1257
+ {
1258
+ int[] faceVertexCounts = [${buildMeshVertexCount( geometry )}]
1259
+ int[] faceVertexIndices = [${buildMeshVertexIndices( geometry )}]
1260
+ normal3f[] normals = [${buildVector3Array( attributes.normal, count )}] (
1261
+ interpolation = "vertex"
1262
+ )
1263
+ point3f[] points = [${buildVector3Array( attributes.position, count )}]
1264
+ ${attributes.uv ?
1265
+ `texCoord2f[] primvars:st = [${buildVector2Array( attributes.uv, count )}] (
1266
+ interpolation = "vertex"
1267
+ )` : '' }
1268
+ ${attributes.uv2 ?
1269
+ `texCoord2f[] primvars:st2 = [${buildVector2Array( attributes.uv2, count )}] (
1270
+ interpolation = "vertex"
1271
+ )` : '' }
1272
+ ${isSkinnedMesh ?
1273
+ `matrix4d primvars:skel:geomBindTransform = ( (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1) ) (
1274
+ elementSize = 1
1275
+ interpolation = "constant"
1276
+ )` : '' }
1277
+ ${attributes.skinIndex ?
1278
+ `int[] primvars:skel:jointIndices = [${buildVector4Array( sortedSkinIndexAttribute, true )}] (
1279
+ elementSize = 4
1280
+ interpolation = "vertex"
1281
+ )` : '' }
1282
+ ${attributes.skinWeight ?
1283
+ `float[] primvars:skel:jointWeights = [${buildVector4Array( attributes.skinWeight )}] (
1284
+ elementSize = 4
1285
+ interpolation = "vertex"
1286
+ )` : '' }
1287
+ ${hasBones ?
1288
+ //`uniform token[] skel:blendShapes
1289
+ `uniform token[] skel:joints = [${bonesArray}]` : '' }
1290
+ uniform token subdivisionScheme = "none"
1291
+ }
1292
+ }
1293
+
1294
+ # This is a workaround for QuickLook/RealityKit not supporting the doubleSided attribute. We're adding a second
1295
+ # geometry definition here, that uses the same mesh data but appends extra faces with reversed winding order.
1296
+ def "${name}_doubleSided" (
1297
+ prepend references = </Geometry>
1298
+ )
1299
+ {
1300
+ over "Geometry"
1301
+ {
1302
+ int[] faceVertexCounts = [${buildMeshVertexCount( geometry ) + ", " + buildMeshVertexCount( geometry )}]
1303
+ int[] faceVertexIndices = [${buildMeshVertexIndices( geometry ) + ", " + buildMeshVertexIndices( geometry, true )}]
1304
+ }
1305
+ }
1306
+ `;
1307
+
1308
+ }
1309
+
1310
+ function buildMeshVertexCount( geometry ) {
1311
+
1312
+ const count = geometry.index !== null ? geometry.index.count : geometry.attributes.position.count;
1313
+
1314
+ return Array( count / 3 ).fill( 3 ).join( ', ' );
1315
+
1316
+ }
1317
+
1318
+ function buildMeshVertexIndices( geometry: BufferGeometry, reverseWinding: boolean = false ) {
1319
+
1320
+ const index = geometry.index;
1321
+ const array: Array<number> = [];
1322
+
1323
+ if ( index !== null ) {
1324
+
1325
+ for ( let i = 0; i < index.count; i ++ ) {
1326
+
1327
+ let val = i;
1328
+ if ( reverseWinding )
1329
+ val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1330
+ array.push( index.getX( val ) );
1331
+
1332
+ }
1333
+
1334
+ } else {
1335
+
1336
+ const length = geometry.attributes.position.count;
1337
+
1338
+ for ( let i = 0; i < length; i ++ ) {
1339
+
1340
+ let val = i;
1341
+ if ( reverseWinding )
1342
+ val = i % 3 === 0 ? i + 2 : i % 3 === 2 ? i - 2 : i;
1343
+ array.push( val );
1344
+
1345
+ }
1346
+
1347
+ }
1348
+
1349
+ return array.join( ', ' );
1350
+
1351
+ }
1352
+
1353
+ function buildVector3Array( attribute, count ) {
1354
+
1355
+ if ( attribute === undefined ) {
1356
+
1357
+ console.warn( 'USDZExporter: Normals missing.' );
1358
+ return Array( count ).fill( '(0, 0, 0)' ).join( ', ' );
1359
+
1360
+ }
1361
+
1362
+ const array: Array<string> = [];
1363
+
1364
+ for ( let i = 0; i < attribute.count; i ++ ) {
1365
+
1366
+ const x = attribute.getX( i );
1367
+ const y = attribute.getY( i );
1368
+ const z = attribute.getZ( i );
1369
+
1370
+ array.push( `(${x.toPrecision( PRECISION )}, ${y.toPrecision( PRECISION )}, ${z.toPrecision( PRECISION )})` );
1371
+
1372
+ }
1373
+
1374
+ return array.join( ', ' );
1375
+
1376
+ }
1377
+
1378
+ function buildVector4Array( attribute, ints = false ) {
1379
+ const array: Array<string> = [];
1380
+
1381
+ for ( let i = 0; i < attribute.count; i ++ ) {
1382
+
1383
+ const x = attribute.getX( i );
1384
+ const y = attribute.getY( i );
1385
+ const z = attribute.getZ( i );
1386
+ const w = attribute.getW( i );
1387
+
1388
+ array.push( `${ints ? x : x.toPrecision( PRECISION )}` );
1389
+ array.push( `${ints ? y : y.toPrecision( PRECISION )}` );
1390
+ array.push( `${ints ? z : z.toPrecision( PRECISION )}` );
1391
+ array.push( `${ints ? w : w.toPrecision( PRECISION )}` );
1392
+
1393
+ }
1394
+
1395
+ return array.join( ', ' );
1396
+
1397
+ }
1398
+
1399
+ function buildVector2Array( attribute, count ) {
1400
+
1401
+ if ( attribute === undefined ) {
1402
+
1403
+ console.warn( 'USDZExporter: UVs missing.' );
1404
+ return Array( count ).fill( '(0, 0)' ).join( ', ' );
1405
+
1406
+ }
1407
+
1408
+ const array: Array<string> = [];
1409
+
1410
+ for ( let i = 0; i < attribute.count; i ++ ) {
1411
+
1412
+ const x = attribute.getX( i );
1413
+ const y = attribute.getY( i );
1414
+
1415
+ array.push( `(${x.toPrecision( PRECISION )}, ${1 - y.toPrecision( PRECISION )})` );
1416
+
1417
+ }
1418
+
1419
+ return array.join( ', ' );
1420
+
1421
+ }
1422
+
1423
+ // Materials
1424
+
1425
+ function buildMaterials( materials: Map<string, Material>, textures: TextureMap, quickLookCompatible = false ) {
1426
+
1427
+ const array: Array<string> = [];
1428
+
1429
+ for ( const uuid in materials ) {
1430
+
1431
+ const material = materials[ uuid ];
1432
+
1433
+ array.push( buildMaterial( material, textures, quickLookCompatible ) );
1434
+
1435
+ }
1436
+
1437
+ return `def "Materials"
1438
+ {
1439
+ ${array.join( '' )}
1440
+ }
1441
+
1442
+ `;
1443
+
1444
+ }
1445
+
1446
+ function buildMaterial( material: MeshBasicMaterial, textures: TextureMap, quickLookCompatible = false ) {
1447
+
1448
+ // https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
1449
+
1450
+ const pad = ' ';
1451
+ const inputs: Array<string> = [];
1452
+ const samplers: Array<string> = [];
1453
+
1454
+ function buildTexture( texture, mapType, color: Color | undefined = undefined, opacity: number | undefined = undefined ) {
1455
+
1456
+ const id = texture.id + ( color ? '_' + color.getHexString() : '' ) + ( opacity !== undefined ? '_' + opacity : '' );
1457
+
1458
+ // Seems neither QuickLook nor usdview support scale/bias on .a values, so we need to bake opacity multipliers into
1459
+ // the texture. This is not ideal.
1460
+ const opacityIsAppliedToTextureAndNotAsScale = quickLookCompatible && opacity !== undefined;
1461
+ const scaleToApply = opacityIsAppliedToTextureAndNotAsScale ? new Vector4(1, 1, 1, opacity) : undefined;
1462
+ textures[ id ] = { texture, scale: scaleToApply };
1463
+
1464
+ const uv = texture.channel > 0 ? 'st' + texture.channel : 'st';
1465
+
1466
+ const isRGBA = formatsWithAlphaChannel.includes( texture.format );
1467
+
1468
+ const WRAPPINGS = {
1469
+ 1000: 'repeat', // RepeatWrapping
1470
+ 1001: 'clamp', // ClampToEdgeWrapping
1471
+ 1002: 'mirror' // MirroredRepeatWrapping
1472
+ };
1473
+
1474
+ const repeat = texture.repeat.clone();
1475
+ const offset = texture.offset.clone();
1476
+ const rotation = texture.rotation;
1477
+
1478
+ // rotation is around the wrong point. after rotation we need to shift offset again so that we're rotating around the right spot
1479
+ const xRotationOffset = Math.sin(rotation);
1480
+ const yRotationOffset = Math.cos(rotation);
1481
+
1482
+ // texture coordinates start in the opposite corner, need to correct
1483
+ offset.y = 1 - offset.y - repeat.y;
1484
+
1485
+ // turns out QuickLook is buggy and interprets texture repeat inverted.
1486
+ // Apple Feedback: FB10036297 and FB11442287
1487
+ if ( quickLookCompatible ) {
1488
+
1489
+ // This is NOT correct yet in QuickLook, but comes close for a range of models.
1490
+ // It becomes more incorrect the bigger the offset is
1491
+
1492
+ offset.x = offset.x / repeat.x;
1493
+ offset.y = offset.y / repeat.y;
1494
+
1495
+ offset.x += xRotationOffset / repeat.x;
1496
+ offset.y += yRotationOffset - 1;
1497
+ }
1498
+
1499
+ else {
1500
+
1501
+ // results match glTF results exactly. verified correct in usdview.
1502
+ offset.x += xRotationOffset * repeat.x;
1503
+ offset.y += (1 - yRotationOffset) * repeat.y;
1504
+
1505
+ }
1506
+
1507
+ const needsTextureTransform = ( repeat.x != 1 || repeat.y != 1 || offset.x != 0 || offset.y != 0 || rotation != 0 );
1508
+ const textureTransformInput = `${materialRoot}/Material_${material.id}/${'uvReader_' + uv}.outputs:result>`; const textureTransformOutput = `${materialRoot}/Material_${material.id}/Transform2d_${mapType}.outputs:result>`;
1509
+ const needsTextureScale = mapType !== 'normal' && (color && (color.r !== 1 || color.g !== 1 || color.b !== 1 || opacity !== 1)) || false;
1510
+
1511
+ const needsNormalScaleAndBias = mapType === 'normal';
1512
+ const normalScale = material instanceof MeshStandardMaterial ? (material.normalScale ? material.normalScale.x * 2 : 2) : 2;
1513
+ const normalScaleValueString = normalScale.toFixed( PRECISION );
1514
+ const normalBiasString = (-1 * (normalScale / 2)).toFixed( PRECISION );
1515
+ const normalBiasZString = (1 - normalScale).toFixed( PRECISION );
1516
+
1517
+ return `
1518
+ ${needsTextureTransform ? `def Shader "Transform2d_${mapType}" (
1519
+ sdrMetadata = {
1520
+ string role = "math"
1521
+ }
1522
+ )
1523
+ {
1524
+ uniform token info:id = "UsdTransform2d"
1525
+ float2 inputs:in.connect = ${textureTransformInput}
1526
+ float2 inputs:scale = ${buildVector2( repeat )}
1527
+ float2 inputs:translation = ${buildVector2( offset )}
1528
+ float inputs:rotation = ${(rotation / Math.PI * 180).toFixed( PRECISION )}
1529
+ float2 outputs:result
1530
+ }
1531
+ ` : '' }
1532
+ def Shader "Texture_${texture.id}_${mapType}"
1533
+ {
1534
+ uniform token info:id = "UsdUVTexture"
1535
+ asset inputs:file = @textures/Texture_${id}.${isRGBA ? 'png' : 'jpg'}@
1536
+ token inputs:sourceColorSpace = "${ texture.colorSpace === 'srgb' ? 'sRGB' : 'raw' }"
1537
+ float2 inputs:st.connect = ${needsTextureTransform ? textureTransformOutput : textureTransformInput}
1538
+ ${needsTextureScale ? `
1539
+ float4 inputs:scale = (${color ? color.r + ', ' + color.g + ', ' + color.b : '1, 1, 1'}, ${(opacity !== undefined && !opacityIsAppliedToTextureAndNotAsScale) ? opacity : '1'})
1540
+ ` : `` }
1541
+ ${needsNormalScaleAndBias ? `
1542
+ float4 inputs:scale = (${normalScaleValueString}, ${normalScaleValueString}, ${normalScaleValueString}, 1)
1543
+ float4 inputs:bias = (${normalBiasString}, ${normalBiasString}, ${normalBiasZString}, 0)
1544
+ ` : `` }
1545
+ token inputs:wrapS = "${ WRAPPINGS[ texture.wrapS ] }"
1546
+ token inputs:wrapT = "${ WRAPPINGS[ texture.wrapT ] }"
1547
+ float outputs:r
1548
+ float outputs:g
1549
+ float outputs:b
1550
+ float3 outputs:rgb
1551
+ ${material.transparent || material.alphaTest > 0.0 ? 'float outputs:a' : ''}
1552
+ }`;
1553
+
1554
+ }
1555
+
1556
+ let effectiveOpacity = ( material.transparent || material.alphaTest ) ? material.opacity : 1;
1557
+
1558
+ if ( material instanceof MeshPhysicalMaterial && material.transmission !== undefined) {
1559
+
1560
+ // TODO does not help when a roughnessMap is used
1561
+ effectiveOpacity *= (1 - material.transmission * (1 - (material.roughness * 0.5)));
1562
+
1563
+ }
1564
+
1565
+ if ( material.map !== null ) {
1566
+
1567
+ inputs.push( `${pad}color3f inputs:diffuseColor.connect = ${materialRoot}/Material_${material.id}/Texture_${material.map.id}_diffuse.outputs:rgb>` );
1568
+
1569
+ if ( material.transparent ) {
1570
+
1571
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.map.id}_diffuse.outputs:a>` );
1572
+
1573
+ } else if ( material.alphaTest > 0.0 ) {
1574
+
1575
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.map.id}_diffuse.outputs:a>` );
1576
+ inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
1577
+
1578
+ }
1579
+
1580
+ samplers.push( buildTexture( material.map, 'diffuse', material.color, effectiveOpacity ) );
1581
+
1582
+ } else {
1583
+
1584
+ inputs.push( `${pad}color3f inputs:diffuseColor = ${buildColor( material.color )}` );
1585
+
1586
+ }
1587
+
1588
+ if ( material.aoMap ) {
1589
+
1590
+ inputs.push( `${pad}float inputs:occlusion.connect = ${materialRoot}/Material_${material.id}/Texture_${material.aoMap.id}_occlusion.outputs:r>` );
1591
+
1592
+ samplers.push( buildTexture( material.aoMap, 'occlusion' ) );
1593
+
1594
+ }
1595
+
1596
+ if ( material.alphaMap ) {
1597
+
1598
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.alphaMap.id}_opacity.outputs:r>` );
1599
+ inputs.push( `${pad}float inputs:opacityThreshold = 0.0001` );
1600
+
1601
+ samplers.push( buildTexture( material.alphaMap, 'opacity', new Color( 1, 1, 1 ), effectiveOpacity ) );
1602
+
1603
+ } else {
1604
+
1605
+ inputs.push( `${pad}float inputs:opacity = ${effectiveOpacity}` );
1606
+
1607
+ if ( material.alphaTest > 0.0 ) {
1608
+
1609
+ inputs.push( `${pad}float inputs:opacityThreshold = ${material.alphaTest}` );
1610
+
1611
+ }
1612
+
1613
+ }
1614
+
1615
+ if ( material instanceof MeshStandardMaterial ) {
1616
+
1617
+ if ( material.emissiveMap ) {
1618
+
1619
+ inputs.push( `${pad}color3f inputs:emissiveColor.connect = ${materialRoot}/Material_${material.id}/Texture_${material.emissiveMap.id}_emissive.outputs:rgb>` );
1620
+ const color = material.emissive.clone();
1621
+ color.multiplyScalar( material.emissiveIntensity );
1622
+ samplers.push( buildTexture( material.emissiveMap, 'emissive', color ) );
1623
+
1624
+ } else if ( material.emissive?.getHex() > 0 ) {
1625
+
1626
+ const color = material.emissive.clone();
1627
+ color.multiplyScalar( material.emissiveIntensity );
1628
+ inputs.push( `${pad}color3f inputs:emissiveColor = ${buildColor( color )}` );
1629
+
1630
+ } else {
1631
+
1632
+ inputs.push( `${pad}color3f inputs:emissiveColor = (0, 0, 0)` );
1633
+
1634
+ }
1635
+
1636
+ if ( material.normalMap ) {
1637
+
1638
+ inputs.push( `${pad}normal3f inputs:normal.connect = ${materialRoot}/Material_${material.id}/Texture_${material.normalMap.id}_normal.outputs:rgb>` );
1639
+
1640
+ samplers.push( buildTexture( material.normalMap, 'normal' ) );
1641
+
1642
+ }
1643
+
1644
+ if ( material.roughnessMap && material.roughness === 1 ) {
1645
+
1646
+ inputs.push( `${pad}float inputs:roughness.connect = ${materialRoot}/Material_${material.id}/Texture_${material.roughnessMap.id}_roughness.outputs:g>` );
1647
+
1648
+ samplers.push( buildTexture( material.roughnessMap, 'roughness' ) );
1649
+
1650
+ } else {
1651
+
1652
+ inputs.push( `${pad}float inputs:roughness = ${material.roughness !== undefined ? material.roughness : 1 }` );
1653
+
1654
+ }
1655
+
1656
+ if ( material.metalnessMap && material.metalness === 1 ) {
1657
+
1658
+ inputs.push( `${pad}float inputs:metallic.connect = ${materialRoot}/Material_${material.id}/Texture_${material.metalnessMap.id}_metallic.outputs:b>` );
1659
+
1660
+ samplers.push( buildTexture( material.metalnessMap, 'metallic' ) );
1661
+
1662
+ } else {
1663
+
1664
+ inputs.push( `${pad}float inputs:metallic = ${material.metalness !== undefined ? material.metalness : 0 }` );
1665
+
1666
+ }
1667
+
1668
+ }
1669
+
1670
+ if ( material instanceof MeshPhysicalMaterial ) {
1671
+
1672
+ inputs.push( `${pad}float inputs:clearcoat = ${material.clearcoat}` );
1673
+ inputs.push( `${pad}float inputs:clearcoatRoughness = ${material.clearcoatRoughness}` );
1674
+ inputs.push( `${pad}float inputs:ior = ${material.ior}` );
1675
+
1676
+ if ( !material.transparent && ! (material.alphaTest > 0.0) && material.transmissionMap) {
1677
+
1678
+ inputs.push( `${pad}float inputs:opacity.connect = ${materialRoot}/Material_${material.id}/Texture_${material.transmissionMap.id}_transmission.outputs:r>` );
1679
+
1680
+ samplers.push( buildTexture( material.transmissionMap, 'transmission' ) );
1681
+ }
1682
+
1683
+ }
1684
+
1685
+ return `
1686
+ def Material "Material_${material.id}"
1687
+ {
1688
+ def Shader "PreviewSurface"
1689
+ {
1690
+ uniform token info:id = "UsdPreviewSurface"
1691
+ ${inputs.join( '\n' )}
1692
+ int inputs:useSpecularWorkflow = 0
1693
+ token outputs:surface
1694
+ }
1695
+
1696
+ token outputs:surface.connect = ${materialRoot}/Material_${material.id}/PreviewSurface.outputs:surface>
1697
+
1698
+ def Shader "uvReader_st"
1699
+ {
1700
+ uniform token info:id = "UsdPrimvarReader_float2"
1701
+ token inputs:varname = "st"
1702
+ float2 inputs:fallback = (0.0, 0.0)
1703
+ float2 outputs:result
1704
+ }
1705
+
1706
+ def Shader "uvReader_st2"
1707
+ {
1708
+ uniform token info:id = "UsdPrimvarReader_float2"
1709
+ token inputs:varname = "st2"
1710
+ float2 inputs:fallback = (0.0, 0.0)
1711
+ float2 outputs:result
1712
+ }
1713
+
1714
+ ${samplers.join( '\n' )}
1715
+
1716
+ }
1717
+ `;
1718
+
1719
+ }
1720
+
1721
+ function buildColor( color ) {
1722
+
1723
+ return `(${color.r}, ${color.g}, ${color.b})`;
1724
+
1725
+ }
1726
+
1727
+ function buildVector2( vector ) {
1728
+
1729
+ return `(${ vector.x }, ${ vector.y })`;
1730
+
1731
+ }
1732
+
1733
+ const formatsWithAlphaChannel = [
1734
+ // uncompressed formats with alpha channel
1735
+ 1023, // RGBAFormat
1736
+ // compressed formats with alpha channel
1737
+ 33777, // RGBA_S3TC_DXT1_Format
1738
+ 33778, // RGBA_S3TC_DXT3_Format
1739
+ 33779, // RGBA_S3TC_DXT5_Format
1740
+ 35842, // RGBA_PVRTC_4BPPV1_Format
1741
+ 35843, // RGBA_PVRTC_2BPPV1_Format
1742
+ 37496, // RGBA_ETC2_EAC_Format
1743
+ 37808, // RGBA_ASTC_4x4_Format
1744
+ 37809, // RGBA_ASTC_5x4_Format
1745
+ 37810, // RGBA_ASTC_5x5_Format
1746
+ 37811, // RGBA_ASTC_6x5_Format
1747
+ 37812, // RGBA_ASTC_6x6_Format
1748
+ 37813, // RGBA_ASTC_8x5_Format
1749
+ 37814, // RGBA_ASTC_8x6_Format
1750
+ 37815, // RGBA_ASTC_8x8_Format
1751
+ 37816, // RGBA_ASTC_10x5_Format
1752
+ 37817, // RGBA_ASTC_10x6_Format
1753
+ 37818, // RGBA_ASTC_10x8_Format
1754
+ 37819, // RGBA_ASTC_10x10_Format
1755
+ 37820, // RGBA_ASTC_12x10_Format
1756
+ 37821, // RGBA_ASTC_12x12_Format
1757
+ 36492, // RGBA_BPTC_Format
1758
+ ];
1759
+
1760
+ export {
1761
+ USDZExporter,
1762
+ USDZExporterContext,
1763
+ USDWriter,
1764
+ USDObject,
1765
+ buildMatrix,
1766
+ getBoneName,
1767
+ getPathToSkeleton,
1768
+ fn as usdNumberFormatting,
1769
+ USDDocument,
1770
+ makeNameSafe as makeNameSafeForUSD,
1771
+ imageToCanvas,
1772
+ decompressGpuTexture,
1773
+ findStructuralNodesInBoneHierarchy,
1774
1774
  };