babylon.glTF2FileLoader.d.ts 38 KB

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