index.js 16 KB

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