babylon.assetsManager.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. 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. img.src = this.url;
  107. }
  108. }
  109. export class TextureAssetTask implements IAssetTask {
  110. public onSuccess: (task: IAssetTask) => void;
  111. public onError: (task: IAssetTask) => void;
  112. public isCompleted = false;
  113. public texture: Texture;
  114. constructor(public name: string, public url: string, public noMipmap?: boolean, public invertY?: boolean, public samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE) {
  115. }
  116. public run(scene: Scene, onSuccess: () => void, onError: () => void) {
  117. var onload = () => {
  118. this.isCompleted = true;
  119. if (this.onSuccess) {
  120. this.onSuccess(this);
  121. }
  122. onSuccess();
  123. };
  124. var onerror = () => {
  125. if (this.onError) {
  126. this.onError(this);
  127. }
  128. onError();
  129. };
  130. this.texture = new Texture(this.url, scene, this.noMipmap, this.invertY, this.samplingMode, onload, onError);
  131. }
  132. }
  133. export class AssetsManager {
  134. private _tasks = new Array<IAssetTask>();
  135. private _scene: Scene;
  136. private _waitingTasksCount = 0;
  137. public onFinish: (tasks: IAssetTask[]) => void;
  138. public onTaskSuccess: (task: IAssetTask) => void;
  139. public onTaskError: (task: IAssetTask) => void;
  140. public useDefaultLoadingScreen = true;
  141. constructor(scene: Scene) {
  142. this._scene = scene;
  143. }
  144. public addMeshTask(taskName: string, meshesNames: any, rootUrl: string, sceneFilename: string): IAssetTask {
  145. var task = new MeshAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
  146. this._tasks.push(task);
  147. return task;
  148. }
  149. public addTextFileTask(taskName: string, url: string): IAssetTask {
  150. var task = new TextFileAssetTask(taskName, url);
  151. this._tasks.push(task);
  152. return task;
  153. }
  154. public addBinaryFileTask(taskName: string, url: string): IAssetTask {
  155. var task = new BinaryFileAssetTask(taskName, url);
  156. this._tasks.push(task);
  157. return task;
  158. }
  159. public addImageTask(taskName: string, url: string): IAssetTask {
  160. var task = new ImageAssetTask(taskName, url);
  161. this._tasks.push(task);
  162. return task;
  163. }
  164. public addTextureTask(taskName: string, url: string, noMipmap?: boolean, invertY?: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE): IAssetTask {
  165. var task = new TextureAssetTask(taskName, url, noMipmap, invertY, samplingMode);
  166. this._tasks.push(task);
  167. return task;
  168. }
  169. private _decreaseWaitingTasksCount(): void {
  170. this._waitingTasksCount--;
  171. if (this._waitingTasksCount === 0) {
  172. if (this.onFinish) {
  173. this.onFinish(this._tasks);
  174. }
  175. this._scene.getEngine().hideLoadingUI();
  176. }
  177. }
  178. private _runTask(task: IAssetTask): void {
  179. task.run(this._scene, () => {
  180. if (this.onTaskSuccess) {
  181. this.onTaskSuccess(task);
  182. }
  183. this._decreaseWaitingTasksCount();
  184. }, () => {
  185. if (this.onTaskError) {
  186. this.onTaskError(task);
  187. }
  188. this._decreaseWaitingTasksCount();
  189. });
  190. }
  191. public reset(): AssetsManager {
  192. this._tasks = new Array<IAssetTask>();
  193. return this;
  194. }
  195. public load(): AssetsManager {
  196. this._waitingTasksCount = this._tasks.length;
  197. if (this._waitingTasksCount === 0) {
  198. if (this.onFinish) {
  199. this.onFinish(this._tasks);
  200. }
  201. return this;
  202. }
  203. if (this.useDefaultLoadingScreen) {
  204. this._scene.getEngine().displayLoadingUI();
  205. }
  206. for (var index = 0; index < this._tasks.length; index++) {
  207. var task = this._tasks[index];
  208. this._runTask(task);
  209. }
  210. return this;
  211. }
  212. }
  213. }