|
@@ -1,9 +1,24 @@
|
|
|
module BABYLON {
|
|
|
|
|
|
+ /**
|
|
|
+ * Defines the list of states available for a task inside a {BABYLON.AssetsManager}
|
|
|
+ */
|
|
|
export enum AssetTaskState {
|
|
|
+ /**
|
|
|
+ * Initialization
|
|
|
+ */
|
|
|
INIT,
|
|
|
+ /**
|
|
|
+ * Running
|
|
|
+ */
|
|
|
RUNNING,
|
|
|
+ /**
|
|
|
+ * Done
|
|
|
+ */
|
|
|
DONE,
|
|
|
+ /**
|
|
|
+ * Error
|
|
|
+ */
|
|
|
ERROR
|
|
|
}
|
|
|
|
|
@@ -20,19 +35,66 @@ module BABYLON {
|
|
|
/**
|
|
|
* Callback called when the task is successful
|
|
|
* @param task contains the successful task
|
|
|
+ * @param message contains the error message
|
|
|
+ * @param exception can contains the inner exception
|
|
|
*/
|
|
|
public onError: (task: any, message?: string, exception?: any) => void;
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a new {BABYLON.AssetsManager}
|
|
|
+ * @param name define the name of the task
|
|
|
+ */
|
|
|
constructor(public name: string) {
|
|
|
- this.taskState = AssetTaskState.INIT;
|
|
|
}
|
|
|
|
|
|
- isCompleted: boolean = false;
|
|
|
- taskState: AssetTaskState;
|
|
|
- errorObject: { message?: string; exception?: any; };
|
|
|
+ private _isCompleted = false;
|
|
|
+ private _taskState = AssetTaskState.INIT;
|
|
|
+ private _errorObject: { message?: string; exception?: any; };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get if the task is completed
|
|
|
+ */
|
|
|
+ public get isCompleted(): boolean {
|
|
|
+ return this._isCompleted;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Gets the current state of the task
|
|
|
+ */
|
|
|
+ public get taskState(): AssetTaskState {
|
|
|
+ return this._taskState;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Gets the current error object (if task is in error)
|
|
|
+ */
|
|
|
+ public get errorObject(): { message?: string; exception?: any; } {
|
|
|
+ return this._errorObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Internal only
|
|
|
+ * @ignore
|
|
|
+ */
|
|
|
+ public _setErrorObject(message?: string, exception?: any) {
|
|
|
+ if (this._errorObject) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this._errorObject = {
|
|
|
+ message: message,
|
|
|
+ exception: exception
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- run(scene: Scene, onSuccess: () => void, onError: (message?: string, exception?: any) => void) {
|
|
|
- this.taskState = AssetTaskState.RUNNING;
|
|
|
+ /**
|
|
|
+ * Execute the current task
|
|
|
+ * @param scene defines the scene where you want your assets to be loaded
|
|
|
+ * @param onSuccess is a callback called when the task is successfully executed
|
|
|
+ * @param onError is a callback called if an error occurs
|
|
|
+ */
|
|
|
+ public run(scene: Scene, onSuccess: () => void, onError: (message?: string, exception?: any) => void) {
|
|
|
+ this._taskState = AssetTaskState.RUNNING;
|
|
|
this.runTask(scene, () => {
|
|
|
this.onDoneCallback(onSuccess, onError);
|
|
|
}, (msg, exception) => {
|
|
@@ -51,9 +113,9 @@ module BABYLON {
|
|
|
}
|
|
|
|
|
|
private onErrorCallback(onError: (message?: string, exception?: any) => void, message?: string, exception?: any) {
|
|
|
- this.taskState = AssetTaskState.ERROR;
|
|
|
+ this._taskState = AssetTaskState.ERROR;
|
|
|
|
|
|
- this.errorObject = {
|
|
|
+ this._errorObject = {
|
|
|
message: message,
|
|
|
exception: exception
|
|
|
}
|
|
@@ -67,8 +129,8 @@ module BABYLON {
|
|
|
|
|
|
private onDoneCallback(onSuccess: () => void, onError: (message?: string, exception?: any) => void) {
|
|
|
try {
|
|
|
- this.taskState = AssetTaskState.DONE;
|
|
|
- this.isCompleted = true;
|
|
|
+ this._taskState = AssetTaskState.DONE;
|
|
|
+ this._isCompleted = true;
|
|
|
|
|
|
if (this.onSuccess) {
|
|
|
this.onSuccess(this);
|
|
@@ -86,19 +148,43 @@ module BABYLON {
|
|
|
* Define the interface used by progress events raised during assets loading
|
|
|
*/
|
|
|
export interface IAssetsProgressEvent {
|
|
|
+ /**
|
|
|
+ * Defines the number of remaining tasks to process
|
|
|
+ */
|
|
|
remainingCount: number;
|
|
|
+ /**
|
|
|
+ * Defines the total number of tasks
|
|
|
+ */
|
|
|
totalCount: number;
|
|
|
+ /**
|
|
|
+ * Defines the task that was just processed
|
|
|
+ */
|
|
|
task: AbstractAssetTask;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Classed used to share progress information about assets loading
|
|
|
+ * Class used to share progress information about assets loading
|
|
|
*/
|
|
|
export class AssetsProgressEvent implements IAssetsProgressEvent {
|
|
|
- remainingCount: number;
|
|
|
- totalCount: number;
|
|
|
- task: AbstractAssetTask;
|
|
|
+ /**
|
|
|
+ * Defines the number of remaining tasks to process
|
|
|
+ */
|
|
|
+ public remainingCount: number;
|
|
|
+ /**
|
|
|
+ * Defines the total number of tasks
|
|
|
+ */
|
|
|
+ public totalCount: number;
|
|
|
+ /**
|
|
|
+ * Defines the task that was just processed
|
|
|
+ */
|
|
|
+ public task: AbstractAssetTask;
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a {BABYLON.AssetsProgressEvent}
|
|
|
+ * @param remainingCount defines the number of remaining tasks to process
|
|
|
+ * @param totalCount defines the total number of tasks
|
|
|
+ * @param task defines the task that was just processed
|
|
|
+ */
|
|
|
constructor(remainingCount: number, totalCount: number, task: AbstractAssetTask) {
|
|
|
this.remainingCount = remainingCount;
|
|
|
this.totalCount = totalCount;
|
|
@@ -117,6 +203,13 @@ module BABYLON {
|
|
|
public onSuccess: (task: MeshAssetTask) => void;
|
|
|
public onError: (task: MeshAssetTask, message?: string, exception?: any) => void;
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a new {BABYLON.MeshAssetTask}
|
|
|
+ * @param name defines the name of the task
|
|
|
+ * @param meshesNames
|
|
|
+ * @param rootUrl
|
|
|
+ * @param sceneFilename
|
|
|
+ */
|
|
|
constructor(public name: string, public meshesNames: any, public rootUrl: string, public sceneFilename: string) {
|
|
|
super(name);
|
|
|
}
|
|
@@ -220,6 +313,9 @@ module BABYLON {
|
|
|
texture: TEX;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Define a task used by {BABYLON.AssetsManager} to load 2D textures
|
|
|
+ */
|
|
|
export class TextureAssetTask extends AbstractAssetTask implements ITextureAssetTask<Texture> {
|
|
|
public texture: Texture;
|
|
|
|
|
@@ -244,6 +340,9 @@ module BABYLON {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Define a task used by {BABYLON.AssetsManager} to load cube textures
|
|
|
+ */
|
|
|
export class CubeTextureAssetTask extends AbstractAssetTask implements ITextureAssetTask<CubeTexture> {
|
|
|
public texture: CubeTexture;
|
|
|
|
|
@@ -268,6 +367,9 @@ module BABYLON {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Define a task used by {BABYLON.AssetsManager} to load HDR cube textures
|
|
|
+ */
|
|
|
export class HDRCubeTextureAssetTask extends AbstractAssetTask implements ITextureAssetTask<HDRCubeTexture> {
|
|
|
public texture: HDRCubeTexture;
|
|
|
|
|
@@ -292,6 +394,10 @@ module BABYLON {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This class can be used to easily import assets into a scene
|
|
|
+ * @see http://doc.babylonjs.com/how_to/how_to_use_assetsmanager
|
|
|
+ */
|
|
|
export class AssetsManager {
|
|
|
private _scene: Scene;
|
|
|
private _isLoading = false;
|
|
@@ -300,24 +406,71 @@ module BABYLON {
|
|
|
protected waitingTasksCount = 0;
|
|
|
protected totalTasksCount = 0;
|
|
|
|
|
|
+ /**
|
|
|
+ * Callback called when all tasks are processed
|
|
|
+ * @param tasks will contains all remaining tasks (ie. all tasks which were not successful)
|
|
|
+ */
|
|
|
public onFinish: (tasks: AbstractAssetTask[]) => void;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Callback called when a task is successful
|
|
|
+ * @param task defines the loaded task
|
|
|
+ */
|
|
|
public onTaskSuccess: (task: AbstractAssetTask) => void;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Callback called when a task had an error
|
|
|
+ * @param task defines failed task
|
|
|
+ */
|
|
|
public onTaskError: (task: AbstractAssetTask) => void;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Callback called when a task is done (whatever the result is)
|
|
|
+ * @param remainingCount defines the number of remaining tasks to process
|
|
|
+ * @param totalCount defines the total number of tasks
|
|
|
+ * @param task defines the task that was just processed
|
|
|
+ */
|
|
|
public onProgress: (remainingCount: number, totalCount: number, task: AbstractAssetTask) => void;
|
|
|
|
|
|
//Observables
|
|
|
|
|
|
+ /**
|
|
|
+ * Observable called when all tasks are processed
|
|
|
+ */
|
|
|
public onTaskSuccessObservable = new Observable<AbstractAssetTask>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Observable called when a task had an error
|
|
|
+ */
|
|
|
public onTaskErrorObservable = new Observable<AbstractAssetTask>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Observable called when a task is successful
|
|
|
+ */
|
|
|
public onTasksDoneObservable = new Observable<AbstractAssetTask[]>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Observable called when a task is done (whatever the result is)
|
|
|
+ */
|
|
|
public onProgressObservable = new Observable<IAssetsProgressEvent>();
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets or sets a boolean defining if the {BABYLON.AssetsManager} should use the default loading screen
|
|
|
+ * @see http://doc.babylonjs.com/how_to/creating_a_custom_loading_screen
|
|
|
+ */
|
|
|
public useDefaultLoadingScreen = true;
|
|
|
|
|
|
constructor(scene: Scene) {
|
|
|
this._scene = scene;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Add a {BABYLON.MeshAssetTask} to the list of active tasks
|
|
|
+ * @param taskName defines the name of the new task
|
|
|
+ * @param meshesNames defines the name of meshes to load
|
|
|
+ * @param rootUrl defines the root url to use to locate files
|
|
|
+ * @param sceneFilename defines the filename of the scene file
|
|
|
+ */
|
|
|
public addMeshTask(taskName: string, meshesNames: any, rootUrl: string, sceneFilename: string): MeshAssetTask {
|
|
|
var task = new MeshAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
|
|
|
this.tasks.push(task);
|
|
@@ -325,6 +478,11 @@ module BABYLON {
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Add a {BABYLON.TextFileAssetTask} to the list of active tasks
|
|
|
+ * @param taskName defines the name of the new task
|
|
|
+ * @param url defines the url of the file to load
|
|
|
+ */
|
|
|
public addTextFileTask(taskName: string, url: string): TextFileAssetTask {
|
|
|
var task = new TextFileAssetTask(taskName, url);
|
|
|
this.tasks.push(task);
|
|
@@ -332,6 +490,11 @@ module BABYLON {
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Add a {BABYLON.BinaryFileAssetTask} to the list of active tasks
|
|
|
+ * @param taskName defines the name of the new task
|
|
|
+ * @param url defines the url of the file to load
|
|
|
+ */
|
|
|
public addBinaryFileTask(taskName: string, url: string): BinaryFileAssetTask {
|
|
|
var task = new BinaryFileAssetTask(taskName, url);
|
|
|
this.tasks.push(task);
|
|
@@ -339,6 +502,11 @@ module BABYLON {
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Add a {BABYLON.ImageAssetTask} to the list of active tasks
|
|
|
+ * @param taskName defines the name of the new task
|
|
|
+ * @param url defines the url of the file to load
|
|
|
+ */
|
|
|
public addImageTask(taskName: string, url: string): ImageAssetTask {
|
|
|
var task = new ImageAssetTask(taskName, url);
|
|
|
this.tasks.push(task);
|
|
@@ -346,6 +514,14 @@ module BABYLON {
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Add a {BABYLON.TextureAssetTask} to the list of active tasks
|
|
|
+ * @param taskName defines the name of the new task
|
|
|
+ * @param url defines the url of the file to load
|
|
|
+ * @param noMipmap defines if the texture must not receive mipmaps (false by default)
|
|
|
+ * @param invertY defines if you want to invert Y axis of the loaded texture (false by default)
|
|
|
+ * @param samplingMode defines the sampling mode to use (BABYLON.Texture.TRILINEAR_SAMPLINGMODE by default)
|
|
|
+ */
|
|
|
public addTextureTask(taskName: string, url: string, noMipmap?: boolean, invertY?: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE): TextureAssetTask {
|
|
|
var task = new TextureAssetTask(taskName, url, noMipmap, invertY, samplingMode);
|
|
|
this.tasks.push(task);
|
|
@@ -353,16 +529,34 @@ module BABYLON {
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public addCubeTextureTask(name: string, url: string, extensions?: string[], noMipmap?: boolean, files?: string[]): CubeTextureAssetTask {
|
|
|
- var task = new CubeTextureAssetTask(name, url, extensions, noMipmap, files);
|
|
|
+ /**
|
|
|
+ * Add a {BABYLON.CubeTextureAssetTask} to the list of active tasks
|
|
|
+ * @param taskName defines the name of the new task
|
|
|
+ * @param url defines the url of the file to load
|
|
|
+ * @param extensions defines the extension to use to load the cube map (can be null)
|
|
|
+ * @param noMipmap defines if the texture must not receive mipmaps (false by default)
|
|
|
+ * @param files defines the list of files to load (can be null)
|
|
|
+ */
|
|
|
+ public addCubeTextureTask(taskName: string, url: string, extensions?: string[], noMipmap?: boolean, files?: string[]): CubeTextureAssetTask {
|
|
|
+ var task = new CubeTextureAssetTask(taskName, url, extensions, noMipmap, files);
|
|
|
this.tasks.push(task);
|
|
|
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
- public addHDRCubeTextureTask(name: string, url: string, size?: number, noMipmap = false, generateHarmonics = true, useInGammaSpace = false, usePMREMGenerator = false): HDRCubeTextureAssetTask {
|
|
|
- var task = new HDRCubeTextureAssetTask(name, url, size, noMipmap, generateHarmonics, useInGammaSpace, usePMREMGenerator);
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * Add a {BABYLON.HDRCubeTextureAssetTask} to the list of active tasks
|
|
|
+ * @param taskName defines the name of the new task
|
|
|
+ * @param url defines the url of the file to load
|
|
|
+ * @param size defines the size you want for the cubemap (can be null)
|
|
|
+ * @param noMipmap defines if the texture must not receive mipmaps (false by default)
|
|
|
+ * @param generateHarmonics defines if you want to automatically generate (true by default)
|
|
|
+ * @param useInGammaSpace defines if the texture must be considered in gamma space (false by default)
|
|
|
+ * @param usePMREMGenerator is a reserved parameter and must be set to false or ignored
|
|
|
+ */
|
|
|
+ public addHDRCubeTextureTask(taskName: string, url: string, size?: number, noMipmap = false, generateHarmonics = true, useInGammaSpace = false, usePMREMGenerator = false): HDRCubeTextureAssetTask {
|
|
|
+ var task = new HDRCubeTextureAssetTask(taskName, url, size, noMipmap, generateHarmonics, useInGammaSpace, usePMREMGenerator);
|
|
|
this.tasks.push(task);
|
|
|
|
|
|
return task;
|
|
@@ -435,10 +629,8 @@ module BABYLON {
|
|
|
}
|
|
|
|
|
|
let error = (message?: string, exception?: any) => {
|
|
|
- task.errorObject = task.errorObject || {
|
|
|
- message: message,
|
|
|
- exception: exception
|
|
|
- }
|
|
|
+ task._setErrorObject(message, exception);
|
|
|
+
|
|
|
if (this.onTaskError) {
|
|
|
this.onTaskError(task);
|
|
|
}
|
|
@@ -449,12 +641,20 @@ module BABYLON {
|
|
|
task.run(this._scene, done, error);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Reset the {BABYLON.AssetsManager} and remove all tasks
|
|
|
+ * @return the current instance of the {BABYLON.AssetsManager}
|
|
|
+ */
|
|
|
public reset(): AssetsManager {
|
|
|
this._isLoading = false;
|
|
|
this.tasks = new Array<AbstractAssetTask>();
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Start the loading process
|
|
|
+ * @return the current instance of the {BABYLON.AssetsManager}
|
|
|
+ */
|
|
|
public load(): AssetsManager {
|
|
|
if (this._isLoading) {
|
|
|
return this;
|