BabylonLoader.js 11 KB

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