index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /// <reference path="../dist/preview release/babylon.d.ts" />
  2. /// <reference path="../dist/preview release/loaders/babylon.glTFFileLoader.d.ts" />
  3. var assetUrl;
  4. var cameraPosition;
  5. var kiosk;
  6. var indexOf = location.href.indexOf("?");
  7. if (indexOf !== -1) {
  8. var params = location.href.substr(indexOf + 1).split("&");
  9. for (var index = 0; index < params.length; index++) {
  10. var param = params[index].split("=");
  11. var name = param[0];
  12. var value = param[1];
  13. switch (name) {
  14. case "assetUrl": {
  15. assetUrl = value;
  16. break;
  17. }
  18. case "cameraPosition": {
  19. cameraPosition = BABYLON.Vector3.FromArray(value.split(",").map(function (component) { return +component; }));
  20. break;
  21. }
  22. case "kiosk": {
  23. kiosk = value === "true" ? true : false;
  24. break;
  25. }
  26. }
  27. }
  28. }
  29. if (BABYLON.Engine.isSupported()) {
  30. var canvas = document.getElementById("renderCanvas");
  31. var engine = new BABYLON.Engine(canvas, true);
  32. var htmlInput = document.getElementById("files");
  33. var footer = document.getElementById("footer");
  34. var btnFullScreen = document.getElementById("btnFullscreen");
  35. var btnPerf = document.getElementById("btnPerf");
  36. var help01 = document.getElementById("help01");
  37. var help02 = document.getElementById("help02");
  38. var errorZone = document.getElementById("errorZone");
  39. var filesInput;
  40. var currentHelpCounter;
  41. var currentScene;
  42. var currentSkybox;
  43. var enableDebugLayer = false;
  44. var currentPluginName;
  45. var skyboxPath = "Assets/environment.dds";
  46. canvas.addEventListener("contextmenu", function (evt) {
  47. evt.preventDefault();
  48. }, false);
  49. currentHelpCounter = localStorage.getItem("helpcounter");
  50. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  51. if (!currentHelpCounter) currentHelpCounter = 0;
  52. // Setting up some GLTF values
  53. BABYLON.GLTFFileLoader.IncrementalLoading = false;
  54. BABYLON.SceneLoader.OnPluginActivatedObservable.add(function (plugin) {
  55. currentPluginName = plugin.name;
  56. });
  57. // Resize
  58. window.addEventListener("resize", function () {
  59. engine.resize();
  60. });
  61. var sceneLoaded = function (sceneFile, babylonScene) {
  62. function displayDebugLayerAndLogs() {
  63. currentScene.debugLayer._displayLogs = true;
  64. enableDebugLayer = true;
  65. currentScene.debugLayer.show();
  66. };
  67. function hideDebugLayerAndLogs() {
  68. currentScene.debugLayer._displayLogs = false;
  69. enableDebugLayer = false;
  70. currentScene.debugLayer.hide();
  71. };
  72. if (enableDebugLayer) {
  73. hideDebugLayerAndLogs();
  74. }
  75. // Clear the error
  76. errorZone.style.display = 'none';
  77. currentScene = babylonScene;
  78. document.title = "BabylonJS - " + sceneFile.name;
  79. // Fix for IE, otherwise it will change the default filter for files selection after first use
  80. htmlInput.value = "";
  81. // Attach camera to canvas inputs
  82. if (!currentScene.activeCamera || currentScene.lights.length === 0) {
  83. currentScene.createDefaultCameraOrLight(true);
  84. if (cameraPosition) {
  85. currentScene.activeCamera.setPosition(cameraPosition);
  86. }
  87. else {
  88. if (currentPluginName === "gltf") {
  89. // glTF assets use a +Z forward convention while the default camera faces +Z. Rotate the camera to look at the front of the asset.
  90. currentScene.activeCamera.alpha += Math.PI;
  91. }
  92. // Enable camera's behaviors
  93. currentScene.activeCamera.useFramingBehavior = true;
  94. var framingBehavior = currentScene.activeCamera.getBehaviorByName("Framing");
  95. framingBehavior.framingTime = 0;
  96. framingBehavior.elevationReturnTime = -1;
  97. if (currentScene.meshes.length) {
  98. var worldExtends = currentScene.getWorldExtends();
  99. currentScene.activeCamera.lowerRadiusLimit = null;
  100. framingBehavior.zoomOnBoundingInfo(worldExtends.min, worldExtends.max);
  101. }
  102. }
  103. currentScene.activeCamera.pinchPrecision = 200 / currentScene.activeCamera.radius;
  104. currentScene.activeCamera.upperRadiusLimit = 5 * currentScene.activeCamera.radius;
  105. currentScene.activeCamera.wheelDeltaPercentage = 0.01;
  106. currentScene.activeCamera.pinchDeltaPercentage = 0.01;
  107. }
  108. currentScene.activeCamera.attachControl(canvas);
  109. // Environment
  110. if (currentPluginName === "gltf") {
  111. var hdrTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(skyboxPath, currentScene);
  112. currentSkybox = currentScene.createDefaultSkybox(hdrTexture, true, (currentScene.activeCamera.maxZ - currentScene.activeCamera.minZ) / 2, 0.3);
  113. }
  114. // In case of error during loading, meshes will be empty and clearColor is set to red
  115. if (currentScene.meshes.length === 0 && currentScene.clearColor.r === 1 && currentScene.clearColor.g === 0 && currentScene.clearColor.b === 0) {
  116. document.getElementById("logo").className = "";
  117. canvas.style.opacity = 0;
  118. displayDebugLayerAndLogs();
  119. }
  120. else {
  121. if (BABYLON.Tools.errorsCount > 0) {
  122. displayDebugLayerAndLogs();
  123. }
  124. document.getElementById("logo").className = "hidden";
  125. canvas.style.opacity = 1;
  126. if (currentScene.activeCamera.keysUp) {
  127. currentScene.activeCamera.keysUp.push(90); // Z
  128. currentScene.activeCamera.keysUp.push(87); // W
  129. currentScene.activeCamera.keysDown.push(83); // S
  130. currentScene.activeCamera.keysLeft.push(65); // A
  131. currentScene.activeCamera.keysLeft.push(81); // Q
  132. currentScene.activeCamera.keysRight.push(69); // E
  133. currentScene.activeCamera.keysRight.push(68); // D
  134. }
  135. }
  136. };
  137. var sceneError = function (sceneFile, babylonScene, message) {
  138. document.title = "BabylonJS - " + sceneFile.name;
  139. document.getElementById("logo").className = "";
  140. canvas.style.opacity = 0;
  141. var errorContent = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>' + message.replace("file:[object File]", "'" + sceneFile.name + "'") + '</div>';
  142. errorZone.style.display = 'block';
  143. errorZone.innerHTML = errorContent;
  144. // Close button error
  145. errorZone.querySelector('.close').addEventListener('click', function () {
  146. errorZone.style.display = 'none';
  147. });
  148. };
  149. if (assetUrl) {
  150. var rootUrl = BABYLON.Tools.GetFolderPath(assetUrl);
  151. var fileName = BABYLON.Tools.GetFilename(assetUrl);
  152. BABYLON.SceneLoader.LoadAsync(rootUrl, fileName, engine).then(function (scene) {
  153. sceneLoaded({ name: fileName }, scene);
  154. scene.whenReadyAsync().then(function () {
  155. engine.runRenderLoop(function () {
  156. scene.render();
  157. });
  158. });
  159. }).catch(function (reason) {
  160. sceneError({ name: fileName }, null, reason);
  161. });
  162. }
  163. else {
  164. filesInput = new BABYLON.FilesInput(engine, null, sceneLoaded, null, null, null, function () { BABYLON.Tools.ClearLogCache() }, null, sceneError);
  165. filesInput.onProcessFileCallback = (function (file, name, extension) {
  166. if (filesInput._filesToLoad && filesInput._filesToLoad.length === 1 && extension && extension.toLowerCase() === "dds") {
  167. BABYLON.FilesInput.FilesToLoad[name] = file;
  168. skyboxPath = "file:" + file.correctName;
  169. return false;
  170. }
  171. return true;
  172. }).bind(this);
  173. filesInput.monitorElementForDragNDrop(canvas);
  174. window.addEventListener("keydown", function (evt) {
  175. // Press R to reload
  176. if (evt.keyCode === 82) {
  177. filesInput.reload();
  178. }
  179. });
  180. htmlInput.addEventListener('change', function (event) {
  181. var filestoLoad;
  182. // Handling data transfer via drag'n'drop
  183. if (event && event.dataTransfer && event.dataTransfer.files) {
  184. filesToLoad = event.dataTransfer.files;
  185. }
  186. // Handling files from input files
  187. if (event && event.target && event.target.files) {
  188. filesToLoad = event.target.files;
  189. }
  190. filesInput.loadFiles(event);
  191. }, false);
  192. }
  193. if (kiosk) {
  194. footer.style.display = "none";
  195. }
  196. else {
  197. // The help tips will be displayed only 5 times
  198. if (currentHelpCounter < 5) {
  199. help01.className = "help shown";
  200. setTimeout(function () {
  201. help01.className = "help";
  202. help02.className = "help2 shown";
  203. setTimeout(function () {
  204. help02.className = "help2";
  205. localStorage.setItem("helpcounter", currentHelpCounter + 1);
  206. }, 5000);
  207. }, 5000);
  208. }
  209. }
  210. btnFullScreen.addEventListener('click', function () {
  211. engine.switchFullscreen(true);
  212. }, false);
  213. btnPerf.addEventListener('click', function () {
  214. if (currentScene) {
  215. if (!enableDebugLayer) {
  216. currentScene.debugLayer.show();
  217. enableDebugLayer = true;
  218. } else {
  219. currentScene.debugLayer.hide();
  220. enableDebugLayer = false;
  221. }
  222. }
  223. }, false);
  224. window.addEventListener("keydown", function (evt) {
  225. // Press Esc to toggle footer
  226. if (evt.keyCode === 27 &&!enableDebugLayer) {
  227. if (footer.style.display === "none") {
  228. footer.style.display = "block";
  229. }
  230. else {
  231. footer.style.display = "none";
  232. errorZone.style.display = "none";
  233. if (enableDebugLayer) {
  234. currentScene.debugLayer.hide();
  235. enableDebugLayer = false;
  236. }
  237. }
  238. }
  239. });
  240. sizeScene();
  241. window.onresize = function () {
  242. sizeScene();
  243. }
  244. }
  245. function sizeScene() {
  246. let divInspWrapper = document.getElementsByClassName('insp-wrapper')[0];
  247. if (divInspWrapper) {
  248. let divFooter = document.getElementsByClassName('footer')[0];
  249. divInspWrapper.style.height = (document.body.clientHeight - divFooter.clientHeight) + "px";
  250. divInspWrapper.style['max-width'] = document.body.clientWidth + "px";
  251. }
  252. }