@onerjs/loaders 8.23.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 (280) hide show
  1. package/OBJ/index.d.ts +4 -0
  2. package/OBJ/index.js +5 -0
  3. package/OBJ/index.js.map +1 -0
  4. package/OBJ/mtlFileLoader.d.ts +41 -0
  5. package/OBJ/mtlFileLoader.js +231 -0
  6. package/OBJ/mtlFileLoader.js.map +1 -0
  7. package/OBJ/objFileLoader.d.ts +136 -0
  8. package/OBJ/objFileLoader.js +318 -0
  9. package/OBJ/objFileLoader.js.map +1 -0
  10. package/OBJ/objFileLoader.metadata.d.ts +4 -0
  11. package/OBJ/objFileLoader.metadata.js +5 -0
  12. package/OBJ/objFileLoader.metadata.js.map +1 -0
  13. package/OBJ/objLoadingOptions.d.ts +47 -0
  14. package/OBJ/objLoadingOptions.js +2 -0
  15. package/OBJ/objLoadingOptions.js.map +1 -0
  16. package/OBJ/solidParser.d.ts +174 -0
  17. package/OBJ/solidParser.js +862 -0
  18. package/OBJ/solidParser.js.map +1 -0
  19. package/SPLAT/index.d.ts +2 -0
  20. package/SPLAT/index.js +4 -0
  21. package/SPLAT/index.js.map +1 -0
  22. package/SPLAT/splatFileLoader.d.ts +88 -0
  23. package/SPLAT/splatFileLoader.js +562 -0
  24. package/SPLAT/splatFileLoader.js.map +1 -0
  25. package/SPLAT/splatFileLoader.metadata.d.ts +14 -0
  26. package/SPLAT/splatFileLoader.metadata.js +12 -0
  27. package/SPLAT/splatFileLoader.metadata.js.map +1 -0
  28. package/SPLAT/splatLoadingOptions.d.ts +13 -0
  29. package/SPLAT/splatLoadingOptions.js +2 -0
  30. package/SPLAT/splatLoadingOptions.js.map +1 -0
  31. package/STL/index.d.ts +1 -0
  32. package/STL/index.js +2 -0
  33. package/STL/index.js.map +1 -0
  34. package/STL/stlFileLoader.d.ts +78 -0
  35. package/STL/stlFileLoader.js +239 -0
  36. package/STL/stlFileLoader.js.map +1 -0
  37. package/STL/stlFileLoader.metadata.d.ts +8 -0
  38. package/STL/stlFileLoader.metadata.js +8 -0
  39. package/STL/stlFileLoader.metadata.js.map +1 -0
  40. package/bvh/bvhFileLoader.d.ts +69 -0
  41. package/bvh/bvhFileLoader.js +133 -0
  42. package/bvh/bvhFileLoader.js.map +1 -0
  43. package/bvh/bvhFileLoader.metadata.d.ts +8 -0
  44. package/bvh/bvhFileLoader.metadata.js +8 -0
  45. package/bvh/bvhFileLoader.metadata.js.map +1 -0
  46. package/bvh/bvhLoader.d.ts +14 -0
  47. package/bvh/bvhLoader.js +329 -0
  48. package/bvh/bvhLoader.js.map +1 -0
  49. package/bvh/bvhLoadingOptions.d.ts +9 -0
  50. package/bvh/bvhLoadingOptions.js +2 -0
  51. package/bvh/bvhLoadingOptions.js.map +1 -0
  52. package/bvh/index.d.ts +2 -0
  53. package/bvh/index.js +4 -0
  54. package/bvh/index.js.map +1 -0
  55. package/bvh/license.md +21 -0
  56. package/dynamic.d.ts +5 -0
  57. package/dynamic.js +57 -0
  58. package/dynamic.js.map +1 -0
  59. package/glTF/1.0/glTFBinaryExtension.d.ts +16 -0
  60. package/glTF/1.0/glTFBinaryExtension.js +65 -0
  61. package/glTF/1.0/glTFBinaryExtension.js.map +1 -0
  62. package/glTF/1.0/glTFLoader.d.ts +144 -0
  63. package/glTF/1.0/glTFLoader.js +1841 -0
  64. package/glTF/1.0/glTFLoader.js.map +1 -0
  65. package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -0
  66. package/glTF/1.0/glTFLoaderInterfaces.js +96 -0
  67. package/glTF/1.0/glTFLoaderInterfaces.js.map +1 -0
  68. package/glTF/1.0/glTFLoaderUtils.d.ts +71 -0
  69. package/glTF/1.0/glTFLoaderUtils.js +255 -0
  70. package/glTF/1.0/glTFLoaderUtils.js.map +1 -0
  71. package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +13 -0
  72. package/glTF/1.0/glTFMaterialsCommonExtension.js +131 -0
  73. package/glTF/1.0/glTFMaterialsCommonExtension.js.map +1 -0
  74. package/glTF/1.0/index.d.ts +5 -0
  75. package/glTF/1.0/index.js +6 -0
  76. package/glTF/1.0/index.js.map +1 -0
  77. package/glTF/2.0/Extensions/EXT_lights_ies.d.ts +41 -0
  78. package/glTF/2.0/Extensions/EXT_lights_ies.js +84 -0
  79. package/glTF/2.0/Extensions/EXT_lights_ies.js.map +1 -0
  80. package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +48 -0
  81. package/glTF/2.0/Extensions/EXT_lights_image_based.js +116 -0
  82. package/glTF/2.0/Extensions/EXT_lights_image_based.js.map +1 -0
  83. package/glTF/2.0/Extensions/EXT_materials_diffuse_roughness.d.ts +43 -0
  84. package/glTF/2.0/Extensions/EXT_materials_diffuse_roughness.js +69 -0
  85. package/glTF/2.0/Extensions/EXT_materials_diffuse_roughness.js.map +1 -0
  86. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +39 -0
  87. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +82 -0
  88. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js.map +1 -0
  89. package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +39 -0
  90. package/glTF/2.0/Extensions/EXT_meshopt_compression.js +51 -0
  91. package/glTF/2.0/Extensions/EXT_meshopt_compression.js.map +1 -0
  92. package/glTF/2.0/Extensions/EXT_texture_avif.d.ts +34 -0
  93. package/glTF/2.0/Extensions/EXT_texture_avif.js +39 -0
  94. package/glTF/2.0/Extensions/EXT_texture_avif.js.map +1 -0
  95. package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +33 -0
  96. package/glTF/2.0/Extensions/EXT_texture_webp.js +38 -0
  97. package/glTF/2.0/Extensions/EXT_texture_webp.js.map +1 -0
  98. package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +48 -0
  99. package/glTF/2.0/Extensions/ExtrasAsMetadata.js +63 -0
  100. package/glTF/2.0/Extensions/ExtrasAsMetadata.js.map +1 -0
  101. package/glTF/2.0/Extensions/KHR_animation_pointer.d.ts +47 -0
  102. package/glTF/2.0/Extensions/KHR_animation_pointer.data.d.ts +1 -0
  103. package/glTF/2.0/Extensions/KHR_animation_pointer.data.js +239 -0
  104. package/glTF/2.0/Extensions/KHR_animation_pointer.data.js.map +1 -0
  105. package/glTF/2.0/Extensions/KHR_animation_pointer.js +78 -0
  106. package/glTF/2.0/Extensions/KHR_animation_pointer.js.map +1 -0
  107. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +47 -0
  108. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +93 -0
  109. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js.map +1 -0
  110. package/glTF/2.0/Extensions/KHR_interactivity/declarationMapper.d.ts +352 -0
  111. package/glTF/2.0/Extensions/KHR_interactivity/declarationMapper.js +1700 -0
  112. package/glTF/2.0/Extensions/KHR_interactivity/declarationMapper.js.map +1 -0
  113. package/glTF/2.0/Extensions/KHR_interactivity/flowGraphGLTFDataProvider.d.ts +33 -0
  114. package/glTF/2.0/Extensions/KHR_interactivity/flowGraphGLTFDataProvider.js +20 -0
  115. package/glTF/2.0/Extensions/KHR_interactivity/flowGraphGLTFDataProvider.js.map +1 -0
  116. package/glTF/2.0/Extensions/KHR_interactivity/index.d.ts +3 -0
  117. package/glTF/2.0/Extensions/KHR_interactivity/index.js +4 -0
  118. package/glTF/2.0/Extensions/KHR_interactivity/index.js.map +1 -0
  119. package/glTF/2.0/Extensions/KHR_interactivity/interactivityGraphParser.d.ts +70 -0
  120. package/glTF/2.0/Extensions/KHR_interactivity/interactivityGraphParser.js +450 -0
  121. package/glTF/2.0/Extensions/KHR_interactivity/interactivityGraphParser.js.map +1 -0
  122. package/glTF/2.0/Extensions/KHR_interactivity.d.ts +38 -0
  123. package/glTF/2.0/Extensions/KHR_interactivity.js +151 -0
  124. package/glTF/2.0/Extensions/KHR_interactivity.js.map +1 -0
  125. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +41 -0
  126. package/glTF/2.0/Extensions/KHR_lights_punctual.js +92 -0
  127. package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +1 -0
  128. package/glTF/2.0/Extensions/KHR_materials_anisotropy.d.ts +42 -0
  129. package/glTF/2.0/Extensions/KHR_materials_anisotropy.js +61 -0
  130. package/glTF/2.0/Extensions/KHR_materials_anisotropy.js.map +1 -0
  131. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +43 -0
  132. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -0
  133. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +1 -0
  134. package/glTF/2.0/Extensions/KHR_materials_diffuse_transmission.d.ts +43 -0
  135. package/glTF/2.0/Extensions/KHR_materials_diffuse_transmission.js +95 -0
  136. package/glTF/2.0/Extensions/KHR_materials_diffuse_transmission.js.map +1 -0
  137. package/glTF/2.0/Extensions/KHR_materials_dispersion.d.ts +43 -0
  138. package/glTF/2.0/Extensions/KHR_materials_dispersion.js +60 -0
  139. package/glTF/2.0/Extensions/KHR_materials_dispersion.js.map +1 -0
  140. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +42 -0
  141. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +52 -0
  142. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js.map +1 -0
  143. package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +46 -0
  144. package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -0
  145. package/glTF/2.0/Extensions/KHR_materials_ior.js.map +1 -0
  146. package/glTF/2.0/Extensions/KHR_materials_iridescence.d.ts +42 -0
  147. package/glTF/2.0/Extensions/KHR_materials_iridescence.js +71 -0
  148. package/glTF/2.0/Extensions/KHR_materials_iridescence.js.map +1 -0
  149. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +42 -0
  150. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +81 -0
  151. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js.map +1 -0
  152. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +43 -0
  153. package/glTF/2.0/Extensions/KHR_materials_sheen.js +85 -0
  154. package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +1 -0
  155. package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +42 -0
  156. package/glTF/2.0/Extensions/KHR_materials_specular.js +85 -0
  157. package/glTF/2.0/Extensions/KHR_materials_specular.js.map +1 -0
  158. package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +42 -0
  159. package/glTF/2.0/Extensions/KHR_materials_transmission.js +306 -0
  160. package/glTF/2.0/Extensions/KHR_materials_transmission.js.map +1 -0
  161. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +42 -0
  162. package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -0
  163. package/glTF/2.0/Extensions/KHR_materials_unlit.js.map +1 -0
  164. package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +112 -0
  165. package/glTF/2.0/Extensions/KHR_materials_variants.js +263 -0
  166. package/glTF/2.0/Extensions/KHR_materials_variants.js.map +1 -0
  167. package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +43 -0
  168. package/glTF/2.0/Extensions/KHR_materials_volume.js +86 -0
  169. package/glTF/2.0/Extensions/KHR_materials_volume.js.map +1 -0
  170. package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +29 -0
  171. package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -0
  172. package/glTF/2.0/Extensions/KHR_mesh_quantization.js.map +1 -0
  173. package/glTF/2.0/Extensions/KHR_node_hoverability.d.ts +31 -0
  174. package/glTF/2.0/Extensions/KHR_node_hoverability.js +190 -0
  175. package/glTF/2.0/Extensions/KHR_node_hoverability.js.map +1 -0
  176. package/glTF/2.0/Extensions/KHR_node_selectability.d.ts +30 -0
  177. package/glTF/2.0/Extensions/KHR_node_selectability.js +124 -0
  178. package/glTF/2.0/Extensions/KHR_node_selectability.js.map +1 -0
  179. package/glTF/2.0/Extensions/KHR_node_visibility.d.ts +30 -0
  180. package/glTF/2.0/Extensions/KHR_node_visibility.js +69 -0
  181. package/glTF/2.0/Extensions/KHR_node_visibility.js.map +1 -0
  182. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +33 -0
  183. package/glTF/2.0/Extensions/KHR_texture_basisu.js +38 -0
  184. package/glTF/2.0/Extensions/KHR_texture_basisu.js.map +1 -0
  185. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +37 -0
  186. package/glTF/2.0/Extensions/KHR_texture_transform.js +59 -0
  187. package/glTF/2.0/Extensions/KHR_texture_transform.js.map +1 -0
  188. package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +39 -0
  189. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +48 -0
  190. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js.map +1 -0
  191. package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +56 -0
  192. package/glTF/2.0/Extensions/MSFT_audio_emitter.js +227 -0
  193. package/glTF/2.0/Extensions/MSFT_audio_emitter.js.map +1 -0
  194. package/glTF/2.0/Extensions/MSFT_lod.d.ts +104 -0
  195. package/glTF/2.0/Extensions/MSFT_lod.js +338 -0
  196. package/glTF/2.0/Extensions/MSFT_lod.js.map +1 -0
  197. package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +27 -0
  198. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +41 -0
  199. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js.map +1 -0
  200. package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +27 -0
  201. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +42 -0
  202. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js.map +1 -0
  203. package/glTF/2.0/Extensions/dynamic.d.ts +4 -0
  204. package/glTF/2.0/Extensions/dynamic.js +156 -0
  205. package/glTF/2.0/Extensions/dynamic.js.map +1 -0
  206. package/glTF/2.0/Extensions/gltfPathToObjectConverter.d.ts +45 -0
  207. package/glTF/2.0/Extensions/gltfPathToObjectConverter.js +101 -0
  208. package/glTF/2.0/Extensions/gltfPathToObjectConverter.js.map +1 -0
  209. package/glTF/2.0/Extensions/index.d.ts +39 -0
  210. package/glTF/2.0/Extensions/index.js +41 -0
  211. package/glTF/2.0/Extensions/index.js.map +1 -0
  212. package/glTF/2.0/Extensions/objectModelMapping.d.ts +289 -0
  213. package/glTF/2.0/Extensions/objectModelMapping.js +828 -0
  214. package/glTF/2.0/Extensions/objectModelMapping.js.map +1 -0
  215. package/glTF/2.0/glTFLoader.d.ts +429 -0
  216. package/glTF/2.0/glTFLoader.js +2408 -0
  217. package/glTF/2.0/glTFLoader.js.map +1 -0
  218. package/glTF/2.0/glTFLoaderAnimation.d.ts +42 -0
  219. package/glTF/2.0/glTFLoaderAnimation.js +80 -0
  220. package/glTF/2.0/glTFLoaderAnimation.js.map +1 -0
  221. package/glTF/2.0/glTFLoaderExtension.d.ts +165 -0
  222. package/glTF/2.0/glTFLoaderExtension.js +2 -0
  223. package/glTF/2.0/glTFLoaderExtension.js.map +1 -0
  224. package/glTF/2.0/glTFLoaderExtensionRegistry.d.ts +25 -0
  225. package/glTF/2.0/glTFLoaderExtensionRegistry.js +33 -0
  226. package/glTF/2.0/glTFLoaderExtensionRegistry.js.map +1 -0
  227. package/glTF/2.0/glTFLoaderInterfaces.d.ts +257 -0
  228. package/glTF/2.0/glTFLoaderInterfaces.js +2 -0
  229. package/glTF/2.0/glTFLoaderInterfaces.js.map +1 -0
  230. package/glTF/2.0/index.d.ts +6 -0
  231. package/glTF/2.0/index.js +8 -0
  232. package/glTF/2.0/index.js.map +1 -0
  233. package/glTF/glTFFileLoader.d.ts +516 -0
  234. package/glTF/glTFFileLoader.js +968 -0
  235. package/glTF/glTFFileLoader.js.map +1 -0
  236. package/glTF/glTFFileLoader.metadata.d.ts +15 -0
  237. package/glTF/glTFFileLoader.metadata.js +18 -0
  238. package/glTF/glTFFileLoader.metadata.js.map +1 -0
  239. package/glTF/glTFValidation.d.ts +29 -0
  240. package/glTF/glTFValidation.js +126 -0
  241. package/glTF/glTFValidation.js.map +1 -0
  242. package/glTF/index.d.ts +5 -0
  243. package/glTF/index.js +7 -0
  244. package/glTF/index.js.map +1 -0
  245. package/index.d.ts +5 -0
  246. package/index.js +7 -0
  247. package/index.js.map +1 -0
  248. package/legacy/legacy-bvhFileLoader.d.ts +1 -0
  249. package/legacy/legacy-bvhFileLoader.js +16 -0
  250. package/legacy/legacy-bvhFileLoader.js.map +1 -0
  251. package/legacy/legacy-glTF.d.ts +2 -0
  252. package/legacy/legacy-glTF.js +19 -0
  253. package/legacy/legacy-glTF.js.map +1 -0
  254. package/legacy/legacy-glTF1.d.ts +2 -0
  255. package/legacy/legacy-glTF1.js +16 -0
  256. package/legacy/legacy-glTF1.js.map +1 -0
  257. package/legacy/legacy-glTF1FileLoader.d.ts +2 -0
  258. package/legacy/legacy-glTF1FileLoader.js +4 -0
  259. package/legacy/legacy-glTF1FileLoader.js.map +1 -0
  260. package/legacy/legacy-glTF2.d.ts +2 -0
  261. package/legacy/legacy-glTF2.js +35 -0
  262. package/legacy/legacy-glTF2.js.map +1 -0
  263. package/legacy/legacy-glTF2FileLoader.d.ts +2 -0
  264. package/legacy/legacy-glTF2FileLoader.js +4 -0
  265. package/legacy/legacy-glTF2FileLoader.js.map +1 -0
  266. package/legacy/legacy-glTFFileLoader.d.ts +3 -0
  267. package/legacy/legacy-glTFFileLoader.js +5 -0
  268. package/legacy/legacy-glTFFileLoader.js.map +1 -0
  269. package/legacy/legacy-objFileLoader.d.ts +1 -0
  270. package/legacy/legacy-objFileLoader.js +16 -0
  271. package/legacy/legacy-objFileLoader.js.map +1 -0
  272. package/legacy/legacy-stlFileLoader.d.ts +1 -0
  273. package/legacy/legacy-stlFileLoader.js +16 -0
  274. package/legacy/legacy-stlFileLoader.js.map +1 -0
  275. package/legacy/legacy.d.ts +7 -0
  276. package/legacy/legacy.js +10 -0
  277. package/legacy/legacy.js.map +1 -0
  278. package/license.md +71 -0
  279. package/package.json +49 -0
  280. package/readme.md +23 -0
