BabylonLoader.js 4.5 KB

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