index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 filesInputAnimation;
  47. var currentScene;
  48. var currentSkybox;
  49. var currentPluginName;
  50. var skyboxPath = skyboxes[defaultSkyboxIndex];
  51. var debugLayerEnabled = false;
  52. engine.loadingUIBackgroundColor = "#2A2342";
  53. btnInspector.classList.add("hidden");
  54. btnEnvironment.classList.add("hidden");
  55. canvas.addEventListener("contextmenu", function(evt) {
  56. evt.preventDefault();
  57. }, false);
  58. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  59. // This is really important to tell Babylon.js to use decomposeLerp and matrix interpolation
  60. BABYLON.Animation.AllowMatricesInterpolation = true;
  61. // Setting up some GLTF values
  62. BABYLON.GLTFFileLoader.IncrementalLoading = false;
  63. BABYLON.SceneLoader.OnPluginActivatedObservable.add(function(plugin) {
  64. currentPluginName = plugin.name;
  65. if (currentPluginName === "gltf") {
  66. plugin.onValidatedObservable.add(function(results) {
  67. if (results.issues.numErrors > 0) {
  68. debugLayerEnabled = true;
  69. }
  70. });
  71. }
  72. });
  73. // Resize
  74. window.addEventListener("resize", function() {
  75. engine.resize();
  76. });
  77. var anyLoaded = function(babylonScene) {
  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. currentGroupIndex = babylonScene.animationGroups.length - 1;
  89. currentGroup = babylonScene.animationGroups[currentGroupIndex];
  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. }
  107. var assetContainerLoaded = function (sceneFile, babylonScene) {
  108. anyLoaded(babylonScene);
  109. }
  110. var sceneLoaded = function (sceneFile, babylonScene) {
  111. engine.clearInternalTexturesCache();
  112. anyLoaded(babylonScene);
  113. // Fix for IE, otherwise it will change the default filter for files selection after first use
  114. htmlInput.value = "";
  115. currentScene = babylonScene;
  116. document.title = "Babylon.js - " + sceneFile.name;
  117. btnInspector.classList.remove("hidden");
  118. btnEnvironment.classList.remove("hidden");
  119. // Attach camera to canvas inputs
  120. if (!currentScene.activeCamera || currentScene.lights.length === 0) {
  121. currentScene.createDefaultCamera(true);
  122. if (cameraPosition) {
  123. currentScene.activeCamera.setPosition(cameraPosition);
  124. }
  125. else {
  126. if (currentPluginName === "gltf") {
  127. // glTF assets use a +Z forward convention while the default camera faces +Z. Rotate the camera to look at the front of the asset.
  128. currentScene.activeCamera.alpha += Math.PI;
  129. }
  130. // Enable camera's behaviors
  131. currentScene.activeCamera.useFramingBehavior = true;
  132. var framingBehavior = currentScene.activeCamera.getBehaviorByName("Framing");
  133. framingBehavior.framingTime = 0;
  134. framingBehavior.elevationReturnTime = -1;
  135. if (currentScene.meshes.length) {
  136. var worldExtends = currentScene.getWorldExtends();
  137. currentScene.activeCamera.lowerRadiusLimit = null;
  138. framingBehavior.zoomOnBoundingInfo(worldExtends.min, worldExtends.max);
  139. }
  140. }
  141. currentScene.activeCamera.pinchPrecision = 200 / currentScene.activeCamera.radius;
  142. currentScene.activeCamera.upperRadiusLimit = 5 * currentScene.activeCamera.radius;
  143. currentScene.activeCamera.wheelDeltaPercentage = 0.01;
  144. currentScene.activeCamera.pinchDeltaPercentage = 0.01;
  145. }
  146. currentScene.activeCamera.attachControl(canvas);
  147. // Lighting
  148. if (currentPluginName === "gltf") {
  149. if (!currentScene.environmentTexture) {
  150. currentScene.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(skyboxPath, currentScene);
  151. }
  152. currentSkybox = currentScene.createDefaultSkybox(currentScene.environmentTexture, true, (currentScene.activeCamera.maxZ - currentScene.activeCamera.minZ) / 2, 0.3, false);
  153. }
  154. else {
  155. var pbrPresent = false;
  156. for (var i = 0; i < currentScene.materials.length; i++) {
  157. if (currentScene.materials[i]._transparencyMode !== undefined) {
  158. pbrPresent = true;
  159. break;
  160. }
  161. }
  162. if (pbrPresent) {
  163. if (!currentScene.environmentTexture) {
  164. currentScene.environmentTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(skyboxPath, currentScene);
  165. }
  166. }
  167. else {
  168. currentScene.createDefaultLight();
  169. }
  170. }
  171. // In case of error during loading, meshes will be empty and clearColor is set to red
  172. if (currentScene.meshes.length === 0 && currentScene.clearColor.r === 1 && currentScene.clearColor.g === 0 && currentScene.clearColor.b === 0) {
  173. document.getElementById("logo").className = "";
  174. canvas.style.opacity = 0;
  175. debugLayerEnabled = true;
  176. }
  177. else {
  178. if (BABYLON.Tools.errorsCount > 0) {
  179. debugLayerEnabled = true;
  180. }
  181. document.getElementById("logo").className = "hidden";
  182. document.getElementById("droptext").className = "hidden";
  183. canvas.style.opacity = 1;
  184. if (currentScene.activeCamera.keysUp) {
  185. currentScene.activeCamera.keysUp.push(90); // Z
  186. currentScene.activeCamera.keysUp.push(87); // W
  187. currentScene.activeCamera.keysDown.push(83); // S
  188. currentScene.activeCamera.keysLeft.push(65); // A
  189. currentScene.activeCamera.keysLeft.push(81); // Q
  190. currentScene.activeCamera.keysRight.push(69); // E
  191. currentScene.activeCamera.keysRight.push(68); // D
  192. }
  193. }
  194. if (debugLayerEnabled) {
  195. currentScene.debugLayer.show();
  196. }
  197. };
  198. var sceneError = function(sceneFile, babylonScene, message) {
  199. document.title = "Babylon.js - " + sceneFile.name;
  200. document.getElementById("logo").className = "";
  201. canvas.style.opacity = 0;
  202. 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>';
  203. errorZone.style.display = 'block';
  204. errorZone.innerHTML = errorContent;
  205. // Close button error
  206. errorZone.querySelector('.close').addEventListener('click', function() {
  207. errorZone.style.display = 'none';
  208. });
  209. };
  210. var loadFromAssetUrl = function() {
  211. var rootUrl = BABYLON.Tools.GetFolderPath(assetUrl);
  212. var fileName = BABYLON.Tools.GetFilename(assetUrl);
  213. BABYLON.SceneLoader.LoadAsync(rootUrl, fileName, engine).then(function(scene) {
  214. if (currentScene) {
  215. currentScene.dispose();
  216. }
  217. sceneLoaded({ name: fileName }, scene);
  218. scene.whenReadyAsync().then(function() {
  219. engine.runRenderLoop(function() {
  220. scene.render();
  221. });
  222. });
  223. }).catch(function(reason) {
  224. sceneError({ name: fileName }, null, reason.message || reason);
  225. });
  226. };
  227. if (assetUrl) {
  228. loadFromAssetUrl();
  229. }
  230. else {
  231. var startProcessingFiles = function() {
  232. BABYLON.Tools.ClearLogCache();
  233. };
  234. filesInput = new BABYLON.FilesInput(engine, null, sceneLoaded, null, null, null, startProcessingFiles, null, sceneError);
  235. filesInput.onProcessFileCallback = (function(file, name, extension) {
  236. if (filesInput._filesToLoad && filesInput._filesToLoad.length === 1 && extension) {
  237. if (extension.toLowerCase() === "dds" || extension.toLowerCase() === "env") {
  238. BABYLON.FilesInput.FilesToLoad[name] = file;
  239. skyboxPath = "file:" + file.correctName;
  240. return false;
  241. }
  242. }
  243. return true;
  244. }).bind(this);
  245. filesInput.monitorElementForDragNDrop(canvas);
  246. htmlInput.addEventListener('change', function(event) {
  247. // Handling data transfer via drag'n'drop
  248. if (event && event.dataTransfer && event.dataTransfer.files) {
  249. filesToLoad = event.dataTransfer.files;
  250. }
  251. // Handling files from input files
  252. if (event && event.target && event.target.files) {
  253. filesToLoad = event.target.files;
  254. }
  255. filesInput.loadFiles(event);
  256. }, false);
  257. }
  258. window.addEventListener("keydown", function (event) {
  259. // Press R to reload
  260. if (event.keyCode === 82 && event.target.nodeName !== "INPUT" && currentScene) {
  261. if (assetUrl) {
  262. loadFromAssetUrl();
  263. }
  264. else {
  265. filesInput.reload();
  266. }
  267. }
  268. });
  269. btnInspector.addEventListener('click', function() {
  270. if (currentScene) {
  271. if (currentScene.debugLayer.isVisible()) {
  272. debugLayerEnabled = false;
  273. currentScene.debugLayer.hide();
  274. }
  275. else {
  276. currentScene.debugLayer.show();
  277. debugLayerEnabled = true;
  278. }
  279. }
  280. }, false);
  281. window.addEventListener("keydown", function(event) {
  282. // Press space to toggle footer
  283. if (event.keyCode === 32 && event.target.nodeName !== "INPUT") {
  284. if (footer.style.display === "none") {
  285. footer.style.display = "block";
  286. canvasZone.style.height = "calc(100% - 56px)";
  287. if (debugLayerEnabled) {
  288. currentScene.debugLayer.show();
  289. }
  290. engine.resize();
  291. }
  292. else {
  293. footer.style.display = "none";
  294. canvasZone.style.height = "100%";
  295. errorZone.style.display = "none";
  296. engine.resize();
  297. if (currentScene.debugLayer.isVisible()) {
  298. currentScene.debugLayer.hide();
  299. }
  300. }
  301. }
  302. });
  303. sizeScene();
  304. window.onresize = function() {
  305. sizeScene();
  306. }
  307. }
  308. function sizeScene() {
  309. let divInspWrapper = document.getElementsByClassName('insp-wrapper')[0];
  310. if (divInspWrapper) {
  311. let divFooter = document.getElementsByClassName('footer')[0];
  312. divInspWrapper.style.height = (document.body.clientHeight - divFooter.clientHeight) + "px";
  313. divInspWrapper.style['max-width'] = document.body.clientWidth + "px";
  314. }
  315. }