@@ -0,0 +1,2408 @@
1
+ import { Deferred } from "@babylonjs/core/Misc/deferred.js";
2
+ import { Quaternion, Vector3, Matrix, TmpVectors } from "@babylonjs/core/Maths/math.vector.js";
3
+ import { Color3 } from "@babylonjs/core/Maths/math.color.js";
4
+ import { Tools } from "@babylonjs/core/Misc/tools.js";
5
+ import { Camera } from "@babylonjs/core/Cameras/camera.js";
6
+ import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera.js";
7
+ import { Bone } from "@babylonjs/core/Bones/bone.js";
8
+ import { Skeleton } from "@babylonjs/core/Bones/skeleton.js";
9
+ import { Material } from "@babylonjs/core/Materials/material.js";
10
+ import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
11
+ import { Texture } from "@babylonjs/core/Materials/Textures/texture.js";
12
+ import { TransformNode } from "@babylonjs/core/Meshes/transformNode.js";
13
+ import { Buffer, VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
14
+ import { Geometry } from "@babylonjs/core/Meshes/geometry.js";
15
+ import { AbstractMesh } from "@babylonjs/core/Meshes/abstractMesh.js";
16
+ import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
17
+ import { MorphTarget } from "@babylonjs/core/Morph/morphTarget.js";
18
+ import { MorphTargetManager } from "@babylonjs/core/Morph/morphTargetManager.js";
19
+ import { GLTFFileLoader, GLTFLoaderState, GLTFLoaderCoordinateSystemMode, GLTFLoaderAnimationStartMode } from "../glTFFileLoader.js";
20
+ import { DecodeBase64UrlToBinary, GetMimeType, IsBase64DataUrl, LoadFileError } from "@babylonjs/core/Misc/fileTools.js";
21
+ import { Logger } from "@babylonjs/core/Misc/logger.js";
22
+ import { BoundingInfo } from "@babylonjs/core/Culling/boundingInfo.js";
23
+ import { registeredGLTFExtensions, registerGLTFExtension, unregisterGLTFExtension } from "./glTFLoaderExtensionRegistry.js";
24
+ import { GetMappingForKey } from "./Extensions/objectModelMapping.js";
25
+ import { deepMerge } from "@babylonjs/core/Misc/deepMerger.js";
26
+ import { GetTypedArrayConstructor } from "@babylonjs/core/Buffers/bufferUtils.js";
27
+ export { GLTFFileLoader };
28
+ /**
29
+ * Helper class for working with arrays when loading the glTF asset
30
+ */
31
+ export class ArrayItem {
32
+ /**
33
+ * Gets an item from the given array.
34
+ * @param context The context when loading the asset
35
+ * @param array The array to get the item from
36
+ * @param index The index to the array
37
+ * @returns The array item
38
+ */
39
+ static Get(context, array, index) {
40
+ if (!array || index == undefined || !array[index]) {
41
+ throw new Error(`${context}: Failed to find index (${index})`);
42
+ }
43
+ return array[index];
44
+ }
45
+ /**
46
+ * Gets an item from the given array or returns null if not available.
47
+ * @param array The array to get the item from
48
+ * @param index The index to the array
49
+ * @returns The array item or null
50
+ */
51
+ static TryGet(array, index) {
52
+ if (!array || index == undefined || !array[index]) {
53
+ return null;
54
+ }
55
+ return array[index];
56
+ }
57
+ /**
58
+ * Assign an `index` field to each item of the given array.
59
+ * @param array The array of items
60
+ */
61
+ static Assign(array) {
62
+ if (array) {
63
+ for (let index = 0; index < array.length; index++) {
64
+ array[index].index = index;
65
+ }
66
+ }
67
+ }
68
+ }
69
+ /** @internal */
70
+ export function LoadBoundingInfoFromPositionAccessor(accessor) {
71
+ if (accessor.min && accessor.max) {
72
+ const minArray = accessor.min;
73
+ const maxArray = accessor.max;
74
+ const minVector = TmpVectors.Vector3[0].copyFromFloats(minArray[0], minArray[1], minArray[2]);
75
+ const maxVector = TmpVectors.Vector3[1].copyFromFloats(maxArray[0], maxArray[1], maxArray[2]);
76
+ if (accessor.normalized && accessor.componentType !== 5126 /* AccessorComponentType.FLOAT */) {
77
+ let divider = 1;
78
+ switch (accessor.componentType) {
79
+ case 5120 /* AccessorComponentType.BYTE */:
80
+ divider = 127.0;
81
+ break;
82
+ case 5121 /* AccessorComponentType.UNSIGNED_BYTE */:
83
+ divider = 255.0;
84
+ break;
85
+ case 5122 /* AccessorComponentType.SHORT */:
86
+ divider = 32767.0;
87
+ break;
88
+ case 5123 /* AccessorComponentType.UNSIGNED_SHORT */:
89
+ divider = 65535.0;
90
+ break;
91
+ }
92
+ const oneOverDivider = 1 / divider;
93
+ minVector.scaleInPlace(oneOverDivider);
94
+ maxVector.scaleInPlace(oneOverDivider);
95
+ }
96
+ return new BoundingInfo(minVector, maxVector);
97
+ }
98
+ return null;
99
+ }
100
+ /**
101
+ * The glTF 2.0 loader
102
+ */
103
+ export class GLTFLoader {
104
+ /**
105
+ * Registers a loader extension.
106
+ * @param name The name of the loader extension.
107
+ * @param factory The factory function that creates the loader extension.
108
+ * @deprecated Please use registerGLTFExtension instead.
109
+ */
110
+ static RegisterExtension(name, factory) {
111
+ registerGLTFExtension(name, false, factory);
112
+ }
113
+ /**
114
+ * Unregisters a loader extension.
115
+ * @param name The name of the loader extension.
116
+ * @returns A boolean indicating whether the extension has been unregistered
117
+ * @deprecated Please use unregisterGLTFExtension instead.
118
+ */
119
+ static UnregisterExtension(name) {
120
+ return unregisterGLTFExtension(name);
121
+ }
122
+ /**
123
+ * The object that represents the glTF JSON.
124
+ */
125
+ get gltf() {
126
+ if (!this._gltf) {
127
+ throw new Error("glTF JSON is not available");
128
+ }
129
+ return this._gltf;
130
+ }
131
+ /**
132
+ * The BIN chunk of a binary glTF.
133
+ */
134
+ get bin() {
135
+ return this._bin;
136
+ }
137
+ /**
138
+ * The parent file loader.
139
+ */
140
+ get parent() {
141
+ return this._parent;
142
+ }
143
+ /**
144
+ * The Babylon scene when loading the asset.
145
+ */
146
+ get babylonScene() {
147
+ if (!this._babylonScene) {
148
+ throw new Error("Scene is not available");
149
+ }
150
+ return this._babylonScene;
151
+ }
152
+ /**
153
+ * The root Babylon node when loading the asset.
154
+ */
155
+ get rootBabylonMesh() {
156
+ return this._rootBabylonMesh;
157
+ }
158
+ /**
159
+ * The root url when loading the asset.
160
+ */
161
+ get rootUrl() {
162
+ return this._rootUrl;
163
+ }
164
+ /**
165
+ * @internal
166
+ */
167
+ constructor(parent) {
168
+ /** @internal */
169
+ this._completePromises = new Array();
170
+ /** @internal */
171
+ this._assetContainer = null;
172
+ /** Storage */
173
+ this._babylonLights = [];
174
+ /** @internal */
175
+ this._disableInstancedMesh = 0;
176
+ /** @internal */
177
+ this._allMaterialsDirtyRequired = false;
178
+ /** @internal */
179
+ this._skipStartAnimationStep = false;
180
+ this._extensions = new Array();
181
+ this._disposed = false;
182
+ this._rootUrl = null;
183
+ this._fileName = null;
184
+ this._uniqueRootUrl = null;
185
+ this._bin = null;
186
+ this._rootBabylonMesh = null;
187
+ this._defaultBabylonMaterialData = {};
188
+ this._postSceneLoadActions = new Array();
189
+ this._parent = parent;
190
+ }
191
+ /** @internal */
192
+ dispose() {
193
+ if (this._disposed) {
194
+ return;
195
+ }
196
+ this._disposed = true;
197
+ this._completePromises.length = 0;
198
+ this._extensions.forEach((extension) => extension.dispose && extension.dispose());
199
+ this._extensions.length = 0;
200
+ this._gltf = null; // TODO
201
+ this._bin = null;
202
+ this._babylonScene = null; // TODO
203
+ this._rootBabylonMesh = null;
204
+ this._defaultBabylonMaterialData = {};
205
+ this._postSceneLoadActions.length = 0;
206
+ this._parent.dispose();
207
+ }
208
+ /**
209
+ * @internal
210
+ */
211
+ async importMeshAsync(meshesNames, scene, container, data, rootUrl, onProgress, fileName = "") {
212
+ // eslint-disable-next-line github/no-then
213
+ return await Promise.resolve().then(async () => {
214
+ this._babylonScene = scene;
215
+ this._assetContainer = container;
216
+ this._loadData(data);
217
+ let nodes = null;
218
+ if (meshesNames) {
219
+ const nodeMap = {};
220
+ if (this._gltf.nodes) {
221
+ for (const node of this._gltf.nodes) {
222
+ if (node.name) {
223
+ nodeMap[node.name] = node.index;
224
+ }
225
+ }
226
+ }
227
+ const names = meshesNames instanceof Array ? meshesNames : [meshesNames];
228
+ nodes = names.map((name) => {
229
+ const node = nodeMap[name];
230
+ if (node === undefined) {
231
+ throw new Error(`Failed to find node '${name}'`);
232
+ }
233
+ return node;
234
+ });
235
+ }
236
+ return await this._loadAsync(rootUrl, fileName, nodes, () => {
237
+ return {
238
+ meshes: this._getMeshes(),
239
+ particleSystems: [],
240
+ skeletons: this._getSkeletons(),
241
+ animationGroups: this._getAnimationGroups(),
242
+ lights: this._babylonLights,
243
+ transformNodes: this._getTransformNodes(),
244
+ geometries: this._getGeometries(),
245
+ spriteManagers: [],
246
+ };
247
+ });
248
+ });
249
+ }
250
+ /**
251
+ * @internal
252
+ */
253
+ async loadAsync(scene, data, rootUrl, onProgress, fileName = "") {
254
+ this._babylonScene = scene;
255
+ this._loadData(data);
256
+ return await this._loadAsync(rootUrl, fileName, null, () => undefined);
257
+ }
258
+ async _loadAsync(rootUrl, fileName, nodes, resultFunc) {
259
+ return await Promise.resolve()
260
+ .then(async () => {
261
+ this._rootUrl = rootUrl;
262
+ this._uniqueRootUrl = !rootUrl.startsWith("file:") && fileName ? rootUrl : `${rootUrl}${Date.now()}/`;
263
+ this._fileName = fileName;
264
+ this._allMaterialsDirtyRequired = false;
265
+ await this._loadExtensionsAsync();
266
+ const loadingToReadyCounterName = `${GLTFLoaderState[GLTFLoaderState.LOADING]} => ${GLTFLoaderState[GLTFLoaderState.READY]}`;
267
+ const loadingToCompleteCounterName = `${GLTFLoaderState[GLTFLoaderState.LOADING]} => ${GLTFLoaderState[GLTFLoaderState.COMPLETE]}`;
268
+ this._parent._startPerformanceCounter(loadingToReadyCounterName);
269
+ this._parent._startPerformanceCounter(loadingToCompleteCounterName);
270
+ this._parent._setState(GLTFLoaderState.LOADING);
271
+ this._extensionsOnLoading();
272
+ const promises = new Array();
273
+ // Block the marking of materials dirty until the scene is loaded.
274
+ const oldBlockMaterialDirtyMechanism = this._babylonScene.blockMaterialDirtyMechanism;
275
+ this._babylonScene.blockMaterialDirtyMechanism = true;
276
+ if (!this.parent.loadOnlyMaterials) {
277
+ if (nodes) {
278
+ promises.push(this.loadSceneAsync("/nodes", { nodes: nodes, index: -1 }));
279
+ }
280
+ else if (this._gltf.scene != undefined || (this._gltf.scenes && this._gltf.scenes[0])) {
281
+ const scene = ArrayItem.Get(`/scene`, this._gltf.scenes, this._gltf.scene || 0);
282
+ promises.push(this.loadSceneAsync(`/scenes/${scene.index}`, scene));
283
+ }
284
+ }
285
+ if (!this.parent.skipMaterials && this.parent.loadAllMaterials && this._gltf.materials) {
286
+ for (let m = 0; m < this._gltf.materials.length; ++m) {
287
+ const material = this._gltf.materials[m];
288
+ const context = "/materials/" + m;
289
+ const babylonDrawMode = Material.TriangleFillMode;
290
+ promises.push(this._loadMaterialAsync(context, material, null, babylonDrawMode, () => { }));
291
+ }
292
+ }
293
+ // Restore the blocking of material dirty.
294
+ if (this._allMaterialsDirtyRequired) {
295
+ // This can happen if we add a light for instance as it will impact the whole scene.
296
+ // This automatically resets everything if needed.
297
+ this._babylonScene.blockMaterialDirtyMechanism = oldBlockMaterialDirtyMechanism;
298
+ }
299
+ else {
300
+ // By default a newly created material is dirty so there is no need to flag the full scene as dirty.
301
+ // For perf reasons, we then bypass blockMaterialDirtyMechanism as this would "dirty" the entire scene.
302
+ this._babylonScene._forceBlockMaterialDirtyMechanism(oldBlockMaterialDirtyMechanism);
303
+ }
304
+ if (this._parent.compileMaterials) {
305
+ promises.push(this._compileMaterialsAsync());
306
+ }
307
+ if (this._parent.compileShadowGenerators) {
308
+ promises.push(this._compileShadowGeneratorsAsync());
309
+ }
310
+ const resultPromise = Promise.all(promises).then(() => {
311
+ if (this._rootBabylonMesh && this._rootBabylonMesh !== this._parent.customRootNode) {
312
+ this._rootBabylonMesh.setEnabled(true);
313
+ }
314
+ // Making sure we enable enough lights to have all lights together
315
+ for (const material of this._babylonScene.materials) {
316
+ const mat = material;
317
+ if (mat.maxSimultaneousLights !== undefined) {
318
+ mat.maxSimultaneousLights = Math.max(mat.maxSimultaneousLights, this._babylonScene.lights.length);
319
+ }
320
+ }
321
+ this._extensionsOnReady();
322
+ this._parent._setState(GLTFLoaderState.READY);
323
+ if (!this._skipStartAnimationStep) {
324
+ this._startAnimations();
325
+ }
326
+ return resultFunc();
327
+ });
328
+ return await resultPromise.then((result) => {
329
+ this._parent._endPerformanceCounter(loadingToReadyCounterName);
330
+ Tools.SetImmediate(() => {
331
+ if (!this._disposed) {
332
+ Promise.all(this._completePromises).then(() => {
333
+ this._parent._endPerformanceCounter(loadingToCompleteCounterName);
334
+ this._parent._setState(GLTFLoaderState.COMPLETE);
335
+ this._parent.onCompleteObservable.notifyObservers(undefined);
336
+ this._parent.onCompleteObservable.clear();
337
+ this.dispose();
338
+ }, (error) => {
339
+ this._parent.onErrorObservable.notifyObservers(error);
340
+ this._parent.onErrorObservable.clear();
341
+ this.dispose();
342
+ });
343
+ }
344
+ });
345
+ return result;
346
+ });
347
+ })
348
+ .catch((error) => {
349
+ if (!this._disposed) {
350
+ this._parent.onErrorObservable.notifyObservers(error);
351
+ this._parent.onErrorObservable.clear();
352
+ this.dispose();
353
+ }
354
+ throw error;
355
+ });
356
+ }
357
+ _loadData(data) {
358
+ this._gltf = data.json;
359
+ this._setupData();
360
+ if (data.bin) {
361
+ const buffers = this._gltf.buffers;
362
+ if (buffers && buffers[0] && !buffers[0].uri) {
363
+ const binaryBuffer = buffers[0];
364
+ if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
365
+ Logger.Warn(`Binary buffer length (${binaryBuffer.byteLength}) from JSON does not match chunk length (${data.bin.byteLength})`);
366
+ }
367
+ this._bin = data.bin;
368
+ }
369
+ else {
370
+ Logger.Warn("Unexpected BIN chunk");
371
+ }
372
+ }
373
+ }
374
+ _setupData() {
375
+ ArrayItem.Assign(this._gltf.accessors);
376
+ ArrayItem.Assign(this._gltf.animations);
377
+ ArrayItem.Assign(this._gltf.buffers);
378
+ ArrayItem.Assign(this._gltf.bufferViews);
379
+ ArrayItem.Assign(this._gltf.cameras);
380
+ ArrayItem.Assign(this._gltf.images);
381
+ ArrayItem.Assign(this._gltf.materials);
382
+ ArrayItem.Assign(this._gltf.meshes);
383
+ ArrayItem.Assign(this._gltf.nodes);
384
+ ArrayItem.Assign(this._gltf.samplers);
385
+ ArrayItem.Assign(this._gltf.scenes);
386
+ ArrayItem.Assign(this._gltf.skins);
387
+ ArrayItem.Assign(this._gltf.textures);
388
+ if (this._gltf.nodes) {
389
+ const nodeParents = {};
390
+ for (const node of this._gltf.nodes) {
391
+ if (node.children) {
392
+ for (const index of node.children) {
393
+ nodeParents[index] = node.index;
394
+ }
395
+ }
396
+ }
397
+ const rootNode = this._createRootNode();
398
+ for (const node of this._gltf.nodes) {
399
+ const parentIndex = nodeParents[node.index];
400
+ node.parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
401
+ }
402
+ }
403
+ }
404
+ async _loadExtensionsAsync() {
405
+ const extensionPromises = [];
406
+ registeredGLTFExtensions.forEach((registeredExtension, name) => {
407
+ // Don't load explicitly disabled extensions.
408
+ if (this.parent.extensionOptions[name]?.enabled === false) {
409
+ // But warn if the disabled extension is used by the model.
410
+ if (registeredExtension.isGLTFExtension && this.isExtensionUsed(name)) {
411
+ Logger.Warn(`Extension ${name} is used but has been explicitly disabled.`);
412
+ }
413
+ }
414
+ // Load loader extensions that are not a glTF extension, as well as extensions that are glTF extensions and are used by the model.
415
+ else if (!registeredExtension.isGLTFExtension || this.isExtensionUsed(name)) {
416
+ extensionPromises.push((async () => {
417
+ const extension = await registeredExtension.factory(this);
418
+ if (extension.name !== name) {
419
+ Logger.Warn(`The name of the glTF loader extension instance does not match the registered name: ${extension.name} !== ${name}`);
420
+ }
421
+ this._parent.onExtensionLoadedObservable.notifyObservers(extension);
422
+ return extension;
423
+ })());
424
+ }
425
+ });
426
+ this._extensions.push(...(await Promise.all(extensionPromises)));
427
+ this._extensions.sort((a, b) => (a.order || Number.MAX_VALUE) - (b.order || Number.MAX_VALUE));
428
+ this._parent.onExtensionLoadedObservable.clear();
429
+ if (this._gltf.extensionsRequired) {
430
+ for (const name of this._gltf.extensionsRequired) {
431
+ const available = this._extensions.some((extension) => extension.name === name && extension.enabled);
432
+ if (!available) {
433
+ if (this.parent.extensionOptions[name]?.enabled === false) {
434
+ throw new Error(`Required extension ${name} is disabled`);
435
+ }
436
+ throw new Error(`Required extension ${name} is not available`);
437
+ }
438
+ }
439
+ }
440
+ }
441
+ _createRootNode() {
442
+ if (this._parent.customRootNode !== undefined) {
443
+ this._rootBabylonMesh = this._parent.customRootNode;
444
+ return {
445
+ // eslint-disable-next-line @typescript-eslint/naming-convention
446
+ _babylonTransformNode: this._rootBabylonMesh === null ? undefined : this._rootBabylonMesh,
447
+ index: -1,
448
+ };
449
+ }
450
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
451
+ const rootMesh = new Mesh("__root__", this._babylonScene);
452
+ this._rootBabylonMesh = rootMesh;
453
+ this._rootBabylonMesh._parentContainer = this._assetContainer;
454
+ this._babylonScene._blockEntityCollection = false;
455
+ this._rootBabylonMesh.setEnabled(false);
456
+ const rootNode = {
457
+ // eslint-disable-next-line @typescript-eslint/naming-convention
458
+ _babylonTransformNode: this._rootBabylonMesh,
459
+ index: -1,
460
+ };
461
+ switch (this._parent.coordinateSystemMode) {
462
+ case GLTFLoaderCoordinateSystemMode.AUTO: {
463
+ if (!this._babylonScene.useRightHandedSystem) {
464
+ rootNode.rotation = [0, 1, 0, 0];
465
+ rootNode.scale = [1, 1, -1];
466
+ GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
467
+ }
468
+ break;
469
+ }
470
+ case GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
471
+ this._babylonScene.useRightHandedSystem = true;
472
+ break;
473
+ }
474
+ default: {
475
+ throw new Error(`Invalid coordinate system mode (${this._parent.coordinateSystemMode})`);
476
+ }
477
+ }
478
+ this._parent.onMeshLoadedObservable.notifyObservers(rootMesh);
479
+ return rootNode;
480
+ }
481
+ /**
482
+ * Loads a glTF scene.
483
+ * @param context The context when loading the asset
484
+ * @param scene The glTF scene property
485
+ * @returns A promise that resolves when the load is complete
486
+ */
487
+ // eslint-disable-next-line no-restricted-syntax, @typescript-eslint/promise-function-async
488
+ loadSceneAsync(context, scene) {
489
+ const extensionPromise = this._extensionsLoadSceneAsync(context, scene);
490
+ if (extensionPromise) {
491
+ return extensionPromise;
492
+ }
493
+ const promises = new Array();
494
+ this.logOpen(`${context} ${scene.name || ""}`);
495
+ if (scene.nodes) {
496
+ for (const index of scene.nodes) {
497
+ const node = ArrayItem.Get(`${context}/nodes/${index}`, this._gltf.nodes, index);
498
+ promises.push(this.loadNodeAsync(`/nodes/${node.index}`, node, (babylonMesh) => {
499
+ babylonMesh.parent = this._rootBabylonMesh;
500
+ }));
501
+ }
502
+ }
503
+ for (const action of this._postSceneLoadActions) {
504
+ action();
505
+ }
506
+ promises.push(this._loadAnimationsAsync());
507
+ this.logClose();
508
+ return Promise.all(promises).then(() => { });
509
+ }
510
+ _forEachPrimitive(node, callback) {
511
+ if (node._primitiveBabylonMeshes) {
512
+ for (const babylonMesh of node._primitiveBabylonMeshes) {
513
+ callback(babylonMesh);
514
+ }
515
+ }
516
+ }
517
+ _getGeometries() {
518
+ const geometries = [];
519
+ const nodes = this._gltf.nodes;
520
+ if (nodes) {
521
+ for (const node of nodes) {
522
+ this._forEachPrimitive(node, (babylonMesh) => {
523
+ const geometry = babylonMesh.geometry;
524
+ if (geometry && geometries.indexOf(geometry) === -1) {
525
+ geometries.push(geometry);
526
+ }
527
+ });
528
+ }
529
+ }
530
+ return geometries;
531
+ }
532
+ _getMeshes() {
533
+ const meshes = [];
534
+ // Root mesh is always first, if available.
535
+ if (this._rootBabylonMesh instanceof AbstractMesh) {
536
+ meshes.push(this._rootBabylonMesh);
537
+ }
538
+ const nodes = this._gltf.nodes;
539
+ if (nodes) {
540
+ for (const node of nodes) {
541
+ this._forEachPrimitive(node, (babylonMesh) => {
542
+ meshes.push(babylonMesh);
543
+ });
544
+ }
545
+ }
546
+ return meshes;
547
+ }
548
+ _getTransformNodes() {
549
+ const transformNodes = [];
550
+ const nodes = this._gltf.nodes;
551
+ if (nodes) {
552
+ for (const node of nodes) {
553
+ if (node._babylonTransformNode && node._babylonTransformNode.getClassName() === "TransformNode") {
554
+ transformNodes.push(node._babylonTransformNode);
555
+ }
556
+ if (node._babylonTransformNodeForSkin) {
557
+ transformNodes.push(node._babylonTransformNodeForSkin);
558
+ }
559
+ }
560
+ }
561
+ return transformNodes;
562
+ }
563
+ _getSkeletons() {
564
+ const skeletons = [];
565
+ const skins = this._gltf.skins;
566
+ if (skins) {
567
+ for (const skin of skins) {
568
+ if (skin._data) {
569
+ skeletons.push(skin._data.babylonSkeleton);
570
+ }
571
+ }
572
+ }
573
+ return skeletons;
574
+ }
575
+ _getAnimationGroups() {
576
+ const animationGroups = [];
577
+ const animations = this._gltf.animations;
578
+ if (animations) {
579
+ for (const animation of animations) {
580
+ if (animation._babylonAnimationGroup) {
581
+ animationGroups.push(animation._babylonAnimationGroup);
582
+ }
583
+ }
584
+ }
585
+ return animationGroups;
586
+ }
587
+ _startAnimations() {
588
+ switch (this._parent.animationStartMode) {
589
+ case GLTFLoaderAnimationStartMode.NONE: {
590
+ // do nothing
591
+ break;
592
+ }
593
+ case GLTFLoaderAnimationStartMode.FIRST: {
594
+ const babylonAnimationGroups = this._getAnimationGroups();
595
+ if (babylonAnimationGroups.length !== 0) {
596
+ babylonAnimationGroups[0].start(true);
597
+ }
598
+ break;
599
+ }
600
+ case GLTFLoaderAnimationStartMode.ALL: {
601
+ const babylonAnimationGroups = this._getAnimationGroups();
602
+ for (const babylonAnimationGroup of babylonAnimationGroups) {
603
+ babylonAnimationGroup.start(true);
604
+ }
605
+ break;
606
+ }
607
+ default: {
608
+ Logger.Error(`Invalid animation start mode (${this._parent.animationStartMode})`);
609
+ return;
610
+ }
611
+ }
612
+ }
613
+ /**
614
+ * Loads a glTF node.
615
+ * @param context The context when loading the asset
616
+ * @param node The glTF node property
617
+ * @param assign A function called synchronously after parsing the glTF properties
618
+ * @returns A promise that resolves with the loaded Babylon mesh when the load is complete
619
+ */
620
+ // eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
621
+ loadNodeAsync(context, node, assign = () => { }) {
622
+ const extensionPromise = this._extensionsLoadNodeAsync(context, node, assign);
623
+ if (extensionPromise) {
624
+ return extensionPromise;
625
+ }
626
+ if (node._babylonTransformNode) {
627
+ throw new Error(`${context}: Invalid recursive node hierarchy`);
628
+ }
629
+ const promises = new Array();
630
+ this.logOpen(`${context} ${node.name || ""}`);
631
+ const loadNode = (babylonTransformNode) => {
632
+ GLTFLoader.AddPointerMetadata(babylonTransformNode, context);
633
+ GLTFLoader._LoadTransform(node, babylonTransformNode);
634
+ if (node.camera != undefined) {
635
+ const camera = ArrayItem.Get(`${context}/camera`, this._gltf.cameras, node.camera);
636
+ promises.push(this.loadCameraAsync(`/cameras/${camera.index}`, camera, (babylonCamera) => {
637
+ babylonCamera.parent = babylonTransformNode;
638
+ if (!this._babylonScene.useRightHandedSystem) {
639
+ babylonTransformNode.scaling.x = -1; // Cancelling root node scaling for handedness so the view matrix does not end up flipped.
640
+ }
641
+ }));
642
+ }
643
+ if (node.children) {
644
+ for (const index of node.children) {
645
+ const childNode = ArrayItem.Get(`${context}/children/${index}`, this._gltf.nodes, index);
646
+ promises.push(this.loadNodeAsync(`/nodes/${childNode.index}`, childNode, (childBabylonMesh) => {
647
+ childBabylonMesh.parent = babylonTransformNode;
648
+ }));
649
+ }
650
+ }
651
+ assign(babylonTransformNode);
652
+ };
653
+ const hasMesh = node.mesh != undefined;
654
+ const hasSkin = this._parent.loadSkins && node.skin != undefined;
655
+ if (!hasMesh || hasSkin) {
656
+ const nodeName = node.name || `node${node.index}`;
657
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
658
+ const transformNode = new TransformNode(nodeName, this._babylonScene);
659
+ transformNode._parentContainer = this._assetContainer;
660
+ this._babylonScene._blockEntityCollection = false;
661
+ if (node.mesh == undefined) {
662
+ node._babylonTransformNode = transformNode;
663
+ }
664
+ else {
665
+ node._babylonTransformNodeForSkin = transformNode;
666
+ }
667
+ loadNode(transformNode);
668
+ }
669
+ if (hasMesh) {
670
+ if (hasSkin) {
671
+ // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
672
+ // This code path will place the skinned mesh as a sibling of the skeleton root node without loading the
673
+ // transform, which effectively ignores the transform of the skinned mesh, as per spec.
674
+ const mesh = ArrayItem.Get(`${context}/mesh`, this._gltf.meshes, node.mesh);
675
+ promises.push(this._loadMeshAsync(`/meshes/${mesh.index}`, node, mesh, (babylonTransformNode) => {
676
+ const babylonTransformNodeForSkin = node._babylonTransformNodeForSkin;
677
+ // Merge the metadata from the skin node to the skinned mesh in case a loader extension added metadata.
678
+ babylonTransformNode.metadata = deepMerge(babylonTransformNodeForSkin.metadata, babylonTransformNode.metadata || {});
679
+ const skin = ArrayItem.Get(`${context}/skin`, this._gltf.skins, node.skin);
680
+ promises.push(this._loadSkinAsync(`/skins/${skin.index}`, node, skin, (babylonSkeleton) => {
681
+ this._forEachPrimitive(node, (babylonMesh) => {
682
+ babylonMesh.skeleton = babylonSkeleton;
683
+ });
684
+ // Wait until all the nodes are parented before parenting the skinned mesh.
685
+ this._postSceneLoadActions.push(() => {
686
+ if (skin.skeleton != undefined) {
687
+ // Place the skinned mesh node as a sibling of the skeleton root node.
688
+ // Handle special case when the parent of the skeleton root is the skinned mesh.
689
+ const parentNode = ArrayItem.Get(`/skins/${skin.index}/skeleton`, this._gltf.nodes, skin.skeleton).parent;
690
+ if (node.index === parentNode.index) {
691
+ babylonTransformNode.parent = babylonTransformNodeForSkin.parent;
692
+ }
693
+ else {
694
+ babylonTransformNode.parent = parentNode._babylonTransformNode;
695
+ }
696
+ }
697
+ else {
698
+ babylonTransformNode.parent = this._rootBabylonMesh;
699
+ }
700
+ this._parent.onSkinLoadedObservable.notifyObservers({ node: babylonTransformNodeForSkin, skinnedNode: babylonTransformNode });
701
+ });
702
+ }));
703
+ }));
704
+ }
705
+ else {
706
+ const mesh = ArrayItem.Get(`${context}/mesh`, this._gltf.meshes, node.mesh);
707
+ promises.push(this._loadMeshAsync(`/meshes/${mesh.index}`, node, mesh, loadNode));
708
+ }
709
+ }
710
+ this.logClose();
711
+ return Promise.all(promises).then(() => {
712
+ this._forEachPrimitive(node, (babylonMesh) => {
713
+ const asMesh = babylonMesh;
714
+ if (!asMesh.isAnInstance && asMesh.geometry && asMesh.geometry.useBoundingInfoFromGeometry) {
715
+ // simply apply the world matrices to the bounding info - the extends are already ok
716
+ babylonMesh._updateBoundingInfo();
717
+ }
718
+ else {
719
+ babylonMesh.refreshBoundingInfo(true, true);
720
+ }
721
+ });
722
+ return node._babylonTransformNode;
723
+ });
724
+ }
725
+ // eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
726
+ _loadMeshAsync(context, node, mesh, assign) {
727
+ const primitives = mesh.primitives;
728
+ if (!primitives || !primitives.length) {
729
+ throw new Error(`${context}: Primitives are missing`);
730
+ }
731
+ if (primitives[0].index == undefined) {
732
+ ArrayItem.Assign(primitives);
733
+ }
734
+ const promises = new Array();
735
+ this.logOpen(`${context} ${mesh.name || ""}`);
736
+ const name = node.name || `node${node.index}`;
737
+ if (primitives.length === 1) {
738
+ const primitive = mesh.primitives[0];
739
+ promises.push(this._loadMeshPrimitiveAsync(`${context}/primitives/${primitive.index}`, name, node, mesh, primitive, (babylonMesh) => {
740
+ node._babylonTransformNode = babylonMesh;
741
+ node._primitiveBabylonMeshes = [babylonMesh];
742
+ }));
743
+ }
744
+ else {
745
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
746
+ node._babylonTransformNode = new TransformNode(name, this._babylonScene);
747
+ node._babylonTransformNode._parentContainer = this._assetContainer;
748
+ this._babylonScene._blockEntityCollection = false;
749
+ node._primitiveBabylonMeshes = [];
750
+ for (const primitive of primitives) {
751
+ promises.push(this._loadMeshPrimitiveAsync(`${context}/primitives/${primitive.index}`, `${name}_primitive${primitive.index}`, node, mesh, primitive, (babylonMesh) => {
752
+ babylonMesh.parent = node._babylonTransformNode;
753
+ node._primitiveBabylonMeshes.push(babylonMesh);
754
+ }));
755
+ }
756
+ }
757
+ assign(node._babylonTransformNode);
758
+ this.logClose();
759
+ return Promise.all(promises).then(() => {
760
+ return node._babylonTransformNode;
761
+ });
762
+ }
763
+ /**
764
+ * @internal Define this method to modify the default behavior when loading data for mesh primitives.
765
+ * @param context The context when loading the asset
766
+ * @param name The mesh name when loading the asset
767
+ * @param node The glTF node when loading the asset
768
+ * @param mesh The glTF mesh when loading the asset
769
+ * @param primitive The glTF mesh primitive property
770
+ * @param assign A function called synchronously after parsing the glTF properties
771
+ * @returns A promise that resolves with the loaded mesh when the load is complete or null if not handled
772
+ */
773
+ // eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
774
+ _loadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign) {
775
+ const extensionPromise = this._extensionsLoadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign);
776
+ if (extensionPromise) {
777
+ return extensionPromise;
778
+ }
779
+ this.logOpen(`${context}`);
780
+ const shouldInstance = this._disableInstancedMesh === 0 && this._parent.createInstances && node.skin == undefined && !mesh.primitives[0].targets;
781
+ let babylonAbstractMesh;
782
+ let promise;
783
+ if (shouldInstance && primitive._instanceData) {
784
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
785
+ babylonAbstractMesh = primitive._instanceData.babylonSourceMesh.createInstance(name);
786
+ babylonAbstractMesh._parentContainer = this._assetContainer;
787
+ this._babylonScene._blockEntityCollection = false;
788
+ promise = primitive._instanceData.promise;
789
+ }
790
+ else {
791
+ const promises = new Array();
792
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
793
+ const babylonMesh = new Mesh(name, this._babylonScene);
794
+ babylonMesh._parentContainer = this._assetContainer;
795
+ this._babylonScene._blockEntityCollection = false;
796
+ babylonMesh.sideOrientation = this._babylonScene.useRightHandedSystem ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
797
+ this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
798
+ promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(async (babylonGeometry) => {
799
+ return await this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry).then(() => {
800
+ if (this._disposed) {
801
+ return;
802
+ }
803
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
804
+ babylonGeometry.applyToMesh(babylonMesh);
805
+ babylonGeometry._parentContainer = this._assetContainer;
806
+ this._babylonScene._blockEntityCollection = false;
807
+ });
808
+ }));
809
+ const babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
810
+ if (primitive.material == undefined) {
811
+ let babylonMaterial = this._defaultBabylonMaterialData[babylonDrawMode];
812
+ if (!babylonMaterial) {
813
+ babylonMaterial = this._createDefaultMaterial("__GLTFLoader._default", babylonDrawMode);
814
+ this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
815
+ this._defaultBabylonMaterialData[babylonDrawMode] = babylonMaterial;
816
+ }
817
+ babylonMesh.material = babylonMaterial;
818
+ }
819
+ else if (!this.parent.skipMaterials) {
820
+ const material = ArrayItem.Get(`${context}/material`, this._gltf.materials, primitive.material);
821
+ promises.push(this._loadMaterialAsync(`/materials/${material.index}`, material, babylonMesh, babylonDrawMode, (babylonMaterial) => {
822
+ babylonMesh.material = babylonMaterial;
823
+ }));
824
+ }
825
+ promise = Promise.all(promises);
826
+ if (shouldInstance) {
827
+ primitive._instanceData = {
828
+ babylonSourceMesh: babylonMesh,
829
+ promise: promise,
830
+ };
831
+ }
832
+ babylonAbstractMesh = babylonMesh;
833
+ }
834
+ GLTFLoader.AddPointerMetadata(babylonAbstractMesh, context);
835
+ this._parent.onMeshLoadedObservable.notifyObservers(babylonAbstractMesh);
836
+ assign(babylonAbstractMesh);
837
+ this.logClose();
838
+ return promise.then(() => {
839
+ return babylonAbstractMesh;
840
+ });
841
+ }
842
+ // eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
843
+ _loadVertexDataAsync(context, primitive, babylonMesh) {
844
+ const extensionPromise = this._extensionsLoadVertexDataAsync(context, primitive, babylonMesh);
845
+ if (extensionPromise) {
846
+ return extensionPromise;
847
+ }
848
+ const attributes = primitive.attributes;
849
+ if (!attributes) {
850
+ throw new Error(`${context}: Attributes are missing`);
851
+ }
852
+ const promises = new Array();
853
+ const babylonGeometry = new Geometry(babylonMesh.name, this._babylonScene);
854
+ if (primitive.indices == undefined) {
855
+ babylonMesh.isUnIndexed = true;
856
+ }
857
+ else {
858
+ const accessor = ArrayItem.Get(`${context}/indices`, this._gltf.accessors, primitive.indices);
859
+ promises.push(this._loadIndicesAccessorAsync(`/accessors/${accessor.index}`, accessor).then((data) => {
860
+ babylonGeometry.setIndices(data);
861
+ }));
862
+ }
863
+ const loadAttribute = (name, kind, callback) => {
864
+ if (attributes[name] == undefined) {
865
+ return;
866
+ }
867
+ babylonMesh._delayInfo = babylonMesh._delayInfo || [];
868
+ if (babylonMesh._delayInfo.indexOf(kind) === -1) {
869
+ babylonMesh._delayInfo.push(kind);
870
+ }
871
+ const accessor = ArrayItem.Get(`${context}/attributes/${name}`, this._gltf.accessors, attributes[name]);
872
+ promises.push(this._loadVertexAccessorAsync(`/accessors/${accessor.index}`, accessor, kind).then((babylonVertexBuffer) => {
873
+ if (babylonVertexBuffer.getKind() === VertexBuffer.PositionKind && !this.parent.alwaysComputeBoundingBox && !babylonMesh.skeleton) {
874
+ const babylonBoundingInfo = LoadBoundingInfoFromPositionAccessor(accessor);
875
+ if (babylonBoundingInfo) {
876
+ babylonGeometry._boundingInfo = babylonBoundingInfo;
877
+ babylonGeometry.useBoundingInfoFromGeometry = true;
878
+ }
879
+ }
880
+ babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
881
+ }));
882
+ if (kind == VertexBuffer.MatricesIndicesExtraKind) {
883
+ babylonMesh.numBoneInfluencers = 8;
884
+ }
885
+ if (callback) {
886
+ callback(accessor);
887
+ }
888
+ };
889
+ loadAttribute("POSITION", VertexBuffer.PositionKind);
890
+ loadAttribute("NORMAL", VertexBuffer.NormalKind);
891
+ loadAttribute("TANGENT", VertexBuffer.TangentKind);
892
+ loadAttribute("TEXCOORD_0", VertexBuffer.UVKind);
893
+ loadAttribute("TEXCOORD_1", VertexBuffer.UV2Kind);
894
+ loadAttribute("TEXCOORD_2", VertexBuffer.UV3Kind);
895
+ loadAttribute("TEXCOORD_3", VertexBuffer.UV4Kind);
896
+ loadAttribute("TEXCOORD_4", VertexBuffer.UV5Kind);
897
+ loadAttribute("TEXCOORD_5", VertexBuffer.UV6Kind);
898
+ loadAttribute("JOINTS_0", VertexBuffer.MatricesIndicesKind);
899
+ loadAttribute("WEIGHTS_0", VertexBuffer.MatricesWeightsKind);
900
+ loadAttribute("JOINTS_1", VertexBuffer.MatricesIndicesExtraKind);
901
+ loadAttribute("WEIGHTS_1", VertexBuffer.MatricesWeightsExtraKind);
902
+ loadAttribute("COLOR_0", VertexBuffer.ColorKind, (accessor) => {
903
+ if (accessor.type === "VEC4" /* AccessorType.VEC4 */) {
904
+ babylonMesh.hasVertexAlpha = true;
905
+ }
906
+ });
907
+ return Promise.all(promises).then(() => {
908
+ return babylonGeometry;
909
+ });
910
+ }
911
+ _createMorphTargets(context, node, mesh, primitive, babylonMesh) {
912
+ if (!primitive.targets || !this._parent.loadMorphTargets) {
913
+ return;
914
+ }
915
+ if (node._numMorphTargets == undefined) {
916
+ node._numMorphTargets = primitive.targets.length;
917
+ }
918
+ else if (primitive.targets.length !== node._numMorphTargets) {
919
+ throw new Error(`${context}: Primitives do not have the same number of targets`);
920
+ }
921
+ const targetNames = mesh.extras ? mesh.extras.targetNames : null;
922
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
923
+ babylonMesh.morphTargetManager = new MorphTargetManager(this._babylonScene);
924
+ babylonMesh.morphTargetManager._parentContainer = this._assetContainer;
925
+ this._babylonScene._blockEntityCollection = false;
926
+ babylonMesh.morphTargetManager.areUpdatesFrozen = true;
927
+ for (let index = 0; index < primitive.targets.length; index++) {
928
+ const weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
929
+ const name = targetNames ? targetNames[index] : `morphTarget${index}`;
930
+ babylonMesh.morphTargetManager.addTarget(new MorphTarget(name, weight, babylonMesh.getScene()));
931
+ // TODO: tell the target whether it has positions, normals, tangents
932
+ }
933
+ }
934
+ // eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
935
+ _loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry) {
936
+ if (!primitive.targets || !this._parent.loadMorphTargets) {
937
+ return Promise.resolve();
938
+ }
939
+ const promises = new Array();
940
+ const morphTargetManager = babylonMesh.morphTargetManager;
941
+ for (let index = 0; index < morphTargetManager.numTargets; index++) {
942
+ const babylonMorphTarget = morphTargetManager.getTarget(index);
943
+ promises.push(this._loadMorphTargetVertexDataAsync(`${context}/targets/${index}`, babylonGeometry, primitive.targets[index], babylonMorphTarget));
944
+ }
945
+ return Promise.all(promises).then(() => {
946
+ morphTargetManager.areUpdatesFrozen = false;
947
+ });
948
+ }
949
+ async _loadMorphTargetVertexDataAsync(context, babylonGeometry, attributes, babylonMorphTarget) {
950
+ const promises = new Array();
951
+ const loadAttribute = (attribute, kind, setData) => {
952
+ if (attributes[attribute] == undefined) {
953
+ return;
954
+ }
955
+ const babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
956
+ if (!babylonVertexBuffer) {
957
+ return;
958
+ }
959
+ const accessor = ArrayItem.Get(`${context}/${attribute}`, this._gltf.accessors, attributes[attribute]);
960
+ promises.push(this._loadFloatAccessorAsync(`/accessors/${accessor.index}`, accessor).then((data) => {
961
+ setData(babylonVertexBuffer, data);
962
+ }));
963
+ };
964
+ loadAttribute("POSITION", VertexBuffer.PositionKind, (babylonVertexBuffer, data) => {
965
+ const positions = new Float32Array(data.length);
966
+ babylonVertexBuffer.forEach(data.length, (value, index) => {
967
+ positions[index] = data[index] + value;
968
+ });
969
+ babylonMorphTarget.setPositions(positions);
970
+ });
971
+ loadAttribute("NORMAL", VertexBuffer.NormalKind, (babylonVertexBuffer, data) => {
972
+ const normals = new Float32Array(data.length);
973
+ babylonVertexBuffer.forEach(normals.length, (value, index) => {
974
+ normals[index] = data[index] + value;
975
+ });
976
+ babylonMorphTarget.setNormals(normals);
977
+ });
978
+ loadAttribute("TANGENT", VertexBuffer.TangentKind, (babylonVertexBuffer, data) => {
979
+ const tangents = new Float32Array((data.length / 3) * 4);
980
+ let dataIndex = 0;
981
+ babylonVertexBuffer.forEach((data.length / 3) * 4, (value, index) => {
982
+ // Tangent data for morph targets is stored as xyz delta.
983
+ // The vertexData.tangent is stored as xyzw.
984
+ // So we need to skip every fourth vertexData.tangent.
985
+ if ((index + 1) % 4 !== 0) {
986
+ tangents[dataIndex] = data[dataIndex] + value;
987
+ dataIndex++;
988
+ }
989
+ });
990
+ babylonMorphTarget.setTangents(tangents);
991
+ });
992
+ loadAttribute("TEXCOORD_0", VertexBuffer.UVKind, (babylonVertexBuffer, data) => {
993
+ const uvs = new Float32Array(data.length);
994
+ babylonVertexBuffer.forEach(data.length, (value, index) => {
995
+ uvs[index] = data[index] + value;
996
+ });
997
+ babylonMorphTarget.setUVs(uvs);
998
+ });
999
+ loadAttribute("TEXCOORD_1", VertexBuffer.UV2Kind, (babylonVertexBuffer, data) => {
1000
+ const uvs = new Float32Array(data.length);
1001
+ babylonVertexBuffer.forEach(data.length, (value, index) => {
1002
+ uvs[index] = data[index] + value;
1003
+ });
1004
+ babylonMorphTarget.setUV2s(uvs);
1005
+ });
1006
+ loadAttribute("COLOR_0", VertexBuffer.ColorKind, (babylonVertexBuffer, data) => {
1007
+ let colors = null;
1008
+ const componentSize = babylonVertexBuffer.getSize();
1009
+ if (componentSize === 3) {
1010
+ colors = new Float32Array((data.length / 3) * 4);
1011
+ babylonVertexBuffer.forEach(data.length, (value, index) => {
1012
+ const pixid = Math.floor(index / 3);
1013
+ const channel = index % 3;
1014
+ colors[4 * pixid + channel] = data[3 * pixid + channel] + value;
1015
+ });
1016
+ for (let i = 0; i < data.length / 3; ++i) {
1017
+ colors[4 * i + 3] = 1;
1018
+ }
1019
+ }
1020
+ else if (componentSize === 4) {
1021
+ colors = new Float32Array(data.length);
1022
+ babylonVertexBuffer.forEach(data.length, (value, index) => {
1023
+ colors[index] = data[index] + value;
1024
+ });
1025
+ }
1026
+ else {
1027
+ throw new Error(`${context}: Invalid number of components (${componentSize}) for COLOR_0 attribute`);
1028
+ }
1029
+ babylonMorphTarget.setColors(colors);
1030
+ });
1031
+ return await Promise.all(promises).then(() => { });
1032
+ }
1033
+ static _LoadTransform(node, babylonNode) {
1034
+ // Ignore the TRS of skinned nodes.
1035
+ // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
1036
+ if (node.skin != undefined) {
1037
+ return;
1038
+ }
1039
+ let position = Vector3.Zero();
1040
+ let rotation = Quaternion.Identity();
1041
+ let scaling = Vector3.One();
1042
+ if (node.matrix) {
1043
+ const matrix = Matrix.FromArray(node.matrix);
1044
+ matrix.decompose(scaling, rotation, position);
1045
+ }
1046
+ else {
1047
+ if (node.translation) {
1048
+ position = Vector3.FromArray(node.translation);
1049
+ }
1050
+ if (node.rotation) {
1051
+ rotation = Quaternion.FromArray(node.rotation);
1052
+ }
1053
+ if (node.scale) {
1054
+ scaling = Vector3.FromArray(node.scale);
1055
+ }
1056
+ }
1057
+ babylonNode.position = position;
1058
+ babylonNode.rotationQuaternion = rotation;
1059
+ babylonNode.scaling = scaling;
1060
+ }
1061
+ // eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
1062
+ _loadSkinAsync(context, node, skin, assign) {
1063
+ if (!this._parent.loadSkins) {
1064
+ return Promise.resolve();
1065
+ }
1066
+ const extensionPromise = this._extensionsLoadSkinAsync(context, node, skin);
1067
+ if (extensionPromise) {
1068
+ return extensionPromise;
1069
+ }
1070
+ if (skin._data) {
1071
+ assign(skin._data.babylonSkeleton);
1072
+ return skin._data.promise;
1073
+ }
1074
+ const skeletonId = `skeleton${skin.index}`;
1075
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1076
+ const babylonSkeleton = new Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
1077
+ babylonSkeleton._parentContainer = this._assetContainer;
1078
+ this._babylonScene._blockEntityCollection = false;
1079
+ this._loadBones(context, skin, babylonSkeleton);
1080
+ const promise = this._loadSkinInverseBindMatricesDataAsync(context, skin).then((inverseBindMatricesData) => {
1081
+ this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
1082
+ });
1083
+ skin._data = {
1084
+ babylonSkeleton: babylonSkeleton,
1085
+ promise: promise,
1086
+ };
1087
+ assign(babylonSkeleton);
1088
+ return promise;
1089
+ }
1090
+ _loadBones(context, skin, babylonSkeleton) {
1091
+ if (skin.skeleton == undefined || this._parent.alwaysComputeSkeletonRootNode) {
1092
+ const rootNode = this._findSkeletonRootNode(`${context}/joints`, skin.joints);
1093
+ if (rootNode) {
1094
+ if (skin.skeleton === undefined) {
1095
+ skin.skeleton = rootNode.index;
1096
+ }
1097
+ else {
1098
+ const isParent = (a, b) => {
1099
+ for (; b.parent; b = b.parent) {
1100
+ if (b.parent === a) {
1101
+ return true;
1102
+ }
1103
+ }
1104
+ return false;
1105
+ };
1106
+ const skeletonNode = ArrayItem.Get(`${context}/skeleton`, this._gltf.nodes, skin.skeleton);
1107
+ if (skeletonNode !== rootNode && !isParent(skeletonNode, rootNode)) {
1108
+ Logger.Warn(`${context}/skeleton: Overriding with nearest common ancestor as skeleton node is not a common root`);
1109
+ skin.skeleton = rootNode.index;
1110
+ }
1111
+ }
1112
+ }
1113
+ else {
1114
+ Logger.Warn(`${context}: Failed to find common root`);
1115
+ }
1116
+ }
1117
+ const babylonBones = {};
1118
+ for (const index of skin.joints) {
1119
+ const node = ArrayItem.Get(`${context}/joints/${index}`, this._gltf.nodes, index);
1120
+ this._loadBone(node, skin, babylonSkeleton, babylonBones);
1121
+ }
1122
+ }
1123
+ _findSkeletonRootNode(context, joints) {
1124
+ if (joints.length === 0) {
1125
+ return null;
1126
+ }
1127
+ const paths = {};
1128
+ for (const index of joints) {
1129
+ const path = [];
1130
+ let node = ArrayItem.Get(`${context}/${index}`, this._gltf.nodes, index);
1131
+ while (node.index !== -1) {
1132
+ path.unshift(node);
1133
+ node = node.parent;
1134
+ }
1135
+ paths[index] = path;
1136
+ }
1137
+ let rootNode = null;
1138
+ for (let i = 0;; ++i) {
1139
+ let path = paths[joints[0]];
1140
+ if (i >= path.length) {
1141
+ return rootNode;
1142
+ }
1143
+ const node = path[i];
1144
+ for (let j = 1; j < joints.length; ++j) {
1145
+ path = paths[joints[j]];
1146
+ if (i >= path.length || node !== path[i]) {
1147
+ return rootNode;
1148
+ }
1149
+ }
1150
+ rootNode = node;
1151
+ }
1152
+ }
1153
+ _loadBone(node, skin, babylonSkeleton, babylonBones) {
1154
+ node._isJoint = true;
1155
+ let babylonBone = babylonBones[node.index];
1156
+ if (babylonBone) {
1157
+ return babylonBone;
1158
+ }
1159
+ let parentBabylonBone = null;
1160
+ if (node.index !== skin.skeleton) {
1161
+ if (node.parent && node.parent.index !== -1) {
1162
+ parentBabylonBone = this._loadBone(node.parent, skin, babylonSkeleton, babylonBones);
1163
+ }
1164
+ else if (skin.skeleton !== undefined) {
1165
+ Logger.Warn(`/skins/${skin.index}/skeleton: Skeleton node is not a common root`);
1166
+ }
1167
+ }
1168
+ const boneIndex = skin.joints.indexOf(node.index);
1169
+ babylonBone = new Bone(node.name || `joint${node.index}`, babylonSkeleton, parentBabylonBone, this._getNodeMatrix(node), null, null, boneIndex);
1170
+ babylonBones[node.index] = babylonBone;
1171
+ // Wait until the scene is loaded to ensure the transform nodes are loaded.
1172
+ this._postSceneLoadActions.push(() => {
1173
+ // Link the Babylon bone with the corresponding Babylon transform node.
1174
+ // A glTF joint is a pointer to a glTF node in the glTF node hierarchy similar to Unity3D.
1175
+ babylonBone.linkTransformNode(node._babylonTransformNode);
1176
+ });
1177
+ return babylonBone;
1178
+ }
1179
+ _loadSkinInverseBindMatricesDataAsync(context, skin) {
1180
+ if (skin.inverseBindMatrices == undefined) {
1181
+ return Promise.resolve(null);
1182
+ }
1183
+ const accessor = ArrayItem.Get(`${context}/inverseBindMatrices`, this._gltf.accessors, skin.inverseBindMatrices);
1184
+ return this._loadFloatAccessorAsync(`/accessors/${accessor.index}`, accessor);
1185
+ }
1186
+ _updateBoneMatrices(babylonSkeleton, inverseBindMatricesData) {
1187
+ for (const babylonBone of babylonSkeleton.bones) {
1188
+ const baseMatrix = Matrix.Identity();
1189
+ const boneIndex = babylonBone._index;
1190
+ if (inverseBindMatricesData && boneIndex !== -1) {
1191
+ Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
1192
+ baseMatrix.invertToRef(baseMatrix);
1193
+ }
1194
+ const babylonParentBone = babylonBone.getParent();
1195
+ if (babylonParentBone) {
1196
+ baseMatrix.multiplyToRef(babylonParentBone.getAbsoluteInverseBindMatrix(), baseMatrix);
1197
+ }
1198
+ babylonBone.updateMatrix(baseMatrix, false, false);
1199
+ babylonBone._updateAbsoluteBindMatrices(undefined, false);
1200
+ }
1201
+ }
1202
+ _getNodeMatrix(node) {
1203
+ return node.matrix
1204
+ ? Matrix.FromArray(node.matrix)
1205
+ : Matrix.Compose(node.scale ? Vector3.FromArray(node.scale) : Vector3.One(), node.rotation ? Quaternion.FromArray(node.rotation) : Quaternion.Identity(), node.translation ? Vector3.FromArray(node.translation) : Vector3.Zero());
1206
+ }
1207
+ /**
1208
+ * Loads a glTF camera.
1209
+ * @param context The context when loading the asset
1210
+ * @param camera The glTF camera property
1211
+ * @param assign A function called synchronously after parsing the glTF properties
1212
+ * @returns A promise that resolves with the loaded Babylon camera when the load is complete
1213
+ */
1214
+ loadCameraAsync(context, camera, assign = () => { }) {
1215
+ const extensionPromise = this._extensionsLoadCameraAsync(context, camera, assign);
1216
+ if (extensionPromise) {
1217
+ return extensionPromise;
1218
+ }
1219
+ const promises = new Array();
1220
+ this.logOpen(`${context} ${camera.name || ""}`);
1221
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1222
+ const babylonCamera = new FreeCamera(camera.name || `camera${camera.index}`, Vector3.Zero(), this._babylonScene, false);
1223
+ babylonCamera._parentContainer = this._assetContainer;
1224
+ this._babylonScene._blockEntityCollection = false;
1225
+ camera._babylonCamera = babylonCamera;
1226
+ // glTF cameras look towards the local -Z axis.
1227
+ babylonCamera.setTarget(new Vector3(0, 0, -1));
1228
+ switch (camera.type) {
1229
+ case "perspective" /* CameraType.PERSPECTIVE */: {
1230
+ const perspective = camera.perspective;
1231
+ if (!perspective) {
1232
+ throw new Error(`${context}: Camera perspective properties are missing`);
1233
+ }
1234
+ babylonCamera.fov = perspective.yfov;
1235
+ babylonCamera.minZ = perspective.znear;
1236
+ babylonCamera.maxZ = perspective.zfar || 0;
1237
+ break;
1238
+ }
1239
+ case "orthographic" /* CameraType.ORTHOGRAPHIC */: {
1240
+ if (!camera.orthographic) {
1241
+ throw new Error(`${context}: Camera orthographic properties are missing`);
1242
+ }
1243
+ babylonCamera.mode = Camera.ORTHOGRAPHIC_CAMERA;
1244
+ babylonCamera.orthoLeft = -camera.orthographic.xmag;
1245
+ babylonCamera.orthoRight = camera.orthographic.xmag;
1246
+ babylonCamera.orthoBottom = -camera.orthographic.ymag;
1247
+ babylonCamera.orthoTop = camera.orthographic.ymag;
1248
+ babylonCamera.minZ = camera.orthographic.znear;
1249
+ babylonCamera.maxZ = camera.orthographic.zfar;
1250
+ break;
1251
+ }
1252
+ default: {
1253
+ throw new Error(`${context}: Invalid camera type (${camera.type})`);
1254
+ }
1255
+ }
1256
+ GLTFLoader.AddPointerMetadata(babylonCamera, context);
1257
+ this._parent.onCameraLoadedObservable.notifyObservers(babylonCamera);
1258
+ assign(babylonCamera);
1259
+ this.logClose();
1260
+ return Promise.all(promises).then(() => {
1261
+ return babylonCamera;
1262
+ });
1263
+ }
1264
+ _loadAnimationsAsync() {
1265
+ const animations = this._gltf.animations;
1266
+ if (!animations) {
1267
+ return Promise.resolve();
1268
+ }
1269
+ const promises = new Array();
1270
+ for (let index = 0; index < animations.length; index++) {
1271
+ const animation = animations[index];
1272
+ promises.push(this.loadAnimationAsync(`/animations/${animation.index}`, animation).then((animationGroup) => {
1273
+ // Delete the animation group if it ended up not having any animations in it.
1274
+ if (animationGroup.targetedAnimations.length === 0) {
1275
+ animationGroup.dispose();
1276
+ }
1277
+ }));
1278
+ }
1279
+ return Promise.all(promises).then(() => { });
1280
+ }
1281
+ /**
1282
+ * Loads a glTF animation.
1283
+ * @param context The context when loading the asset
1284
+ * @param animation The glTF animation property
1285
+ * @returns A promise that resolves with the loaded Babylon animation group when the load is complete
1286
+ */
1287
+ loadAnimationAsync(context, animation) {
1288
+ const promise = this._extensionsLoadAnimationAsync(context, animation);
1289
+ if (promise) {
1290
+ return promise;
1291
+ }
1292
+ // eslint-disable-next-line @typescript-eslint/naming-convention
1293
+ return import("@babylonjs/core/Animations/animationGroup.js").then(({ AnimationGroup }) => {
1294
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1295
+ const babylonAnimationGroup = new AnimationGroup(animation.name || `animation${animation.index}`, this._babylonScene);
1296
+ babylonAnimationGroup._parentContainer = this._assetContainer;
1297
+ this._babylonScene._blockEntityCollection = false;
1298
+ animation._babylonAnimationGroup = babylonAnimationGroup;
1299
+ const promises = new Array();
1300
+ ArrayItem.Assign(animation.channels);
1301
+ ArrayItem.Assign(animation.samplers);
1302
+ for (const channel of animation.channels) {
1303
+ promises.push(this._loadAnimationChannelAsync(`${context}/channels/${channel.index}`, context, animation, channel, (babylonTarget, babylonAnimation) => {
1304
+ babylonTarget.animations = babylonTarget.animations || [];
1305
+ babylonTarget.animations.push(babylonAnimation);
1306
+ babylonAnimationGroup.addTargetedAnimation(babylonAnimation, babylonTarget);
1307
+ }));
1308
+ }
1309
+ return Promise.all(promises).then(() => {
1310
+ babylonAnimationGroup.normalize(0);
1311
+ return babylonAnimationGroup;
1312
+ });
1313
+ });
1314
+ }
1315
+ /**
1316
+ * @hidden
1317
+ * Loads a glTF animation channel.
1318
+ * @param context The context when loading the asset
1319
+ * @param animationContext The context of the animation when loading the asset
1320
+ * @param animation The glTF animation property
1321
+ * @param channel The glTF animation channel property
1322
+ * @param onLoad Called for each animation loaded
1323
+ * @returns A void promise that resolves when the load is complete
1324
+ */
1325
+ async _loadAnimationChannelAsync(context, animationContext, animation, channel, onLoad) {
1326
+ const promise = this._extensionsLoadAnimationChannelAsync(context, animationContext, animation, channel, onLoad);
1327
+ if (promise) {
1328
+ return await promise;
1329
+ }
1330
+ if (channel.target.node == undefined) {
1331
+ return await Promise.resolve();
1332
+ }
1333
+ const targetNode = ArrayItem.Get(`${context}/target/node`, this._gltf.nodes, channel.target.node);
1334
+ const channelTargetPath = channel.target.path;
1335
+ const pathIsWeights = channelTargetPath === "weights" /* AnimationChannelTargetPath.WEIGHTS */;
1336
+ // Ignore animations that have no animation targets.
1337
+ if ((pathIsWeights && !targetNode._numMorphTargets) || (!pathIsWeights && !targetNode._babylonTransformNode)) {
1338
+ return await Promise.resolve();
1339
+ }
1340
+ // Don't load node animations if disabled.
1341
+ if (!this._parent.loadNodeAnimations && !pathIsWeights && !targetNode._isJoint) {
1342
+ return await Promise.resolve();
1343
+ }
1344
+ // async-load the animation sampler to provide the interpolation of the channelTargetPath
1345
+ await import("./glTFLoaderAnimation.js");
1346
+ let properties;
1347
+ switch (channelTargetPath) {
1348
+ case "translation" /* AnimationChannelTargetPath.TRANSLATION */: {
1349
+ properties = GetMappingForKey("/nodes/{}/translation")?.interpolation;
1350
+ break;
1351
+ }
1352
+ case "rotation" /* AnimationChannelTargetPath.ROTATION */: {
1353
+ properties = GetMappingForKey("/nodes/{}/rotation")?.interpolation;
1354
+ break;
1355
+ }
1356
+ case "scale" /* AnimationChannelTargetPath.SCALE */: {
1357
+ properties = GetMappingForKey("/nodes/{}/scale")?.interpolation;
1358
+ break;
1359
+ }
1360
+ case "weights" /* AnimationChannelTargetPath.WEIGHTS */: {
1361
+ properties = GetMappingForKey("/nodes/{}/weights")?.interpolation;
1362
+ break;
1363
+ }
1364
+ default: {
1365
+ throw new Error(`${context}/target/path: Invalid value (${channel.target.path})`);
1366
+ }
1367
+ }
1368
+ // stay safe
1369
+ if (!properties) {
1370
+ throw new Error(`${context}/target/path: Could not find interpolation properties for target path (${channel.target.path})`);
1371
+ }
1372
+ const targetInfo = {
1373
+ object: targetNode,
1374
+ info: properties,
1375
+ };
1376
+ return await this._loadAnimationChannelFromTargetInfoAsync(context, animationContext, animation, channel, targetInfo, onLoad);
1377
+ }
1378
+ /**
1379
+ * @hidden
1380
+ * Loads a glTF animation channel.
1381
+ * @param context The context when loading the asset
1382
+ * @param animationContext The context of the animation when loading the asset
1383
+ * @param animation The glTF animation property
1384
+ * @param channel The glTF animation channel property
1385
+ * @param targetInfo The glTF target and properties
1386
+ * @param onLoad Called for each animation loaded
1387
+ * @returns A void promise that resolves when the load is complete
1388
+ */
1389
+ _loadAnimationChannelFromTargetInfoAsync(context, animationContext, animation, channel, targetInfo, onLoad) {
1390
+ const fps = this.parent.targetFps;
1391
+ const invfps = 1 / fps;
1392
+ const sampler = ArrayItem.Get(`${context}/sampler`, animation.samplers, channel.sampler);
1393
+ return this._loadAnimationSamplerAsync(`${animationContext}/samplers/${channel.sampler}`, sampler).then((data) => {
1394
+ let numAnimations = 0;
1395
+ const target = targetInfo.object;
1396
+ const propertyInfos = targetInfo.info;
1397
+ // Extract the corresponding values from the read value.
1398
+ // GLTF values may be dispatched to several Babylon properties.
1399
+ // For example, baseColorFactor [`r`, `g`, `b`, `a`] is dispatched to
1400
+ // - albedoColor as Color3(`r`, `g`, `b`)
1401
+ // - alpha as `a`
1402
+ for (const propertyInfo of propertyInfos) {
1403
+ const stride = propertyInfo.getStride(target);
1404
+ const input = data.input;
1405
+ const output = data.output;
1406
+ const keys = new Array(input.length);
1407
+ let outputOffset = 0;
1408
+ switch (data.interpolation) {
1409
+ case "STEP" /* AnimationSamplerInterpolation.STEP */: {
1410
+ for (let index = 0; index < input.length; index++) {
1411
+ const value = propertyInfo.getValue(target, output, outputOffset, 1);
1412
+ outputOffset += stride;
1413
+ keys[index] = {
1414
+ frame: input[index] * fps,
1415
+ value: value,
1416
+ interpolation: 1 /* AnimationKeyInterpolation.STEP */,
1417
+ };
1418
+ }
1419
+ break;
1420
+ }
1421
+ case "CUBICSPLINE" /* AnimationSamplerInterpolation.CUBICSPLINE */: {
1422
+ for (let index = 0; index < input.length; index++) {
1423
+ const inTangent = propertyInfo.getValue(target, output, outputOffset, invfps);
1424
+ outputOffset += stride;
1425
+ const value = propertyInfo.getValue(target, output, outputOffset, 1);
1426
+ outputOffset += stride;
1427
+ const outTangent = propertyInfo.getValue(target, output, outputOffset, invfps);
1428
+ outputOffset += stride;
1429
+ keys[index] = {
1430
+ frame: input[index] * fps,
1431
+ inTangent: inTangent,
1432
+ value: value,
1433
+ outTangent: outTangent,
1434
+ };
1435
+ }
1436
+ break;
1437
+ }
1438
+ case "LINEAR" /* AnimationSamplerInterpolation.LINEAR */: {
1439
+ for (let index = 0; index < input.length; index++) {
1440
+ const value = propertyInfo.getValue(target, output, outputOffset, 1);
1441
+ outputOffset += stride;
1442
+ keys[index] = {
1443
+ frame: input[index] * fps,
1444
+ value: value,
1445
+ };
1446
+ }
1447
+ break;
1448
+ }
1449
+ }
1450
+ if (outputOffset > 0) {
1451
+ const name = `${animation.name || `animation${animation.index}`}_channel${channel.index}_${numAnimations}`;
1452
+ const babylonAnimations = propertyInfo.buildAnimations(target, name, fps, keys);
1453
+ for (const babylonAnimation of babylonAnimations) {
1454
+ numAnimations++;
1455
+ onLoad(babylonAnimation.babylonAnimatable, babylonAnimation.babylonAnimation);
1456
+ }
1457
+ }
1458
+ }
1459
+ });
1460
+ }
1461
+ _loadAnimationSamplerAsync(context, sampler) {
1462
+ if (sampler._data) {
1463
+ return sampler._data;
1464
+ }
1465
+ const interpolation = sampler.interpolation || "LINEAR" /* AnimationSamplerInterpolation.LINEAR */;
1466
+ switch (interpolation) {
1467
+ case "STEP" /* AnimationSamplerInterpolation.STEP */:
1468
+ case "LINEAR" /* AnimationSamplerInterpolation.LINEAR */:
1469
+ case "CUBICSPLINE" /* AnimationSamplerInterpolation.CUBICSPLINE */: {
1470
+ break;
1471
+ }
1472
+ default: {
1473
+ throw new Error(`${context}/interpolation: Invalid value (${sampler.interpolation})`);
1474
+ }
1475
+ }
1476
+ const inputAccessor = ArrayItem.Get(`${context}/input`, this._gltf.accessors, sampler.input);
1477
+ const outputAccessor = ArrayItem.Get(`${context}/output`, this._gltf.accessors, sampler.output);
1478
+ sampler._data = Promise.all([
1479
+ this._loadFloatAccessorAsync(`/accessors/${inputAccessor.index}`, inputAccessor),
1480
+ this._loadFloatAccessorAsync(`/accessors/${outputAccessor.index}`, outputAccessor),
1481
+ ]).then(([inputData, outputData]) => {
1482
+ return {
1483
+ input: inputData,
1484
+ interpolation: interpolation,
1485
+ output: outputData,
1486
+ };
1487
+ });
1488
+ return sampler._data;
1489
+ }
1490
+ /**
1491
+ * Loads a glTF buffer.
1492
+ * @param context The context when loading the asset
1493
+ * @param buffer The glTF buffer property
1494
+ * @param byteOffset The byte offset to use
1495
+ * @param byteLength The byte length to use
1496
+ * @returns A promise that resolves with the loaded data when the load is complete
1497
+ */
1498
+ loadBufferAsync(context, buffer, byteOffset, byteLength) {
1499
+ const extensionPromise = this._extensionsLoadBufferAsync(context, buffer, byteOffset, byteLength);
1500
+ if (extensionPromise) {
1501
+ return extensionPromise;
1502
+ }
1503
+ if (!buffer._data) {
1504
+ if (buffer.uri) {
1505
+ buffer._data = this.loadUriAsync(`${context}/uri`, buffer, buffer.uri);
1506
+ }
1507
+ else {
1508
+ if (!this._bin) {
1509
+ throw new Error(`${context}: Uri is missing or the binary glTF is missing its binary chunk`);
1510
+ }
1511
+ buffer._data = this._bin.readAsync(0, buffer.byteLength);
1512
+ }
1513
+ }
1514
+ return buffer._data.then((data) => {
1515
+ try {
1516
+ return new Uint8Array(data.buffer, data.byteOffset + byteOffset, byteLength);
1517
+ }
1518
+ catch (e) {
1519
+ throw new Error(`${context}: ${e.message}`);
1520
+ }
1521
+ });
1522
+ }
1523
+ /**
1524
+ * Loads a glTF buffer view.
1525
+ * @param context The context when loading the asset
1526
+ * @param bufferView The glTF buffer view property
1527
+ * @returns A promise that resolves with the loaded data when the load is complete
1528
+ */
1529
+ loadBufferViewAsync(context, bufferView) {
1530
+ const extensionPromise = this._extensionsLoadBufferViewAsync(context, bufferView);
1531
+ if (extensionPromise) {
1532
+ return extensionPromise;
1533
+ }
1534
+ if (bufferView._data) {
1535
+ return bufferView._data;
1536
+ }
1537
+ const buffer = ArrayItem.Get(`${context}/buffer`, this._gltf.buffers, bufferView.buffer);
1538
+ bufferView._data = this.loadBufferAsync(`/buffers/${buffer.index}`, buffer, bufferView.byteOffset || 0, bufferView.byteLength);
1539
+ return bufferView._data;
1540
+ }
1541
+ _loadAccessorAsync(context, accessor, constructor) {
1542
+ if (accessor._data) {
1543
+ return accessor._data;
1544
+ }
1545
+ const numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
1546
+ const byteStride = numComponents * VertexBuffer.GetTypeByteLength(accessor.componentType);
1547
+ const length = numComponents * accessor.count;
1548
+ if (accessor.bufferView == undefined) {
1549
+ accessor._data = Promise.resolve(new constructor(length));
1550
+ }
1551
+ else {
1552
+ const bufferView = ArrayItem.Get(`${context}/bufferView`, this._gltf.bufferViews, accessor.bufferView);
1553
+ accessor._data = this.loadBufferViewAsync(`/bufferViews/${bufferView.index}`, bufferView).then((data) => {
1554
+ if (accessor.componentType === 5126 /* AccessorComponentType.FLOAT */ && !accessor.normalized && (!bufferView.byteStride || bufferView.byteStride === byteStride)) {
1555
+ return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
1556
+ }
1557
+ else {
1558
+ const typedArray = new constructor(length);
1559
+ VertexBuffer.ForEach(data, accessor.byteOffset || 0, bufferView.byteStride || byteStride, numComponents, accessor.componentType, typedArray.length, accessor.normalized || false, (value, index) => {
1560
+ typedArray[index] = value;
1561
+ });
1562
+ return typedArray;
1563
+ }
1564
+ });
1565
+ }
1566
+ if (accessor.sparse) {
1567
+ const sparse = accessor.sparse;
1568
+ accessor._data = accessor._data.then((data) => {
1569
+ const typedArray = data;
1570
+ const indicesBufferView = ArrayItem.Get(`${context}/sparse/indices/bufferView`, this._gltf.bufferViews, sparse.indices.bufferView);
1571
+ const valuesBufferView = ArrayItem.Get(`${context}/sparse/values/bufferView`, this._gltf.bufferViews, sparse.values.bufferView);
1572
+ return Promise.all([
1573
+ this.loadBufferViewAsync(`/bufferViews/${indicesBufferView.index}`, indicesBufferView),
1574
+ this.loadBufferViewAsync(`/bufferViews/${valuesBufferView.index}`, valuesBufferView),
1575
+ ]).then(([indicesData, valuesData]) => {
1576
+ const indices = GLTFLoader._GetTypedArray(`${context}/sparse/indices`, sparse.indices.componentType, indicesData, sparse.indices.byteOffset, sparse.count);
1577
+ const sparseLength = numComponents * sparse.count;
1578
+ let values;
1579
+ if (accessor.componentType === 5126 /* AccessorComponentType.FLOAT */ && !accessor.normalized) {
1580
+ values = GLTFLoader._GetTypedArray(`${context}/sparse/values`, accessor.componentType, valuesData, sparse.values.byteOffset, sparseLength);
1581
+ }
1582
+ else {
1583
+ const sparseData = GLTFLoader._GetTypedArray(`${context}/sparse/values`, accessor.componentType, valuesData, sparse.values.byteOffset, sparseLength);
1584
+ values = new constructor(sparseLength);
1585
+ VertexBuffer.ForEach(sparseData, 0, byteStride, numComponents, accessor.componentType, values.length, accessor.normalized || false, (value, index) => {
1586
+ values[index] = value;
1587
+ });
1588
+ }
1589
+ let valuesIndex = 0;
1590
+ for (let indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
1591
+ let dataIndex = indices[indicesIndex] * numComponents;
1592
+ for (let componentIndex = 0; componentIndex < numComponents; componentIndex++) {
1593
+ typedArray[dataIndex++] = values[valuesIndex++];
1594
+ }
1595
+ }
1596
+ return typedArray;
1597
+ });
1598
+ });
1599
+ }
1600
+ return accessor._data;
1601
+ }
1602
+ /**
1603
+ * @internal
1604
+ */
1605
+ _loadFloatAccessorAsync(context, accessor) {
1606
+ return this._loadAccessorAsync(context, accessor, Float32Array);
1607
+ }
1608
+ /**
1609
+ * @internal
1610
+ */
1611
+ _loadIndicesAccessorAsync(context, accessor) {
1612
+ if (accessor.type !== "SCALAR" /* AccessorType.SCALAR */) {
1613
+ throw new Error(`${context}/type: Invalid value ${accessor.type}`);
1614
+ }
1615
+ if (accessor.componentType !== 5121 /* AccessorComponentType.UNSIGNED_BYTE */ &&
1616
+ accessor.componentType !== 5123 /* AccessorComponentType.UNSIGNED_SHORT */ &&
1617
+ accessor.componentType !== 5125 /* AccessorComponentType.UNSIGNED_INT */) {
1618
+ throw new Error(`${context}/componentType: Invalid value ${accessor.componentType}`);
1619
+ }
1620
+ if (accessor._data) {
1621
+ return accessor._data;
1622
+ }
1623
+ if (accessor.sparse) {
1624
+ const constructor = GLTFLoader._GetTypedArrayConstructor(`${context}/componentType`, accessor.componentType);
1625
+ accessor._data = this._loadAccessorAsync(context, accessor, constructor);
1626
+ }
1627
+ else {
1628
+ const bufferView = ArrayItem.Get(`${context}/bufferView`, this._gltf.bufferViews, accessor.bufferView);
1629
+ accessor._data = this.loadBufferViewAsync(`/bufferViews/${bufferView.index}`, bufferView).then((data) => {
1630
+ return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
1631
+ });
1632
+ }
1633
+ return accessor._data;
1634
+ }
1635
+ /**
1636
+ * @internal
1637
+ */
1638
+ _loadVertexBufferViewAsync(bufferView) {
1639
+ if (bufferView._babylonBuffer) {
1640
+ return bufferView._babylonBuffer;
1641
+ }
1642
+ const engine = this._babylonScene.getEngine();
1643
+ bufferView._babylonBuffer = this.loadBufferViewAsync(`/bufferViews/${bufferView.index}`, bufferView).then((data) => {
1644
+ return new Buffer(engine, data, false);
1645
+ });
1646
+ return bufferView._babylonBuffer;
1647
+ }
1648
+ /**
1649
+ * @internal
1650
+ */
1651
+ _loadVertexAccessorAsync(context, accessor, kind) {
1652
+ if (accessor._babylonVertexBuffer?.[kind]) {
1653
+ return accessor._babylonVertexBuffer[kind];
1654
+ }
1655
+ if (!accessor._babylonVertexBuffer) {
1656
+ accessor._babylonVertexBuffer = {};
1657
+ }
1658
+ const engine = this._babylonScene.getEngine();
1659
+ if (accessor.sparse || accessor.bufferView == undefined) {
1660
+ accessor._babylonVertexBuffer[kind] = this._loadFloatAccessorAsync(context, accessor).then((data) => {
1661
+ return new VertexBuffer(engine, data, kind, false);
1662
+ });
1663
+ }
1664
+ else {
1665
+ const bufferView = ArrayItem.Get(`${context}/bufferView`, this._gltf.bufferViews, accessor.bufferView);
1666
+ accessor._babylonVertexBuffer[kind] = this._loadVertexBufferViewAsync(bufferView).then((babylonBuffer) => {
1667
+ const numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
1668
+ return new VertexBuffer(engine, babylonBuffer, kind, false, undefined, bufferView.byteStride, undefined, accessor.byteOffset, numComponents, accessor.componentType, accessor.normalized, true, undefined, true);
1669
+ });
1670
+ }
1671
+ return accessor._babylonVertexBuffer[kind];
1672
+ }
1673
+ _loadMaterialMetallicRoughnessPropertiesAsync(context, properties, babylonMaterial) {
1674
+ if (!(babylonMaterial instanceof PBRMaterial)) {
1675
+ throw new Error(`${context}: Material type not supported`);
1676
+ }
1677
+ const promises = new Array();
1678
+ if (properties) {
1679
+ if (properties.baseColorFactor) {
1680
+ babylonMaterial.albedoColor = Color3.FromArray(properties.baseColorFactor);
1681
+ babylonMaterial.alpha = properties.baseColorFactor[3];
1682
+ }
1683
+ else {
1684
+ babylonMaterial.albedoColor = Color3.White();
1685
+ }
1686
+ babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
1687
+ babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
1688
+ if (properties.baseColorTexture) {
1689
+ promises.push(this.loadTextureInfoAsync(`${context}/baseColorTexture`, properties.baseColorTexture, (texture) => {
1690
+ texture.name = `${babylonMaterial.name} (Base Color)`;
1691
+ babylonMaterial.albedoTexture = texture;
1692
+ }));
1693
+ }
1694
+ if (properties.metallicRoughnessTexture) {
1695
+ properties.metallicRoughnessTexture.nonColorData = true;
1696
+ promises.push(this.loadTextureInfoAsync(`${context}/metallicRoughnessTexture`, properties.metallicRoughnessTexture, (texture) => {
1697
+ texture.name = `${babylonMaterial.name} (Metallic Roughness)`;
1698
+ babylonMaterial.metallicTexture = texture;
1699
+ }));
1700
+ babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
1701
+ babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
1702
+ babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
1703
+ }
1704
+ }
1705
+ return Promise.all(promises).then(() => { });
1706
+ }
1707
+ /**
1708
+ * @internal
1709
+ */
1710
+ _loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign = () => { }) {
1711
+ const extensionPromise = this._extensionsLoadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign);
1712
+ if (extensionPromise) {
1713
+ return extensionPromise;
1714
+ }
1715
+ material._data = material._data || {};
1716
+ let babylonData = material._data[babylonDrawMode];
1717
+ if (!babylonData) {
1718
+ this.logOpen(`${context} ${material.name || ""}`);
1719
+ const babylonMaterial = this.createMaterial(context, material, babylonDrawMode);
1720
+ babylonData = {
1721
+ babylonMaterial: babylonMaterial,
1722
+ babylonMeshes: [],
1723
+ promise: this.loadMaterialPropertiesAsync(context, material, babylonMaterial),
1724
+ };
1725
+ material._data[babylonDrawMode] = babylonData;
1726
+ GLTFLoader.AddPointerMetadata(babylonMaterial, context);
1727
+ this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
1728
+ this.logClose();
1729
+ }
1730
+ if (babylonMesh) {
1731
+ babylonData.babylonMeshes.push(babylonMesh);
1732
+ babylonMesh.onDisposeObservable.addOnce(() => {
1733
+ const index = babylonData.babylonMeshes.indexOf(babylonMesh);
1734
+ if (index !== -1) {
1735
+ babylonData.babylonMeshes.splice(index, 1);
1736
+ }
1737
+ });
1738
+ }
1739
+ assign(babylonData.babylonMaterial);
1740
+ return babylonData.promise.then(() => {
1741
+ return babylonData.babylonMaterial;
1742
+ });
1743
+ }
1744
+ _createDefaultMaterial(name, babylonDrawMode) {
1745
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1746
+ const babylonMaterial = new PBRMaterial(name, this._babylonScene);
1747
+ babylonMaterial._parentContainer = this._assetContainer;
1748
+ this._babylonScene._blockEntityCollection = false;
1749
+ // Moved to mesh so user can change materials on gltf meshes: babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
1750
+ babylonMaterial.fillMode = babylonDrawMode;
1751
+ babylonMaterial.enableSpecularAntiAliasing = true;
1752
+ babylonMaterial.useRadianceOverAlpha = !this._parent.transparencyAsCoverage;
1753
+ babylonMaterial.useSpecularOverAlpha = !this._parent.transparencyAsCoverage;
1754
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
1755
+ babylonMaterial.metallic = 1;
1756
+ babylonMaterial.roughness = 1;
1757
+ return babylonMaterial;
1758
+ }
1759
+ /**
1760
+ * Creates a Babylon material from a glTF material.
1761
+ * @param context The context when loading the asset
1762
+ * @param material The glTF material property
1763
+ * @param babylonDrawMode The draw mode for the Babylon material
1764
+ * @returns The Babylon material
1765
+ */
1766
+ createMaterial(context, material, babylonDrawMode) {
1767
+ const extensionPromise = this._extensionsCreateMaterial(context, material, babylonDrawMode);
1768
+ if (extensionPromise) {
1769
+ return extensionPromise;
1770
+ }
1771
+ const name = material.name || `material${material.index}`;
1772
+ const babylonMaterial = this._createDefaultMaterial(name, babylonDrawMode);
1773
+ return babylonMaterial;
1774
+ }
1775
+ /**
1776
+ * Loads properties from a glTF material into a Babylon material.
1777
+ * @param context The context when loading the asset
1778
+ * @param material The glTF material property
1779
+ * @param babylonMaterial The Babylon material
1780
+ * @returns A promise that resolves when the load is complete
1781
+ */
1782
+ loadMaterialPropertiesAsync(context, material, babylonMaterial) {
1783
+ const extensionPromise = this._extensionsLoadMaterialPropertiesAsync(context, material, babylonMaterial);
1784
+ if (extensionPromise) {
1785
+ return extensionPromise;
1786
+ }
1787
+ const promises = new Array();
1788
+ promises.push(this.loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
1789
+ if (material.pbrMetallicRoughness) {
1790
+ promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(`${context}/pbrMetallicRoughness`, material.pbrMetallicRoughness, babylonMaterial));
1791
+ }
1792
+ this.loadMaterialAlphaProperties(context, material, babylonMaterial);
1793
+ return Promise.all(promises).then(() => { });
1794
+ }
1795
+ /**
1796
+ * Loads the normal, occlusion, and emissive properties from a glTF material into a Babylon material.
1797
+ * @param context The context when loading the asset
1798
+ * @param material The glTF material property
1799
+ * @param babylonMaterial The Babylon material
1800
+ * @returns A promise that resolves when the load is complete
1801
+ */
1802
+ loadMaterialBasePropertiesAsync(context, material, babylonMaterial) {
1803
+ if (!(babylonMaterial instanceof PBRMaterial)) {
1804
+ throw new Error(`${context}: Material type not supported`);
1805
+ }
1806
+ const promises = new Array();
1807
+ babylonMaterial.emissiveColor = material.emissiveFactor ? Color3.FromArray(material.emissiveFactor) : new Color3(0, 0, 0);
1808
+ if (material.doubleSided) {
1809
+ babylonMaterial.backFaceCulling = false;
1810
+ babylonMaterial.twoSidedLighting = true;
1811
+ }
1812
+ if (material.normalTexture) {
1813
+ material.normalTexture.nonColorData = true;
1814
+ promises.push(this.loadTextureInfoAsync(`${context}/normalTexture`, material.normalTexture, (texture) => {
1815
+ texture.name = `${babylonMaterial.name} (Normal)`;
1816
+ babylonMaterial.bumpTexture = texture;
1817
+ }));
1818
+ babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
1819
+ babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
1820
+ if (material.normalTexture.scale != undefined && babylonMaterial.bumpTexture) {
1821
+ babylonMaterial.bumpTexture.level = material.normalTexture.scale;
1822
+ }
1823
+ babylonMaterial.forceIrradianceInFragment = true;
1824
+ }
1825
+ if (material.occlusionTexture) {
1826
+ material.occlusionTexture.nonColorData = true;
1827
+ promises.push(this.loadTextureInfoAsync(`${context}/occlusionTexture`, material.occlusionTexture, (texture) => {
1828
+ texture.name = `${babylonMaterial.name} (Occlusion)`;
1829
+ babylonMaterial.ambientTexture = texture;
1830
+ }));
1831
+ babylonMaterial.useAmbientInGrayScale = true;
1832
+ if (material.occlusionTexture.strength != undefined) {
1833
+ babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
1834
+ }
1835
+ }
1836
+ if (material.emissiveTexture) {
1837
+ promises.push(this.loadTextureInfoAsync(`${context}/emissiveTexture`, material.emissiveTexture, (texture) => {
1838
+ texture.name = `${babylonMaterial.name} (Emissive)`;
1839
+ babylonMaterial.emissiveTexture = texture;
1840
+ }));
1841
+ }
1842
+ return Promise.all(promises).then(() => { });
1843
+ }
1844
+ /**
1845
+ * Loads the alpha properties from a glTF material into a Babylon material.
1846
+ * Must be called after the setting the albedo texture of the Babylon material when the material has an albedo texture.
1847
+ * @param context The context when loading the asset
1848
+ * @param material The glTF material property
1849
+ * @param babylonMaterial The Babylon material
1850
+ */
1851
+ loadMaterialAlphaProperties(context, material, babylonMaterial) {
1852
+ if (!(babylonMaterial instanceof PBRMaterial)) {
1853
+ throw new Error(`${context}: Material type not supported`);
1854
+ }
1855
+ const alphaMode = material.alphaMode || "OPAQUE" /* MaterialAlphaMode.OPAQUE */;
1856
+ switch (alphaMode) {
1857
+ case "OPAQUE" /* MaterialAlphaMode.OPAQUE */: {
1858
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
1859
+ babylonMaterial.alpha = 1.0; // Force alpha to 1.0 for opaque mode.
1860
+ break;
1861
+ }
1862
+ case "MASK" /* MaterialAlphaMode.MASK */: {
1863
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHATEST;
1864
+ babylonMaterial.alphaCutOff = material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff;
1865
+ if (babylonMaterial.albedoTexture) {
1866
+ babylonMaterial.albedoTexture.hasAlpha = true;
1867
+ }
1868
+ break;
1869
+ }
1870
+ case "BLEND" /* MaterialAlphaMode.BLEND */: {
1871
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
1872
+ if (babylonMaterial.albedoTexture) {
1873
+ babylonMaterial.albedoTexture.hasAlpha = true;
1874
+ babylonMaterial.useAlphaFromAlbedoTexture = true;
1875
+ }
1876
+ break;
1877
+ }
1878
+ default: {
1879
+ throw new Error(`${context}/alphaMode: Invalid value (${material.alphaMode})`);
1880
+ }
1881
+ }
1882
+ }
1883
+ /**
1884
+ * Loads a glTF texture info.
1885
+ * @param context The context when loading the asset
1886
+ * @param textureInfo The glTF texture info property
1887
+ * @param assign A function called synchronously after parsing the glTF properties
1888
+ * @returns A promise that resolves with the loaded Babylon texture when the load is complete
1889
+ */
1890
+ loadTextureInfoAsync(context, textureInfo, assign = () => { }) {
1891
+ const extensionPromise = this._extensionsLoadTextureInfoAsync(context, textureInfo, assign);
1892
+ if (extensionPromise) {
1893
+ return extensionPromise;
1894
+ }
1895
+ this.logOpen(`${context}`);
1896
+ if (textureInfo.texCoord >= 6) {
1897
+ throw new Error(`${context}/texCoord: Invalid value (${textureInfo.texCoord})`);
1898
+ }
1899
+ const texture = ArrayItem.Get(`${context}/index`, this._gltf.textures, textureInfo.index);
1900
+ texture._textureInfo = textureInfo;
1901
+ const promise = this._loadTextureAsync(`/textures/${textureInfo.index}`, texture, (babylonTexture) => {
1902
+ babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
1903
+ GLTFLoader.AddPointerMetadata(babylonTexture, context);
1904
+ this._parent.onTextureLoadedObservable.notifyObservers(babylonTexture);
1905
+ assign(babylonTexture);
1906
+ });
1907
+ this.logClose();
1908
+ return promise;
1909
+ }
1910
+ /**
1911
+ * @internal
1912
+ */
1913
+ _loadTextureAsync(context, texture, assign = () => { }) {
1914
+ const extensionPromise = this._extensionsLoadTextureAsync(context, texture, assign);
1915
+ if (extensionPromise) {
1916
+ return extensionPromise;
1917
+ }
1918
+ this.logOpen(`${context} ${texture.name || ""}`);
1919
+ const sampler = texture.sampler == undefined ? GLTFLoader.DefaultSampler : ArrayItem.Get(`${context}/sampler`, this._gltf.samplers, texture.sampler);
1920
+ const image = ArrayItem.Get(`${context}/source`, this._gltf.images, texture.source);
1921
+ const promise = this._createTextureAsync(context, sampler, image, assign, undefined, !texture._textureInfo.nonColorData);
1922
+ this.logClose();
1923
+ return promise;
1924
+ }
1925
+ /**
1926
+ * @internal
1927
+ */
1928
+ _createTextureAsync(context, sampler, image, assign = () => { }, textureLoaderOptions, useSRGBBuffer) {
1929
+ const samplerData = this._loadSampler(`/samplers/${sampler.index}`, sampler);
1930
+ const promises = new Array();
1931
+ const deferred = new Deferred();
1932
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1933
+ const textureCreationOptions = {
1934
+ noMipmap: samplerData.noMipMaps,
1935
+ invertY: false,
1936
+ samplingMode: samplerData.samplingMode,
1937
+ onLoad: () => {
1938
+ if (!this._disposed) {
1939
+ deferred.resolve();
1940
+ }
1941
+ },
1942
+ onError: (message, exception) => {
1943
+ if (!this._disposed) {
1944
+ deferred.reject(new Error(`${context}: ${exception && exception.message ? exception.message : message || "Failed to load texture"}`));
1945
+ }
1946
+ },
1947
+ mimeType: image.mimeType ?? GetMimeType(image.uri ?? ""),
1948
+ loaderOptions: textureLoaderOptions,
1949
+ useSRGBBuffer: !!useSRGBBuffer && this._parent.useSRGBBuffers,
1950
+ };
1951
+ const babylonTexture = new Texture(null, this._babylonScene, textureCreationOptions);
1952
+ babylonTexture._parentContainer = this._assetContainer;
1953
+ this._babylonScene._blockEntityCollection = false;
1954
+ promises.push(deferred.promise);
1955
+ promises.push(this.loadImageAsync(`/images/${image.index}`, image).then((data) => {
1956
+ const name = image.uri || `${this._fileName}#image${image.index}`;
1957
+ const dataUrl = `data:${this._uniqueRootUrl}${name}`;
1958
+ babylonTexture.updateURL(dataUrl, data);
1959
+ // Set the internal texture label.
1960
+ const internalTexture = babylonTexture.getInternalTexture();
1961
+ if (internalTexture) {
1962
+ internalTexture.label = image.name;
1963
+ }
1964
+ }));
1965
+ babylonTexture.wrapU = samplerData.wrapU;
1966
+ babylonTexture.wrapV = samplerData.wrapV;
1967
+ assign(babylonTexture);
1968
+ if (this._parent.useGltfTextureNames) {
1969
+ babylonTexture.name = image.name || image.uri || `image${image.index}`;
1970
+ }
1971
+ return Promise.all(promises).then(() => {
1972
+ return babylonTexture;
1973
+ });
1974
+ }
1975
+ _loadSampler(context, sampler) {
1976
+ if (!sampler._data) {
1977
+ sampler._data = {
1978
+ noMipMaps: sampler.minFilter === 9728 /* TextureMinFilter.NEAREST */ || sampler.minFilter === 9729 /* TextureMinFilter.LINEAR */,
1979
+ samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler),
1980
+ wrapU: GLTFLoader._GetTextureWrapMode(`${context}/wrapS`, sampler.wrapS),
1981
+ wrapV: GLTFLoader._GetTextureWrapMode(`${context}/wrapT`, sampler.wrapT),
1982
+ };
1983
+ }
1984
+ return sampler._data;
1985
+ }
1986
+ /**
1987
+ * Loads a glTF image.
1988
+ * @param context The context when loading the asset
1989
+ * @param image The glTF image property
1990
+ * @returns A promise that resolves with the loaded data when the load is complete
1991
+ */
1992
+ loadImageAsync(context, image) {
1993
+ if (!image._data) {
1994
+ this.logOpen(`${context} ${image.name || ""}`);
1995
+ if (image.uri) {
1996
+ image._data = this.loadUriAsync(`${context}/uri`, image, image.uri);
1997
+ }
1998
+ else {
1999
+ const bufferView = ArrayItem.Get(`${context}/bufferView`, this._gltf.bufferViews, image.bufferView);
2000
+ image._data = this.loadBufferViewAsync(`/bufferViews/${bufferView.index}`, bufferView);
2001
+ }
2002
+ this.logClose();
2003
+ }
2004
+ return image._data;
2005
+ }
2006
+ /**
2007
+ * Loads a glTF uri.
2008
+ * @param context The context when loading the asset
2009
+ * @param property The glTF property associated with the uri
2010
+ * @param uri The base64 or relative uri
2011
+ * @returns A promise that resolves with the loaded data when the load is complete
2012
+ */
2013
+ loadUriAsync(context, property, uri) {
2014
+ const extensionPromise = this._extensionsLoadUriAsync(context, property, uri);
2015
+ if (extensionPromise) {
2016
+ return extensionPromise;
2017
+ }
2018
+ if (!GLTFLoader._ValidateUri(uri)) {
2019
+ throw new Error(`${context}: '${uri}' is invalid`);
2020
+ }
2021
+ if (IsBase64DataUrl(uri)) {
2022
+ const data = new Uint8Array(DecodeBase64UrlToBinary(uri));
2023
+ this.log(`${context}: Decoded ${uri.substring(0, 64)}... (${data.length} bytes)`);
2024
+ return Promise.resolve(data);
2025
+ }
2026
+ this.log(`${context}: Loading ${uri}`);
2027
+ return this._parent.preprocessUrlAsync(this._rootUrl + uri).then((url) => {
2028
+ return new Promise((resolve, reject) => {
2029
+ this._parent._loadFile(this._babylonScene, url, (data) => {
2030
+ if (!this._disposed) {
2031
+ this.log(`${context}: Loaded ${uri} (${data.byteLength} bytes)`);
2032
+ resolve(new Uint8Array(data));
2033
+ }
2034
+ }, true, (request) => {
2035
+ reject(new LoadFileError(`${context}: Failed to load '${uri}'${request ? ": " + request.status + " " + request.statusText : ""}`, request));
2036
+ });
2037
+ });
2038
+ });
2039
+ }
2040
+ /**
2041
+ * Adds a JSON pointer to the _internalMetadata of the Babylon object at `<object>._internalMetadata.gltf.pointers`.
2042
+ * @param babylonObject the Babylon object with _internalMetadata
2043
+ * @param pointer the JSON pointer
2044
+ */
2045
+ static AddPointerMetadata(babylonObject, pointer) {
2046
+ babylonObject.metadata = babylonObject.metadata || {};
2047
+ const metadata = (babylonObject._internalMetadata = babylonObject._internalMetadata || {});
2048
+ const gltf = (metadata.gltf = metadata.gltf || {});
2049
+ const pointers = (gltf.pointers = gltf.pointers || []);
2050
+ pointers.push(pointer);
2051
+ }
2052
+ static _GetTextureWrapMode(context, mode) {
2053
+ // Set defaults if undefined
2054
+ mode = mode == undefined ? 10497 /* TextureWrapMode.REPEAT */ : mode;
2055
+ switch (mode) {
2056
+ case 33071 /* TextureWrapMode.CLAMP_TO_EDGE */:
2057
+ return Texture.CLAMP_ADDRESSMODE;
2058
+ case 33648 /* TextureWrapMode.MIRRORED_REPEAT */:
2059
+ return Texture.MIRROR_ADDRESSMODE;
2060
+ case 10497 /* TextureWrapMode.REPEAT */:
2061
+ return Texture.WRAP_ADDRESSMODE;
2062
+ default:
2063
+ Logger.Warn(`${context}: Invalid value (${mode})`);
2064
+ return Texture.WRAP_ADDRESSMODE;
2065
+ }
2066
+ }
2067
+ static _GetTextureSamplingMode(context, sampler) {
2068
+ // Set defaults if undefined
2069
+ const magFilter = sampler.magFilter == undefined ? 9729 /* TextureMagFilter.LINEAR */ : sampler.magFilter;
2070
+ const minFilter = sampler.minFilter == undefined ? 9987 /* TextureMinFilter.LINEAR_MIPMAP_LINEAR */ : sampler.minFilter;
2071
+ if (magFilter === 9729 /* TextureMagFilter.LINEAR */) {
2072
+ switch (minFilter) {
2073
+ case 9728 /* TextureMinFilter.NEAREST */:
2074
+ return Texture.LINEAR_NEAREST;
2075
+ case 9729 /* TextureMinFilter.LINEAR */:
2076
+ return Texture.LINEAR_LINEAR;
2077
+ case 9984 /* TextureMinFilter.NEAREST_MIPMAP_NEAREST */:
2078
+ return Texture.LINEAR_NEAREST_MIPNEAREST;
2079
+ case 9985 /* TextureMinFilter.LINEAR_MIPMAP_NEAREST */:
2080
+ return Texture.LINEAR_LINEAR_MIPNEAREST;
2081
+ case 9986 /* TextureMinFilter.NEAREST_MIPMAP_LINEAR */:
2082
+ return Texture.LINEAR_NEAREST_MIPLINEAR;
2083
+ case 9987 /* TextureMinFilter.LINEAR_MIPMAP_LINEAR */:
2084
+ return Texture.LINEAR_LINEAR_MIPLINEAR;
2085
+ default:
2086
+ Logger.Warn(`${context}/minFilter: Invalid value (${minFilter})`);
2087
+ return Texture.LINEAR_LINEAR_MIPLINEAR;
2088
+ }
2089
+ }
2090
+ else {
2091
+ if (magFilter !== 9728 /* TextureMagFilter.NEAREST */) {
2092
+ Logger.Warn(`${context}/magFilter: Invalid value (${magFilter})`);
2093
+ }
2094
+ switch (minFilter) {
2095
+ case 9728 /* TextureMinFilter.NEAREST */:
2096
+ return Texture.NEAREST_NEAREST;
2097
+ case 9729 /* TextureMinFilter.LINEAR */:
2098
+ return Texture.NEAREST_LINEAR;
2099
+ case 9984 /* TextureMinFilter.NEAREST_MIPMAP_NEAREST */:
2100
+ return Texture.NEAREST_NEAREST_MIPNEAREST;
2101
+ case 9985 /* TextureMinFilter.LINEAR_MIPMAP_NEAREST */:
2102
+ return Texture.NEAREST_LINEAR_MIPNEAREST;
2103
+ case 9986 /* TextureMinFilter.NEAREST_MIPMAP_LINEAR */:
2104
+ return Texture.NEAREST_NEAREST_MIPLINEAR;
2105
+ case 9987 /* TextureMinFilter.LINEAR_MIPMAP_LINEAR */:
2106
+ return Texture.NEAREST_LINEAR_MIPLINEAR;
2107
+ default:
2108
+ Logger.Warn(`${context}/minFilter: Invalid value (${minFilter})`);
2109
+ return Texture.NEAREST_NEAREST_MIPNEAREST;
2110
+ }
2111
+ }
2112
+ }
2113
+ static _GetTypedArrayConstructor(context, componentType) {
2114
+ try {
2115
+ return GetTypedArrayConstructor(componentType);
2116
+ }
2117
+ catch (e) {
2118
+ throw new Error(`${context}: ${e.message}`);
2119
+ }
2120
+ }
2121
+ static _GetTypedArray(context, componentType, bufferView, byteOffset, length) {
2122
+ const buffer = bufferView.buffer;
2123
+ byteOffset = bufferView.byteOffset + (byteOffset || 0);
2124
+ const constructor = GLTFLoader._GetTypedArrayConstructor(`${context}/componentType`, componentType);
2125
+ const componentTypeLength = VertexBuffer.GetTypeByteLength(componentType);
2126
+ if (byteOffset % componentTypeLength !== 0) {
2127
+ // HACK: Copy the buffer if byte offset is not a multiple of component type byte length.
2128
+ Logger.Warn(`${context}: Copying buffer as byte offset (${byteOffset}) is not a multiple of component type byte length (${componentTypeLength})`);
2129
+ return new constructor(buffer.slice(byteOffset, byteOffset + length * componentTypeLength), 0);
2130
+ }
2131
+ return new constructor(buffer, byteOffset, length);
2132
+ }
2133
+ static _GetNumComponents(context, type) {
2134
+ switch (type) {
2135
+ case "SCALAR":
2136
+ return 1;
2137
+ case "VEC2":
2138
+ return 2;
2139
+ case "VEC3":
2140
+ return 3;
2141
+ case "VEC4":
2142
+ return 4;
2143
+ case "MAT2":
2144
+ return 4;
2145
+ case "MAT3":
2146
+ return 9;
2147
+ case "MAT4":
2148
+ return 16;
2149
+ }
2150
+ throw new Error(`${context}: Invalid type (${type})`);
2151
+ }
2152
+ static _ValidateUri(uri) {
2153
+ return Tools.IsBase64(uri) || uri.indexOf("..") === -1;
2154
+ }
2155
+ /**
2156
+ * @internal
2157
+ */
2158
+ static _GetDrawMode(context, mode) {
2159
+ if (mode == undefined) {
2160
+ mode = 4 /* MeshPrimitiveMode.TRIANGLES */;
2161
+ }
2162
+ switch (mode) {
2163
+ case 0 /* MeshPrimitiveMode.POINTS */:
2164
+ return Material.PointListDrawMode;
2165
+ case 1 /* MeshPrimitiveMode.LINES */:
2166
+ return Material.LineListDrawMode;
2167
+ case 2 /* MeshPrimitiveMode.LINE_LOOP */:
2168
+ return Material.LineLoopDrawMode;
2169
+ case 3 /* MeshPrimitiveMode.LINE_STRIP */:
2170
+ return Material.LineStripDrawMode;
2171
+ case 4 /* MeshPrimitiveMode.TRIANGLES */:
2172
+ return Material.TriangleFillMode;
2173
+ case 5 /* MeshPrimitiveMode.TRIANGLE_STRIP */:
2174
+ return Material.TriangleStripDrawMode;
2175
+ case 6 /* MeshPrimitiveMode.TRIANGLE_FAN */:
2176
+ return Material.TriangleFanDrawMode;
2177
+ }
2178
+ throw new Error(`${context}: Invalid mesh primitive mode (${mode})`);
2179
+ }
2180
+ _compileMaterialsAsync() {
2181
+ this._parent._startPerformanceCounter("Compile materials");
2182
+ const promises = new Array();
2183
+ if (this._gltf.materials) {
2184
+ for (const material of this._gltf.materials) {
2185
+ if (material._data) {
2186
+ for (const babylonDrawMode in material._data) {
2187
+ const babylonData = material._data[babylonDrawMode];
2188
+ for (const babylonMesh of babylonData.babylonMeshes) {
2189
+ // Ensure nonUniformScaling is set if necessary.
2190
+ babylonMesh.computeWorldMatrix(true);
2191
+ const babylonMaterial = babylonData.babylonMaterial;
2192
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
2193
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { useInstances: true }));
2194
+ if (this._parent.useClipPlane) {
2195
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
2196
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true, useInstances: true }));
2197
+ }
2198
+ }
2199
+ }
2200
+ }
2201
+ }
2202
+ }
2203
+ return Promise.all(promises).then(() => {
2204
+ this._parent._endPerformanceCounter("Compile materials");
2205
+ });
2206
+ }
2207
+ _compileShadowGeneratorsAsync() {
2208
+ this._parent._startPerformanceCounter("Compile shadow generators");
2209
+ const promises = new Array();
2210
+ const lights = this._babylonScene.lights;
2211
+ for (const light of lights) {
2212
+ const generator = light.getShadowGenerator();
2213
+ if (generator) {
2214
+ promises.push(generator.forceCompilationAsync());
2215
+ }
2216
+ }
2217
+ return Promise.all(promises).then(() => {
2218
+ this._parent._endPerformanceCounter("Compile shadow generators");
2219
+ });
2220
+ }
2221
+ _forEachExtensions(action) {
2222
+ for (const extension of this._extensions) {
2223
+ if (extension.enabled) {
2224
+ action(extension);
2225
+ }
2226
+ }
2227
+ }
2228
+ _applyExtensions(property, functionName, actionAsync) {
2229
+ for (const extension of this._extensions) {
2230
+ if (extension.enabled) {
2231
+ const id = `${extension.name}.${functionName}`;
2232
+ const loaderProperty = property;
2233
+ loaderProperty._activeLoaderExtensionFunctions = loaderProperty._activeLoaderExtensionFunctions || {};
2234
+ const activeLoaderExtensionFunctions = loaderProperty._activeLoaderExtensionFunctions;
2235
+ if (!activeLoaderExtensionFunctions[id]) {
2236
+ activeLoaderExtensionFunctions[id] = true;
2237
+ try {
2238
+ const result = actionAsync(extension);
2239
+ if (result) {
2240
+ return result;
2241
+ }
2242
+ }
2243
+ finally {
2244
+ delete activeLoaderExtensionFunctions[id];
2245
+ }
2246
+ }
2247
+ }
2248
+ }
2249
+ return null;
2250
+ }
2251
+ _extensionsOnLoading() {
2252
+ this._forEachExtensions((extension) => extension.onLoading && extension.onLoading());
2253
+ }
2254
+ _extensionsOnReady() {
2255
+ this._forEachExtensions((extension) => extension.onReady && extension.onReady());
2256
+ }
2257
+ // eslint-disable-next-line no-restricted-syntax
2258
+ _extensionsLoadSceneAsync(context, scene) {
2259
+ return this._applyExtensions(scene, "loadScene", (extension) => extension.loadSceneAsync && extension.loadSceneAsync(context, scene));
2260
+ }
2261
+ // eslint-disable-next-line no-restricted-syntax
2262
+ _extensionsLoadNodeAsync(context, node, assign) {
2263
+ return this._applyExtensions(node, "loadNode", (extension) => extension.loadNodeAsync && extension.loadNodeAsync(context, node, assign));
2264
+ }
2265
+ // eslint-disable-next-line no-restricted-syntax
2266
+ _extensionsLoadCameraAsync(context, camera, assign) {
2267
+ return this._applyExtensions(camera, "loadCamera", (extension) => extension.loadCameraAsync && extension.loadCameraAsync(context, camera, assign));
2268
+ }
2269
+ // eslint-disable-next-line no-restricted-syntax
2270
+ _extensionsLoadVertexDataAsync(context, primitive, babylonMesh) {
2271
+ return this._applyExtensions(primitive, "loadVertexData", (extension) => extension._loadVertexDataAsync && extension._loadVertexDataAsync(context, primitive, babylonMesh));
2272
+ }
2273
+ // eslint-disable-next-line no-restricted-syntax
2274
+ _extensionsLoadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign) {
2275
+ return this._applyExtensions(primitive, "loadMeshPrimitive", (extension) => extension._loadMeshPrimitiveAsync && extension._loadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign));
2276
+ }
2277
+ // eslint-disable-next-line no-restricted-syntax
2278
+ _extensionsLoadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign) {
2279
+ return this._applyExtensions(material, "loadMaterial", (extension) => extension._loadMaterialAsync && extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign));
2280
+ }
2281
+ _extensionsCreateMaterial(context, material, babylonDrawMode) {
2282
+ return this._applyExtensions(material, "createMaterial", (extension) => extension.createMaterial && extension.createMaterial(context, material, babylonDrawMode));
2283
+ }
2284
+ // eslint-disable-next-line no-restricted-syntax
2285
+ _extensionsLoadMaterialPropertiesAsync(context, material, babylonMaterial) {
2286
+ return this._applyExtensions(material, "loadMaterialProperties", (extension) => extension.loadMaterialPropertiesAsync && extension.loadMaterialPropertiesAsync(context, material, babylonMaterial));
2287
+ }
2288
+ // eslint-disable-next-line no-restricted-syntax
2289
+ _extensionsLoadTextureInfoAsync(context, textureInfo, assign) {
2290
+ return this._applyExtensions(textureInfo, "loadTextureInfo", (extension) => extension.loadTextureInfoAsync && extension.loadTextureInfoAsync(context, textureInfo, assign));
2291
+ }
2292
+ // eslint-disable-next-line no-restricted-syntax
2293
+ _extensionsLoadTextureAsync(context, texture, assign) {
2294
+ return this._applyExtensions(texture, "loadTexture", (extension) => extension._loadTextureAsync && extension._loadTextureAsync(context, texture, assign));
2295
+ }
2296
+ // eslint-disable-next-line no-restricted-syntax
2297
+ _extensionsLoadAnimationAsync(context, animation) {
2298
+ return this._applyExtensions(animation, "loadAnimation", (extension) => extension.loadAnimationAsync && extension.loadAnimationAsync(context, animation));
2299
+ }
2300
+ // eslint-disable-next-line no-restricted-syntax
2301
+ _extensionsLoadAnimationChannelAsync(context, animationContext, animation, channel, onLoad) {
2302
+ return this._applyExtensions(animation, "loadAnimationChannel", (extension) => extension._loadAnimationChannelAsync && extension._loadAnimationChannelAsync(context, animationContext, animation, channel, onLoad));
2303
+ }
2304
+ // eslint-disable-next-line no-restricted-syntax
2305
+ _extensionsLoadSkinAsync(context, node, skin) {
2306
+ return this._applyExtensions(skin, "loadSkin", (extension) => extension._loadSkinAsync && extension._loadSkinAsync(context, node, skin));
2307
+ }
2308
+ // eslint-disable-next-line no-restricted-syntax
2309
+ _extensionsLoadUriAsync(context, property, uri) {
2310
+ return this._applyExtensions(property, "loadUri", (extension) => extension._loadUriAsync && extension._loadUriAsync(context, property, uri));
2311
+ }
2312
+ // eslint-disable-next-line no-restricted-syntax
2313
+ _extensionsLoadBufferViewAsync(context, bufferView) {
2314
+ return this._applyExtensions(bufferView, "loadBufferView", (extension) => extension.loadBufferViewAsync && extension.loadBufferViewAsync(context, bufferView));
2315
+ }
2316
+ // eslint-disable-next-line no-restricted-syntax
2317
+ _extensionsLoadBufferAsync(context, buffer, byteOffset, byteLength) {
2318
+ return this._applyExtensions(buffer, "loadBuffer", (extension) => extension.loadBufferAsync && extension.loadBufferAsync(context, buffer, byteOffset, byteLength));
2319
+ }
2320
+ /**
2321
+ * Helper method called by a loader extension to load an glTF extension.
2322
+ * @param context The context when loading the asset
2323
+ * @param property The glTF property to load the extension from
2324
+ * @param extensionName The name of the extension to load
2325
+ * @param actionAsync The action to run
2326
+ * @returns The promise returned by actionAsync or null if the extension does not exist
2327
+ */
2328
+ // eslint-disable-next-line no-restricted-syntax, @typescript-eslint/naming-convention
2329
+ static LoadExtensionAsync(context, property, extensionName, actionAsync) {
2330
+ if (!property.extensions) {
2331
+ return null;
2332
+ }
2333
+ const extensions = property.extensions;
2334
+ const extension = extensions[extensionName];
2335
+ if (!extension) {
2336
+ return null;
2337
+ }
2338
+ return actionAsync(`${context}/extensions/${extensionName}`, extension);
2339
+ }
2340
+ /**
2341
+ * Helper method called by a loader extension to load a glTF extra.
2342
+ * @param context The context when loading the asset
2343
+ * @param property The glTF property to load the extra from
2344
+ * @param extensionName The name of the extension to load
2345
+ * @param actionAsync The action to run
2346
+ * @returns The promise returned by actionAsync or null if the extra does not exist
2347
+ */
2348
+ // eslint-disable-next-line no-restricted-syntax, @typescript-eslint/naming-convention
2349
+ static LoadExtraAsync(context, property, extensionName, actionAsync) {
2350
+ if (!property.extras) {
2351
+ return null;
2352
+ }
2353
+ const extras = property.extras;
2354
+ const extra = extras[extensionName];
2355
+ if (!extra) {
2356
+ return null;
2357
+ }
2358
+ return actionAsync(`${context}/extras/${extensionName}`, extra);
2359
+ }
2360
+ /**
2361
+ * Checks for presence of an extension.
2362
+ * @param name The name of the extension to check
2363
+ * @returns A boolean indicating the presence of the given extension name in `extensionsUsed`
2364
+ */
2365
+ isExtensionUsed(name) {
2366
+ return !!this._gltf.extensionsUsed && this._gltf.extensionsUsed.indexOf(name) !== -1;
2367
+ }
2368
+ /**
2369
+ * Increments the indentation level and logs a message.
2370
+ * @param message The message to log
2371
+ */
2372
+ logOpen(message) {
2373
+ this._parent._logOpen(message);
2374
+ }
2375
+ /**
2376
+ * Decrements the indentation level.
2377
+ */
2378
+ logClose() {
2379
+ this._parent._logClose();
2380
+ }
2381
+ /**
2382
+ * Logs a message
2383
+ * @param message The message to log
2384
+ */
2385
+ log(message) {
2386
+ this._parent._log(message);
2387
+ }
2388
+ /**
2389
+ * Starts a performance counter.
2390
+ * @param counterName The name of the performance counter
2391
+ */
2392
+ startPerformanceCounter(counterName) {
2393
+ this._parent._startPerformanceCounter(counterName);
2394
+ }
2395
+ /**
2396
+ * Ends a performance counter.
2397
+ * @param counterName The name of the performance counter
2398
+ */
2399
+ endPerformanceCounter(counterName) {
2400
+ this._parent._endPerformanceCounter(counterName);
2401
+ }
2402
+ }
2403
+ /**
2404
+ * The default glTF sampler.
2405
+ */
2406
+ GLTFLoader.DefaultSampler = { index: -1 };
2407
+ GLTFFileLoader._CreateGLTF2Loader = (parent) => new GLTFLoader(parent);
2408
+ //# sourceMappingURL=glTFLoader.js.map