sceneLoader.ts 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. * Detailled 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. },
  689. pluginExtension);
  690. });
  691. }
  692. /**
  693. * Load a scene
  694. * @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)
  695. * @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)
  696. * @param engine is the instance of BABYLON.Engine to use to create the scene
  697. * @param onSuccess a callback with the scene when import succeeds
  698. * @param onProgress a callback with a progress event for each file being loaded
  699. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  700. * @param pluginExtension the extension used to determine the plugin
  701. * @returns The loaded plugin
  702. */
  703. 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> {
  704. if (!engine) {
  705. Tools.Error("No engine available");
  706. return null;
  707. }
  708. return SceneLoader.Append(rootUrl, sceneFilename, new Scene(engine), onSuccess, onProgress, onError, pluginExtension);
  709. }
  710. /**
  711. * Load a scene
  712. * @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)
  713. * @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)
  714. * @param engine is the instance of BABYLON.Engine to use to create the scene
  715. * @param onProgress a callback with a progress event for each file being loaded
  716. * @param pluginExtension the extension used to determine the plugin
  717. * @returns The loaded scene
  718. */
  719. 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> {
  720. return new Promise((resolve, reject) => {
  721. SceneLoader.Load(rootUrl, sceneFilename, engine, (scene) => {
  722. resolve(scene);
  723. }, onProgress, (scene, message, exception) => {
  724. reject(exception || new Error(message));
  725. }, pluginExtension);
  726. });
  727. }
  728. /**
  729. * Append a scene
  730. * @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)
  731. * @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)
  732. * @param scene is the instance of BABYLON.Scene to append to
  733. * @param onSuccess a callback with the scene when import succeeds
  734. * @param onProgress a callback with a progress event for each file being loaded
  735. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  736. * @param pluginExtension the extension used to determine the plugin
  737. * @returns The loaded plugin
  738. */
  739. 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> {
  740. if (!scene) {
  741. Logger.Error("No scene available to append to");
  742. return null;
  743. }
  744. const fileInfo = SceneLoader._GetFileInfo(rootUrl, sceneFilename);
  745. if (!fileInfo) {
  746. return null;
  747. }
  748. if (SceneLoader.ShowLoadingScreen && !this._showingLoadingScreen) {
  749. this._showingLoadingScreen = true;
  750. scene.getEngine().displayLoadingUI();
  751. scene.executeWhenReady(() => {
  752. scene.getEngine().hideLoadingUI();
  753. this._showingLoadingScreen = false;
  754. });
  755. }
  756. var loadingToken = {};
  757. scene._addPendingData(loadingToken);
  758. var disposeHandler = () => {
  759. scene._removePendingData(loadingToken);
  760. };
  761. var errorHandler = (message: Nullable<string>, exception?: any) => {
  762. let errorMessage = "Unable to load from " + fileInfo.url + (message ? ": " + message : "");
  763. if (onError) {
  764. onError(scene, errorMessage, exception);
  765. } else {
  766. Logger.Error(errorMessage);
  767. // should the exception be thrown?
  768. }
  769. disposeHandler();
  770. };
  771. var progressHandler = onProgress ? (event: ISceneLoaderProgressEvent) => {
  772. try {
  773. onProgress(event);
  774. }
  775. catch (e) {
  776. errorHandler("Error in onProgress callback", e);
  777. }
  778. } : undefined;
  779. var successHandler = () => {
  780. if (onSuccess) {
  781. try {
  782. onSuccess(scene);
  783. }
  784. catch (e) {
  785. errorHandler("Error in onSuccess callback", e);
  786. }
  787. }
  788. scene._removePendingData(loadingToken);
  789. };
  790. return SceneLoader._LoadData(fileInfo, scene, (plugin, data) => {
  791. if ((<any>plugin).load) {
  792. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  793. if (!syncedPlugin.load(scene, data, fileInfo.rootUrl, errorHandler)) {
  794. return;
  795. }
  796. scene.loadingPluginName = plugin.name;
  797. successHandler();
  798. } else {
  799. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  800. asyncedPlugin.loadAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then(() => {
  801. scene.loadingPluginName = plugin.name;
  802. successHandler();
  803. }).catch((error) => {
  804. errorHandler(error.message, error);
  805. });
  806. }
  807. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  808. }
  809. /**
  810. * Append a scene
  811. * @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)
  812. * @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)
  813. * @param scene is the instance of BABYLON.Scene to append to
  814. * @param onProgress a callback with a progress event for each file being loaded
  815. * @param pluginExtension the extension used to determine the plugin
  816. * @returns The given scene
  817. */
  818. 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> {
  819. return new Promise((resolve, reject) => {
  820. SceneLoader.Append(rootUrl, sceneFilename, scene, (scene) => {
  821. resolve(scene);
  822. }, onProgress, (scene, message, exception) => {
  823. reject(exception || new Error(message));
  824. }, pluginExtension);
  825. });
  826. }
  827. /**
  828. * Load a scene into an asset container
  829. * @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)
  830. * @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)
  831. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  832. * @param onSuccess a callback with the scene when import succeeds
  833. * @param onProgress a callback with a progress event for each file being loaded
  834. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  835. * @param pluginExtension the extension used to determine the plugin
  836. * @returns The loaded plugin
  837. */
  838. public static LoadAssetContainer(
  839. rootUrl: string,
  840. sceneFilename: string | File = "",
  841. scene: Nullable<Scene> = EngineStore.LastCreatedScene,
  842. onSuccess: Nullable<(assets: AssetContainer) => void> = null,
  843. onProgress: Nullable<(event: ISceneLoaderProgressEvent) => void> = null,
  844. onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null,
  845. pluginExtension: Nullable<string> = null
  846. ): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
  847. if (!scene) {
  848. Logger.Error("No scene available to load asset container to");
  849. return null;
  850. }
  851. const fileInfo = SceneLoader._GetFileInfo(rootUrl, sceneFilename);
  852. if (!fileInfo) {
  853. return null;
  854. }
  855. var loadingToken = {};
  856. scene._addPendingData(loadingToken);
  857. var disposeHandler = () => {
  858. scene._removePendingData(loadingToken);
  859. };
  860. var errorHandler = (message: Nullable<string>, exception?: any) => {
  861. let errorMessage = "Unable to load assets from " + fileInfo.url + (message ? ": " + message : "");
  862. if (exception && exception.message) {
  863. errorMessage += ` (${exception.message})`;
  864. }
  865. if (onError) {
  866. onError(scene, errorMessage, exception);
  867. } else {
  868. Logger.Error(errorMessage);
  869. // should the exception be thrown?
  870. }
  871. disposeHandler();
  872. };
  873. var progressHandler = onProgress ? (event: ISceneLoaderProgressEvent) => {
  874. try {
  875. onProgress(event);
  876. }
  877. catch (e) {
  878. errorHandler("Error in onProgress callback", e);
  879. }
  880. } : undefined;
  881. var successHandler = (assets: AssetContainer) => {
  882. if (onSuccess) {
  883. try {
  884. onSuccess(assets);
  885. }
  886. catch (e) {
  887. errorHandler("Error in onSuccess callback", e);
  888. }
  889. }
  890. scene._removePendingData(loadingToken);
  891. };
  892. return SceneLoader._LoadData(fileInfo, scene, (plugin, data) => {
  893. if ((<any>plugin).loadAssetContainer) {
  894. var syncedPlugin = <ISceneLoaderPlugin>plugin;
  895. var assetContainer = syncedPlugin.loadAssetContainer(scene, data, fileInfo.rootUrl, errorHandler);
  896. if (!assetContainer) {
  897. return;
  898. }
  899. scene.loadingPluginName = plugin.name;
  900. successHandler(assetContainer);
  901. } else if ((<any>plugin).loadAssetContainerAsync) {
  902. var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
  903. asyncedPlugin.loadAssetContainerAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then((assetContainer) => {
  904. scene.loadingPluginName = plugin.name;
  905. successHandler(assetContainer);
  906. }).catch((error) => {
  907. errorHandler(error.message, error);
  908. });
  909. } else {
  910. errorHandler("LoadAssetContainer is not supported by this plugin. Plugin did not provide a loadAssetContainer or loadAssetContainerAsync method.");
  911. }
  912. }, progressHandler, errorHandler, disposeHandler, pluginExtension);
  913. }
  914. /**
  915. * Load a scene into an asset container
  916. * @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)
  917. * @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)
  918. * @param scene is the instance of Scene to append to
  919. * @param onProgress a callback with a progress event for each file being loaded
  920. * @param pluginExtension the extension used to determine the plugin
  921. * @returns The loaded asset container
  922. */
  923. 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> {
  924. return new Promise((resolve, reject) => {
  925. SceneLoader.LoadAssetContainer(rootUrl, sceneFilename, scene, (assetContainer) => {
  926. resolve(assetContainer);
  927. }, onProgress, (scene, message, exception) => {
  928. reject(exception || new Error(message));
  929. }, pluginExtension);
  930. });
  931. }
  932. /**
  933. * Import animations from a file into a scene
  934. * @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)
  935. * @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)
  936. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  937. * @param overwriteAnimations when true, animations are cleaned before importing new ones. Animations are appended otherwise
  938. * @param animationGroupLoadingMode defines how to handle old animations groups before importing new ones
  939. * @param targetConverter defines a function used to convert animation targets from loaded scene to current scene (default: search node by name)
  940. * @param onSuccess a callback with the scene when import succeeds
  941. * @param onProgress a callback with a progress event for each file being loaded
  942. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  943. * @param pluginExtension the extension used to determine the plugin
  944. */
  945. 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 {
  946. if (!scene) {
  947. Logger.Error("No scene available to load animations to");
  948. return;
  949. }
  950. if (overwriteAnimations) {
  951. // Reset, stop and dispose all animations before loading new ones
  952. for (let animatable of scene.animatables) {
  953. animatable.reset();
  954. }
  955. scene.stopAllAnimations();
  956. scene.animationGroups.slice().forEach((animationGroup) => {
  957. animationGroup.dispose();
  958. });
  959. let nodes = scene.getNodes();
  960. nodes.forEach((node) => {
  961. if (node.animations) {
  962. node.animations = [];
  963. }
  964. });
  965. }
  966. else {
  967. switch (animationGroupLoadingMode) {
  968. case SceneLoaderAnimationGroupLoadingMode.Clean:
  969. scene.animationGroups.slice().forEach((animationGroup) => {
  970. animationGroup.dispose();
  971. });
  972. break;
  973. case SceneLoaderAnimationGroupLoadingMode.Stop:
  974. scene.animationGroups.forEach((animationGroup) => {
  975. animationGroup.stop();
  976. });
  977. break;
  978. case SceneLoaderAnimationGroupLoadingMode.Sync:
  979. scene.animationGroups.forEach((animationGroup) => {
  980. animationGroup.reset();
  981. animationGroup.restart();
  982. });
  983. break;
  984. case SceneLoaderAnimationGroupLoadingMode.NoSync:
  985. // nothing to do
  986. break;
  987. default:
  988. Logger.Error("Unknown animation group loading mode value '" + animationGroupLoadingMode + "'");
  989. return;
  990. }
  991. }
  992. let startingIndexForNewAnimatables = scene.animatables.length;
  993. let onAssetContainerLoaded = (container: AssetContainer) => {
  994. container.mergeAnimationsTo(scene, scene.animatables.slice(startingIndexForNewAnimatables), targetConverter);
  995. container.dispose();
  996. scene.onAnimationFileImportedObservable.notifyObservers(scene);
  997. if (onSuccess) {
  998. onSuccess(scene);
  999. }
  1000. };
  1001. this.LoadAssetContainer(rootUrl, sceneFilename, scene, onAssetContainerLoaded, onProgress, onError, pluginExtension);
  1002. }
  1003. /**
  1004. * Import animations from a file into a scene
  1005. * @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)
  1006. * @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)
  1007. * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
  1008. * @param overwriteAnimations when true, animations are cleaned before importing new ones. Animations are appended otherwise
  1009. * @param animationGroupLoadingMode defines how to handle old animations groups before importing new ones
  1010. * @param targetConverter defines a function used to convert animation targets from loaded scene to current scene (default: search node by name)
  1011. * @param onSuccess a callback with the scene when import succeeds
  1012. * @param onProgress a callback with a progress event for each file being loaded
  1013. * @param onError a callback with the scene, a message, and possibly an exception when import fails
  1014. * @param pluginExtension the extension used to determine the plugin
  1015. * @returns the updated scene with imported animations
  1016. */
  1017. 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> {
  1018. return new Promise((resolve, reject) => {
  1019. SceneLoader.ImportAnimations(rootUrl, sceneFilename, scene, overwriteAnimations, animationGroupLoadingMode, targetConverter, (_scene: Scene) => {
  1020. resolve(_scene);
  1021. }, onProgress, (_scene: Scene, message: string, exception: any) => {
  1022. reject(exception || new Error(message));
  1023. }, pluginExtension);
  1024. });
  1025. }
  1026. }