BabylonLoader.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = null;
  29. function Loader() {
  30. queue = [];
  31. }
  32. Loader.prototype.onReady = function (callback) {
  33. this.callback = callback;
  34. }
  35. Loader.prototype.dequeue = function () {
  36. if (queue.length == 0) {
  37. console.log('Scripts loaded');
  38. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  39. if (this.callback) {
  40. this.callback();
  41. }
  42. return;
  43. }
  44. var url = queue.shift();
  45. var head = document.getElementsByTagName('head')[0];
  46. var script = document.createElement('script');
  47. script.type = 'text/javascript';
  48. script.src = url;
  49. var self = this;
  50. script.onload = function() {
  51. self.dequeue();
  52. };
  53. head.appendChild(script);
  54. }
  55. Loader.prototype.loadScript = function (url) {
  56. queue.push(url);
  57. }
  58. Loader.prototype.loadScripts = function (urls) {
  59. for (var i = 0; i< urls.length; i++) {
  60. this.loadScript(urls[i]);
  61. }
  62. }
  63. Loader.prototype.loadLibrary = function (library) {
  64. var i = 0;
  65. for (; i < library.files.length; i++) {
  66. var file = library.files[i];
  67. file = file.replace('.ts', '.js');
  68. this.loadScript(file);
  69. }
  70. if (library.shaderFiles && library.shaderFiles.length > 0) {
  71. var shaderFile = library.shaderFiles[0];
  72. var endDirectoryIndex = shaderFile.lastIndexOf('/');
  73. shaderFile = shaderFile.substring(0, endDirectoryIndex + 1);
  74. shaderFile += library.output.replace('.js', '.js.fx');
  75. this.loadScript(shaderFile);
  76. }
  77. }
  78. Loader.prototype.loadModule = function (module) {
  79. for (var i = 0; i< module.libraries.length; i++) {
  80. this.loadLibrary(module.libraries[i]);
  81. }
  82. }
  83. Loader.prototype.loadBJSScripts = function (settings) {
  84. this.loadScripts(settings.core.files);
  85. this.loadScripts(settings.extras.files);
  86. for (var i = 0; i< settings.modules.length; i++) {
  87. this.loadModule(settings[settings.modules[i]]);
  88. }
  89. }
  90. Loader.prototype.load = function () {
  91. var self = this;
  92. getJson('/Tools/Gulp/config.json',
  93. function(data) {
  94. self.loadBJSScripts(data);
  95. self.dequeue();
  96. },
  97. function(reason) {
  98. console.error(reason);
  99. });
  100. };
  101. return Loader;
  102. }());
  103. var loader = new Loader();
  104. loader.load();
  105. BABYLONDEVTOOLS.Loader = loader;
  106. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))