BabylonLoader.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.loadScripts = function (urls) {
  84. for (var i = 0; i< urls.length; i++) {
  85. this.loadScript(urls[i]);
  86. }
  87. }
  88. Loader.prototype.loadLibrary = function (library, module) {
  89. if (!useDist) {
  90. var i = 0;
  91. for (; i < library.files.length; i++) {
  92. var file = library.files[i];
  93. if (file == 'lib.d.ts') {
  94. continue;
  95. }
  96. file = file.replace('.ts', '.js');
  97. file = file.replace('../', '');
  98. file = babylonJSPath + '/' + file;
  99. this.loadScript(file);
  100. }
  101. if (library.shaderFiles && library.shaderFiles.length > 0) {
  102. var shaderFile = library.shaderFiles[0];
  103. var endDirectoryIndex = shaderFile.lastIndexOf('/');
  104. shaderFile = shaderFile.substring(0, endDirectoryIndex + 1);
  105. shaderFile += library.output.replace('.js', '.js.fx');
  106. file = file.replace('../', '');
  107. file = babylonJSPath + '/' + file;
  108. this.loadScript(shaderFile);
  109. }
  110. }
  111. else if (min) {
  112. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output.replace('.js', '.min.js'));
  113. }
  114. else {
  115. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  116. }
  117. }
  118. Loader.prototype.loadModule = function (module) {
  119. for (var i = 0; i< module.libraries.length; i++) {
  120. this.loadLibrary(module.libraries[i], module);
  121. }
  122. }
  123. Loader.prototype.loadBJSScripts = function (settings) {
  124. if (!useDist) {
  125. this.loadScripts(settings.core.files);
  126. this.loadScripts(settings.extras.files);
  127. }
  128. else if (min) {
  129. this.loadScript('/dist/preview release/babylon.js');
  130. }
  131. else {
  132. this.loadScript('/dist/preview release/babylon.max.js');
  133. }
  134. for (var i = 0; i< settings.modules.length; i++) {
  135. this.loadModule(settings[settings.modules[i]]);
  136. }
  137. }
  138. Loader.prototype.load = function (newCallback) {
  139. var self = this;
  140. if (newCallback) {
  141. callback = newCallback;
  142. }
  143. getJson('/Tools/Gulp/config.json',
  144. function(data) {
  145. self.loadBJSScripts(data);
  146. if (dependencies) {
  147. self.loadScripts(dependencies);
  148. }
  149. self.dequeue();
  150. },
  151. function(reason) {
  152. console.error(reason);
  153. }
  154. );
  155. };
  156. return Loader;
  157. }());
  158. var loader = new Loader();
  159. BABYLONDEVTOOLS.Loader = loader;
  160. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))