index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 = "#2A2342";
  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. var pbrPresent = false;
  149. for (var i = 0; i < currentScene.materials.length; i++) {
  150. if (currentScene.materials[i]._transparencyMode !== undefined) {
  151. pbrPresent = true;
  152. break;
  153. }
  154. }
  155. if (pbrPresent) {
  156. if (!currentScene.environmentTexture) {
  157. currentScene.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(skyboxPath, currentScene);
  158. }
  159. }
  160. else {
  161. currentScene.createDefaultLight();
  162. }
  163. }
  164. // In case of error during loading, meshes will be empty and clearColor is set to red
  165. if (currentScene.meshes.length === 0 && currentScene.clearColor.r === 1 && currentScene.clearColor.g === 0 && currentScene.clearColor.b === 0) {
  166. document.getElementById("logo").className = "";
  167. canvas.style.opacity = 0;
  168. debugLayerEnabled = true;
  169. }
  170. else {
  171. if (BABYLON.Tools.errorsCount > 0) {
  172. debugLayerEnabled = true;
  173. }
  174. document.getElementById("logo").className = "hidden";
  175. document.getElementById("droptext").className = "hidden";
  176. canvas.style.opacity = 1;
  177. if (currentScene.activeCamera.keysUp) {
  178. currentScene.activeCamera.keysUp.push(90); // Z
  179. currentScene.activeCamera.keysUp.push(87); // W
  180. currentScene.activeCamera.keysDown.push(83); // S
  181. currentScene.activeCamera.keysLeft.push(65); // A
  182. currentScene.activeCamera.keysLeft.push(81); // Q
  183. currentScene.activeCamera.keysRight.push(69); // E
  184. currentScene.activeCamera.keysRight.push(68); // D
  185. }
  186. }
  187. if (debugLayerEnabled) {
  188. currentScene.debugLayer.show();
  189. }
  190. };
  191. var sceneError = function(sceneFile, babylonScene, message) {
  192. document.title = "Babylon.js - " + sceneFile.name;
  193. document.getElementById("logo").className = "";
  194. canvas.style.opacity = 0;
  195. 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>';
  196. errorZone.style.display = 'block';
  197. errorZone.innerHTML = errorContent;
  198. // Close button error
  199. errorZone.querySelector('.close').addEventListener('click', function() {
  200. errorZone.style.display = 'none';
  201. });
  202. };
  203. var loadFromAssetUrl = function() {
  204. var rootUrl = BABYLON.Tools.GetFolderPath(assetUrl);
  205. var fileName = BABYLON.Tools.GetFilename(assetUrl);
  206. BABYLON.SceneLoader.LoadAsync(rootUrl, fileName, engine).then(function(scene) {
  207. if (currentScene) {
  208. currentScene.dispose();
  209. }
  210. sceneLoaded({ name: fileName }, scene);
  211. scene.whenReadyAsync().then(function() {
  212. engine.runRenderLoop(function() {
  213. scene.render();
  214. });
  215. });
  216. }).catch(function(reason) {
  217. sceneError({ name: fileName }, null, reason.message || reason);
  218. });
  219. };
  220. if (assetUrl) {
  221. loadFromAssetUrl();
  222. }
  223. else {
  224. var startProcessingFiles = function() {
  225. BABYLON.Tools.ClearLogCache();
  226. };
  227. filesInput = new BABYLON.FilesInput(engine, null, sceneLoaded, null, null, null, startProcessingFiles, null, sceneError);
  228. filesInput.onProcessFileCallback = (function(file, name, extension) {
  229. if (filesInput._filesToLoad && filesInput._filesToLoad.length === 1 && extension) {
  230. if (extension.toLowerCase() === "dds" || extension.toLowerCase() === "env") {
  231. BABYLON.FilesInput.FilesToLoad[name] = file;
  232. skyboxPath = "file:" + file.correctName;
  233. return false;
  234. }
  235. }
  236. return true;
  237. }).bind(this);
  238. filesInput.monitorElementForDragNDrop(canvas);
  239. htmlInput.addEventListener('change', function(event) {
  240. // Handling data transfer via drag'n'drop
  241. if (event && event.dataTransfer && event.dataTransfer.files) {
  242. filesToLoad = event.dataTransfer.files;
  243. }
  244. // Handling files from input files
  245. if (event && event.target && event.target.files) {
  246. filesToLoad = event.target.files;
  247. }
  248. filesInput.loadFiles(event);
  249. }, false);
  250. }
  251. window.addEventListener("keydown", function(event) {
  252. // Press R to reload
  253. if (event.keyCode === 82 && event.target.nodeName !== "INPUT" && currentScene) {
  254. if (assetUrl) {
  255. loadFromAssetUrl();
  256. }
  257. else {
  258. filesInput.reload();
  259. }
  260. }
  261. });
  262. btnInspector.addEventListener('click', function() {
  263. if (currentScene) {
  264. if (currentScene.debugLayer.isVisible()) {
  265. debugLayerEnabled = false;
  266. currentScene.debugLayer.hide();
  267. }
  268. else {
  269. currentScene.debugLayer.show();
  270. debugLayerEnabled = true;
  271. }
  272. }
  273. }, false);
  274. window.addEventListener("keydown", function(event) {
  275. // Press space to toggle footer
  276. if (event.keyCode === 32 && event.target.nodeName !== "INPUT") {
  277. if (footer.style.display === "none") {
  278. footer.style.display = "block";
  279. canvasZone.style.height = "calc(100% - 56px)";
  280. if (debugLayerEnabled) {
  281. currentScene.debugLayer.show();
  282. }
  283. engine.resize();
  284. }
  285. else {
  286. footer.style.display = "none";
  287. canvasZone.style.height = "100%";
  288. errorZone.style.display = "none";
  289. engine.resize();
  290. if (currentScene.debugLayer.isVisible()) {
  291. currentScene.debugLayer.hide();
  292. }
  293. }
  294. }
  295. });
  296. sizeScene();
  297. window.onresize = function() {
  298. sizeScene();
  299. }
  300. }
  301. function sizeScene() {
  302. let divInspWrapper = document.getElementsByClassName('insp-wrapper')[0];
  303. if (divInspWrapper) {
  304. let divFooter = document.getElementsByClassName('footer')[0];
  305. divInspWrapper.style.height = (document.body.clientHeight - divFooter.clientHeight) + "px";
  306. divInspWrapper.style['max-width'] = document.body.clientWidth + "px";
  307. }
  308. }