babylon.assetsManager.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. Tools.SetCorsBehavior(this.url, img);
  93. img.onload = () => {
  94. this.image = img;
  95. this.isCompleted = true;
  96. if (this.onSuccess) {
  97. this.onSuccess(this);
  98. }
  99. onSuccess();
  100. };
  101. img.onerror = () => {
  102. if (this.onError) {
  103. this.onError(this);
  104. }
  105. onError();
  106. };
  107. img.src = this.url;
  108. }
  109. }
  110. export class TextureAssetTask implements IAssetTask {
  111. public onSuccess: (task: IAssetTask) => void;
  112. public onError: (task: IAssetTask) => void;
  113. public isCompleted = false;
  114. public texture: Texture;
  115. constructor(public name: string, public url: string, public noMipmap?: boolean, public invertY?: boolean, public samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE) {
  116. }
  117. public run(scene: Scene, onSuccess: () => void, onError: () => void) {
  118. var onload = () => {
  119. this.isCompleted = true;
  120. if (this.onSuccess) {
  121. this.onSuccess(this);
  122. }
  123. onSuccess();
  124. };
  125. var onerror = () => {
  126. if (this.onError) {
  127. this.onError(this);
  128. }
  129. onError();
  130. };
  131. this.texture = new Texture(this.url, scene, this.noMipmap, this.invertY, this.samplingMode, onload, onerror);
  132. }
  133. }
  134. export class CubeTextureAssetTask implements IAssetTask {
  135. public onSuccess: (task: IAssetTask) => void;
  136. public onError: (task: IAssetTask) => void;
  137. public isCompleted = false;
  138. public texture: CubeTexture;
  139. constructor(public name: string, public url: string, public extensions?: string[], public noMipmap?: boolean, public files?: string[]) {
  140. }
  141. public run(scene: Scene, onSuccess: () => void, onError: () => void) {
  142. var onload = () => {
  143. this.isCompleted = true;
  144. if (this.onSuccess) {
  145. this.onSuccess(this);
  146. }
  147. onSuccess();
  148. };
  149. var onerror = () => {
  150. if (this.onError) {
  151. this.onError(this);
  152. }
  153. onError();
  154. };
  155. this.texture = new CubeTexture(this.url, scene, this.extensions, this.noMipmap, this.files, onload, onerror);
  156. }
  157. }
  158. export class AssetsManager {
  159. private _scene: Scene;
  160. protected tasks = new Array<IAssetTask>();
  161. protected waitingTasksCount = 0;
  162. public onFinish: (tasks: IAssetTask[]) => void;
  163. public onTaskSuccess: (task: IAssetTask) => void;
  164. public onTaskError: (task: IAssetTask) => void;
  165. public useDefaultLoadingScreen = true;
  166. constructor(scene: Scene) {
  167. this._scene = scene;
  168. }
  169. public addMeshTask(taskName: string, meshesNames: any, rootUrl: string, sceneFilename: string): IAssetTask {
  170. var task = new MeshAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
  171. this.tasks.push(task);
  172. return task;
  173. }
  174. public addTextFileTask(taskName: string, url: string): IAssetTask {
  175. var task = new TextFileAssetTask(taskName, url);
  176. this.tasks.push(task);
  177. return task;
  178. }
  179. public addBinaryFileTask(taskName: string, url: string): IAssetTask {
  180. var task = new BinaryFileAssetTask(taskName, url);
  181. this.tasks.push(task);
  182. return task;
  183. }
  184. public addImageTask(taskName: string, url: string): IAssetTask {
  185. var task = new ImageAssetTask(taskName, url);
  186. this.tasks.push(task);
  187. return task;
  188. }
  189. public addTextureTask(taskName: string, url: string, noMipmap?: boolean, invertY?: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE): IAssetTask {
  190. var task = new TextureAssetTask(taskName, url, noMipmap, invertY, samplingMode);
  191. this.tasks.push(task);
  192. return task;
  193. }
  194. private _decreaseWaitingTasksCount(): void {
  195. this.waitingTasksCount--;
  196. if (this.waitingTasksCount === 0) {
  197. if (this.onFinish) {
  198. this.onFinish(this.tasks);
  199. }
  200. this._scene.getEngine().hideLoadingUI();
  201. }
  202. }
  203. private _runTask(task: IAssetTask): void {
  204. task.run(this._scene, () => {
  205. if (this.onTaskSuccess) {
  206. this.onTaskSuccess(task);
  207. }
  208. this._decreaseWaitingTasksCount();
  209. }, () => {
  210. if (this.onTaskError) {
  211. this.onTaskError(task);
  212. }
  213. this._decreaseWaitingTasksCount();
  214. });
  215. }
  216. public reset(): AssetsManager {
  217. this.tasks = new Array<IAssetTask>();
  218. return this;
  219. }
  220. public load(): AssetsManager {
  221. this.waitingTasksCount = this.tasks.length;
  222. if (this.waitingTasksCount === 0) {
  223. if (this.onFinish) {
  224. this.onFinish(this.tasks);
  225. }
  226. return this;
  227. }
  228. if (this.useDefaultLoadingScreen) {
  229. this._scene.getEngine().displayLoadingUI();
  230. }
  231. for (var index = 0; index < this.tasks.length; index++) {
  232. var task = this.tasks[index];
  233. this._runTask(task);
  234. }
  235. return this;
  236. }
  237. }
  238. }