BabylonLoader.js 5.7 KB

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