BabylonLoader.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. }
  107. else if (min) {
  108. this.loadScript('/dist/preview release/babylon.js');
  109. }
  110. else {
  111. this.loadScript('/dist/preview release/babylon.max.js');
  112. }
  113. for (var i = 0; i< settings.modules.length; i++) {
  114. this.loadModule(settings[settings.modules[i]]);
  115. }
  116. }
  117. Loader.prototype.load = function (callback) {
  118. var self = this;
  119. if (callback) {
  120. self.callback = callback;
  121. }
  122. getJson('/Tools/Gulp/config.json',
  123. function(data) {
  124. self.loadBJSScripts(data);
  125. if (typeof self.dependencies === 'string') {
  126. self.loadScript(self.dependencies);
  127. }
  128. else if (self.dependencies) {
  129. self.loadScripts(self.dependencies);
  130. }
  131. self.dequeue();
  132. },
  133. function(reason) {
  134. console.error(reason);
  135. }
  136. );
  137. };
  138. return Loader;
  139. }());
  140. var loader = new Loader();
  141. BABYLONDEVTOOLS.Loader = loader;
  142. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))