BabylonLoader.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. var tempDirectory = '/.temp' + module.build.distOutputDirectory;
  135. this.loadScript(babylonJSPath + tempDirectory + library.output);
  136. }
  137. else if (module.build.distOutputDirectory && (!testMode || !module.build.ignoreInTestMode)) {
  138. if (min) {
  139. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  140. }
  141. else {
  142. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + (library.maxOutput || library.output.replace(".min", "")));
  143. }
  144. }
  145. }
  146. Loader.prototype.loadModule = function(module) {
  147. for (var i = 0; i < module.libraries.length; i++) {
  148. this.loadLibrary(module.libraries[i], module);
  149. }
  150. }
  151. Loader.prototype.processDependency = function(settings, dependency, filesToLoad) {
  152. if (dependency.dependUpon) {
  153. for (var i = 0; i < dependency.dependUpon.length; i++) {
  154. var dependencyName = dependency.dependUpon[i];
  155. var parent = settings.workloads[dependencyName];
  156. this.processDependency(settings, parent, filesToLoad);
  157. }
  158. }
  159. for (var i = 0; i < dependency.files.length; i++) {
  160. var file = dependency.files[i];
  161. if (filesToLoad.indexOf(file) === -1) {
  162. filesToLoad.push(file);
  163. }
  164. }
  165. }
  166. Loader.prototype.loadBJSScripts = function(settings) {
  167. // Load all the modules from the config.json.
  168. for (var i = 0; i < settings.modules.length; i++) {
  169. this.loadModule(settings[settings.modules[i]]);
  170. }
  171. }
  172. Loader.prototype.load = function(newCallback) {
  173. var self = this;
  174. if (newCallback) {
  175. callback = newCallback;
  176. }
  177. getJson('/Tools/Gulp/config.json',
  178. function(data) {
  179. self.loadBJSScripts(data);
  180. if (dependencies) {
  181. self.loadScripts(dependencies);
  182. }
  183. self.dequeue();
  184. },
  185. function(reason) {
  186. console.error(reason);
  187. }
  188. );
  189. };
  190. return Loader;
  191. }());
  192. var loader = new Loader();
  193. BABYLONDEVTOOLS.Loader = loader;
  194. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))