123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- module BABYLON {
- export interface IAssetTask {
- onSuccess: (task: IAssetTask) => void;
- onError: (task: IAssetTask) => void;
- isCompleted: boolean;
- run(scene: Scene, onSuccess: () => void, onError: () => void);
- }
- export class MeshAssetTask implements IAssetTask {
- public loadedMeshes: Array<AbstractMesh>;
- public loadedParticleSystems: Array<ParticleSystem>;
- public loadedSkeletons: Array<Skeleton>;
- public onSuccess: (task: IAssetTask) => void;
- public onError: (task: IAssetTask) => void;
- public isCompleted = false;
- constructor(public name: string, public meshesNames: any, public rootUrl: string, public sceneFilename: string) {
- }
- public run(scene: Scene, onSuccess: () => void, onError: () => void) {
- BABYLON.SceneLoader.ImportMesh(this.meshesNames, this.rootUrl, this.sceneFilename, scene,
- (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => {
- this.loadedMeshes = meshes;
- this.loadedParticleSystems = particleSystems;
- this.loadedSkeletons = skeletons;
- this.isCompleted = true;
- if (this.onSuccess) {
- this.onSuccess(this);
- }
- onSuccess();
- }, null, () => {
- if (this.onError) {
- this.onError(this);
- }
- onError();
- }
- );
- }
- }
- export class TextFileAssetTask implements IAssetTask {
- public onSuccess: (task: IAssetTask) => void;
- public onError: (task: IAssetTask) => void;
- public isCompleted = false;
- public text: string;
- constructor(public name: string, public url: string) {
- }
- public run(scene: Scene, onSuccess: () => void, onError: () => void) {
- Tools.LoadFile(this.url, (data) => {
- this.text = data;
- this.isCompleted = true;
- if (this.onSuccess) {
- this.onSuccess(this);
- }
- onSuccess();
- }, null, scene.database, false, () => {
- if (this.onError) {
- this.onError(this);
- }
- onError();
- });
- }
- }
- export class BinaryFileAssetTask implements IAssetTask {
- public onSuccess: (task: IAssetTask) => void;
- public onError: (task: IAssetTask) => void;
- public isCompleted = false;
- public data: ArrayBuffer;
- constructor(public name: string, public url: string) {
- }
- public run(scene: Scene, onSuccess: () => void, onError: () => void) {
- Tools.LoadFile(this.url, (data) => {
- this.data = data;
- this.isCompleted = true;
- if (this.onSuccess) {
- this.onSuccess(this);
- }
- onSuccess();
- }, null, scene.database, true, () => {
- if (this.onError) {
- this.onError(this);
- }
- onError();
- });
- }
- }
- export class ImageAssetTask implements IAssetTask {
- public onSuccess: (task: IAssetTask) => void;
- public onError: (task: IAssetTask) => void;
- public isCompleted = false;
- public image: HTMLImageElement;
- constructor(public name: string, public url: string) {
- }
- public run(scene: Scene, onSuccess: () => void, onError: () => void) {
- var img = new Image();
- img.onload = () => {
- this.image = img;
- this.isCompleted = true;
- if (this.onSuccess) {
- this.onSuccess(this);
- }
- onSuccess();
- };
- img.onerror = () => {
- if (this.onError) {
- this.onError(this);
- }
- onError();
- };
- }
- }
- export class AssetsManager {
- private _tasks = new Array<IAssetTask>();
- private _scene: Scene;
- private _waitingTasksCount = 0;
- public onFinish: (tasks: IAssetTask[]) => void;
- public onTaskSuccess: (task: IAssetTask) => void;
- public onTaskError: (task: IAssetTask) => void;
- public useDefaultLoadingScreen = true;
- constructor(scene: Scene) {
- this._scene = scene;
- }
- public addMeshTask(taskName: string, meshesNames: any, rootUrl: string, sceneFilename: string): IAssetTask {
- var task = new MeshAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
- this._tasks.push(task);
- return task;
- }
- public addTextFileTask(taskName: string, url: string): IAssetTask {
- var task = new TextFileAssetTask(taskName, url);
- this._tasks.push(task);
- return task;
- }
- public addBinaryFileTask(taskName: string, url: string): IAssetTask {
- var task = new BinaryFileAssetTask(taskName, url);
- this._tasks.push(task);
- return task;
- }
- public addImageTask(taskName: string, url: string): IAssetTask {
- var task = new ImageAssetTask(taskName, url);
- this._tasks.push(task);
- return task;
- }
- private _decreaseWaitingTasksCount(): void {
- this._waitingTasksCount--;
- if (this._waitingTasksCount === 0) {
- if (this.onFinish) {
- this.onFinish(this._tasks);
- }
- this._scene.getEngine().hideLoadingUI();
- }
- }
- private _runTask(task: IAssetTask): void {
- task.run(this._scene, () => {
- if (this.onTaskSuccess) {
- this.onTaskSuccess(task);
- }
- this._decreaseWaitingTasksCount();
- }, () => {
- if (this.onTaskError) {
- this.onTaskError(task);
- }
- this._decreaseWaitingTasksCount();
- });
- }
- public reset(): AssetsManager {
- this._tasks = new Array<IAssetTask>();
- return this;
- }
- public load(): AssetsManager {
- this._waitingTasksCount = this._tasks.length;
- if (this._waitingTasksCount === 0) {
- if (this.onFinish) {
- this.onFinish(this._tasks);
- }
- return this;
- }
- if (this.useDefaultLoadingScreen) {
- this._scene.getEngine().displayLoadingUI();
- }
- for (var index = 0; index < this._tasks.length; index++) {
- var task = this._tasks[index];
- this._runTask(task);
- }
- return this;
- }
- }
- }
|