babylon.glTF2FileLoader.d.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. declare module BABYLON {
  2. /**
  3. * Mode that determines the coordinate system to use.
  4. */
  5. enum GLTFLoaderCoordinateSystemMode {
  6. /**
  7. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  8. */
  9. AUTO = 0,
  10. /**
  11. * Sets the useRightHandedSystem flag on the scene.
  12. */
  13. FORCE_RIGHT_HANDED = 1,
  14. }
  15. /**
  16. * Mode that determines what animations will start.
  17. */
  18. enum GLTFLoaderAnimationStartMode {
  19. /**
  20. * No animation will start.
  21. */
  22. NONE = 0,
  23. /**
  24. * The first animation will start.
  25. */
  26. FIRST = 1,
  27. /**
  28. * All animations will start.
  29. */
  30. ALL = 2,
  31. }
  32. /**
  33. * Interface that contains the data for the glTF asset.
  34. */
  35. interface IGLTFLoaderData {
  36. /**
  37. * JSON that represents the glTF.
  38. */
  39. json: Object;
  40. /**
  41. * The BIN chunk of a binary glTF
  42. */
  43. bin: Nullable<ArrayBufferView>;
  44. }
  45. /**
  46. * Interface for extending the loader.
  47. */
  48. interface IGLTFLoaderExtension {
  49. /**
  50. * The name of this extension.
  51. */
  52. readonly name: string;
  53. /**
  54. * Defines whether this extension is enabled.
  55. */
  56. enabled: boolean;
  57. }
  58. /**
  59. * Loader state.
  60. */
  61. enum GLTFLoaderState {
  62. /**
  63. * The asset is loading.
  64. */
  65. LOADING = 0,
  66. /**
  67. * The asset is ready for rendering.
  68. */
  69. READY = 1,
  70. /**
  71. * The asset is completely loaded.
  72. */
  73. COMPLETE = 2,
  74. }
  75. /**
  76. * Loader interface.
  77. */
  78. interface IGLTFLoader extends IDisposable {
  79. /**
  80. * Mode that determines the coordinate system to use.
  81. */
  82. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  83. /**
  84. * Mode that determines what animations will start.
  85. */
  86. animationStartMode: GLTFLoaderAnimationStartMode;
  87. /**
  88. * Defines if the loader should compile materials.
  89. */
  90. compileMaterials: boolean;
  91. /**
  92. * Defines if the loader should also compile materials with clip planes.
  93. */
  94. useClipPlane: boolean;
  95. /**
  96. * Defines if the loader should compile shadow generators.
  97. */
  98. compileShadowGenerators: boolean;
  99. /**
  100. * Defines if the Alpha blended materials are only applied as coverage.
  101. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  102. * If true, no extra effects are applied to transparent pixels.
  103. */
  104. transparencyAsCoverage: boolean;
  105. /** @hidden */
  106. _normalizeAnimationGroupsToBeginAtZero: boolean;
  107. /**
  108. * Function called before loading a url referenced by the asset.
  109. */
  110. preprocessUrlAsync: (url: string) => Promise<string>;
  111. /**
  112. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  113. */
  114. onMeshLoadedObservable: Observable<AbstractMesh>;
  115. /**
  116. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  117. */
  118. onTextureLoadedObservable: Observable<BaseTexture>;
  119. /**
  120. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  121. */
  122. onMaterialLoadedObservable: Observable<Material>;
  123. /**
  124. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  125. */
  126. onCameraLoadedObservable: Observable<Camera>;
  127. /**
  128. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  129. * For assets with LODs, raised when all of the LODs are complete.
  130. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  131. */
  132. onCompleteObservable: Observable<IGLTFLoader>;
  133. /**
  134. * Observable raised after the loader is disposed.
  135. */
  136. onDisposeObservable: Observable<IGLTFLoader>;
  137. /**
  138. * Observable raised after a loader extension is created.
  139. * Set additional options for a loader extension in this event.
  140. */
  141. onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  142. /**
  143. * Loader state or null if the loader is not active.
  144. */
  145. state: Nullable<GLTFLoaderState>;
  146. /**
  147. * Imports meshes from the given data and adds them to the scene.
  148. */
  149. importMeshAsync: (meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void) => Promise<{
  150. meshes: AbstractMesh[];
  151. particleSystems: ParticleSystem[];
  152. skeletons: Skeleton[];
  153. animationGroups: AnimationGroup[];
  154. }>;
  155. /**
  156. * Loads all objects from the given data and adds them to the scene.
  157. */
  158. loadAsync: (scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void) => Promise<void>;
  159. }
  160. /**
  161. * File loader for loading glTF files into a scene.
  162. */
  163. class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISceneLoaderPluginFactory {
  164. /**
  165. * Factory function that creates a glTF 1.0 loader
  166. * @hidden
  167. */
  168. static _CreateGLTFLoaderV1: () => IGLTFLoader;
  169. /**
  170. * Factory function that creates a glTF 2.0 loader
  171. * @hidden
  172. */
  173. static _CreateGLTFLoaderV2: () => IGLTFLoader;
  174. /**
  175. * Raised when the asset has been parsed
  176. */
  177. onParsedObservable: Observable<IGLTFLoaderData>;
  178. private _onParsedObserver;
  179. /**
  180. * Raised when the asset has been parsed
  181. */
  182. onParsed: (loaderData: IGLTFLoaderData) => void;
  183. /**
  184. * Set this property to false to disable incremental loading which delays the loader from calling the success callback until after loading the meshes and shaders.
  185. * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled.
  186. * Defaults to true.
  187. */
  188. static IncrementalLoading: boolean;
  189. /**
  190. * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters.
  191. * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates.
  192. */
  193. static HomogeneousCoordinates: boolean;
  194. /**
  195. * The coordinate system mode. Defaults to AUTO.
  196. */
  197. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  198. /**
  199. * The animation start mode. Defaults to FIRST.
  200. */
  201. animationStartMode: GLTFLoaderAnimationStartMode;
  202. /**
  203. * Defines if the loader should compile materials before raising the success callback. Defaults to false.
  204. */
  205. compileMaterials: boolean;
  206. /**
  207. * Defines if the loader should also compile materials with clip planes. Defaults to false.
  208. */
  209. useClipPlane: boolean;
  210. /**
  211. * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false.
  212. */
  213. compileShadowGenerators: boolean;
  214. /**
  215. * Defines if the Alpha blended materials are only applied as coverage.
  216. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  217. * If true, no extra effects are applied to transparent pixels.
  218. */
  219. transparencyAsCoverage: boolean;
  220. /** @hidden */
  221. _normalizeAnimationGroupsToBeginAtZero: boolean;
  222. /**
  223. * Function called before loading a url referenced by the asset.
  224. */
  225. preprocessUrlAsync: (url: string) => Promise<string>;
  226. /**
  227. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  228. */
  229. readonly onMeshLoadedObservable: Observable<AbstractMesh>;
  230. private _onMeshLoadedObserver;
  231. /**
  232. * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  233. */
  234. onMeshLoaded: (mesh: AbstractMesh) => void;
  235. /**
  236. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  237. */
  238. readonly onTextureLoadedObservable: Observable<BaseTexture>;
  239. private _onTextureLoadedObserver;
  240. /**
  241. * Callback raised when the loader creates a texture after parsing the glTF properties of the texture.
  242. */
  243. onTextureLoaded: (texture: BaseTexture) => void;
  244. /**
  245. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  246. */
  247. readonly onMaterialLoadedObservable: Observable<Material>;
  248. private _onMaterialLoadedObserver;
  249. /**
  250. * Callback raised when the loader creates a material after parsing the glTF properties of the material.
  251. */
  252. onMaterialLoaded: (material: Material) => void;
  253. /**
  254. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  255. */
  256. readonly onCameraLoadedObservable: Observable<Camera>;
  257. private _onCameraLoadedObserver;
  258. /**
  259. * Callback raised when the loader creates a camera after parsing the glTF properties of the camera.
  260. */
  261. onCameraLoaded: (camera: Camera) => void;
  262. /**
  263. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  264. * For assets with LODs, raised when all of the LODs are complete.
  265. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  266. */
  267. readonly onCompleteObservable: Observable<GLTFFileLoader>;
  268. private _onCompleteObserver;
  269. /**
  270. * Callback raised when the asset is completely loaded, immediately before the loader is disposed.
  271. */
  272. onComplete: () => void;
  273. /**
  274. * Observable raised after the loader is disposed.
  275. */
  276. readonly onDisposeObservable: Observable<GLTFFileLoader>;
  277. private _onDisposeObserver;
  278. /**
  279. * Callback raised after the loader is disposed.
  280. */
  281. onDispose: () => void;
  282. /**
  283. * Observable raised after a loader extension is created.
  284. * Set additional options for a loader extension in this event.
  285. */
  286. readonly onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  287. private _onExtensionLoadedObserver;
  288. /**
  289. * Callback raised after a loader extension is created.
  290. */
  291. onExtensionLoaded: (extension: IGLTFLoaderExtension) => void;
  292. /**
  293. * Returns a promise that resolves when the asset is completely loaded.
  294. * @returns a promise that resolves when the asset is completely loaded.
  295. */
  296. whenCompleteAsync(): Promise<void>;
  297. /**
  298. * The loader state or null if the loader is not active.
  299. */
  300. readonly loaderState: Nullable<GLTFLoaderState>;
  301. private _loader;
  302. /**
  303. * Name of the loader ("gltf")
  304. */
  305. name: string;
  306. /**
  307. * Supported file extensions of the loader (.gltf, .glb)
  308. */
  309. extensions: ISceneLoaderPluginExtensions;
  310. /**
  311. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  312. */
  313. dispose(): void;
  314. /**
  315. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  316. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  317. * @param scene the scene the meshes should be added to
  318. * @param data the glTF data to load
  319. * @param rootUrl root url to load from
  320. * @param onProgress event that fires when loading progress has occured
  321. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  322. */
  323. importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<{
  324. meshes: AbstractMesh[];
  325. particleSystems: ParticleSystem[];
  326. skeletons: Skeleton[];
  327. animationGroups: AnimationGroup[];
  328. }>;
  329. /**
  330. * Imports all objects from the loaded glTF data and adds them to the scene
  331. * @param scene the scene the objects should be added to
  332. * @param data the glTF data to load
  333. * @param rootUrl root url to load from
  334. * @param onProgress event that fires when loading progress has occured
  335. * @returns a promise which completes when objects have been loaded to the scene
  336. */
  337. loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<void>;
  338. /**
  339. * Load into an asset container.
  340. * @param scene The scene to load into
  341. * @param data The data to import
  342. * @param rootUrl The root url for scene and resources
  343. * @param onProgress The callback when the load progresses
  344. * @returns The loaded asset container
  345. */
  346. loadAssetContainerAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<AssetContainer>;
  347. /**
  348. * If the data string can be loaded directly.
  349. * @param data string contianing the file data
  350. * @returns if the data can be loaded directly
  351. */
  352. canDirectLoad(data: string): boolean;
  353. /**
  354. * Rewrites a url by combining a root url and response url.
  355. */
  356. rewriteRootURL: (rootUrl: string, responseURL?: string) => string;
  357. /**
  358. * Instantiates a glTF file loader plugin.
  359. * @returns the created plugin
  360. */
  361. createPlugin(): ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  362. private _parse(data);
  363. private _getLoader(loaderData);
  364. private static _parseBinary(data);
  365. private static _parseV1(binaryReader);
  366. private static _parseV2(binaryReader);
  367. private static _parseVersion(version);
  368. private static _compareVersion(a, b);
  369. private static _decodeBufferToText(buffer);
  370. }
  371. }
  372. declare module BABYLON.GLTF2 {
  373. /** @hidden */
  374. interface _IArrayItem {
  375. _index: number;
  376. }
  377. /** @hidden */
  378. interface _ILoaderAccessor extends IAccessor, _IArrayItem {
  379. _data?: Promise<ArrayBufferView>;
  380. _babylonVertexBuffer?: Promise<VertexBuffer>;
  381. }
  382. /** @hidden */
  383. interface _ILoaderAnimationChannel extends IAnimationChannel, _IArrayItem {
  384. }
  385. /** @hidden */
  386. interface _ILoaderAnimationSamplerData {
  387. input: Float32Array;
  388. interpolation: AnimationSamplerInterpolation;
  389. output: Float32Array;
  390. }
  391. /** @hidden */
  392. interface _ILoaderAnimationSampler extends IAnimationSampler, _IArrayItem {
  393. _data?: Promise<_ILoaderAnimationSamplerData>;
  394. }
  395. /** @hidden */
  396. interface _ILoaderAnimation extends IAnimation, _IArrayItem {
  397. channels: _ILoaderAnimationChannel[];
  398. samplers: _ILoaderAnimationSampler[];
  399. _babylonAnimationGroup?: AnimationGroup;
  400. }
  401. /** @hidden */
  402. interface _ILoaderBuffer extends IBuffer, _IArrayItem {
  403. _data?: Promise<ArrayBufferView>;
  404. }
  405. /** @hidden */
  406. interface _ILoaderBufferView extends IBufferView, _IArrayItem {
  407. _data?: Promise<ArrayBufferView>;
  408. _babylonBuffer?: Promise<Buffer>;
  409. }
  410. /** @hidden */
  411. interface _ILoaderCamera extends ICamera, _IArrayItem {
  412. }
  413. /** @hidden */
  414. interface _ILoaderImage extends IImage, _IArrayItem {
  415. _blob?: Promise<Blob>;
  416. }
  417. /** @hidden */
  418. interface _ILoaderMaterial extends IMaterial, _IArrayItem {
  419. _babylonData?: {
  420. [drawMode: number]: {
  421. material: Material;
  422. meshes: AbstractMesh[];
  423. loaded: Promise<void>;
  424. };
  425. };
  426. }
  427. /** @hidden */
  428. interface _ILoaderMesh extends IMesh, _IArrayItem {
  429. primitives: _ILoaderMeshPrimitive[];
  430. }
  431. /** @hidden */
  432. interface _ILoaderMeshPrimitive extends IMeshPrimitive, _IArrayItem {
  433. }
  434. /** @hidden */
  435. interface _ILoaderNode extends INode, _IArrayItem {
  436. _parent?: _ILoaderNode;
  437. _babylonMesh?: Mesh;
  438. _primitiveBabylonMeshes?: Mesh[];
  439. _babylonBones?: Bone[];
  440. _numMorphTargets?: number;
  441. }
  442. /** @hidden */
  443. interface _ILoaderSamplerData {
  444. noMipMaps: boolean;
  445. samplingMode: number;
  446. wrapU: number;
  447. wrapV: number;
  448. }
  449. /** @hidden */
  450. interface _ILoaderSampler extends ISampler, _IArrayItem {
  451. _data?: _ILoaderSamplerData;
  452. }
  453. /** @hidden */
  454. interface _ILoaderScene extends IScene, _IArrayItem {
  455. }
  456. /** @hidden */
  457. interface _ILoaderSkin extends ISkin, _IArrayItem {
  458. _babylonSkeleton?: Skeleton;
  459. _loaded?: Promise<void>;
  460. }
  461. /** @hidden */
  462. interface _ILoaderTexture extends ITexture, _IArrayItem {
  463. }
  464. /** @hidden */
  465. interface _ILoaderGLTF extends IGLTF {
  466. accessors?: _ILoaderAccessor[];
  467. animations?: _ILoaderAnimation[];
  468. buffers?: _ILoaderBuffer[];
  469. bufferViews?: _ILoaderBufferView[];
  470. cameras?: _ILoaderCamera[];
  471. images?: _ILoaderImage[];
  472. materials?: _ILoaderMaterial[];
  473. meshes?: _ILoaderMesh[];
  474. nodes?: _ILoaderNode[];
  475. samplers?: _ILoaderSampler[];
  476. scenes?: _ILoaderScene[];
  477. skins?: _ILoaderSkin[];
  478. textures?: _ILoaderTexture[];
  479. }
  480. }
  481. /**
  482. * Defines the module used to import/export glTF 2.0 assets
  483. */
  484. declare module BABYLON.GLTF2 {
  485. /**
  486. * Loader for loading a glTF 2.0 asset
  487. */
  488. class GLTFLoader implements IGLTFLoader {
  489. /** @hidden */
  490. _gltf: _ILoaderGLTF;
  491. /** @hidden */
  492. _babylonScene: Scene;
  493. /** @hidden */
  494. _completePromises: Promise<void>[];
  495. /** @hidden */
  496. _onReadyObservable: Observable<IGLTFLoader>;
  497. private _disposed;
  498. private _state;
  499. private _extensions;
  500. private _rootUrl;
  501. private _rootBabylonMesh;
  502. private _defaultSampler;
  503. private _defaultBabylonMaterials;
  504. private _progressCallback?;
  505. private _requests;
  506. private static _ExtensionNames;
  507. private static _ExtensionFactories;
  508. /** @hidden */
  509. static _Register(name: string, factory: (loader: GLTFLoader) => GLTFLoaderExtension): void;
  510. /**
  511. * Mode that determines the coordinate system to use.
  512. */
  513. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  514. /**
  515. * Mode that determines what animations will start.
  516. */
  517. animationStartMode: GLTFLoaderAnimationStartMode;
  518. /**
  519. * Defines if the loader should compile materials.
  520. */
  521. compileMaterials: boolean;
  522. /**
  523. * Defines if the loader should also compile materials with clip planes.
  524. */
  525. useClipPlane: boolean;
  526. /**
  527. * Defines if the loader should compile shadow generators.
  528. */
  529. compileShadowGenerators: boolean;
  530. /**
  531. * Defines if the Alpha blended materials are only applied as coverage.
  532. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
  533. * If true, no extra effects are applied to transparent pixels.
  534. */
  535. transparencyAsCoverage: boolean;
  536. /** @hidden */
  537. _normalizeAnimationGroupsToBeginAtZero: boolean;
  538. /**
  539. * Function called before loading a url referenced by the asset.
  540. */
  541. preprocessUrlAsync: (url: string) => Promise<string>;
  542. /**
  543. * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  544. */
  545. readonly onMeshLoadedObservable: Observable<AbstractMesh>;
  546. /**
  547. * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
  548. */
  549. readonly onTextureLoadedObservable: Observable<BaseTexture>;
  550. /**
  551. * Observable raised when the loader creates a material after parsing the glTF properties of the material.
  552. */
  553. readonly onMaterialLoadedObservable: Observable<Material>;
  554. /**
  555. * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
  556. */
  557. readonly onCameraLoadedObservable: Observable<Camera>;
  558. /**
  559. * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
  560. * For assets with LODs, raised when all of the LODs are complete.
  561. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  562. */
  563. readonly onCompleteObservable: Observable<IGLTFLoader>;
  564. /**
  565. * Observable raised after the loader is disposed.
  566. */
  567. readonly onDisposeObservable: Observable<IGLTFLoader>;
  568. /**
  569. * Observable raised after a loader extension is created.
  570. * Set additional options for a loader extension in this event.
  571. */
  572. readonly onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  573. /**
  574. * Loader state or null if the loader is not active.
  575. */
  576. readonly state: Nullable<GLTFLoaderState>;
  577. /**
  578. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  579. */
  580. dispose(): void;
  581. /**
  582. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  583. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  584. * @param scene the scene the meshes should be added to
  585. * @param data the glTF data to load
  586. * @param rootUrl root url to load from
  587. * @param onProgress event that fires when loading progress has occured
  588. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  589. */
  590. importMeshAsync(meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<{
  591. meshes: AbstractMesh[];
  592. particleSystems: ParticleSystem[];
  593. skeletons: Skeleton[];
  594. animationGroups: AnimationGroup[];
  595. }>;
  596. /**
  597. * Imports all objects from the loaded glTF data and adds them to the scene
  598. * @param scene the scene the objects should be added to
  599. * @param data the glTF data to load
  600. * @param rootUrl root url to load from
  601. * @param onProgress event that fires when loading progress has occured
  602. * @returns a promise which completes when objects have been loaded to the scene
  603. */
  604. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<void>;
  605. private _loadAsync(nodes);
  606. private _loadData(data);
  607. private _setupData();
  608. private _loadExtensions();
  609. private _checkExtensions();
  610. private _createRootNode();
  611. private _loadNodesAsync(nodes);
  612. /** @hidden */
  613. _loadSceneAsync(context: string, scene: _ILoaderScene): Promise<void>;
  614. private _forEachPrimitive(node, callback);
  615. private _getMeshes();
  616. private _getSkeletons();
  617. private _getAnimationGroups();
  618. private _startAnimations();
  619. /** @hidden */
  620. _loadNodeAsync(context: string, node: _ILoaderNode): Promise<void>;
  621. private _loadMeshAsync(context, node, mesh, babylonMesh);
  622. private _loadPrimitiveAsync(context, node, mesh, primitive, babylonMesh);
  623. private _loadVertexDataAsync(context, primitive, babylonMesh);
  624. private _createMorphTargets(context, node, mesh, primitive, babylonMesh);
  625. private _loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry);
  626. private _loadMorphTargetVertexDataAsync(context, babylonGeometry, attributes, babylonMorphTarget);
  627. private static _LoadTransform(node, babylonNode);
  628. private _loadSkinAsync(context, node, mesh, skin);
  629. private _loadBones(context, skin);
  630. private _loadBone(node, skin, babylonBones);
  631. private _loadSkinInverseBindMatricesDataAsync(context, skin);
  632. private _updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
  633. private _getNodeMatrix(node);
  634. private _loadCamera(context, camera, babylonMesh);
  635. private _loadAnimationsAsync();
  636. private _loadAnimationAsync(context, animation);
  637. private _loadAnimationChannelAsync(context, animationContext, animation, channel, babylonAnimationGroup);
  638. private _loadAnimationSamplerAsync(context, sampler);
  639. private _loadBufferAsync(context, buffer);
  640. /** @hidden */
  641. _loadBufferViewAsync(context: string, bufferView: _ILoaderBufferView): Promise<ArrayBufferView>;
  642. private _loadIndicesAccessorAsync(context, accessor);
  643. private _loadFloatAccessorAsync(context, accessor);
  644. /** @hidden */
  645. _loadVertexBufferViewAsync(context: string, bufferView: _ILoaderBufferView, kind: string): Promise<Buffer>;
  646. private _loadVertexAccessorAsync(context, accessor, kind);
  647. private _getDefaultMaterial(drawMode);
  648. private _loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial);
  649. /** @hidden */
  650. _loadMaterialAsync(context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Promise<void>;
  651. /** @hidden */
  652. _createMaterial(name: string, drawMode: number): PBRMaterial;
  653. /** @hidden */
  654. _loadMaterialBasePropertiesAsync(context: string, material: _ILoaderMaterial, babylonMaterial: PBRMaterial): Promise<void>;
  655. /** @hidden */
  656. _loadMaterialAlphaProperties(context: string, material: _ILoaderMaterial, babylonMaterial: PBRMaterial): void;
  657. /** @hidden */
  658. _loadTextureAsync(context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Promise<void>;
  659. private _loadSampler(context, sampler);
  660. private _loadImageAsync(context, image);
  661. /** @hidden */
  662. _loadUriAsync(context: string, uri: string): Promise<ArrayBufferView>;
  663. private _onProgress();
  664. /** @hidden */
  665. static _GetProperty<T>(context: string, array: ArrayLike<T> | undefined, index: number | undefined): T;
  666. private static _GetTextureWrapMode(context, mode);
  667. private static _GetTextureSamplingMode(context, magFilter?, minFilter?);
  668. private static _GetTypedArray(context, componentType, bufferView, byteOffset, length);
  669. private static _GetNumComponents(context, type);
  670. private static _ValidateUri(uri);
  671. private static _GetDrawMode(context, mode);
  672. private _compileMaterialsAsync();
  673. private _compileShadowGeneratorsAsync();
  674. private _clear();
  675. /** @hidden */
  676. _applyExtensions<T>(actionAsync: (extension: GLTFLoaderExtension) => Nullable<Promise<T>>): Nullable<Promise<T>>;
  677. }
  678. }
  679. declare module BABYLON.GLTF2 {
  680. /**
  681. * Abstract class that can be implemented to extend existing glTF loader behavior.
  682. */
  683. abstract class GLTFLoaderExtension implements IGLTFLoaderExtension, IDisposable {
  684. /**
  685. * Gets or sets a boolean indicating if the extension is enabled
  686. */
  687. enabled: boolean;
  688. /**
  689. * Gets or sets extension name
  690. */
  691. readonly abstract name: string;
  692. protected _loader: GLTFLoader;
  693. /**
  694. * Creates new GLTFLoaderExtension
  695. * @param loader defines the GLTFLoader to use
  696. */
  697. constructor(loader: GLTFLoader);
  698. /**
  699. * Release all resources
  700. */
  701. dispose(): void;
  702. /**
  703. * Override this method to modify the default behavior for loading scenes.
  704. * @hidden
  705. */
  706. protected _loadSceneAsync(context: string, node: _ILoaderScene): Nullable<Promise<void>>;
  707. /**
  708. * Override this method to modify the default behavior for loading nodes.
  709. * @hidden
  710. */
  711. protected _loadNodeAsync(context: string, node: _ILoaderNode): Nullable<Promise<void>>;
  712. /**
  713. * Override this method to modify the default behavior for loading mesh primitive vertex data.
  714. * @hidden
  715. */
  716. protected _loadVertexDataAsync(context: string, primitive: _ILoaderMeshPrimitive, babylonMesh: Mesh): Nullable<Promise<Geometry>>;
  717. /**
  718. * Override this method to modify the default behavior for loading materials.
  719. * @hidden
  720. */
  721. protected _loadMaterialAsync(context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  722. /**
  723. * Override this method to modify the default behavior for loading textures.
  724. * @hidden
  725. */
  726. protected _loadTextureAsync(context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Nullable<Promise<void>>;
  727. /**
  728. * Override this method to modify the default behavior for loading uris.
  729. * @hidden
  730. */
  731. protected _loadUriAsync(context: string, uri: string): Nullable<Promise<ArrayBufferView>>;
  732. /**
  733. * Helper method called by a loader extension to load an glTF extension.
  734. * @hidden
  735. */
  736. protected _loadExtensionAsync<TProperty, TResult = void>(context: string, property: IProperty, actionAsync: (extensionContext: string, extension: TProperty) => Nullable<Promise<TResult>>): Nullable<Promise<TResult>>;
  737. /**
  738. * Helper method called by the loader to allow extensions to override loading scenes.
  739. * @hidden
  740. */
  741. protected _loadExtrasValueAsync<TProperty, TResult = void>(context: string, property: IProperty, actionAsync: (extensionContext: string, value: TProperty) => Nullable<Promise<TResult>>): Nullable<Promise<TResult>>;
  742. /**
  743. * Helper method called by the loader to allow extensions to override loading scenes.
  744. * @hidden
  745. */
  746. static _LoadSceneAsync(loader: GLTFLoader, context: string, scene: _ILoaderScene): Nullable<Promise<void>>;
  747. /**
  748. * Helper method called by the loader to allow extensions to override loading nodes.
  749. * @hidden
  750. */
  751. static _LoadNodeAsync(loader: GLTFLoader, context: string, node: _ILoaderNode): Nullable<Promise<void>>;
  752. /**
  753. * Helper method called by the loader to allow extensions to override loading mesh primitive vertex data.
  754. * @hidden
  755. */
  756. static _LoadVertexDataAsync(loader: GLTFLoader, context: string, primitive: _ILoaderMeshPrimitive, babylonMesh: Mesh): Nullable<Promise<Geometry>>;
  757. /**
  758. * Helper method called by the loader to allow extensions to override loading materials.
  759. * @hidden
  760. */
  761. static _LoadMaterialAsync(loader: GLTFLoader, context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  762. /**
  763. * Helper method called by the loader to allow extensions to override loading textures.
  764. * @hidden
  765. */
  766. static _LoadTextureAsync(loader: GLTFLoader, context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Nullable<Promise<void>>;
  767. /**
  768. * Helper method called by the loader to allow extensions to override loading uris.
  769. * @hidden
  770. */
  771. static _LoadUriAsync(loader: GLTFLoader, context: string, uri: string): Nullable<Promise<ArrayBufferView>>;
  772. }
  773. }
  774. /**
  775. * Defines the module of the glTF 2.0 loader extensions.
  776. */
  777. declare module BABYLON.GLTF2.Extensions {
  778. }
  779. declare module BABYLON.GLTF2.Extensions {
  780. /**
  781. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_lod)
  782. */
  783. class MSFT_lod extends GLTFLoaderExtension {
  784. readonly name: string;
  785. /**
  786. * Maximum number of LODs to load, starting from the lowest LOD.
  787. */
  788. maxLODsToLoad: number;
  789. /**
  790. * Observable raised when all node LODs of one level are loaded.
  791. * The event data is the index of the loaded LOD starting from zero.
  792. * Dispose the loader to cancel the loading of the next level of LODs.
  793. */
  794. onNodeLODsLoadedObservable: Observable<number>;
  795. /**
  796. * Observable raised when all material LODs of one level are loaded.
  797. * The event data is the index of the loaded LOD starting from zero.
  798. * Dispose the loader to cancel the loading of the next level of LODs.
  799. */
  800. onMaterialLODsLoadedObservable: Observable<number>;
  801. private _loadingNodeLOD;
  802. private _loadNodeSignals;
  803. private _loadNodePromises;
  804. private _loadingMaterialLOD;
  805. private _loadMaterialSignals;
  806. private _loadMaterialPromises;
  807. constructor(loader: GLTFLoader);
  808. dispose(): void;
  809. protected _loadNodeAsync(context: string, node: _ILoaderNode): Nullable<Promise<void>>;
  810. protected _loadMaterialAsync(context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  811. protected _loadUriAsync(context: string, uri: string): Nullable<Promise<ArrayBufferView>>;
  812. /**
  813. * Gets an array of LOD properties from lowest to highest.
  814. */
  815. private _getLODs<T>(context, property, array, ids);
  816. }
  817. }
  818. declare module BABYLON.GLTF2.Extensions {
  819. /** @hidden */
  820. class MSFT_minecraftMesh extends GLTFLoaderExtension {
  821. readonly name: string;
  822. protected _loadMaterialAsync(context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  823. }
  824. }
  825. declare module BABYLON.GLTF2.Extensions {
  826. /** @hidden */
  827. class MSFT_sRGBFactors extends GLTFLoaderExtension {
  828. readonly name: string;
  829. protected _loadMaterialAsync(context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  830. }
  831. }
  832. declare module BABYLON.GLTF2.Extensions {
  833. /**
  834. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression)
  835. */
  836. class KHR_draco_mesh_compression extends GLTFLoaderExtension {
  837. readonly name: string;
  838. private _dracoCompression;
  839. constructor(loader: GLTFLoader);
  840. dispose(): void;
  841. protected _loadVertexDataAsync(context: string, primitive: _ILoaderMeshPrimitive, babylonMesh: Mesh): Nullable<Promise<Geometry>>;
  842. }
  843. }
  844. declare module BABYLON.GLTF2.Extensions {
  845. /**
  846. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness)
  847. */
  848. class KHR_materials_pbrSpecularGlossiness extends GLTFLoaderExtension {
  849. readonly name: string;
  850. /** @hidden */
  851. protected _loadMaterialAsync(context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  852. private _loadSpecularGlossinessPropertiesAsync(context, material, properties, babylonMaterial);
  853. }
  854. }
  855. declare module BABYLON.GLTF2.Extensions {
  856. /**
  857. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit)
  858. */
  859. class KHR_materials_unlit extends GLTFLoaderExtension {
  860. readonly name: string;
  861. protected _loadMaterialAsync(context: string, material: _ILoaderMaterial, mesh: _ILoaderMesh, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  862. private _loadUnlitPropertiesAsync(context, material, babylonMaterial);
  863. }
  864. }
  865. declare module BABYLON.GLTF2.Extensions {
  866. /**
  867. * [Specification](https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights) (Experimental)
  868. */
  869. class KHR_lights extends GLTFLoaderExtension {
  870. readonly name: string;
  871. protected _loadSceneAsync(context: string, scene: _ILoaderScene): Nullable<Promise<void>>;
  872. protected _loadNodeAsync(context: string, node: _ILoaderNode): Nullable<Promise<void>>;
  873. private readonly _lights;
  874. }
  875. }
  876. declare module BABYLON.GLTF2.Extensions {
  877. /**
  878. * [Specification](https://github.com/AltspaceVR/glTF/blob/avr-sampler-offset-tile/extensions/2.0/Khronos/KHR_texture_transform/README.md) (Experimental)
  879. */
  880. class KHR_texture_transform extends GLTFLoaderExtension {
  881. readonly name: string;
  882. protected _loadTextureAsync(context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Nullable<Promise<void>>;
  883. }
  884. }