babylon.assetsManager.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. module BABYLON {
  2. export interface IAssetTask {
  3. onSuccess: (task: IAssetTask) => void;
  4. onError: (task: IAssetTask) => void;
  5. isCompleted: boolean;
  6. run(scene: Scene, onSuccess: () => void, onError: () => void);
  7. }
  8. export class MeshAssetTask implements IAssetTask {
  9. public loadedMeshes: Array<AbstractMesh>;
  10. public loadedParticleSystems: Array<ParticleSystem>;
  11. public loadedSkeletons: Array<Skeleton>;
  12. public onSuccess: (task: IAssetTask) => void;
  13. public onError: (task: IAssetTask) => void;
  14. public isCompleted = false;
  15. constructor(public name: string, public meshesNames: any, public rootUrl: string, public sceneFilename: string) {
  16. }
  17. public run(scene: Scene, onSuccess: () => void, onError: () => void) {
  18. BABYLON.SceneLoader.ImportMesh(this.meshesNames, this.rootUrl, this.sceneFilename, scene,
  19. (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => {
  20. this.loadedMeshes = meshes;
  21. this.loadedParticleSystems = particleSystems;
  22. this.loadedSkeletons = skeletons;
  23. this.isCompleted = true;
  24. if (this.onSuccess) {
  25. this.onSuccess(this);
  26. }
  27. onSuccess();
  28. }, null, () => {
  29. if (this.onError) {
  30. this.onError(this);
  31. }
  32. onError();
  33. }
  34. );
  35. }
  36. }
  37. export class TextFileAssetTask implements IAssetTask {
  38. public onSuccess: (task: IAssetTask) => void;
  39. public onError: (task: IAssetTask) => void;
  40. public isCompleted = false;
  41. public text: string;
  42. constructor(public name: string, public url: string) {
  43. }
  44. public run(scene: Scene, onSuccess: () => void, onError: () => void) {
  45. Tools.LoadFile(this.url, (data) => {
  46. this.text = data;
  47. this.isCompleted = true;
  48. if (this.onSuccess) {
  49. this.onSuccess(this);
  50. }
  51. onSuccess();
  52. }, null, scene.database, false, () => {
  53. if (this.onError) {
  54. this.onError(this);
  55. }
  56. onError();
  57. });
  58. }
  59. }
  60. export class BinaryFileAssetTask implements IAssetTask {
  61. public onSuccess: (task: IAssetTask) => void;
  62. public onError: (task: IAssetTask) => void;
  63. public isCompleted = false;
  64. public data: ArrayBuffer;
  65. constructor(public name: string, public url: string) {
  66. }
  67. public run(scene: Scene, onSuccess: () => void, onError: () => void) {
  68. Tools.LoadFile(this.url, (data) => {
  69. this.data = data;
  70. this.isCompleted = true;
  71. if (this.onSuccess) {
  72. this.onSuccess(this);
  73. }
  74. onSuccess();
  75. }, null, scene.database, true, () => {
  76. if (this.onError) {
  77. this.onError(this);
  78. }
  79. onError();
  80. });
  81. }
  82. }
  83. export class AssetsManager {
  84. private _tasks = new Array<IAssetTask>();
  85. private _scene: Scene;
  86. private _waitingTasksCount = 0;
  87. public onFinish: (tasks: IAssetTask[]) => void;
  88. public onTaskSuccess: (task: IAssetTask) => void;
  89. public onTaskError: (task: IAssetTask) => void;
  90. public useDefaultLoadingScreen = true;
  91. constructor(scene: Scene) {
  92. this._scene = scene;
  93. }
  94. public addMeshTask(taskName: string, meshesNames: any, rootUrl: string, sceneFilename: string): IAssetTask {
  95. var task = new MeshAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
  96. this._tasks.push(task);
  97. return task;
  98. }
  99. public addTextFileTask(taskName: string, url: string): IAssetTask {
  100. var task = new TextFileAssetTask(taskName, url);
  101. this._tasks.push(task);
  102. return task;
  103. }
  104. public addBinaryFileTask(taskName: string, url: string): IAssetTask {
  105. var task = new BinaryFileAssetTask(taskName, url);
  106. this._tasks.push(task);
  107. return task;
  108. }
  109. private _decreaseWaitingTasksCount(): void {
  110. this._waitingTasksCount--;
  111. if (this._waitingTasksCount === 0) {
  112. if (this.onFinish) {
  113. this.onFinish(this._tasks);
  114. }
  115. this._scene.getEngine().hideLoadingUI();
  116. }
  117. }
  118. private _runTask(task: IAssetTask): void {
  119. task.run(this._scene, () => {
  120. if (this.onTaskSuccess) {
  121. this.onTaskSuccess(task);
  122. }
  123. this._decreaseWaitingTasksCount();
  124. }, () => {
  125. if (this.onTaskError) {
  126. this.onTaskError(task);
  127. }
  128. this._decreaseWaitingTasksCount();
  129. });
  130. }
  131. public reset(): AssetsManager {
  132. this._tasks = new Array<IAssetTask>();
  133. return this;
  134. }
  135. public load(): AssetsManager {
  136. this._waitingTasksCount = this._tasks.length;
  137. if (this._waitingTasksCount === 0) {
  138. if (this.onFinish) {
  139. this.onFinish(this._tasks);
  140. }
  141. return this;
  142. }
  143. if (this.useDefaultLoadingScreen) {
  144. this._scene.getEngine().displayLoadingUI();
  145. }
  146. for (var index = 0; index < this._tasks.length; index++) {
  147. var task = this._tasks[index];
  148. this._runTask(task);
  149. }
  150. return this;
  151. }
  152. }
  153. }