BabylonLoader.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 || document.location.href.toLowerCase().indexOf('dist=true') > 0);
  39. babylonJSPath = '';
  40. }
  41. Loader.prototype.root = function (newBabylonJSPath) {
  42. babylonJSPath = newBabylonJSPath;
  43. return this;
  44. }
  45. Loader.prototype.require = function (newDependencies) {
  46. if (typeof newDependencies === 'string') {
  47. dependencies.push(newDependencies);
  48. }
  49. else if (newDependencies) {
  50. for (var i = 0; i < newDependencies.length; i++) {
  51. dependencies.push(newDependencies[i]);
  52. }
  53. }
  54. return this;
  55. }
  56. Loader.prototype.onReady = function (newCallback) {
  57. callback = newCallback;
  58. return this;
  59. }
  60. Loader.prototype.dequeue = function () {
  61. if (queue.length == 0) {
  62. console.log('Scripts loaded');
  63. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  64. if (callback) {
  65. callback();
  66. }
  67. return;
  68. }
  69. var url = queue.shift();
  70. var head = document.getElementsByTagName('head')[0];
  71. var script = document.createElement('script');
  72. script.type = 'text/javascript';
  73. script.src = url;
  74. var self = this;
  75. script.onload = function() {
  76. self.dequeue();
  77. };
  78. head.appendChild(script);
  79. }
  80. Loader.prototype.loadScript = function (url) {
  81. queue.push(url);
  82. }
  83. Loader.prototype.loadCss = function (url) {
  84. var head = document.getElementsByTagName('head')[0];
  85. var style = document.createElement('link');
  86. style.href = url;
  87. style.rel = "stylesheet";
  88. style.type = "text/css"
  89. document.head.appendChild(style);
  90. }
  91. Loader.prototype.loadScripts = function (urls) {
  92. for (var i = 0; i< urls.length; i++) {
  93. this.loadScript(urls[i]);
  94. }
  95. }
  96. Loader.prototype.loadLibrary = function (library, module) {
  97. if (!useDist) {
  98. var i = 0;
  99. for (; i < library.files.length; i++) {
  100. var file = library.files[i];
  101. if (file.indexOf('lib.d.ts') > 0) {
  102. continue;
  103. }
  104. file = file.replace('.ts', '.js');
  105. file = file.replace('../', '');
  106. file = babylonJSPath + '/' + file;
  107. this.loadScript(file);
  108. }
  109. if (library.shaderFiles && library.shaderFiles.length > 0) {
  110. var shaderFile = library.shaderFiles[0];
  111. var endDirectoryIndex = shaderFile.lastIndexOf('/');
  112. shaderFile = shaderFile.substring(0, endDirectoryIndex + 1);
  113. shaderFile += library.output.replace('.js', '.js.fx');
  114. file = file.replace('../', '');
  115. file = babylonJSPath + '/' + file;
  116. this.loadScript(shaderFile);
  117. }
  118. }
  119. else if (min) {
  120. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output.replace('.js', '.min.js'));
  121. }
  122. else {
  123. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  124. }
  125. if (library.sassFiles && library.sassFiles.length > 0) {
  126. var cssFile = library.output.replace('.js', '.css');
  127. cssFile = babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + cssFile;
  128. this.loadCss(cssFile);
  129. }
  130. }
  131. Loader.prototype.loadModule = function (module) {
  132. for (var i = 0; i< module.libraries.length; i++) {
  133. this.loadLibrary(module.libraries[i], module);
  134. }
  135. }
  136. Loader.prototype.loadBJSScripts = function (settings) {
  137. if (!useDist) {
  138. this.loadScripts(settings.core.files);
  139. this.loadScripts(settings.extras.files);
  140. }
  141. else if (min) {
  142. this.loadScript('/dist/preview release/babylon.js');
  143. }
  144. else {
  145. this.loadScript('/dist/preview release/babylon.max.js');
  146. }
  147. for (var i = 0; i< settings.modules.length; i++) {
  148. this.loadModule(settings[settings.modules[i]]);
  149. }
  150. }
  151. Loader.prototype.load = function (newCallback) {
  152. var self = this;
  153. if (newCallback) {
  154. callback = newCallback;
  155. }
  156. getJson('/Tools/Gulp/config.json',
  157. function(data) {
  158. self.loadScript('/dist/split.js');
  159. self.loadBJSScripts(data);
  160. if (dependencies) {
  161. self.loadScripts(dependencies);
  162. }
  163. self.dequeue();
  164. },
  165. function(reason) {
  166. console.error(reason);
  167. }
  168. );
  169. };
  170. return Loader;
  171. }());
  172. var loader = new Loader();
  173. BABYLONDEVTOOLS.Loader = loader;
  174. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))