index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 currentGroup; // animation group
  7. var currentGroupIndex;
  8. var currentScene;
  9. // html balise
  10. var animationBar = document.getElementById("animationBar");
  11. var dropdownLabel = document.getElementById("dropdownLabel");
  12. var dropdownContent = document.getElementById("dropdownContent");
  13. var playBtn = document.getElementById("playBtn");
  14. var slider = document.getElementById("slider");
  15. var indexOf = location.href.indexOf("?");
  16. if (indexOf !== -1) {
  17. var params = location.href.substr(indexOf + 1).split("&");
  18. for (var index = 0; index < params.length; index++) {
  19. var param = params[index].split("=");
  20. var name = param[0];
  21. var value = param[1];
  22. switch (name) {
  23. case "assetUrl": {
  24. assetUrl = value;
  25. break;
  26. }
  27. case "cameraPosition": {
  28. cameraPosition = BABYLON.Vector3.FromArray(value.split(",").map(function (component) { return +component; }));
  29. break;
  30. }
  31. case "kiosk": {
  32. kiosk = value === "true" ? true : false;
  33. break;
  34. }
  35. }
  36. }
  37. }
  38. if (BABYLON.Engine.isSupported()) {
  39. var canvas = document.getElementById("renderCanvas");
  40. var engine = new BABYLON.Engine(canvas, true);
  41. var htmlInput = document.getElementById("files");
  42. var footer = document.getElementById("footer");
  43. var btnFullScreen = document.getElementById("btnFullscreen");
  44. var btnInspector = document.getElementById("btnInspector");
  45. var errorZone = document.getElementById("errorZone");
  46. var filesInput;
  47. var currentScene;
  48. var currentSkybox;
  49. var enableDebugLayer = false;
  50. var currentPluginName;
  51. var skyboxPath = "Assets/environment.dds";
  52. engine.loadingUIBackgroundColor = "#80a8bf";
  53. btnFullScreen.classList.add("hidden");
  54. btnInspector.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. });
  66. // Resize
  67. window.addEventListener("resize", function () {
  68. engine.resize();
  69. });
  70. var sceneLoaded = function (sceneFile, babylonScene) {
  71. function displayDebugLayerAndLogs() {
  72. currentScene.debugLayer._displayLogs = true;
  73. enableDebugLayer = true;
  74. currentScene.debugLayer.show();
  75. };
  76. function hideDebugLayerAndLogs() {
  77. currentScene.debugLayer._displayLogs = false;
  78. enableDebugLayer = false;
  79. currentScene.debugLayer.hide();
  80. };
  81. if (enableDebugLayer) {
  82. hideDebugLayerAndLogs();
  83. }
  84. // Clear dropdown that contains animation names
  85. dropdownContent.innerHTML = "";
  86. animationBar.style.display = "none";
  87. currentGroup = null;
  88. if(babylonScene.animationGroups.length > 0) {
  89. animationBar.style.display = "flex";
  90. for (var index = 0; index < babylonScene.animationGroups.length; index++) {
  91. var group = babylonScene.animationGroups[index];
  92. createDropdownLink(group,index);
  93. }
  94. currentGroup = babylonScene.animationGroups[0];
  95. currentGroupIndex = 0;
  96. document.getElementById( formatId(currentGroup.name+"-"+currentGroupIndex)).click();
  97. }
  98. // Sync the slider with the current frame
  99. babylonScene.registerBeforeRender(function () {
  100. if (currentGroup != null && currentGroup.targetedAnimations[0].animation.runtimeAnimations[0] != null) {
  101. var currentValue = slider.valueAsNumber;
  102. var newValue = currentGroup.targetedAnimations[0].animation.runtimeAnimations[0].currentFrame;
  103. if (Math.abs(currentValue - newValue) > 0.01) {
  104. slider.value = newValue
  105. }
  106. }
  107. });
  108. // Clear the error
  109. errorZone.style.display = 'none';
  110. btnFullScreen.classList.remove("hidden");
  111. btnInspector.classList.remove("hidden");
  112. currentScene = babylonScene;
  113. document.title = "BabylonJS - " + sceneFile.name;
  114. // Fix for IE, otherwise it will change the default filter for files selection after first use
  115. htmlInput.value = "";
  116. // Attach camera to canvas inputs
  117. if (!currentScene.activeCamera || currentScene.lights.length === 0) {
  118. currentScene.createDefaultCameraOrLight(true);
  119. if (cameraPosition) {
  120. currentScene.activeCamera.setPosition(cameraPosition);
  121. }
  122. else {
  123. if (currentPluginName === "gltf") {
  124. // glTF assets use a +Z forward convention while the default camera faces +Z. Rotate the camera to look at the front of the asset.
  125. currentScene.activeCamera.alpha += Math.PI;
  126. }
  127. // Enable camera's behaviors
  128. currentScene.activeCamera.useFramingBehavior = true;
  129. var framingBehavior = currentScene.activeCamera.getBehaviorByName("Framing");
  130. framingBehavior.framingTime = 0;
  131. framingBehavior.elevationReturnTime = -1;
  132. if (currentScene.meshes.length) {
  133. var worldExtends = currentScene.getWorldExtends();
  134. currentScene.activeCamera.lowerRadiusLimit = null;
  135. framingBehavior.zoomOnBoundingInfo(worldExtends.min, worldExtends.max);
  136. }
  137. }
  138. currentScene.activeCamera.pinchPrecision = 200 / currentScene.activeCamera.radius;
  139. currentScene.activeCamera.upperRadiusLimit = 5 * currentScene.activeCamera.radius;
  140. currentScene.activeCamera.wheelDeltaPercentage = 0.01;
  141. currentScene.activeCamera.pinchDeltaPercentage = 0.01;
  142. }
  143. currentScene.activeCamera.attachControl(canvas);
  144. // Environment
  145. if (currentPluginName === "gltf") {
  146. var hdrTexture = BABYLON.CubeTexture.CreateFromPrefilteredData(skyboxPath, currentScene);
  147. currentSkybox = currentScene.createDefaultSkybox(hdrTexture, true, (currentScene.activeCamera.maxZ - currentScene.activeCamera.minZ) / 2, 0.3);
  148. }
  149. // In case of error during loading, meshes will be empty and clearColor is set to red
  150. if (currentScene.meshes.length === 0 && currentScene.clearColor.r === 1 && currentScene.clearColor.g === 0 && currentScene.clearColor.b === 0) {
  151. document.getElementById("logo").className = "";
  152. canvas.style.opacity = 0;
  153. displayDebugLayerAndLogs();
  154. }
  155. else {
  156. if (BABYLON.Tools.errorsCount > 0) {
  157. displayDebugLayerAndLogs();
  158. }
  159. document.getElementById("logo").className = "hidden";
  160. canvas.style.opacity = 1;
  161. if (currentScene.activeCamera.keysUp) {
  162. currentScene.activeCamera.keysUp.push(90); // Z
  163. currentScene.activeCamera.keysUp.push(87); // W
  164. currentScene.activeCamera.keysDown.push(83); // S
  165. currentScene.activeCamera.keysLeft.push(65); // A
  166. currentScene.activeCamera.keysLeft.push(81); // Q
  167. currentScene.activeCamera.keysRight.push(69); // E
  168. currentScene.activeCamera.keysRight.push(68); // D
  169. }
  170. }
  171. };
  172. var sceneError = function (sceneFile, babylonScene, message) {
  173. document.title = "BabylonJS - " + sceneFile.name;
  174. document.getElementById("logo").className = "";
  175. canvas.style.opacity = 0;
  176. 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>';
  177. errorZone.style.display = 'block';
  178. errorZone.innerHTML = errorContent;
  179. // Close button error
  180. errorZone.querySelector('.close').addEventListener('click', function () {
  181. errorZone.style.display = 'none';
  182. });
  183. };
  184. if (assetUrl) {
  185. var rootUrl = BABYLON.Tools.GetFolderPath(assetUrl);
  186. var fileName = BABYLON.Tools.GetFilename(assetUrl);
  187. BABYLON.SceneLoader.LoadAsync(rootUrl, fileName, engine).then(function (scene) {
  188. sceneLoaded({ name: fileName }, scene);
  189. currentScene = scene;
  190. scene.whenReadyAsync().then(function () {
  191. engine.runRenderLoop(function () {
  192. scene.render();
  193. });
  194. });
  195. }).catch(function (reason) {
  196. sceneError({ name: fileName }, null, reason);
  197. });
  198. }
  199. else {
  200. filesInput = new BABYLON.FilesInput(engine, null, sceneLoaded, null, null, null, function () { BABYLON.Tools.ClearLogCache() }, null, sceneError);
  201. filesInput.onProcessFileCallback = (function (file, name, extension) {
  202. if (filesInput._filesToLoad && filesInput._filesToLoad.length === 1 && extension && extension.toLowerCase() === "dds") {
  203. BABYLON.FilesInput.FilesToLoad[name] = file;
  204. skyboxPath = "file:" + file.correctName;
  205. return false;
  206. }
  207. return true;
  208. }).bind(this);
  209. filesInput.monitorElementForDragNDrop(canvas);
  210. window.addEventListener("keydown", function (evt) {
  211. // Press R to reload
  212. if (evt.keyCode === 82) {
  213. filesInput.reload();
  214. }
  215. });
  216. htmlInput.addEventListener('change', function (event) {
  217. var filestoLoad;
  218. // Handling data transfer via drag'n'drop
  219. if (event && event.dataTransfer && event.dataTransfer.files) {
  220. filesToLoad = event.dataTransfer.files;
  221. }
  222. // Handling files from input files
  223. if (event && event.target && event.target.files) {
  224. filesToLoad = event.target.files;
  225. }
  226. filesInput.loadFiles(event);
  227. }, false);
  228. }
  229. if (kiosk) {
  230. footer.style.display = "none";
  231. }
  232. btnFullScreen.addEventListener('click', function () {
  233. engine.switchFullscreen(true);
  234. }, false);
  235. btnInspector.addEventListener('click', function () {
  236. if (currentScene) {
  237. if (!enableDebugLayer) {
  238. currentScene.debugLayer.show();
  239. enableDebugLayer = true;
  240. } else {
  241. currentScene.debugLayer.hide();
  242. enableDebugLayer = false;
  243. }
  244. }
  245. }, false);
  246. window.addEventListener("keydown", function (evt) {
  247. // Press Esc to toggle footer
  248. if (evt.keyCode === 27 &&!enableDebugLayer) {
  249. if (footer.style.display === "none") {
  250. footer.style.display = "block";
  251. }
  252. else {
  253. footer.style.display = "none";
  254. errorZone.style.display = "none";
  255. if (enableDebugLayer) {
  256. currentScene.debugLayer.hide();
  257. enableDebugLayer = false;
  258. }
  259. }
  260. }
  261. });
  262. sizeScene();
  263. window.onresize = function () {
  264. sizeScene();
  265. }
  266. }
  267. function sizeScene() {
  268. let divInspWrapper = document.getElementsByClassName('insp-wrapper')[0];
  269. if (divInspWrapper) {
  270. let divFooter = document.getElementsByClassName('footer')[0];
  271. divInspWrapper.style.height = (document.body.clientHeight - divFooter.clientHeight) + "px";
  272. divInspWrapper.style['max-width'] = document.body.clientWidth + "px";
  273. }
  274. }
  275. // animation
  276. // event on the dropdown
  277. function formatId(name){
  278. return "data-" + name.replace(/\s/g,'');
  279. }
  280. function createDropdownLink(group,index) {
  281. var animation = document.createElement("a");
  282. animation.innerHTML = group.name;
  283. animation.setAttribute("id", formatId(group.name+"-"+index));
  284. animation.addEventListener("click", function() {
  285. // stop the current animation group
  286. currentGroup.reset();
  287. currentGroup.stop();
  288. document.getElementById( formatId(currentGroup.name+"-"+currentGroupIndex)).classList.remove("active");
  289. playBtn.classList.remove("play");
  290. playBtn.classList.add("pause");
  291. // start the new animation group
  292. currentGroup = group;
  293. currentGroupIndex = index;
  294. currentGroup.start(true);
  295. this.classList.add("active");
  296. dropdownLabel.innerHTML = currentGroup.name;
  297. // set the slider
  298. slider.setAttribute("min", currentGroup.from);
  299. slider.setAttribute("max", currentGroup.to);
  300. currentSliderValue = 0;
  301. slider.value = 0;
  302. });
  303. dropdownContent.appendChild(animation);
  304. }
  305. // event on the play/pause button
  306. playBtn.addEventListener("click", function() {
  307. // click on the button to run the animation
  308. if( this.classList.contains("play") ) {
  309. this.classList.remove("play");
  310. this.classList.add("pause");
  311. var currentFrame = slider.value;
  312. currentGroup.play(true);
  313. }
  314. // click on the button to pause the animation
  315. else {
  316. this.classList.add("play");
  317. this.classList.remove("pause");
  318. currentGroup.pause();
  319. }
  320. });
  321. // event on the slider
  322. slider.addEventListener("input", function() {
  323. if( playBtn.classList.contains("play") ) {
  324. currentGroup.play(true);
  325. currentGroup.goToFrame(this.value);
  326. currentGroup.pause();
  327. } else {
  328. currentGroup.goToFrame(this.value);
  329. }
  330. });
  331. var sliderPause = false;
  332. slider.addEventListener("mousedown", function() {
  333. if( playBtn.classList.contains("pause") ) {
  334. sliderPause = true;
  335. playBtn.click();
  336. }
  337. });
  338. slider.addEventListener("mouseup", function() {
  339. if( sliderPause ) {
  340. sliderPause = false;
  341. playBtn.click();
  342. }
  343. });