babylon.assetsManager.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 ImageAssetTask implements IAssetTask {
  84. public onSuccess: (task: IAssetTask) => void;
  85. public onError: (task: IAssetTask) => void;
  86. public isCompleted = false;
  87. public image: HTMLImageElement;
  88. constructor(public name: string, public url: string) {
  89. }
  90. public run(scene: Scene, onSuccess: () => void, onError: () => void) {
  91. var img = new Image();
  92. img.onload = () => {
  93. this.image = img;
  94. this.isCompleted = true;
  95. if (this.onSuccess) {
  96. this.onSuccess(this);
  97. }
  98. onSuccess();
  99. };
  100. img.onerror = () => {
  101. if (this.onError) {
  102. this.onError(this);
  103. }
  104. onError();
  105. };
  106. }
  107. }
  108. export class AssetsManager {
  109. private _tasks = new Array<IAssetTask>();
  110. private _scene: Scene;
  111. private _waitingTasksCount = 0;
  112. public onFinish: (tasks: IAssetTask[]) => void;
  113. public onTaskSuccess: (task: IAssetTask) => void;
  114. public onTaskError: (task: IAssetTask) => void;
  115. public useDefaultLoadingScreen = true;
  116. constructor(scene: Scene) {
  117. this._scene = scene;
  118. }
  119. public addMeshTask(taskName: string, meshesNames: any, rootUrl: string, sceneFilename: string): IAssetTask {
  120. var task = new MeshAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
  121. this._tasks.push(task);
  122. return task;
  123. }
  124. public addTextFileTask(taskName: string, url: string): IAssetTask {
  125. var task = new TextFileAssetTask(taskName, url);
  126. this._tasks.push(task);
  127. return task;
  128. }
  129. public addBinaryFileTask(taskName: string, url: string): IAssetTask {
  130. var task = new BinaryFileAssetTask(taskName, url);
  131. this._tasks.push(task);
  132. return task;
  133. }
  134. public addImageTask(taskName: string, url: string): IAssetTask {
  135. var task = new ImageAssetTask(taskName, url);
  136. this._tasks.push(task);
  137. return task;
  138. }
  139. private _decreaseWaitingTasksCount(): void {
  140. this._waitingTasksCount--;
  141. if (this._waitingTasksCount === 0) {
  142. if (this.onFinish) {
  143. this.onFinish(this._tasks);
  144. }
  145. this._scene.getEngine().hideLoadingUI();
  146. }
  147. }
  148. private _runTask(task: IAssetTask): void {
  149. task.run(this._scene, () => {
  150. if (this.onTaskSuccess) {
  151. this.onTaskSuccess(task);
  152. }
  153. this._decreaseWaitingTasksCount();
  154. }, () => {
  155. if (this.onTaskError) {
  156. this.onTaskError(task);
  157. }
  158. this._decreaseWaitingTasksCount();
  159. });
  160. }
  161. public reset(): AssetsManager {
  162. this._tasks = new Array<IAssetTask>();
  163. return this;
  164. }
  165. public load(): AssetsManager {
  166. this._waitingTasksCount = this._tasks.length;
  167. if (this._waitingTasksCount === 0) {
  168. if (this.onFinish) {
  169. this.onFinish(this._tasks);
  170. }
  171. return this;
  172. }
  173. if (this.useDefaultLoadingScreen) {
  174. this._scene.getEngine().displayLoadingUI();
  175. }
  176. for (var index = 0; index < this._tasks.length; index++) {
  177. var task = this._tasks[index];
  178. this._runTask(task);
  179. }
  180. return this;
  181. }
  182. }
  183. }