babylon.assetsManager.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var MeshAssetTask = (function () {
  4. function MeshAssetTask(name, meshesNames, rootUrl, sceneFilename) {
  5. this.name = name;
  6. this.meshesNames = meshesNames;
  7. this.rootUrl = rootUrl;
  8. this.sceneFilename = sceneFilename;
  9. this.isCompleted = false;
  10. }
  11. MeshAssetTask.prototype.run = function (scene, onSuccess, onError) {
  12. var _this = this;
  13. BABYLON.SceneLoader.ImportMesh(this.meshesNames, this.rootUrl, this.sceneFilename, scene, function (meshes, particleSystems, skeletons) {
  14. _this.loadedMeshes = meshes;
  15. _this.loadedParticleSystems = particleSystems;
  16. _this.loadedSkeletons = skeletons;
  17. _this.isCompleted = true;
  18. if (_this.onSuccess) {
  19. _this.onSuccess(_this);
  20. }
  21. onSuccess();
  22. }, null, function () {
  23. if (_this.onError) {
  24. _this.onError(_this);
  25. }
  26. onError();
  27. });
  28. };
  29. return MeshAssetTask;
  30. })();
  31. BABYLON.MeshAssetTask = MeshAssetTask;
  32. var TextFileAssetTask = (function () {
  33. function TextFileAssetTask(name, url) {
  34. this.name = name;
  35. this.url = url;
  36. this.isCompleted = false;
  37. }
  38. TextFileAssetTask.prototype.run = function (scene, onSuccess, onError) {
  39. var _this = this;
  40. BABYLON.Tools.LoadFile(this.url, function (data) {
  41. _this.text = data;
  42. _this.isCompleted = true;
  43. if (_this.onSuccess) {
  44. _this.onSuccess(_this);
  45. }
  46. onSuccess();
  47. }, null, scene.database, false, function () {
  48. if (_this.onError) {
  49. _this.onError(_this);
  50. }
  51. onError();
  52. });
  53. };
  54. return TextFileAssetTask;
  55. })();
  56. BABYLON.TextFileAssetTask = TextFileAssetTask;
  57. var BinaryFileAssetTask = (function () {
  58. function BinaryFileAssetTask(name, url) {
  59. this.name = name;
  60. this.url = url;
  61. this.isCompleted = false;
  62. }
  63. BinaryFileAssetTask.prototype.run = function (scene, onSuccess, onError) {
  64. var _this = this;
  65. BABYLON.Tools.LoadFile(this.url, function (data) {
  66. _this.data = data;
  67. _this.isCompleted = true;
  68. if (_this.onSuccess) {
  69. _this.onSuccess(_this);
  70. }
  71. onSuccess();
  72. }, null, scene.database, true, function () {
  73. if (_this.onError) {
  74. _this.onError(_this);
  75. }
  76. onError();
  77. });
  78. };
  79. return BinaryFileAssetTask;
  80. })();
  81. BABYLON.BinaryFileAssetTask = BinaryFileAssetTask;
  82. var ImageAssetTask = (function () {
  83. function ImageAssetTask(name, url) {
  84. this.name = name;
  85. this.url = url;
  86. this.isCompleted = false;
  87. }
  88. ImageAssetTask.prototype.run = function (scene, onSuccess, onError) {
  89. var _this = this;
  90. var img = new Image();
  91. img.onload = function () {
  92. _this.image = img;
  93. _this.isCompleted = true;
  94. if (_this.onSuccess) {
  95. _this.onSuccess(_this);
  96. }
  97. onSuccess();
  98. };
  99. img.onerror = function () {
  100. if (_this.onError) {
  101. _this.onError(_this);
  102. }
  103. onError();
  104. };
  105. img.src = this.url;
  106. };
  107. return ImageAssetTask;
  108. })();
  109. BABYLON.ImageAssetTask = ImageAssetTask;
  110. var TextureAssetTask = (function () {
  111. function TextureAssetTask(name, url, noMipmap, invertY, samplingMode) {
  112. if (typeof samplingMode === "undefined") { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  113. this.name = name;
  114. this.url = url;
  115. this.noMipmap = noMipmap;
  116. this.invertY = invertY;
  117. this.samplingMode = samplingMode;
  118. this.isCompleted = false;
  119. }
  120. TextureAssetTask.prototype.run = function (scene, onSuccess, onError) {
  121. var _this = this;
  122. var onload = function () {
  123. _this.isCompleted = true;
  124. if (_this.onSuccess) {
  125. _this.onSuccess(_this);
  126. }
  127. onSuccess();
  128. };
  129. var onerror = function () {
  130. if (_this.onError) {
  131. _this.onError(_this);
  132. }
  133. onError();
  134. };
  135. this.texture = new BABYLON.Texture(this.url, scene, this.noMipmap, this.invertY, this.samplingMode, onload, onError);
  136. };
  137. return TextureAssetTask;
  138. })();
  139. BABYLON.TextureAssetTask = TextureAssetTask;
  140. var AssetsManager = (function () {
  141. function AssetsManager(scene) {
  142. this._tasks = new Array();
  143. this._waitingTasksCount = 0;
  144. this.useDefaultLoadingScreen = true;
  145. this._scene = scene;
  146. }
  147. AssetsManager.prototype.addMeshTask = function (taskName, meshesNames, rootUrl, sceneFilename) {
  148. var task = new MeshAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
  149. this._tasks.push(task);
  150. return task;
  151. };
  152. AssetsManager.prototype.addTextFileTask = function (taskName, url) {
  153. var task = new TextFileAssetTask(taskName, url);
  154. this._tasks.push(task);
  155. return task;
  156. };
  157. AssetsManager.prototype.addBinaryFileTask = function (taskName, url) {
  158. var task = new BinaryFileAssetTask(taskName, url);
  159. this._tasks.push(task);
  160. return task;
  161. };
  162. AssetsManager.prototype.addImageTask = function (taskName, url) {
  163. var task = new ImageAssetTask(taskName, url);
  164. this._tasks.push(task);
  165. return task;
  166. };
  167. AssetsManager.prototype.addTextureTask = function (taskName, url, noMipmap, invertY, samplingMode) {
  168. if (typeof samplingMode === "undefined") { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  169. var task = new TextureAssetTask(taskName, url, noMipmap, invertY, samplingMode);
  170. this._tasks.push(task);
  171. return task;
  172. };
  173. AssetsManager.prototype._decreaseWaitingTasksCount = function () {
  174. this._waitingTasksCount--;
  175. if (this._waitingTasksCount === 0) {
  176. if (this.onFinish) {
  177. this.onFinish(this._tasks);
  178. }
  179. this._scene.getEngine().hideLoadingUI();
  180. }
  181. };
  182. AssetsManager.prototype._runTask = function (task) {
  183. var _this = this;
  184. task.run(this._scene, function () {
  185. if (_this.onTaskSuccess) {
  186. _this.onTaskSuccess(task);
  187. }
  188. _this._decreaseWaitingTasksCount();
  189. }, function () {
  190. if (_this.onTaskError) {
  191. _this.onTaskError(task);
  192. }
  193. _this._decreaseWaitingTasksCount();
  194. });
  195. };
  196. AssetsManager.prototype.reset = function () {
  197. this._tasks = new Array();
  198. return this;
  199. };
  200. AssetsManager.prototype.load = function () {
  201. this._waitingTasksCount = this._tasks.length;
  202. if (this._waitingTasksCount === 0) {
  203. if (this.onFinish) {
  204. this.onFinish(this._tasks);
  205. }
  206. return this;
  207. }
  208. if (this.useDefaultLoadingScreen) {
  209. this._scene.getEngine().displayLoadingUI();
  210. }
  211. for (var index = 0; index < this._tasks.length; index++) {
  212. var task = this._tasks[index];
  213. this._runTask(task);
  214. }
  215. return this;
  216. };
  217. return AssetsManager;
  218. })();
  219. BABYLON.AssetsManager = AssetsManager;
  220. })(BABYLON || (BABYLON = {}));
  221. //# sourceMappingURL=babylon.assetsManager.js.map