BabylonLoader.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. if (library.webpack) {
  121. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output.replace('.js', '.bundle.js'));
  122. }
  123. else {
  124. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output.replace('.js', '.min.js'));
  125. }
  126. }
  127. else {
  128. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  129. }
  130. if (!min && library.sassFiles && library.sassFiles.length > 0) {
  131. var cssFile = library.output.replace('.js', '.css');
  132. cssFile = babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + cssFile;
  133. this.loadCss(cssFile);
  134. }
  135. }
  136. Loader.prototype.loadModule = function (module) {
  137. for (var i = 0; i< module.libraries.length; i++) {
  138. this.loadLibrary(module.libraries[i], module);
  139. }
  140. }
  141. Loader.prototype.loadBJSScripts = function (settings) {
  142. if (!useDist) {
  143. this.loadScripts(settings.core.files);
  144. this.loadScripts(settings.extras.files);
  145. }
  146. else if (min) {
  147. this.loadScript('/dist/preview release/babylon.js');
  148. }
  149. else {
  150. this.loadScript('/dist/preview release/babylon.max.js');
  151. }
  152. for (var i = 0; i< settings.modules.length; i++) {
  153. this.loadModule(settings[settings.modules[i]]);
  154. }
  155. }
  156. Loader.prototype.load = function (newCallback) {
  157. var self = this;
  158. if (newCallback) {
  159. callback = newCallback;
  160. }
  161. getJson('/Tools/Gulp/config.json',
  162. function(data) {
  163. if (!min) {
  164. self.loadScript('/dist/preview release/split.js');
  165. }
  166. self.loadBJSScripts(data);
  167. if (dependencies) {
  168. self.loadScripts(dependencies);
  169. }
  170. self.dequeue();
  171. },
  172. function(reason) {
  173. console.error(reason);
  174. }
  175. );
  176. };
  177. return Loader;
  178. }());
  179. var loader = new Loader();
  180. BABYLONDEVTOOLS.Loader = loader;
  181. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))