BabylonLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Old Fashion Way for IE 11 Devs. Yes, that still exists ;-)
  2. var BABYLONDEVTOOLS;
  3. (function(BABYLONDEVTOOLS) {
  4. var getJson = function(url, callback, errorCallback) {
  5. var xhr = new XMLHttpRequest();
  6. xhr.open('GET', url);
  7. xhr.onload = function() {
  8. if (this.status >= 200 && this.status < 300) {
  9. var data = JSON.parse(xhr.response);
  10. callback(data)
  11. } else {
  12. errorCallback({
  13. status: this.status,
  14. statusText: xhr.statusText
  15. });
  16. }
  17. };
  18. xhr.onerror = function() {
  19. errorCallback({
  20. status: this.status,
  21. statusText: xhr.statusText
  22. });
  23. };
  24. xhr.send();
  25. }
  26. var Loader = (function() {
  27. var queue;
  28. var callback;
  29. var dependencies;
  30. var useDist;
  31. var testMode;
  32. var min;
  33. var babylonJSPath;
  34. function Loader() {
  35. queue = [];
  36. dependencies = [];
  37. callback = null;
  38. min = (document.location.href.toLowerCase().indexOf('dist=min') > 0);
  39. useDist = (min || useDist || document.location.href.toLowerCase().indexOf('dist=true') > 0);
  40. babylonJSPath = '';
  41. }
  42. Loader.prototype.debugShortcut = function(engine) {
  43. // Add inspector shortcut
  44. var map = {};
  45. var onkey = function(e) {
  46. e = e || event; // to deal with IE
  47. map[e.keyCode] = e.type == 'keydown';
  48. if (map[17] && map[16] && map[18] && map[73]) {
  49. if (engine.scenes && engine.scenes.length > 0) {
  50. for (var i = 0; i < engine.scenes.length; i++) {
  51. if (engine.scenes[0].debugLayer.isVisible()) {
  52. engine.scenes[0].debugLayer.hide();
  53. }
  54. else {
  55. engine.scenes[0].debugLayer.show();
  56. }
  57. }
  58. }
  59. map = {};
  60. return false;
  61. }
  62. };
  63. document.addEventListener("keydown", onkey);
  64. document.addEventListener("keyup", onkey);
  65. }
  66. Loader.prototype.root = function(newBabylonJSPath) {
  67. babylonJSPath = newBabylonJSPath;
  68. return this;
  69. }
  70. Loader.prototype.require = function(newDependencies) {
  71. if (typeof newDependencies === 'string') {
  72. dependencies.push(newDependencies);
  73. }
  74. else if (newDependencies) {
  75. for (var i = 0; i < newDependencies.length; i++) {
  76. dependencies.push(newDependencies[i]);
  77. }
  78. }
  79. return this;
  80. }
  81. Loader.prototype.onReady = function(newCallback) {
  82. callback = newCallback;
  83. return this;
  84. }
  85. Loader.prototype.testMode = function() {
  86. testMode = true;
  87. return this;
  88. }
  89. Loader.prototype.useDist = function() {
  90. useDist = true;
  91. return this;
  92. }
  93. Loader.prototype.dequeue = function() {
  94. if (queue.length == 0) {
  95. console.log('Scripts loaded');
  96. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  97. if (callback) {
  98. callback();
  99. }
  100. return;
  101. }
  102. var url = queue.shift();
  103. var head = document.getElementsByTagName('head')[0];
  104. var script = document.createElement('script');
  105. script.type = 'text/javascript';
  106. script.src = url;
  107. var self = this;
  108. script.onload = function() {
  109. self.dequeue();
  110. };
  111. head.appendChild(script);
  112. }
  113. Loader.prototype.loadScript = function(url) {
  114. queue.push(url);
  115. }
  116. Loader.prototype.loadCss = function(url) {
  117. var head = document.getElementsByTagName('head')[0];
  118. var style = document.createElement('link');
  119. style.href = url;
  120. style.rel = "stylesheet";
  121. style.type = "text/css";
  122. document.head.appendChild(style);
  123. }
  124. Loader.prototype.loadScripts = function(urls) {
  125. for (var i = 0; i < urls.length; i++) {
  126. this.loadScript(urls[i]);
  127. }
  128. }
  129. Loader.prototype.loadLibrary = function(library, module) {
  130. if (library.preventLoadLibrary) {
  131. return;
  132. }
  133. if (!useDist) {
  134. if (library.useOutputForDebugging) {
  135. this.loadScript(babylonJSPath + '/.temp' + module.build.distOutputDirectory + library.output);
  136. } else {
  137. var i = 0;
  138. for (; i < library.files.length; i++) {
  139. var file = library.files[i];
  140. if (file.indexOf('lib.d.ts') > 0) {
  141. continue;
  142. }
  143. // Manage exclude files.
  144. if (library.excludeFromLoader && library.excludeFromLoader.indexOf(file) > -1) {
  145. continue;
  146. }
  147. file = file.replace('.ts', '.js');
  148. file = file.replace('../', '');
  149. file = babylonJSPath + '/' + file;
  150. this.loadScript(file);
  151. }
  152. if (library.shaderFiles && library.shaderFiles.length > 0) {
  153. var shaderFile = library.shaderFiles[0];
  154. var endDirectoryIndex = shaderFile.lastIndexOf('/');
  155. shaderFile = shaderFile.substring(0, endDirectoryIndex + 1);
  156. shaderFile += library.output.replace('.js', '.js.fx');
  157. this.loadScript(shaderFile);
  158. if (library.shadersIncludeFiles) {
  159. var includeShaderFile = shaderFile.replace('.js.fx', '.js.include.fx');
  160. this.loadScript(includeShaderFile);
  161. }
  162. }
  163. }
  164. }
  165. else if (min) {
  166. if (library.webpack) {
  167. if (module.build.distOutputDirectory)
  168. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  169. }
  170. else {
  171. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output.replace('.js', '.min.js'));
  172. }
  173. }
  174. else {
  175. if (module.build.distOutputDirectory && (!testMode || !module.build.ignoreInTestMode))
  176. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  177. }
  178. // Currently not being used
  179. if (!min && library.sassFiles && library.sassFiles.length > 0) {
  180. var cssFile = library.output.replace('.js', '.css');
  181. cssFile = babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + cssFile;
  182. this.loadCss(cssFile);
  183. }
  184. }
  185. Loader.prototype.loadModule = function(module) {
  186. for (var i = 0; i < module.libraries.length; i++) {
  187. this.loadLibrary(module.libraries[i], module);
  188. }
  189. }
  190. Loader.prototype.processDependency = function(settings, dependency, filesToLoad) {
  191. if (dependency.dependUpon) {
  192. for (var i = 0; i < dependency.dependUpon.length; i++) {
  193. var dependencyName = dependency.dependUpon[i];
  194. var parent = settings.workloads[dependencyName];
  195. this.processDependency(settings, parent, filesToLoad);
  196. }
  197. }
  198. for (var i = 0; i < dependency.files.length; i++) {
  199. var file = dependency.files[i];
  200. if (filesToLoad.indexOf(file) === -1) {
  201. filesToLoad.push(file);
  202. }
  203. }
  204. }
  205. Loader.prototype.loadBJSScripts = function(settings) {
  206. var loadModules = true;
  207. // Main bjs files
  208. if (!useDist) {
  209. var currentConfig = settings.build.currentConfig;
  210. var buildConfiguration = settings.buildConfigurations[currentConfig];
  211. var filesToLoad = [];
  212. for (var index = 0; index < buildConfiguration.length; index++) {
  213. var dependencyName = buildConfiguration[index];
  214. var dependency = settings.workloads[dependencyName];
  215. this.processDependency(settings, dependency, filesToLoad);
  216. }
  217. this.loadScripts(filesToLoad);
  218. if (currentConfig !== "all") {
  219. loadModules = false;
  220. }
  221. }
  222. else if (min) {
  223. this.loadScript('/dist/preview release/babylon.js');
  224. }
  225. else {
  226. this.loadScript('/dist/preview release/babylon.max.js');
  227. }
  228. // Modules
  229. if (loadModules) {
  230. for (var i = 0; i < settings.modules.length; i++) {
  231. this.loadModule(settings[settings.modules[i]]);
  232. }
  233. }
  234. }
  235. Loader.prototype.load = function(newCallback) {
  236. var self = this;
  237. if (newCallback) {
  238. callback = newCallback;
  239. }
  240. getJson('/Tools/Gulp/config.json',
  241. function(data) {
  242. self.loadBJSScripts(data);
  243. if (dependencies) {
  244. self.loadScripts(dependencies);
  245. }
  246. self.dequeue();
  247. },
  248. function(reason) {
  249. console.error(reason);
  250. }
  251. );
  252. };
  253. return Loader;
  254. }());
  255. var loader = new Loader();
  256. BABYLONDEVTOOLS.Loader = loader;
  257. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))