index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /// <reference path="../dist/preview release/babylon.d.ts" />
  2. var assetUrl;
  3. var cameraPosition;
  4. var kiosk;
  5. var currentGroup; // animation group
  6. var currentGroupIndex;
  7. var currentScene;
  8. // html tags
  9. var footer = document.getElementById("footer");
  10. var canvas = document.getElementById("renderCanvas");
  11. var canvasZone = document.getElementById("canvasZone");
  12. // Check URL
  13. var indexOf = location.href.indexOf("?");
  14. if (indexOf !== -1) {
  15. var params = location.href.substr(indexOf + 1).split("&");
  16. for (var index = 0; index < params.length; index++) {
  17. var param = params[index].split("=");
  18. var name = param[0];
  19. var value = param[1];
  20. switch (name) {
  21. case "assetUrl": {
  22. assetUrl = value;
  23. break;
  24. }
  25. case "cameraPosition": {
  26. cameraPosition = BABYLON.Vector3.FromArray(value.split(",").map(function(component) { return +component; }));
  27. break;
  28. }
  29. case "kiosk": {
  30. kiosk = value === "true" ? true : false;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. if (kiosk) {
  37. footer.style.display = "none";
  38. canvasZone.style.height = "100%";
  39. }
  40. if (BABYLON.Engine.isSupported()) {
  41. var engine = new BABYLON.Engine(canvas, true, { premultipliedAlpha: false, preserveDrawingBuffer: true });
  42. var htmlInput = document.getElementById("files");
  43. var btnInspector = document.getElementById("btnInspector");
  44. var errorZone = document.getElementById("errorZone");
  45. var filesInput;
  46. var currentScene;
  47. var currentSkybox;
  48. var currentPluginName;
  49. var skyboxPath = skyboxes[defaultSkyboxIndex];
  50. var debugLayerEnabled = false;
  51. engine.loadingUIBackgroundColor = "#a9b5bc";
  52. btnInspector.classList.add("hidden");
  53. btnEnvironment.classList.add("hidden");
  54. canvas.addEventListener("contextmenu", function(evt) {
  55. evt.preventDefault();
  56. }, false);
  57. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  58. // This is really important to tell Babylon.js to use decomposeLerp and matrix interpolation
  59. BABYLON.Animation.AllowMatricesInterpolation = true;
  60. // Setting up some GLTF values
  61. BABYLON.GLTFFileLoader.IncrementalLoading = false;
  62. BABYLON.SceneLoader.OnPluginActivatedObservable.add(function(plugin) {
  63. currentPluginName = plugin.name;
  64. if (currentPluginName === "gltf") {
  65. plugin.onValidatedObservable.add(function(results) {
  66. if (results.issues.numErrors > 0) {
  67. debugLayerEnabled = true;
  68. }
  69. });
  70. }
  71. });
  72. // Resize
  73. window.addEventListener("resize", function() {
  74. engine.resize();
  75. });
  76. var sceneLoaded = function(sceneFile, babylonScene) {
  77. engine.clearInternalTexturesCache();
  78. // Clear dropdown that contains animation names
  79. dropdownContent.innerHTML = "";
  80. animationBar.style.display = "none";
  81. currentGroup = null;
  82. if (babylonScene.animationGroups.length > 0) {
  83. animationBar.style.display = "flex";
  84. for (var index = 0; index < babylonScene.animationGroups.length; index++) {
  85. var group = babylonScene.animationGroups[index];
  86. createDropdownLink(group, index);
  87. }
  88. currentGroup = babylonScene.animationGroups[0];
  89. currentGroupIndex = 0;
  90. currentGroup.play(true);
  91. }
  92. // Sync the slider with the current frame
  93. babylonScene.registerBeforeRender(function() {
  94. if (currentGroup) {
  95. var targetedAnimations = currentGroup.targetedAnimations;
  96. if (targetedAnimations.length > 0) {
  97. var runtimeAnimations = currentGroup.targetedAnimations[0].animation.runtimeAnimations;
  98. if (runtimeAnimations.length > 0) {
  99. slider.value = runtimeAnimations[0].currentFrame;
  100. }
  101. }
  102. }
  103. });
  104. // Clear the error
  105. errorZone.style.display = 'none';
  106. btnInspector.classList.remove("hidden");
  107. btnEnvironment.classList.remove("hidden");
  108. currentScene = babylonScene;
  109. document.title = "Babylon.js - " + sceneFile.name;
  110. // Fix for IE, otherwise it will change the default filter for files selection after first use
  111. htmlInput.value = "";
  112. // Attach camera to canvas inputs
  113. if (!currentScene.activeCamera || currentScene.lights.length === 0) {
  114. currentScene.createDefaultCamera(true);
  115. if (cameraPosition) {
  116. currentScene.activeCamera.setPosition(cameraPosition);
  117. }
  118. else {
  119. if (currentPluginName === "gltf") {
  120. // glTF assets use a +Z forward convention while the default camera faces +Z. Rotate the camera to look at the front of the asset.
  121. currentScene.activeCamera.alpha += Math.PI;
  122. }
  123. // Enable camera's behaviors
  124. currentScene.activeCamera.useFramingBehavior = true;
  125. var framingBehavior = currentScene.activeCamera.getBehaviorByName("Framing");
  126. framingBehavior.framingTime = 0;
  127. framingBehavior.elevationReturnTime = -1;
  128. if (currentScene.meshes.length) {
  129. var worldExtends = currentScene.getWorldExtends();
  130. currentScene.activeCamera.lowerRadiusLimit = null;
  131. framingBehavior.zoomOnBoundingInfo(worldExtends.min, worldExtends.max);
  132. }
  133. }
  134. currentScene.activeCamera.pinchPrecision = 200 / currentScene.activeCamera.radius;
  135. currentScene.activeCamera.upperRadiusLimit = 5 * currentScene.activeCamera.radius;
  136. currentScene.activeCamera.wheelDeltaPercentage = 0.01;
  137. currentScene.activeCamera.pinchDeltaPercentage = 0.01;
  138. }
  139. currentScene.activeCamera.attachControl(canvas);
  140. // Lighting
  141. if (currentPluginName === "gltf") {
  142. if (!currentScene.environmentTexture) {
  143. currentScene.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(skyboxPath, currentScene);
  144. }
  145. currentSkybox = currentScene.createDefaultSkybox(currentScene.environmentTexture, true, (currentScene.activeCamera.maxZ - currentScene.activeCamera.minZ) / 2, 0.3, false);
  146. }
  147. else {
  148. currentScene.createDefaultLight();
  149. }
  150. // In case of error during loading, meshes will be empty and clearColor is set to red
  151. if (currentScene.meshes.length === 0 && currentScene.clearColor.r === 1 && currentScene.clearColor.g === 0 && currentScene.clearColor.b === 0) {
  152. document.getElementById("logo").className = "";
  153. canvas.style.opacity = 0;
  154. debugLayerEnabled = true;
  155. }
  156. else {
  157. if (BABYLON.Tools.errorsCount > 0) {
  158. debugLayerEnabled = true;
  159. }
  160. document.getElementById("logo").className = "hidden";
  161. document.getElementById("droptext").className = "hidden";
  162. canvas.style.opacity = 1;
  163. if (currentScene.activeCamera.keysUp) {
  164. currentScene.activeCamera.keysUp.push(90); // Z
  165. currentScene.activeCamera.keysUp.push(87); // W
  166. currentScene.activeCamera.keysDown.push(83); // S
  167. currentScene.activeCamera.keysLeft.push(65); // A
  168. currentScene.activeCamera.keysLeft.push(81); // Q
  169. currentScene.activeCamera.keysRight.push(69); // E
  170. currentScene.activeCamera.keysRight.push(68); // D
  171. }
  172. }
  173. if (debugLayerEnabled) {
  174. currentScene.debugLayer.show();
  175. }
  176. };
  177. var sceneError = function(sceneFile, babylonScene, message) {
  178. document.title = "Babylon.js - " + sceneFile.name;
  179. document.getElementById("logo").className = "";
  180. canvas.style.opacity = 0;
  181. 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>';
  182. errorZone.style.display = 'block';
  183. errorZone.innerHTML = errorContent;
  184. // Close button error
  185. errorZone.querySelector('.close').addEventListener('click', function() {
  186. errorZone.style.display = 'none';
  187. });
  188. };
  189. var loadFromAssetUrl = function() {
  190. var rootUrl = BABYLON.Tools.GetFolderPath(assetUrl);
  191. var fileName = BABYLON.Tools.GetFilename(assetUrl);
  192. BABYLON.SceneLoader.LoadAsync(rootUrl, fileName, engine).then(function(scene) {
  193. if (currentScene) {
  194. currentScene.dispose();
  195. }
  196. sceneLoaded({ name: fileName }, scene);
  197. scene.whenReadyAsync().then(function() {
  198. engine.runRenderLoop(function() {
  199. scene.render();
  200. });
  201. });
  202. }).catch(function(reason) {
  203. sceneError({ name: fileName }, null, reason.message || reason);
  204. });
  205. };
  206. if (assetUrl) {
  207. loadFromAssetUrl();
  208. }
  209. else {
  210. var startProcessingFiles = function() {
  211. BABYLON.Tools.ClearLogCache();
  212. };
  213. filesInput = new BABYLON.FilesInput(engine, null, sceneLoaded, null, null, null, startProcessingFiles, null, sceneError);
  214. filesInput.onProcessFileCallback = (function(file, name, extension) {
  215. if (filesInput._filesToLoad && filesInput._filesToLoad.length === 1 && extension) {
  216. if (extension.toLowerCase() === "dds" || extension.toLowerCase() === "env") {
  217. BABYLON.FilesInput.FilesToLoad[name] = file;
  218. skyboxPath = "file:" + file.correctName;
  219. return false;
  220. }
  221. }
  222. return true;
  223. }).bind(this);
  224. filesInput.monitorElementForDragNDrop(canvas);
  225. htmlInput.addEventListener('change', function(event) {
  226. // Handling data transfer via drag'n'drop
  227. if (event && event.dataTransfer && event.dataTransfer.files) {
  228. filesToLoad = event.dataTransfer.files;
  229. }
  230. // Handling files from input files
  231. if (event && event.target && event.target.files) {
  232. filesToLoad = event.target.files;
  233. }
  234. filesInput.loadFiles(event);
  235. }, false);
  236. }
  237. window.addEventListener("keydown", function(event) {
  238. // Press R to reload
  239. if (event.keyCode === 82 && event.target.nodeName !== "INPUT" && currentScene) {
  240. if (assetUrl) {
  241. loadFromAssetUrl();
  242. }
  243. else {
  244. filesInput.reload();
  245. }
  246. }
  247. });
  248. btnInspector.addEventListener('click', function() {
  249. if (currentScene) {
  250. if (currentScene.debugLayer.isVisible()) {
  251. debugLayerEnabled = false;
  252. currentScene.debugLayer.hide();
  253. }
  254. else {
  255. currentScene.debugLayer.show();
  256. debugLayerEnabled = true;
  257. }
  258. }
  259. }, false);
  260. window.addEventListener("keydown", function(event) {
  261. // Press space to toggle footer
  262. if (event.keyCode === 32 && event.target.nodeName !== "INPUT") {
  263. if (footer.style.display === "none") {
  264. footer.style.display = "block";
  265. canvasZone.style.height = "calc(100% - 56px)";
  266. if (debugLayerEnabled) {
  267. currentScene.debugLayer.show();
  268. }
  269. engine.resize();
  270. }
  271. else {
  272. footer.style.display = "none";
  273. canvasZone.style.height = "100%";
  274. errorZone.style.display = "none";
  275. engine.resize();
  276. if (currentScene.debugLayer.isVisible()) {
  277. currentScene.debugLayer.hide();
  278. }
  279. }
  280. }
  281. });
  282. sizeScene();
  283. window.onresize = function() {
  284. sizeScene();
  285. }
  286. }
  287. function sizeScene() {
  288. let divInspWrapper = document.getElementsByClassName('insp-wrapper')[0];
  289. if (divInspWrapper) {
  290. let divFooter = document.getElementsByClassName('footer')[0];
  291. divInspWrapper.style.height = (document.body.clientHeight - divFooter.clientHeight) + "px";
  292. divInspWrapper.style['max-width'] = document.body.clientWidth + "px";
  293. }
  294. }