123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- import { Tools } from "../Misc/tools";
- import { Observable } from "../Misc/observable";
- import { FilesInputStore } from "../Misc/filesInputStore";
- import { Nullable } from "../types";
- import { Scene } from "../scene";
- import { Engine } from "../Engines/engine";
- import { EngineStore } from "../Engines/engineStore";
- import { AbstractMesh } from "../Meshes/abstractMesh";
- import { AnimationGroup } from "../Animations/animationGroup";
- import { _TimeToken } from "../Instrumentation/timeToken";
- import { IOfflineProvider } from "../Offline/IOfflineProvider";
- import { _DepthCullingState, _StencilState, _AlphaState } from "../States/index";
- import { AssetContainer } from "../assetContainer";
- import { IParticleSystem } from "../Particles/IParticleSystem";
- import { Skeleton } from "../Bones/skeleton";
- import { Logger } from "../Misc/logger";
- import { Constants } from "../Engines/constants";
- import { SceneLoaderFlags } from "./sceneLoaderFlags";
- import { IFileRequest } from '../Misc/fileRequest';
- /**
- * Class used to represent data loading progression
- */
- export class SceneLoaderProgressEvent {
- /**
- * Create a new progress event
- * @param lengthComputable defines if data length to load can be evaluated
- * @param loaded defines the loaded data length
- * @param total defines the data length to load
- */
- constructor(
- /** defines if data length to load can be evaluated */
- public readonly lengthComputable: boolean,
- /** defines the loaded data length */
- public readonly loaded: number,
- /** defines the data length to load */
- public readonly total: number) {
- }
- /**
- * Creates a new SceneLoaderProgressEvent from a ProgressEvent
- * @param event defines the source event
- * @returns a new SceneLoaderProgressEvent
- */
- public static FromProgressEvent(event: ProgressEvent): SceneLoaderProgressEvent {
- return new SceneLoaderProgressEvent(event.lengthComputable, event.loaded, event.total);
- }
- }
- /**
- * Interface used by SceneLoader plugins to define supported file extensions
- */
- export interface ISceneLoaderPluginExtensions {
- /**
- * Defines the list of supported extensions
- */
- [extension: string]: {
- isBinary: boolean;
- };
- }
- /**
- * Interface used by SceneLoader plugin factory
- */
- export interface ISceneLoaderPluginFactory {
- /**
- * Defines the name of the factory
- */
- name: string;
- /**
- * Function called to create a new plugin
- * @return the new plugin
- */
- createPlugin(): ISceneLoaderPlugin | ISceneLoaderPluginAsync;
- /**
- * Boolean indicating if the plugin can direct load specific data
- */
- canDirectLoad?: (data: string) => boolean;
- }
- /**
- * Interface used to define a SceneLoader plugin
- */
- export interface ISceneLoaderPlugin {
- /**
- * The friendly name of this plugin.
- */
- name: string;
- /**
- * The file extensions supported by this plugin.
- */
- extensions: string | ISceneLoaderPluginExtensions;
- /**
- * Import meshes into a scene.
- * @param meshesNames An array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
- * @param scene The scene to import into
- * @param data The data to import
- * @param rootUrl The root url for scene and resources
- * @param meshes The meshes array to import into
- * @param particleSystems The particle systems array to import into
- * @param skeletons The skeletons array to import into
- * @param onError The callback when import fails
- * @returns True if successful or false otherwise
- */
- importMesh(meshesNames: any, scene: Scene, data: any, rootUrl: string, meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], onError?: (message: string, exception?: any) => void): boolean;
- /**
- * Load into a scene.
- * @param scene The scene to load into
- * @param data The data to import
- * @param rootUrl The root url for scene and resources
- * @param onError The callback when import fails
- * @returns true if successful or false otherwise
- */
- load(scene: Scene, data: string, rootUrl: string, onError?: (message: string, exception?: any) => void): boolean;
- /**
- * The callback that returns true if the data can be directly loaded.
- */
- canDirectLoad?: (data: string) => boolean;
- /**
- * The callback that allows custom handling of the root url based on the response url.
- */
- rewriteRootURL?: (rootUrl: string, responseURL?: string) => string;
- /**
- * Load into an asset container.
- * @param scene The scene to load into
- * @param data The data to import
- * @param rootUrl The root url for scene and resources
- * @param onError The callback when import fails
- * @returns The loaded asset container
- */
- loadAssetContainer(scene: Scene, data: string, rootUrl: string, onError?: (message: string, exception?: any) => void): AssetContainer;
- }
- /**
- * Interface used to define an async SceneLoader plugin
- */
- export interface ISceneLoaderPluginAsync {
- /**
- * The friendly name of this plugin.
- */
- name: string;
- /**
- * The file extensions supported by this plugin.
- */
- extensions: string | ISceneLoaderPluginExtensions;
- /**
- * Import meshes into a scene.
- * @param meshesNames An array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
- * @param scene The scene to import into
- * @param data The data to import
- * @param rootUrl The root url for scene and resources
- * @param onProgress The callback when the load progresses
- * @param fileName Defines the name of the file to load
- * @returns The loaded meshes, particle systems, skeletons, and animation groups
- */
- importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void, fileName?: string): Promise<{ meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[] }>;
- /**
- * Load into a scene.
- * @param scene The scene to load into
- * @param data The data to import
- * @param rootUrl The root url for scene and resources
- * @param onProgress The callback when the load progresses
- * @param fileName Defines the name of the file to load
- * @returns Nothing
- */
- loadAsync(scene: Scene, data: string, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void, fileName?: string): Promise<void>;
- /**
- * The callback that returns true if the data can be directly loaded.
- */
- canDirectLoad?: (data: string) => boolean;
- /**
- * The callback that allows custom handling of the root url based on the response url.
- */
- rewriteRootURL?: (rootUrl: string, responseURL?: string) => string;
- /**
- * Load into an asset container.
- * @param scene The scene to load into
- * @param data The data to import
- * @param rootUrl The root url for scene and resources
- * @param onProgress The callback when the load progresses
- * @param fileName Defines the name of the file to load
- * @returns The loaded asset container
- */
- loadAssetContainerAsync(scene: Scene, data: string, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void, fileName?: string): Promise<AssetContainer>;
- }
- /**
- * Defines a plugin registered by the SceneLoader
- */
- interface IRegisteredPlugin {
- /**
- * Defines the plugin to use
- */
- plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync | ISceneLoaderPluginFactory;
- /**
- * Defines if the plugin supports binary data
- */
- isBinary: boolean;
- }
- /**
- * Defines file information
- */
- interface IFileInfo {
- /**
- * Gets the file url
- */
- url: string;
- /**
- * Gets the root url
- */
- rootUrl: string;
- /**
- * Gets filename
- */
- name: string;
- /**
- * Gets the file
- */
- file: Nullable<File>;
- }
- /**
- * Class used to load scene from various file formats using registered plugins
- * @see http://doc.babylonjs.com/how_to/load_from_any_file_type
- */
- export class SceneLoader {
- /**
- * No logging while loading
- */
- public static readonly NO_LOGGING = Constants.SCENELOADER_NO_LOGGING;
- /**
- * Minimal logging while loading
- */
- public static readonly MINIMAL_LOGGING = Constants.SCENELOADER_MINIMAL_LOGGING;
- /**
- * Summary logging while loading
- */
- public static readonly SUMMARY_LOGGING = Constants.SCENELOADER_SUMMARY_LOGGING;
- /**
- * Detailled logging while loading
- */
- public static readonly DETAILED_LOGGING = Constants.SCENELOADER_DETAILED_LOGGING;
- /**
- * Gets or sets a boolean indicating if entire scene must be loaded even if scene contains incremental data
- */
- public static get ForceFullSceneLoadingForIncremental() {
- return SceneLoaderFlags.ForceFullSceneLoadingForIncremental;
- }
- public static set ForceFullSceneLoadingForIncremental(value: boolean) {
- SceneLoaderFlags.ForceFullSceneLoadingForIncremental = value;
- }
- /**
- * Gets or sets a boolean indicating if loading screen must be displayed while loading a scene
- */
- public static get ShowLoadingScreen(): boolean {
- return SceneLoaderFlags.ShowLoadingScreen;
- }
- public static set ShowLoadingScreen(value: boolean) {
- SceneLoaderFlags.ShowLoadingScreen = value;
- }
- /**
- * Defines the current logging level (while loading the scene)
- * @ignorenaming
- */
- public static get loggingLevel(): number {
- return SceneLoaderFlags.loggingLevel;
- }
- public static set loggingLevel(value: number) {
- SceneLoaderFlags.loggingLevel = value;
- }
- /**
- * Gets or set a boolean indicating if matrix weights must be cleaned upon loading
- */
- public static get CleanBoneMatrixWeights(): boolean {
- return SceneLoaderFlags.CleanBoneMatrixWeights;
- }
- public static set CleanBoneMatrixWeights(value: boolean) {
- SceneLoaderFlags.CleanBoneMatrixWeights = value;
- }
- // Members
- /**
- * Event raised when a plugin is used to load a scene
- */
- public static OnPluginActivatedObservable = new Observable<ISceneLoaderPlugin | ISceneLoaderPluginAsync>();
- private static _registeredPlugins: { [extension: string]: IRegisteredPlugin } = {};
- private static _getDefaultPlugin(): IRegisteredPlugin {
- return SceneLoader._registeredPlugins[".babylon"];
- }
- private static _getPluginForExtension(extension: string): IRegisteredPlugin {
- var registeredPlugin = SceneLoader._registeredPlugins[extension];
- if (registeredPlugin) {
- return registeredPlugin;
- }
- 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: http://doc.babylonjs.com/how_to/load_from_any_file_type");
- return SceneLoader._getDefaultPlugin();
- }
- private static _getPluginForDirectLoad(data: string): IRegisteredPlugin {
- for (var extension in SceneLoader._registeredPlugins) {
- var plugin = SceneLoader._registeredPlugins[extension].plugin;
- if (plugin.canDirectLoad && plugin.canDirectLoad(data)) {
- return SceneLoader._registeredPlugins[extension];
- }
- }
- return SceneLoader._getDefaultPlugin();
- }
- private static _getPluginForFilename(sceneFilename: string): IRegisteredPlugin {
- var queryStringPosition = sceneFilename.indexOf("?");
- if (queryStringPosition !== -1) {
- sceneFilename = sceneFilename.substring(0, queryStringPosition);
- }
- var dotPosition = sceneFilename.lastIndexOf(".");
- var extension = sceneFilename.substring(dotPosition, sceneFilename.length).toLowerCase();
- return SceneLoader._getPluginForExtension(extension);
- }
- // use babylon file loader directly if sceneFilename is prefixed with "data:"
- private static _getDirectLoad(sceneFilename: string): Nullable<string> {
- if (sceneFilename.substr(0, 5) === "data:") {
- return sceneFilename.substr(5);
- }
- return null;
- }
- private static _loadData(fileInfo: IFileInfo, scene: Scene, onSuccess: (plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync, data: any, responseURL?: string) => void, onProgress: ((event: SceneLoaderProgressEvent) => void) | undefined, onError: (message: string, exception?: any) => void, onDispose: () => void, pluginExtension: Nullable<string>): ISceneLoaderPlugin | ISceneLoaderPluginAsync {
- let directLoad = SceneLoader._getDirectLoad(fileInfo.name);
- let registeredPlugin = pluginExtension ? SceneLoader._getPluginForExtension(pluginExtension) : (directLoad ? SceneLoader._getPluginForDirectLoad(fileInfo.name) : SceneLoader._getPluginForFilename(fileInfo.name));
- let plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync;
- if ((registeredPlugin.plugin as ISceneLoaderPluginFactory).createPlugin) {
- plugin = (registeredPlugin.plugin as ISceneLoaderPluginFactory).createPlugin();
- }
- else {
- plugin = <any>registeredPlugin.plugin;
- }
- if (!plugin) {
- 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.";
- }
- let useArrayBuffer = registeredPlugin.isBinary;
- let offlineProvider: IOfflineProvider;
- SceneLoader.OnPluginActivatedObservable.notifyObservers(plugin);
- let dataCallback = (data: any, responseURL?: string) => {
- if (scene.isDisposed) {
- onError("Scene has been disposed");
- return;
- }
- scene.offlineProvider = offlineProvider;
- onSuccess(plugin, data, responseURL);
- };
- let request: Nullable<IFileRequest> = null;
- let pluginDisposed = false;
- let onDisposeObservable = (plugin as any).onDisposeObservable as Observable<ISceneLoaderPlugin | ISceneLoaderPluginAsync>;
- if (onDisposeObservable) {
- onDisposeObservable.add(() => {
- pluginDisposed = true;
- if (request) {
- request.abort();
- request = null;
- }
- onDispose();
- });
- }
- let manifestChecked = () => {
- if (pluginDisposed) {
- return;
- }
- request = Tools.LoadFile(fileInfo.url, dataCallback, onProgress ? (event) => {
- onProgress(SceneLoaderProgressEvent.FromProgressEvent(event));
- } : undefined, offlineProvider, useArrayBuffer, (request, exception) => {
- onError("Failed to load scene." + (exception ? " " + exception.message : ""), exception);
- });
- };
- if (directLoad) {
- dataCallback(directLoad);
- return plugin;
- }
- const file = fileInfo.file || FilesInputStore.FilesToLoad[fileInfo.name.toLowerCase()];
- if (fileInfo.rootUrl.indexOf("file:") === -1 || (fileInfo.rootUrl.indexOf("file:") !== -1 && !file)) {
- let engine = scene.getEngine();
- let canUseOfflineSupport = engine.enableOfflineSupport;
- if (canUseOfflineSupport) {
- // Also check for exceptions
- let exceptionFound = false;
- for (var regex of scene.disableOfflineSupportExceptionRules) {
- if (regex.test(fileInfo.url)) {
- exceptionFound = true;
- break;
- }
- }
- canUseOfflineSupport = !exceptionFound;
- }
- if (canUseOfflineSupport && Engine.OfflineProviderFactory) {
- // Checking if a manifest file has been set for this scene and if offline mode has been requested
- offlineProvider = Engine.OfflineProviderFactory(fileInfo.url, manifestChecked, engine.disableManifestCheck);
- }
- else {
- manifestChecked();
- }
- }
- // Loading file from disk via input file or drag'n'drop
- else {
- if (file) {
- request = Tools.ReadFile(file, dataCallback, onProgress, useArrayBuffer);
- } else {
- onError("Unable to find file named " + fileInfo.name);
- }
- }
- return plugin;
- }
- private static _getFileInfo(rootUrl: string, sceneFilename: string | File): Nullable<IFileInfo> {
- let url: string;
- let name: string;
- let file: Nullable<File> = null;
- if (!sceneFilename) {
- url = rootUrl;
- name = Tools.GetFilename(rootUrl);
- rootUrl = Tools.GetFolderPath(rootUrl);
- }
- else if ((sceneFilename as File).name) {
- const sceneFile = sceneFilename as File;
- url = rootUrl + sceneFile.name;
- name = sceneFile.name;
- file = sceneFile;
- }
- else {
- const filename = sceneFilename as string;
- if (filename.substr(0, 1) === "/") {
- Tools.Error("Wrong sceneFilename parameter");
- return null;
- }
- url = rootUrl + filename;
- name = filename;
- }
- return {
- url: url,
- rootUrl: rootUrl,
- name: name,
- file: file
- };
- }
- // Public functions
- /**
- * Gets a plugin that can load the given extension
- * @param extension defines the extension to load
- * @returns a plugin or null if none works
- */
- public static GetPluginForExtension(extension: string): ISceneLoaderPlugin | ISceneLoaderPluginAsync | ISceneLoaderPluginFactory {
- return SceneLoader._getPluginForExtension(extension).plugin;
- }
- /**
- * Gets a boolean indicating that the given extension can be loaded
- * @param extension defines the extension to load
- * @returns true if the extension is supported
- */
- public static IsPluginForExtensionAvailable(extension: string): boolean {
- return !!SceneLoader._registeredPlugins[extension];
- }
- /**
- * Adds a new plugin to the list of registered plugins
- * @param plugin defines the plugin to add
- */
- public static RegisterPlugin(plugin: ISceneLoaderPlugin | ISceneLoaderPluginAsync): void {
- if (typeof plugin.extensions === "string") {
- var extension = <string>plugin.extensions;
- SceneLoader._registeredPlugins[extension.toLowerCase()] = {
- plugin: plugin,
- isBinary: false
- };
- }
- else {
- var extensions = <ISceneLoaderPluginExtensions>plugin.extensions;
- Object.keys(extensions).forEach((extension) => {
- SceneLoader._registeredPlugins[extension.toLowerCase()] = {
- plugin: plugin,
- isBinary: extensions[extension].isBinary
- };
- });
- }
- }
- /**
- * Import meshes into a scene
- * @param meshNames an array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
- * @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)
- * @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)
- * @param scene the instance of BABYLON.Scene to append to
- * @param onSuccess a callback with a list of imported meshes, particleSystems, and skeletons when import succeeds
- * @param onProgress a callback with a progress event for each file being loaded
- * @param onError a callback with the scene, a message, and possibly an exception when import fails
- * @param pluginExtension the extension used to determine the plugin
- * @returns The loaded plugin
- */
- public static ImportMesh(meshNames: any, rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onSuccess: Nullable<(meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[]) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
- if (!scene) {
- Logger.Error("No scene available to import mesh to");
- return null;
- }
- const fileInfo = SceneLoader._getFileInfo(rootUrl, sceneFilename);
- if (!fileInfo) {
- return null;
- }
- var loadingToken = {};
- scene._addPendingData(loadingToken);
- var disposeHandler = () => {
- scene._removePendingData(loadingToken);
- };
- var errorHandler = (message: string, exception?: any) => {
- let errorMessage = "Unable to import meshes from " + fileInfo.url + ": " + message;
- if (onError) {
- onError(scene, errorMessage, exception);
- } else {
- Logger.Error(errorMessage);
- // should the exception be thrown?
- }
- disposeHandler();
- };
- var progressHandler = onProgress ? (event: SceneLoaderProgressEvent) => {
- try {
- onProgress(event);
- }
- catch (e) {
- errorHandler("Error in onProgress callback: " + e, e);
- }
- } : undefined;
- var successHandler = (meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[]) => {
- scene.importedMeshesFiles.push(fileInfo.url);
- if (onSuccess) {
- try {
- onSuccess(meshes, particleSystems, skeletons, animationGroups);
- }
- catch (e) {
- errorHandler("Error in onSuccess callback: " + e, e);
- }
- }
- scene._removePendingData(loadingToken);
- };
- return SceneLoader._loadData(fileInfo, scene, (plugin, data, responseURL) => {
- if (plugin.rewriteRootURL) {
- fileInfo.rootUrl = plugin.rewriteRootURL(fileInfo.rootUrl, responseURL);
- }
- if ((<any>plugin).importMesh) {
- var syncedPlugin = <ISceneLoaderPlugin>plugin;
- var meshes = new Array<AbstractMesh>();
- var particleSystems = new Array<IParticleSystem>();
- var skeletons = new Array<Skeleton>();
- if (!syncedPlugin.importMesh(meshNames, scene, data, fileInfo.rootUrl, meshes, particleSystems, skeletons, errorHandler)) {
- return;
- }
- scene.loadingPluginName = plugin.name;
- successHandler(meshes, particleSystems, skeletons, []);
- }
- else {
- var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
- asyncedPlugin.importMeshAsync(meshNames, scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then((result) => {
- scene.loadingPluginName = plugin.name;
- successHandler(result.meshes, result.particleSystems, result.skeletons, result.animationGroups);
- }).catch((error) => {
- errorHandler(error.message, error);
- });
- }
- }, progressHandler, errorHandler, disposeHandler, pluginExtension);
- }
- /**
- * Import meshes into a scene
- * @param meshNames an array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
- * @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)
- * @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)
- * @param scene the instance of BABYLON.Scene to append to
- * @param onProgress a callback with a progress event for each file being loaded
- * @param pluginExtension the extension used to determine the plugin
- * @returns The loaded list of imported meshes, particle systems, skeletons, and animation groups
- */
- public static ImportMeshAsync(meshNames: any, rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<{ meshes: AbstractMesh[], particleSystems: IParticleSystem[], skeletons: Skeleton[], animationGroups: AnimationGroup[] }> {
- return new Promise((resolve, reject) => {
- SceneLoader.ImportMesh(meshNames, rootUrl, sceneFilename, scene, (meshes, particleSystems, skeletons, animationGroups) => {
- resolve({
- meshes: meshes,
- particleSystems: particleSystems,
- skeletons: skeletons,
- animationGroups: animationGroups
- });
- }, onProgress, (scene, message, exception) => {
- reject(exception || new Error(message));
- },
- pluginExtension);
- });
- }
- /**
- * Load a scene
- * @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)
- * @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)
- * @param engine is the instance of BABYLON.Engine to use to create the scene
- * @param onSuccess a callback with the scene when import succeeds
- * @param onProgress a callback with a progress event for each file being loaded
- * @param onError a callback with the scene, a message, and possibly an exception when import fails
- * @param pluginExtension the extension used to determine the plugin
- * @returns The loaded plugin
- */
- public static Load(rootUrl: string, sceneFilename: string | File = "", engine: Nullable<Engine> = EngineStore.LastCreatedEngine, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
- if (!engine) {
- Tools.Error("No engine available");
- return null;
- }
- return SceneLoader.Append(rootUrl, sceneFilename, new Scene(engine), onSuccess, onProgress, onError, pluginExtension);
- }
- /**
- * Load a scene
- * @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)
- * @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)
- * @param engine is the instance of BABYLON.Engine to use to create the scene
- * @param onProgress a callback with a progress event for each file being loaded
- * @param pluginExtension the extension used to determine the plugin
- * @returns The loaded scene
- */
- public static LoadAsync(rootUrl: string, sceneFilename: string | File = "", engine: Nullable<Engine> = EngineStore.LastCreatedEngine, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<Scene> {
- return new Promise((resolve, reject) => {
- SceneLoader.Load(rootUrl, sceneFilename, engine, (scene) => {
- resolve(scene);
- }, onProgress, (scene, message, exception) => {
- reject(exception || new Error(message));
- }, pluginExtension);
- });
- }
- /**
- * Append a scene
- * @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)
- * @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)
- * @param scene is the instance of BABYLON.Scene to append to
- * @param onSuccess a callback with the scene when import succeeds
- * @param onProgress a callback with a progress event for each file being loaded
- * @param onError a callback with the scene, a message, and possibly an exception when import fails
- * @param pluginExtension the extension used to determine the plugin
- * @returns The loaded plugin
- */
- public static Append(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onSuccess: Nullable<(scene: Scene) => void> = null, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null, pluginExtension: Nullable<string> = null): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
- if (!scene) {
- Logger.Error("No scene available to append to");
- return null;
- }
- const fileInfo = SceneLoader._getFileInfo(rootUrl, sceneFilename);
- if (!fileInfo) {
- return null;
- }
- if (SceneLoader.ShowLoadingScreen) {
- scene.getEngine().displayLoadingUI();
- }
- var loadingToken = {};
- scene._addPendingData(loadingToken);
- var disposeHandler = () => {
- scene._removePendingData(loadingToken);
- scene.getEngine().hideLoadingUI();
- };
- var errorHandler = (message: Nullable<string>, exception?: any) => {
- let errorMessage = "Unable to load from " + fileInfo.url + (message ? ": " + message : "");
- if (onError) {
- onError(scene, errorMessage, exception);
- } else {
- Logger.Error(errorMessage);
- // should the exception be thrown?
- }
- disposeHandler();
- };
- var progressHandler = onProgress ? (event: SceneLoaderProgressEvent) => {
- try {
- onProgress(event);
- }
- catch (e) {
- errorHandler("Error in onProgress callback", e);
- }
- } : undefined;
- var successHandler = () => {
- if (onSuccess) {
- try {
- onSuccess(scene);
- }
- catch (e) {
- errorHandler("Error in onSuccess callback", e);
- }
- }
- scene._removePendingData(loadingToken);
- };
- return SceneLoader._loadData(fileInfo, scene, (plugin, data) => {
- if ((<any>plugin).load) {
- var syncedPlugin = <ISceneLoaderPlugin>plugin;
- if (!syncedPlugin.load(scene, data, fileInfo.rootUrl, errorHandler)) {
- return;
- }
- scene.loadingPluginName = plugin.name;
- successHandler();
- } else {
- var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
- asyncedPlugin.loadAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then(() => {
- scene.loadingPluginName = plugin.name;
- successHandler();
- }).catch((error) => {
- errorHandler(error.message, error);
- });
- }
- if (SceneLoader.ShowLoadingScreen) {
- scene.executeWhenReady(() => {
- scene.getEngine().hideLoadingUI();
- });
- }
- }, progressHandler, errorHandler, disposeHandler, pluginExtension);
- }
- /**
- * Append a scene
- * @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)
- * @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)
- * @param scene is the instance of BABYLON.Scene to append to
- * @param onProgress a callback with a progress event for each file being loaded
- * @param pluginExtension the extension used to determine the plugin
- * @returns The given scene
- */
- public static AppendAsync(rootUrl: string, sceneFilename: string | File = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<Scene> {
- return new Promise((resolve, reject) => {
- SceneLoader.Append(rootUrl, sceneFilename, scene, (scene) => {
- resolve(scene);
- }, onProgress, (scene, message, exception) => {
- reject(exception || new Error(message));
- }, pluginExtension);
- });
- }
- /**
- * Load a scene into an asset container
- * @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)
- * @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)
- * @param scene is the instance of BABYLON.Scene to append to (default: last created scene)
- * @param onSuccess a callback with the scene when import succeeds
- * @param onProgress a callback with a progress event for each file being loaded
- * @param onError a callback with the scene, a message, and possibly an exception when import fails
- * @param pluginExtension the extension used to determine the plugin
- * @returns The loaded plugin
- */
- public static LoadAssetContainer(
- rootUrl: string,
- sceneFilename: string | File = "",
- scene: Nullable<Scene> = EngineStore.LastCreatedScene,
- onSuccess: Nullable<(assets: AssetContainer) => void> = null,
- onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null,
- onError: Nullable<(scene: Scene, message: string, exception?: any) => void> = null,
- pluginExtension: Nullable<string> = null
- ): Nullable<ISceneLoaderPlugin | ISceneLoaderPluginAsync> {
- if (!scene) {
- Logger.Error("No scene available to load asset container to");
- return null;
- }
- const fileInfo = SceneLoader._getFileInfo(rootUrl, sceneFilename);
- if (!fileInfo) {
- return null;
- }
- var loadingToken = {};
- scene._addPendingData(loadingToken);
- var disposeHandler = () => {
- scene._removePendingData(loadingToken);
- };
- var errorHandler = (message: Nullable<string>, exception?: any) => {
- let errorMessage = "Unable to load assets from " + fileInfo.url + (message ? ": " + message : "");
- if (onError) {
- onError(scene, errorMessage, exception);
- } else {
- Logger.Error(errorMessage);
- // should the exception be thrown?
- }
- disposeHandler();
- };
- var progressHandler = onProgress ? (event: SceneLoaderProgressEvent) => {
- try {
- onProgress(event);
- }
- catch (e) {
- errorHandler("Error in onProgress callback", e);
- }
- } : undefined;
- var successHandler = (assets: AssetContainer) => {
- if (onSuccess) {
- try {
- onSuccess(assets);
- }
- catch (e) {
- errorHandler("Error in onSuccess callback", e);
- }
- }
- scene._removePendingData(loadingToken);
- };
- return SceneLoader._loadData(fileInfo, scene, (plugin, data) => {
- if ((<any>plugin).loadAssetContainer) {
- var syncedPlugin = <ISceneLoaderPlugin>plugin;
- var assetContainer = syncedPlugin.loadAssetContainer(scene, data, fileInfo.rootUrl, errorHandler);
- if (!assetContainer) {
- return;
- }
- scene.loadingPluginName = plugin.name;
- successHandler(assetContainer);
- } else if ((<any>plugin).loadAssetContainerAsync) {
- var asyncedPlugin = <ISceneLoaderPluginAsync>plugin;
- asyncedPlugin.loadAssetContainerAsync(scene, data, fileInfo.rootUrl, progressHandler, fileInfo.name).then((assetContainer) => {
- scene.loadingPluginName = plugin.name;
- successHandler(assetContainer);
- }).catch((error) => {
- errorHandler(error.message, error);
- });
- } else {
- errorHandler("LoadAssetContainer is not supported by this plugin. Plugin did not provide a loadAssetContainer or loadAssetContainerAsync method.");
- }
- if (SceneLoader.ShowLoadingScreen) {
- scene.executeWhenReady(() => {
- scene.getEngine().hideLoadingUI();
- });
- }
- }, progressHandler, errorHandler, disposeHandler, pluginExtension);
- }
- /**
- * Load a scene into an asset container
- * @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)
- * @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)
- * @param scene is the instance of Scene to append to
- * @param onProgress a callback with a progress event for each file being loaded
- * @param pluginExtension the extension used to determine the plugin
- * @returns The loaded asset container
- */
- public static LoadAssetContainerAsync(rootUrl: string, sceneFilename: string = "", scene: Nullable<Scene> = EngineStore.LastCreatedScene, onProgress: Nullable<(event: SceneLoaderProgressEvent) => void> = null, pluginExtension: Nullable<string> = null): Promise<AssetContainer> {
- return new Promise((resolve, reject) => {
- SceneLoader.LoadAssetContainer(rootUrl, sceneFilename, scene, (assetContainer) => {
- resolve(assetContainer);
- }, onProgress, (scene, message, exception) => {
- reject(exception || new Error(message));
- }, pluginExtension);
- });
- }
- }
|