BabylonLoader.js 6.1 KB

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