sceneLoader.ts 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. import { Tools } from "../Misc/tools";
  2. import { Observable } from "../Misc/observable";
  3. import { FilesInputStore } from "../Misc/filesInputStore";
  4. import { Nullable } from "../types";
  5. import { Scene } from "../scene";
  6. import { Engine } from "../Engines/engine";
  7. import { EngineStore } from "../Engines/engineStore";
  8. import { AbstractMesh } from "../Meshes/abstractMesh";
  9. import { AnimationGroup } from "../Animations/animationGroup";
  10. import { AssetContainer } from "../assetContainer";
  11. import { IParticleSystem } from "../Particles/IParticleSystem";
  12. import { Skeleton } from "../Bones/skeleton";
  13. import { Logger } from "../Misc/logger";
  14. import { Constants } from "../Engines/constants";
  15. import { SceneLoaderFlags } from "./sceneLoaderFlags";
  16. import { IFileRequest } from "../Misc/fileRequest";
  17. import { WebRequest } from "../Misc/webRequest";
  18. import { RequestFileError, ReadFileError } from '../Misc/fileTools';
  19. import { TransformNode } from '../Meshes/transformNode';
  20. import { Geometry } from '../Meshes/geometry';
  21. import { Light } from '../Lights/light';
  22. import { StringTools } from '../Misc/stringTools';
  23. /**
  24. * Type used for the success callback of ImportMesh
  25. */
  26. export type SceneLoaderSuccessCallback = (
  27. meshes: AbstractMesh[],
  28. particleSystems: IParticleSystem[],
  29. skeletons: Skeleton[],
  30. animationGroups: AnimationGroup[],
  31. transformNodes: TransformNode[],
  32. geometries: Geometry[],
  33. lights: Light[]
  34. ) => void;
  35. /**
  36. * Interface used for the result of ImportMeshAsync
  37. */
  38. export interface ISceneLoaderAsyncResult {
  39. /**
  40. * The array of loaded meshes
  41. */
  42. readonly meshes: AbstractMesh[];
  43. /**
  44. * The array of loaded particle systems
  45. */
  46. readonly particleSystems: IParticleSystem[];
  47. /**
  48. * The array of loaded skeletons
  49. */
  50. readonly skeletons: Skeleton[];
  51. /**
  52. * The array of loaded animation groups
  53. */
  54. readonly animationGroups: AnimationGroup[];
  55. /**
  56. * The array of loaded transform nodes
  57. */
  58. readonly transformNodes: TransformNode[];
  59. /**
  60. * The array of loaded geometries
  61. */
  62. readonly geometries: Geometry[];
  63. /**
  64. * The array of loaded lights
  65. */
  66. readonly lights: Light[];
  67. }
  68. /**
  69. * Interface used to represent data loading progression
  70. */
  71. export interface ISceneLoaderProgressEvent {
  72. /**
  73. * Defines if data length to load can be evaluated
  74. */
  75. readonly lengthComputable: boolean;
  76. /**
  77. * Defines the loaded data length
  78. */
  79. readonly loaded: number;
  80. /**
  81. * Defines the data length to load
  82. */
  83. readonly total: number;
  84. }
  85. /**
  86. * Interface used by SceneLoader plugins to define supported file extensions
  87. */
  88. export interface ISceneLoaderPluginExtensions {
  89. /**
  90. * Defines the list of supported extensions
  91. */
  92. [extension: string]: {
  93. isBinary: boolean;
  94. };
  95. }
  96. /**
  97. * Interface used by SceneLoader plugin factory
  98. */
  99. export interface ISceneLoaderPluginFactory {
  100. /**
  101. * Defines the name of the factory
  102. */
  103. name: string;
  104. /**
  105. * Function called to create a new plugin
  106. * @return the new plugin
  107. */
  108. createPlugin(): ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  109. /**
  110. * The callback that returns true if the data can be directly loaded.
  111. * @param data string containing the file data
  112. * @returns if the data can be loaded directly
  113. */
  114. canDirectLoad?(data: string): boolean;
  115. }
  116. /**
  117. * Interface used to define the base of ISceneLoaderPlugin and ISceneLoaderPluginAsync
  118. */
  119. export interface ISceneLoaderPluginBase {
  120. /**
  121. * The friendly name of this plugin.
  122. */
  123. name: string;
  124. /**
  125. * The file extensions supported by this plugin.
  126. */
  127. extensions: string | ISceneLoaderPluginExtensions;
  128. /**
  129. * The callback called when loading from a url.
  130. * @param scene scene loading this url
  131. * @param url url to load
  132. * @param onSuccess callback called when the file successfully loads
  133. * @param onProgress callback called while file is loading (if the server supports this mode)
  134. * @param useArrayBuffer defines a boolean indicating that date must be returned as ArrayBuffer
  135. * @param onError callback called when the file fails to load
  136. * @returns a file request object
  137. */
  138. requestFile?(scene: Scene, url: string, onSuccess: (data: any, request?: WebRequest) => void, onProgress?: (ev: ISceneLoaderProgressEvent) => void, useArrayBuffer?: boolean, onError?: (error: any) => void): IFileRequest;
  139. /**
  140. * The callback called when loading from a file object.
  141. * @param scene scene loading this file
  142. * @param file defines the file to load
  143. * @param onSuccess defines the callback to call when data is loaded
  144. * @param onProgress defines the callback to call during loading process
  145. * @param useArrayBuffer defines a boolean indicating that data must be returned as an ArrayBuffer
  146. * @param onError defines the callback to call when an error occurs
  147. * @returns a file request object
  148. */
  149. readFile?(scene: Scene, file: File, onSuccess: (data: any) => void, onProgress?: (ev: ISceneLoaderProgressEvent) => any, useArrayBuffer?: boolean, onError?: (error: any) => void): IFileRequest;
  150. /**
  151. * The callback that returns true if the data can be directly loaded.
  152. * @param data string containing the file data
  153. * @returns if the data can be loaded directly
  154. */
  155. canDirectLoad?(data: string): boolean;
  156. /**
  157. * The callback that returns the data to pass to the plugin if the data can be directly loaded.
  158. * @param scene scene loading this data
  159. * @param data string containing the data
  160. * @returns data to pass to the plugin
  161. */
  162. directLoad?(scene: Scene, data: string): any;
  163. /**
  164. * The callback that allows custom handling of the root url based on the response url.
  165. * @param rootUrl the original root url
  166. * @param responseURL the response url if available
  167. * @returns the new root url
  168. */
  169. rewriteRootURL?(rootUrl: string, responseURL?: string): string;
  170. }
  171. /**
  172. * Interface used to define a SceneLoader plugin
  173. */
  174. export interface ISceneLoaderPlugin extends ISceneLoaderPluginBase {
  175. /**
  176. * Import meshes into a scene.
  177. * @param meshesNames An array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  178. * @param scene The scene to import into
  179. * @param data The data to import
  180. * @param rootUrl The root url for scene and resources
  181. * @param meshes The meshes array to import into
  182. * @param particleSystems The particle systems array to import into
  183. * @param skeletons The skeletons array to import into
  184. * @param onError The callback when import fails
  185. * @returns True if successful or false otherwise
  186. */
  187. importMesh(meshesNames: any, scene: Scene, data: any, rootUrl: string, meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], onError?: (message: string, exception?: any) => void): boolean;
  188. /**
  189. * Load into a scene.
  190. * @param scene The scene to load into
  191. * @param data The data to import
  192. * @param rootUrl The root url for scene and resources
  193. * @param onError The callback when import fails
  194. * @returns True if successful or false otherwise
  195. */
  196. load(scene: Scene, data: any, rootUrl: string, onError?: (message: string, exception?: any) => void): boolean;
  197. /**
  198. * Load into an asset container.
  199. * @param scene The scene to load into
  200. * @param data The data to import
  201. * @param rootUrl The root url for scene and resources
  202. * @param onError The callback when import fails
  203. * @returns The loaded asset container
  204. */
  205. loadAssetContainer(scene: Scene, data: any, rootUrl: string, onError?: (message: string, exception?: any) => void): AssetContainer;
  206. }
  207. /**
  208. * Interface used to define an async SceneLoader plugin
  209. */
  210. export interface ISceneLoaderPluginAsync extends ISceneLoaderPluginBase {
  211. /**
  212. * Import meshes into a scene.
  213. * @param meshesNames An array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  214. * @param scene The scene to import into
  215. * @param data The data to import
  216. * @param rootUrl The root url for scene and resources
  217. * @param onProgress The callback when the load progresses
  218. * @param fileName Defines the name of the file to load
  219. * @returns The loaded objects (e.g. meshes, particle systems, skeletons, animation groups, etc.)
  220. */
  221. importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void, fileName?: string): Promise<ISceneLoaderAsyncResult>;
  222. /**
  223. * Load into a scene.
  224. * @param scene The scene to load into
  225. * @param data The data to import
  226. * @param rootUrl The root url for scene and resources
  227. * @param onProgress The callback when the load progresses
  228. * @param fileName Defines the name of the file to load
  229. * @returns Nothing
  230. */
  231. loadAsync(scene: Scene, data: any, rootUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void, fileName?: string): Promise<void>;
  232. /**
  233. * Load into an asset container.
  234. * @param scene The scene to load into
  235. * @param data The data to import
  236. * @param rootUrl The root url for scene and resources
  237. * @param onProgress The callback when the load progresses
  238. * @param fileName Defines the name of the file to load
  239. * @returns The loaded asset container
  240. */
  241. loadAssetContainerAsync(scene: Scene, data: any, rootUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void, fileName?: string): Promise<AssetContainer>;
  242. }
  243. /**
  244. * Mode that determines how to handle old animation groups before loading new ones.
  245. */
  246. export enum SceneLoaderAnimationGroupLoadingMode {
  247. /**
  248. * Reset all old animations to initial state then dispose them.
  249. */
  250. Clean = 0,
  251. /**
  252. * Stop all old animations.
  253. */
  254. Stop = 1,
  255. /**
  256. * Restart old animations from first frame.
  257. */
  258. Sync = 2,
  259. /**
  260. * Old animations remains untouched.
  261. */
  262. NoSync = 3
  263. }
  264. /**
  265. * Defines a plugin registered by the SceneLoader
  266. */
  267. interface IRegisteredPlugin {
  268. /**
  269. * Defines the plugin to use
  270. */
  271. plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync | ISceneLoaderPluginFactory;
  272. /**
  273. * Defines if the plugin supports binary data
  274. */
  275. isBinary: boolean;
  276. }
  277. /**
  278. * Defines file information
  279. */
  280. interface IFileInfo {
  281. /**
  282. * Gets the file url
  283. */
  284. url: string;
  285. /**
  286. * Gets the root url
  287. */
  288. rootUrl: string;
  289. /**
  290. * Gets filename
  291. */
  292. name: string;
  293. /**
  294. * Gets the file
  295. */
  296. file: Nullable<File>;
  297. }
  298. /**
  299. * Class used to load scene from various file formats using registered plugins
  300. * @see https://doc.babylonjs.com/how_to/load_from_any_file_type
  301. */
  302. export class SceneLoader {
  303. /**
  304. * No logging while loading
  305. */
  306. public static readonly NO_LOGGING = Constants.SCENELOADER_NO_LOGGING;
  307. /**
  308. * Minimal logging while loading
  309. */
  310. public static readonly MINIMAL_LOGGING = Constants.SCENELOADER_MINIMAL_LOGGING;
  311. /**
  312. * Summary logging while loading
  313. */
  314. public static readonly SUMMARY_LOGGING = Constants.SCENELOADER_SUMMARY_LOGGING;
  315. /**
  316. * Detailed logging while loading
  317. */
  318. public static readonly DETAILED_LOGGING = Constants.SCENELOADER_DETAILED_LOGGING;
  319. /**
  320. * Gets or sets a boolean indicating if entire scene must be loaded even if scene contains incremental data
  321. */
  322. public static get ForceFullSceneLoadingForIncremental() {
  323. return SceneLoaderFlags.ForceFullSceneLoadingForIncremental;
  324. }
  325. public static set ForceFullSceneLoadingForIncremental(value: boolean) {
  326. SceneLoaderFlags.ForceFullSceneLoadingForIncremental = value;
  327. }
  328. /**
  329. * Gets or sets a boolean indicating if loading screen must be displayed while loading a scene
  330. */
  331. public static get ShowLoadingScreen(): boolean {
  332. return SceneLoaderFlags.ShowLoadingScreen;
  333. }
  334. public static set ShowLoadingScreen(value: boolean) {
  335. SceneLoaderFlags.ShowLoadingScreen = value;
  336. }
  337. /**
  338. * Defines the current logging level (while loading the scene)
  339. * @ignorenaming
  340. */
  341. public static get loggingLevel(): number {
  342. return SceneLoaderFlags.loggingLevel;
  343. }
  344. public static set loggingLevel(value: number) {
  345. SceneLoaderFlags.loggingLevel = value;
  346. }
  347. /**
  348. * Gets or set a boolean indicating if matrix weights must be cleaned upon loading
  349. */
  350. public static get CleanBoneMatrixWeights(): boolean {
  351. return SceneLoaderFlags.CleanBoneMatrixWeights;
  352. }
  353. public static set CleanBoneMatrixWeights(value: boolean) {
  354. SceneLoaderFlags.CleanBoneMatrixWeights = value;
  355. }
  356. // Members
  357. /**
  358. * Event raised when a plugin is used to load a scene
  359. */
  360. public static OnPluginActivatedObservable = new Observable<ISceneLoaderPlugin | ISceneLoaderPluginAsync>();
  361. private static _registeredPlugins: { [extension: string]: IRegisteredPlugin } = {};
  362. private static _showingLoadingScreen = false;
  363. /**
  364. * Gets the default plugin (used to load Babylon files)
  365. * @returns the .babylon plugin
  366. */
  367. public static GetDefaultPlugin(): IRegisteredPlugin {
  368. return SceneLoader._registeredPlugins[".babylon"];
  369. }
  370. private static _GetPluginForExtension(extension: string): IRegisteredPlugin {
  371. var registeredPlugin = SceneLoader._registeredPlugins[extension];
  372. if (registeredPlugin) {
  373. return registeredPlugin;
  374. }
  375. Logger.Warn("Unable to find a plugin to load " + extension + " files. Trying to use .babylon default plugin. To load from a specific filetype (eg. gltf) see: https://doc.babylonjs.com/how_to/load_from_any_file_type");
  376. return SceneLoader.GetDefaultPlugin();
  377. }
  378. private static _GetPluginForDirectLoad(data: string): IRegisteredPlugin {
  379. for (var extension in SceneLoader._registeredPlugins) {
  380. var plugin = SceneLoader._registeredPlugins[extension].plugin;
  381. if (plugin.canDirectLoad && plugin.canDirectLoad(data)) {
  382. return SceneLoader._registeredPlugins[extension];
  383. }
  384. }
  385. return SceneLoader.GetDefaultPlugin();
  386. }
  387. private static _GetPluginForFilename(sceneFilename: string): IRegisteredPlugin {
  388. var queryStringPosition = sceneFilename.indexOf("?");
  389. if (queryStringPosition !== -1) {
  390. sceneFilename = sceneFilename.substring(0, queryStringPosition);
  391. }
  392. var dotPosition = sceneFilename.lastIndexOf(".");
  393. var extension = sceneFilename.substring(dotPosition, sceneFilename.length).toLowerCase();
  394. return SceneLoader._GetPluginForExtension(extension);
  395. }
  396. private static _GetDirectLoad(sceneFilename: string): Nullable<string> {
  397. if (sceneFilename.substr(0, 5) === "data:") {
  398. return sceneFilename.substr(5);
  399. }
  400. return null;
  401. }
  402. private static _LoadData(fileInfo: IFileInfo, scene: Scene, onSuccess: (plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync, data: any, responseURL?: string) => void, onProgress: ((event: ISceneLoaderProgressEvent) => void) | undefined, onError: (message: string, exception?: any) => void, onDispose: () => void, pluginExtension: Nullable<string>): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  403. const directLoad = SceneLoader._GetDirectLoad(fileInfo.url);
  404. const registeredPlugin = pluginExtension ? SceneLoader._GetPluginForExtension(pluginExtension) : (directLoad ? SceneLoader._GetPluginForDirectLoad(fileInfo.url) : SceneLoader._GetPluginForFilename(fileInfo.url));
  405. let plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  406. if ((registeredPlugin.plugin as ISceneLoaderPluginFactory).createPlugin !== undefined) {
  407. plugin = (registeredPlugin.plugin as ISceneLoaderPluginFactory).createPlugin();
  408. }
  409. else {
  410. plugin = <any>registeredPlugin.plugin;
  411. }
  412. if (!plugin) {
  413. throw "The loader plugin corresponding to the file type you are trying to load has not been found. If using es6, please import the plugin you wish to use before.";
  414. }
  415. SceneLoader.OnPluginActivatedObservable.notifyObservers(plugin);
  416. if (directLoad) {
  417. if (plugin.directLoad) {
  418. const result = plugin.directLoad(scene, directLoad);
  419. if (result.then) {
  420. result.then((data: any) => {
  421. onSuccess(plugin, data);
  422. }).catch((error: any) => {
  423. onError("Error in directLoad of _loadData: " + error, error);
  424. });
  425. }
  426. else {
  427. onSuccess(plugin, result);
  428. }
  429. } else {
  430. onSuccess(plugin, directLoad);
  431. }
  432. return plugin;
  433. }
  434. const useArrayBuffer = registeredPlugin.isBinary;
  435. const dataCallback = (data: any, responseURL?: string) => {
  436. if (scene.isDisposed) {
  437. onError("Scene has been disposed");
  438. return;
  439. }
  440. onSuccess(plugin, data, responseURL);
  441. };
  442. let request: Nullable<IFileRequest> = null;
  443. let pluginDisposed = false;
  444. const onDisposeObservable = (plugin as any).onDisposeObservable as Observable<ISceneLoaderPlugin | ISceneLoaderPluginAsync>;
  445. if (onDisposeObservable) {
  446. onDisposeObservable.add(() => {
  447. pluginDisposed = true;
  448. if (request) {
  449. request.abort();
  450. request = null;
  451. }
  452. onDispose();
  453. });
  454. }
  455. const manifestChecked = () => {
  456. if (pluginDisposed) {
  457. return;
  458. }
  459. const successCallback = (data: string | ArrayBuffer, request?: WebRequest) => {
  460. dataCallback(data, request ? request.responseURL : undefined);
  461. };
  462. const errorCallback = (error: RequestFileError) => {
  463. onError(error.message, error);
  464. };
  465. request = plugin.requestFile
  466. ? plugin.requestFile(scene, fileInfo.url, successCallback, onProgress, useArrayBuffer, errorCallback)
  467. : scene._requestFile(fileInfo.url, successCallback, onProgress, true, useArrayBuffer, errorCallback);
  468. };
  469. if (StringTools.StartsWith(fileInfo.url, "file:")) {
  470. // Loading file from disk via input file or drag'n'drop
  471. if (fileInfo.file) {
  472. const errorCallback = (error: ReadFileError) => {
  473. onError(error.message, error);
  474. };
  475. request = plugin.readFile
  476. ? plugin.readFile(scene, fileInfo.file, dataCallback, onProgress, useArrayBuffer, errorCallback)
  477. : scene._readFile(fileInfo.file, dataCallback, onProgress, useArrayBuffer, errorCallback);
  478. } else {
  479. onError("Unable to find file named " + fileInfo.name);
  480. }
  481. } else {
  482. const engine = scene.getEngine();
  483. let canUseOfflineSupport = engine.enableOfflineSupport;
  484. if (canUseOfflineSupport) {
  485. // Also check for exceptions
  486. let exceptionFound = false;
  487. for (var regex of scene.disableOfflineSupportExceptionRules) {
  488. if (regex.test(fileInfo.url)) {
  489. exceptionFound = true;
  490. break;
  491. }
  492. }
  493. canUseOfflineSupport = !exceptionFound;
  494. }
  495. if (canUseOfflineSupport && Engine.OfflineProviderFactory) {
  496. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  497. scene.offlineProvider = Engine.OfflineProviderFactory(fileInfo.url, manifestChecked, engine.disableManifestCheck);
  498. }
  499. else {
  500. manifestChecked();
  501. }
  502. }
  503. return plugin;
  504. }
  505. private static _GetFileInfo(rootUrl: string, sceneFilename: string | File): Nullable<IFileInfo> {
  506. let url: string;
  507. let name: string;
  508. let file: Nullable<File> = null;
  509. if (!sceneFilename) {
  510. url = rootUrl;
  511. name = Tools.GetFilename(rootUrl);
  512. rootUrl = Tools.GetFolderPath(rootUrl);
  513. }
  514. else if ((sceneFilename as File).name) {
  515. const sceneFile = sceneFilename as File;
  516. url = rootUrl + sceneFile.name;
  517. name = sceneFile.name;
  518. file = sceneFile;
  519. }
  520. else if (typeof sceneFilename === "string" && Tools.IsBase64(sceneFilename)) {
  521. url = rootUrl + sceneFilename;
  522. name = "";
  523. }
  524. else {
  525. const filename = sceneFilename as string;
  526. if (filename.substr(0, 1) === "/") {
  527. Tools.Error("Wrong sceneFilename parameter");
  528. return null;
  529. }
  530. url = rootUrl + filename;
  531. name = filename;
  532. }
  533. if (!file && name && StringTools.StartsWith(url, "file:")) {
  534. file = FilesInputStore.FilesToLoad[name.toLowerCase()] || null;
  535. }
  536. return {
  537. url: url,
  538. rootUrl: rootUrl,
  539. name: name,
  540. file: file
  541. };
  542. }
  543. // Public functions
  544. /**
  545. * Gets a plugin that can load the given extension
  546. * @param extension defines the extension to load
  547. * @returns a plugin or null if none works
  548. */
  549. public static GetPluginForExtension(extension: string): ISceneLoaderPlugin | ISceneLoaderPluginAsync | ISceneLoaderPluginFactory {
  550. return SceneLoader._GetPluginForExtension(extension).plugin;
  551. }
  552. /**
  553. * Gets a boolean indicating that the given extension can be loaded
  554. * @param extension defines the extension to load
  555. * @returns true if the extension is supported
  556. */
  557. public static IsPluginForExtensionAvailable(extension: string): boolean {
  558. return !!SceneLoader._registeredPlugins[extension];
  559. }
  560. /**
  561. * Adds a new plugin to the list of registered plugins
  562. * @param plugin defines the plugin to add
  563. */
  564. public static RegisterPlugin(plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync): void {
  565. if (typeof plugin.extensions === "string") {
  566. var extension = <string>plugin.extensions;
  567. SceneLoader._registeredPlugins[extension.toLowerCase()] = {
  568. plugin: plugin,
  569. isBinary: false
  570. };
  571. }
  572. else {
  573. var extensions = <ISceneLoaderPluginExtensions>plugin.extensions;
  574. Object.keys(extensions).forEach((extension) => {
  575. SceneLoader._registeredPlugins[extension.toLowerCase()] = {
  576. plugin: plugin,
  577. isBinary: extensions[extension].isBinary
  578. };
  579. });
  580. }
  581. }
  582. /**
  583. * Import meshes into a scene
  584. * @param meshNames an array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  585. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  586. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  587. * @param scene the instance of BABYLON.Scene to append to
  588. * @param onSuccess a callback with a list of imported meshes, particleSystems, skeletons, and animationGroups when import succeeds
  589. * @param onProgress a callback with a progress event for each file being loaded
  590. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  591. * @param pluginExtension the extension used to determine the plugin
  592. * @returns The loaded plugin
  593. */
  594. public static ImportMesh(meshNames: any, rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onSuccess: Nullable<SceneLoaderSuccessCallback> = null, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  595. if (!scene) {
  596. Logger.Error("No scene available to import mesh to");
  597. return null;
  598. }
  599. const fileInfo = SceneLoader._GetFileInfo(rootUrl, sceneFilename);
  600. if (!fileInfo) {
  601. return null;
  602. }
  603. var loadingToken = {};
  604. scene._addPendingData(loadingToken);
  605. var disposeHandler = () => {
  606. scene._removePendingData(loadingToken);
  607. };
  608. var errorHandler = (message: string, exception?: any) => {
  609. let errorMessage = "Unable to import meshes from " + fileInfo.url + ": " + message;
  610. if (onError) {
  611. onError(scene, errorMessage, exception);
  612. } else {
  613. Logger.Error(errorMessage);
  614. // should the exception be thrown?
  615. }
  616. disposeHandler();
  617. };
  618. var progressHandler = onProgress ? (event: ISceneLoaderProgressEvent) => {
  619. try {
  620. onProgress(event);
  621. }
  622. catch (e) {
  623. errorHandler("Error in onProgress callback: " + e, e);
  624. }
  625. } : undefined;
  626. var successHandler: SceneLoaderSuccessCallback = (meshes, particleSystems, skeletons, animationGroups, transformNodes, geometries, lights) => {
  627. scene.importedMeshesFiles.push(fileInfo.url);
  628. if (onSuccess) {
  629. try {
  630. onSuccess(meshes, particleSystems, skeletons, animationGroups, transformNodes, geometries, lights);
  631. }
  632. catch (e) {
  633. errorHandler("Error in onSuccess callback: " + e, e);
  634. }
  635. }
  636. scene._removePendingData(loadingToken);
  637. };
  638. return SceneLoader._LoadData(fileInfo, scene, (plugin, data, responseURL) => {
  639. if (plugin.rewriteRootURL) {
  640. fileInfo.rootUrl = plugin.rewriteRootURL(fileInfo.rootUrl, responseURL);
  641. }
  642. if ((<any>plugin).importMesh) {
  643. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  644. var meshes = new Array<AbstractMesh>();
  645. var particleSystems = new Array<IParticleSystem>();
  646. var skeletons = new Array<Skeleton>();
  647. if (!syncedPlugin.importMesh(meshNames, scene, data, fileInfo.rootUrl, meshes, particleSystems, skeletons, errorHandler)) {
  648. return;
  649. }
  650. scene.loadingPluginName = plugin.name;
  651. successHandler(meshes, particleSystems, skeletons, [], [], [], []);
  652. }
  653. else {
  654. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  655. asyncedPlugin.importMeshAsync(meshNames, scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then((result) => {
  656. scene.loadingPluginName = plugin.name;
  657. successHandler(result.meshes, result.particleSystems, result.skeletons, result.animationGroups, result.transformNodes, result.geometries, result.lights);
  658. }).catch((error) => {
  659. errorHandler(error.message, error);
  660. });
  661. }
  662. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  663. }
  664. /**
  665. * Import meshes into a scene
  666. * @param meshNames an array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  667. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  668. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  669. * @param scene the instance of BABYLON.Scene to append to
  670. * @param onProgress a callback with a progress event for each file being loaded
  671. * @param pluginExtension the extension used to determine the plugin
  672. * @returns The loaded list of imported meshes, particle systems, skeletons, and animation groups
  673. */
  674. public static ImportMeshAsync(meshNames: any, rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<ISceneLoaderAsyncResult> {
  675. return new Promise((resolve, reject) => {
  676. SceneLoader.ImportMesh(meshNames, rootUrl, sceneFilename, scene, (meshes, particleSystems, skeletons, animationGroups, transformNodes, geometries, lights) => {
  677. resolve({
  678. meshes: meshes,
  679. particleSystems: particleSystems,
  680. skeletons: skeletons,
  681. animationGroups: animationGroups,
  682. transformNodes: transformNodes,
  683. geometries: geometries,
  684. lights: lights
  685. });
  686. }, onProgress, (scene, message, exception) => {
  687. reject(exception || new Error(message));
  688. }, pluginExtension);
  689. });
  690. }
  691. /**
  692. * Load a scene
  693. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  694. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  695. * @param engine is the instance of BABYLON.Engine to use to create the scene
  696. * @param onSuccess a callback with the scene when import succeeds
  697. * @param onProgress a callback with a progress event for each file being loaded
  698. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  699. * @param pluginExtension the extension used to determine the plugin
  700. * @returns The loaded plugin
  701. */
  702. public static Load(rootUrl: string, sceneFilename: string | File = "", engine: Nullable<Engine> = EngineStore.LastCreatedEngine, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  703. if (!engine) {
  704. Tools.Error("No engine available");
  705. return null;
  706. }
  707. return SceneLoader.Append(rootUrl, sceneFilename, new Scene(engine), onSuccess, onProgress, onError, pluginExtension);
  708. }
  709. /**
  710. * Load a scene
  711. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  712. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  713. * @param engine is the instance of BABYLON.Engine to use to create the scene
  714. * @param onProgress a callback with a progress event for each file being loaded
  715. * @param pluginExtension the extension used to determine the plugin
  716. * @returns The loaded scene
  717. */
  718. public static LoadAsync(rootUrl: string, sceneFilename: string | File = "", engine: Nullable<Engine> = EngineStore.LastCreatedEngine, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<Scene> {
  719. return new Promise((resolve, reject) => {
  720. SceneLoader.Load(rootUrl, sceneFilename, engine, (scene) => {
  721. resolve(scene);
  722. }, onProgress, (scene, message, exception) => {
  723. reject(exception || new Error(message));
  724. }, pluginExtension);
  725. });
  726. }
  727. /**
  728. * Append a scene
  729. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  730. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  731. * @param scene is the instance of BABYLON.Scene to append to
  732. * @param onSuccess a callback with the scene when import succeeds
  733. * @param onProgress a callback with a progress event for each file being loaded
  734. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  735. * @param pluginExtension the extension used to determine the plugin
  736. * @returns The loaded plugin
  737. */
  738. public static Append(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  739. if (!scene) {
  740. Logger.Error("No scene available to append to");
  741. return null;
  742. }
  743. const fileInfo = SceneLoader._GetFileInfo(rootUrl, sceneFilename);
  744. if (!fileInfo) {
  745. return null;
  746. }
  747. if (SceneLoader.ShowLoadingScreen && !this._showingLoadingScreen) {
  748. this._showingLoadingScreen = true;
  749. scene.getEngine().displayLoadingUI();
  750. scene.executeWhenReady(() => {
  751. scene.getEngine().hideLoadingUI();
  752. this._showingLoadingScreen = false;
  753. });
  754. }
  755. var loadingToken = {};
  756. scene._addPendingData(loadingToken);
  757. var disposeHandler = () => {
  758. scene._removePendingData(loadingToken);
  759. };
  760. var errorHandler = (message: Nullable<string>, exception?: any) => {
  761. let errorMessage = "Unable to load from " + fileInfo.url + (message ? ": " + message : "");
  762. if (onError) {
  763. onError(scene, errorMessage, exception);
  764. } else {
  765. Logger.Error(errorMessage);
  766. // should the exception be thrown?
  767. }
  768. disposeHandler();
  769. };
  770. var progressHandler = onProgress ? (event: ISceneLoaderProgressEvent) => {
  771. try {
  772. onProgress(event);
  773. }
  774. catch (e) {
  775. errorHandler("Error in onProgress callback", e);
  776. }
  777. } : undefined;
  778. var successHandler = () => {
  779. if (onSuccess) {
  780. try {
  781. onSuccess(scene);
  782. }
  783. catch (e) {
  784. errorHandler("Error in onSuccess callback", e);
  785. }
  786. }
  787. scene._removePendingData(loadingToken);
  788. };
  789. return SceneLoader._LoadData(fileInfo, scene, (plugin, data) => {
  790. if ((<any>plugin).load) {
  791. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  792. if (!syncedPlugin.load(scene, data, fileInfo.rootUrl, errorHandler)) {
  793. return;
  794. }
  795. scene.loadingPluginName = plugin.name;
  796. successHandler();
  797. } else {
  798. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  799. asyncedPlugin.loadAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then(() => {
  800. scene.loadingPluginName = plugin.name;
  801. successHandler();
  802. }).catch((error) => {
  803. errorHandler(error.message, error);
  804. });
  805. }
  806. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  807. }
  808. /**
  809. * Append a scene
  810. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  811. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  812. * @param scene is the instance of BABYLON.Scene to append to
  813. * @param onProgress a callback with a progress event for each file being loaded
  814. * @param pluginExtension the extension used to determine the plugin
  815. * @returns The given scene
  816. */
  817. public static AppendAsync(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<Scene> {
  818. return new Promise((resolve, reject) => {
  819. SceneLoader.Append(rootUrl, sceneFilename, scene, (scene) => {
  820. resolve(scene);
  821. }, onProgress, (scene, message, exception) => {
  822. reject(exception || new Error(message));
  823. }, pluginExtension);
  824. });
  825. }
  826. /**
  827. * Load a scene into an asset container
  828. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  829. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  830. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  831. * @param onSuccess a callback with the scene when import succeeds
  832. * @param onProgress a callback with a progress event for each file being loaded
  833. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  834. * @param pluginExtension the extension used to determine the plugin
  835. * @returns The loaded plugin
  836. */
  837. public static LoadAssetContainer(
  838. rootUrl: string,
  839. sceneFilename: string | File = "",
  840. scene: Nullable<Scene> = EngineStore.LastCreatedScene,
  841. onSuccess: Nullable<(assets: AssetContainer) => void> = null,
  842. onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null,
  843. onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null,
  844. pluginExtension: Nullable<string> = null
  845. ): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  846. if (!scene) {
  847. Logger.Error("No scene available to load asset container to");
  848. return null;
  849. }
  850. const fileInfo = SceneLoader._GetFileInfo(rootUrl, sceneFilename);
  851. if (!fileInfo) {
  852. return null;
  853. }
  854. var loadingToken = {};
  855. scene._addPendingData(loadingToken);
  856. var disposeHandler = () => {
  857. scene._removePendingData(loadingToken);
  858. };
  859. var errorHandler = (message: Nullable<string>, exception?: any) => {
  860. let errorMessage = "Unable to load assets from " + fileInfo.url + (message ? ": " + message : "");
  861. if (exception && exception.message) {
  862. errorMessage += ` (${exception.message})`;
  863. }
  864. if (onError) {
  865. onError(scene, errorMessage, exception);
  866. } else {
  867. Logger.Error(errorMessage);
  868. // should the exception be thrown?
  869. }
  870. disposeHandler();
  871. };
  872. var progressHandler = onProgress ? (event: ISceneLoaderProgressEvent) => {
  873. try {
  874. onProgress(event);
  875. }
  876. catch (e) {
  877. errorHandler("Error in onProgress callback", e);
  878. }
  879. } : undefined;
  880. var successHandler = (assets: AssetContainer) => {
  881. if (onSuccess) {
  882. try {
  883. onSuccess(assets);
  884. }
  885. catch (e) {
  886. errorHandler("Error in onSuccess callback", e);
  887. }
  888. }
  889. scene._removePendingData(loadingToken);
  890. };
  891. return SceneLoader._LoadData(fileInfo, scene, (plugin, data) => {
  892. if ((<any>plugin).loadAssetContainer) {
  893. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  894. var assetContainer = syncedPlugin.loadAssetContainer(scene, data, fileInfo.rootUrl, errorHandler);
  895. if (!assetContainer) {
  896. return;
  897. }
  898. scene.loadingPluginName = plugin.name;
  899. successHandler(assetContainer);
  900. } else if ((<any>plugin).loadAssetContainerAsync) {
  901. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  902. asyncedPlugin.loadAssetContainerAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then((assetContainer) => {
  903. scene.loadingPluginName = plugin.name;
  904. successHandler(assetContainer);
  905. }).catch((error) => {
  906. errorHandler(error.message, error);
  907. });
  908. } else {
  909. errorHandler("LoadAssetContainer is not supported by this plugin. Plugin did not provide a loadAssetContainer or loadAssetContainerAsync method.");
  910. }
  911. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  912. }
  913. /**
  914. * Load a scene into an asset container
  915. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  916. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene (default: empty string)
  917. * @param scene is the instance of Scene to append to
  918. * @param onProgress a callback with a progress event for each file being loaded
  919. * @param pluginExtension the extension used to determine the plugin
  920. * @returns The loaded asset container
  921. */
  922. public static LoadAssetContainerAsync(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<AssetContainer> {
  923. return new Promise((resolve, reject) => {
  924. SceneLoader.LoadAssetContainer(rootUrl, sceneFilename, scene, (assetContainer) => {
  925. resolve(assetContainer);
  926. }, onProgress, (scene, message, exception) => {
  927. reject(exception || new Error(message));
  928. }, pluginExtension);
  929. });
  930. }
  931. /**
  932. * Import animations from a file into a scene
  933. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  934. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  935. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  936. * @param overwriteAnimations when true, animations are cleaned before importing new ones. Animations are appended otherwise
  937. * @param animationGroupLoadingMode defines how to handle old animations groups before importing new ones
  938. * @param targetConverter defines a function used to convert animation targets from loaded scene to current scene (default: search node by name)
  939. * @param onSuccess a callback with the scene when import succeeds
  940. * @param onProgress a callback with a progress event for each file being loaded
  941. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  942. * @param pluginExtension the extension used to determine the plugin
  943. */
  944. public static ImportAnimations(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, overwriteAnimations = true, animationGroupLoadingMode = SceneLoaderAnimationGroupLoadingMode.Clean, targetConverter: Nullable<(target: any) => any> = null, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): void {
  945. if (!scene) {
  946. Logger.Error("No scene available to load animations to");
  947. return;
  948. }
  949. if (overwriteAnimations) {
  950. // Reset, stop and dispose all animations before loading new ones
  951. for (let animatable of scene.animatables) {
  952. animatable.reset();
  953. }
  954. scene.stopAllAnimations();
  955. scene.animationGroups.slice().forEach((animationGroup) => {
  956. animationGroup.dispose();
  957. });
  958. let nodes = scene.getNodes();
  959. nodes.forEach((node) => {
  960. if (node.animations) {
  961. node.animations = [];
  962. }
  963. });
  964. }
  965. else {
  966. switch (animationGroupLoadingMode) {
  967. case SceneLoaderAnimationGroupLoadingMode.Clean:
  968. scene.animationGroups.slice().forEach((animationGroup) => {
  969. animationGroup.dispose();
  970. });
  971. break;
  972. case SceneLoaderAnimationGroupLoadingMode.Stop:
  973. scene.animationGroups.forEach((animationGroup) => {
  974. animationGroup.stop();
  975. });
  976. break;
  977. case SceneLoaderAnimationGroupLoadingMode.Sync:
  978. scene.animationGroups.forEach((animationGroup) => {
  979. animationGroup.reset();
  980. animationGroup.restart();
  981. });
  982. break;
  983. case SceneLoaderAnimationGroupLoadingMode.NoSync:
  984. // nothing to do
  985. break;
  986. default:
  987. Logger.Error("Unknown animation group loading mode value '" + animationGroupLoadingMode + "'");
  988. return;
  989. }
  990. }
  991. let startingIndexForNewAnimatables = scene.animatables.length;
  992. let onAssetContainerLoaded = (container: AssetContainer) => {
  993. container.mergeAnimationsTo(scene, scene.animatables.slice(startingIndexForNewAnimatables), targetConverter);
  994. container.dispose();
  995. scene.onAnimationFileImportedObservable.notifyObservers(scene);
  996. if (onSuccess) {
  997. onSuccess(scene);
  998. }
  999. };
  1000. this.LoadAssetContainer(rootUrl, sceneFilename, scene, onAssetContainerLoaded, onProgress, onError, pluginExtension);
  1001. }
  1002. /**
  1003. * Import animations from a file into a scene
  1004. * @param rootUrl a string that defines the root url for the scene and resources or the concatenation of rootURL and filename (e.g. http://example.com/test.glb)
  1005. * @param sceneFilename a string that defines the name of the scene file or starts with "data:" following by the stringified version of the scene or a File object (default: empty string)
  1006. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  1007. * @param overwriteAnimations when true, animations are cleaned before importing new ones. Animations are appended otherwise
  1008. * @param animationGroupLoadingMode defines how to handle old animations groups before importing new ones
  1009. * @param targetConverter defines a function used to convert animation targets from loaded scene to current scene (default: search node by name)
  1010. * @param onSuccess a callback with the scene when import succeeds
  1011. * @param onProgress a callback with a progress event for each file being loaded
  1012. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  1013. * @param pluginExtension the extension used to determine the plugin
  1014. * @returns the updated scene with imported animations
  1015. */
  1016. public static ImportAnimationsAsync(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, overwriteAnimations = true, animationGroupLoadingMode = SceneLoaderAnimationGroupLoadingMode.Clean, targetConverter: Nullable<(target: any) => any> = null, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Promise<Scene> {
  1017. return new Promise((resolve, reject) => {
  1018. SceneLoader.ImportAnimations(rootUrl, sceneFilename, scene, overwriteAnimations, animationGroupLoadingMode, targetConverter, (_scene: Scene) => {
  1019. resolve(_scene);
  1020. }, onProgress, (_scene: Scene, message: string, exception: any) => {
  1021. reject(exception || new Error(message));
  1022. }, pluginExtension);
  1023. });
  1024. }
  1025. }