BabylonLoader.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 esmQueue;
  29. var callback;
  30. var dependencies;
  31. var useDist;
  32. var testMode;
  33. var min;
  34. var babylonJSPath;
  35. function Loader() {
  36. queue = [];
  37. esmQueue = [];
  38. dependencies = [];
  39. callback = null;
  40. min = (document.location.href.toLowerCase().indexOf('dist=min') > 0);
  41. useDist = (min || useDist || document.location.href.toLowerCase().indexOf('dist=true') > 0);
  42. babylonJSPath = '';
  43. }
  44. Loader.prototype.debugShortcut = function(engine) {
  45. // Add inspector shortcut
  46. var map = {};
  47. var onkey = function(e) {
  48. e = e || event; // to deal with IE
  49. map[e.keyCode] = e.type == 'keydown';
  50. if (map[17] && map[16] && map[18] && map[73]) {
  51. if (engine.scenes && engine.scenes.length > 0) {
  52. for (var i = 0; i < engine.scenes.length; i++) {
  53. if (engine.scenes[0].debugLayer.isVisible()) {
  54. engine.scenes[0].debugLayer.hide();
  55. }
  56. else {
  57. engine.scenes[0].debugLayer.show();
  58. }
  59. }
  60. }
  61. map = {};
  62. return false;
  63. }
  64. };
  65. document.addEventListener("keydown", onkey);
  66. document.addEventListener("keyup", onkey);
  67. }
  68. Loader.prototype.root = function(newBabylonJSPath) {
  69. babylonJSPath = newBabylonJSPath;
  70. return this;
  71. }
  72. Loader.prototype.require = function(newDependencies) {
  73. if (typeof newDependencies === 'string') {
  74. dependencies.push(newDependencies);
  75. }
  76. else if (newDependencies) {
  77. for (var i = 0; i < newDependencies.length; i++) {
  78. dependencies.push(newDependencies[i]);
  79. }
  80. }
  81. return this;
  82. }
  83. Loader.prototype.onReady = function(newCallback) {
  84. callback = newCallback;
  85. return this;
  86. }
  87. Loader.prototype.testMode = function() {
  88. testMode = true;
  89. return this;
  90. }
  91. Loader.prototype.useDist = function() {
  92. useDist = true;
  93. return this;
  94. }
  95. Loader.prototype.dequeue = function() {
  96. if (queue.length + esmQueue.length == 0) {
  97. console.log('Scripts loaded');
  98. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  99. if (callback) {
  100. callback();
  101. }
  102. return;
  103. }
  104. var head = document.getElementsByTagName('head')[0];
  105. var script = document.createElement('script');
  106. if (esmQueue.length) {
  107. script.type = 'module';
  108. script.src = esmQueue.shift();
  109. }
  110. else {
  111. script.type = 'text/javascript';
  112. script.src = queue.shift();
  113. }
  114. var self = this;
  115. script.onload = function() {
  116. self.dequeue();
  117. };
  118. head.appendChild(script);
  119. }
  120. Loader.prototype.loadScript = function(url) {
  121. queue.push(url);
  122. }
  123. Loader.prototype.loadESMScript = function(url) {
  124. esmQueue.push(url);
  125. }
  126. Loader.prototype.loadCss = function(url) {
  127. var style = document.createElement('link');
  128. style.href = url;
  129. style.rel = "stylesheet";
  130. style.type = "text/css";
  131. document.head.appendChild(style);
  132. }
  133. Loader.prototype.loadScripts = function(urls) {
  134. for (var i = 0; i < urls.length; i++) {
  135. this.loadScript(urls[i]);
  136. }
  137. }
  138. Loader.prototype.loadLibrary = function(library, module) {
  139. if (library.preventLoadLibrary) {
  140. return;
  141. }
  142. if (!useDist) {
  143. var tempDirectory = '/.temp' + module.build.distOutputDirectory;
  144. this.loadScript(babylonJSPath + tempDirectory + library.output);
  145. }
  146. else if (module.build.distOutputDirectory && (!testMode || !module.build.ignoreInTestMode)) {
  147. if (min) {
  148. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  149. }
  150. else {
  151. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + (library.maxOutput || library.output.replace(".min", "")));
  152. }
  153. }
  154. }
  155. Loader.prototype.loadCoreDev = function() {
  156. // Es6 core import
  157. this.loadESMScript("/.temp/es6LocalDev/core/legacy/legacy.js");
  158. }
  159. Loader.prototype.loadModule = function(module) {
  160. for (var i = 0; i < module.libraries.length; i++) {
  161. if (!useDist && module.isCore) {
  162. this.loadCoreDev();
  163. }
  164. else {
  165. this.loadLibrary(module.libraries[i], module);
  166. }
  167. }
  168. }
  169. Loader.prototype.processDependency = function(settings, dependency, filesToLoad) {
  170. if (dependency.dependUpon) {
  171. for (var i = 0; i < dependency.dependUpon.length; i++) {
  172. var dependencyName = dependency.dependUpon[i];
  173. var parent = settings.workloads[dependencyName];
  174. this.processDependency(settings, parent, filesToLoad);
  175. }
  176. }
  177. for (var i = 0; i < dependency.files.length; i++) {
  178. var file = dependency.files[i];
  179. if (filesToLoad.indexOf(file) === -1) {
  180. filesToLoad.push(file);
  181. }
  182. }
  183. }
  184. Loader.prototype.loadBJSScripts = function(settings) {
  185. // Load all the modules from the config.json.
  186. for (var i = 0; i < settings.modules.length; i++) {
  187. this.loadModule(settings[settings.modules[i]]);
  188. }
  189. }
  190. Loader.prototype.load = function(newCallback) {
  191. var self = this;
  192. if (newCallback) {
  193. callback = newCallback;
  194. }
  195. getJson('/Tools/Gulp/config.json',
  196. function(data) {
  197. self.loadBJSScripts(data);
  198. if (dependencies) {
  199. self.loadScripts(dependencies);
  200. }
  201. self.dequeue();
  202. },
  203. function(reason) {
  204. console.error(reason);
  205. }
  206. );
  207. };
  208. return Loader;
  209. }());
  210. var loader = new Loader();
  211. BABYLONDEVTOOLS.Loader = loader;
  212. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))