index.js 14 KB

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