BabylonLoader.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.debugShortcut = function(engine) {
  42. // Add inspector shortcut
  43. var map = {};
  44. var onkey = function(e){
  45. e = e || event; // to deal with IE
  46. map[e.keyCode] = e.type == 'keydown';
  47. if(map[17] && map[16] && map[18] && map[73]) {
  48. if (engine.scenes && engine.scenes.length > 0) {
  49. for (var i = 0; i < engine.scenes.length; i ++) {
  50. if (engine.scenes[0].debugLayer.isVisible()) {
  51. engine.scenes[0].debugLayer.hide();
  52. }
  53. else {
  54. engine.scenes[0].debugLayer.show();
  55. }
  56. }
  57. }
  58. map = {};
  59. return false;
  60. }
  61. };
  62. document.addEventListener("keydown", onkey);
  63. document.addEventListener("keyup", onkey);
  64. }
  65. Loader.prototype.root = function (newBabylonJSPath) {
  66. babylonJSPath = newBabylonJSPath;
  67. return this;
  68. }
  69. Loader.prototype.require = function (newDependencies) {
  70. if (typeof newDependencies === 'string') {
  71. dependencies.push(newDependencies);
  72. }
  73. else if (newDependencies) {
  74. for (var i = 0; i < newDependencies.length; i++) {
  75. dependencies.push(newDependencies[i]);
  76. }
  77. }
  78. return this;
  79. }
  80. Loader.prototype.onReady = function (newCallback) {
  81. callback = newCallback;
  82. return this;
  83. }
  84. Loader.prototype.dequeue = function () {
  85. if (queue.length == 0) {
  86. console.log('Scripts loaded');
  87. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  88. if (callback) {
  89. callback();
  90. }
  91. return;
  92. }
  93. var url = queue.shift();
  94. var head = document.getElementsByTagName('head')[0];
  95. var script = document.createElement('script');
  96. script.type = 'text/javascript';
  97. script.src = url;
  98. var self = this;
  99. script.onload = function() {
  100. self.dequeue();
  101. };
  102. head.appendChild(script);
  103. }
  104. Loader.prototype.loadScript = function (url) {
  105. queue.push(url);
  106. }
  107. Loader.prototype.loadCss = function (url) {
  108. var head = document.getElementsByTagName('head')[0];
  109. var style = document.createElement('link');
  110. style.href = url;
  111. style.rel = "stylesheet";
  112. style.type = "text/css"
  113. document.head.appendChild(style);
  114. }
  115. Loader.prototype.loadScripts = function (urls) {
  116. for (var i = 0; i< urls.length; i++) {
  117. this.loadScript(urls[i]);
  118. }
  119. }
  120. Loader.prototype.loadLibrary = function (library, module) {
  121. if (!useDist) {
  122. var i = 0;
  123. for (; i < library.files.length; i++) {
  124. var file = library.files[i];
  125. if (file.indexOf('lib.d.ts') > 0) {
  126. continue;
  127. }
  128. file = file.replace('.ts', '.js');
  129. file = file.replace('../', '');
  130. file = babylonJSPath + '/' + file;
  131. this.loadScript(file);
  132. }
  133. if (library.shaderFiles && library.shaderFiles.length > 0) {
  134. var shaderFile = library.shaderFiles[0];
  135. var endDirectoryIndex = shaderFile.lastIndexOf('/');
  136. shaderFile = shaderFile.substring(0, endDirectoryIndex + 1);
  137. shaderFile += library.output.replace('.js', '.js.fx');
  138. file = file.replace('../', '');
  139. file = babylonJSPath + '/' + file;
  140. this.loadScript(shaderFile);
  141. }
  142. }
  143. else if (min) {
  144. if (library.webpack) {
  145. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output.replace('.js', '.bundle.js'));
  146. }
  147. else {
  148. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output.replace('.js', '.min.js'));
  149. }
  150. }
  151. else {
  152. this.loadScript(babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + library.output);
  153. }
  154. if (!min && library.sassFiles && library.sassFiles.length > 0) {
  155. var cssFile = library.output.replace('.js', '.css');
  156. cssFile = babylonJSPath + '/dist/preview release' + module.build.distOutputDirectory + cssFile;
  157. this.loadCss(cssFile);
  158. }
  159. }
  160. Loader.prototype.loadModule = function (module) {
  161. for (var i = 0; i< module.libraries.length; i++) {
  162. this.loadLibrary(module.libraries[i], module);
  163. }
  164. }
  165. Loader.prototype.loadBJSScripts = function (settings) {
  166. if (!useDist) {
  167. this.loadScripts(settings.core.files);
  168. this.loadScripts(settings.extras.files);
  169. }
  170. else if (min) {
  171. this.loadScript('/dist/preview release/babylon.js');
  172. }
  173. else {
  174. this.loadScript('/dist/preview release/babylon.max.js');
  175. }
  176. for (var i = 0; i< settings.modules.length; i++) {
  177. this.loadModule(settings[settings.modules[i]]);
  178. }
  179. }
  180. Loader.prototype.load = function (newCallback) {
  181. var self = this;
  182. if (newCallback) {
  183. callback = newCallback;
  184. }
  185. getJson('/Tools/Gulp/config.json',
  186. function(data) {
  187. if (!min) {
  188. self.loadScript('/dist/preview release/split.js');
  189. }
  190. self.loadBJSScripts(data);
  191. if (dependencies) {
  192. self.loadScripts(dependencies);
  193. }
  194. self.dequeue();
  195. },
  196. function(reason) {
  197. console.error(reason);
  198. }
  199. );
  200. };
  201. return Loader;
  202. }());
  203. var loader = new Loader();
  204. BABYLONDEVTOOLS.Loader = loader;
  205. })(BABYLONDEVTOOLS || (BABYLONDEVTOOLS = {}))