index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. document.getElementById("droptext").className = "hidden";
  165. canvas.style.opacity = 1;
  166. if (currentScene.activeCamera.keysUp) {
  167. currentScene.activeCamera.keysUp.push(90); // Z
  168. currentScene.activeCamera.keysUp.push(87); // W
  169. currentScene.activeCamera.keysDown.push(83); // S
  170. currentScene.activeCamera.keysLeft.push(65); // A
  171. currentScene.activeCamera.keysLeft.push(81); // Q
  172. currentScene.activeCamera.keysRight.push(69); // E
  173. currentScene.activeCamera.keysRight.push(68); // D
  174. }
  175. }
  176. };
  177. var sceneError = function (sceneFile, babylonScene, message) {
  178. document.title = "BabylonJS - " + sceneFile.name;
  179. document.getElementById("logo").className = "";
  180. canvas.style.opacity = 0;
  181. 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>';
  182. errorZone.style.display = 'block';
  183. errorZone.innerHTML = errorContent;
  184. // Close button error
  185. errorZone.querySelector('.close').addEventListener('click', function () {
  186. errorZone.style.display = 'none';
  187. });
  188. };
  189. if (assetUrl) {
  190. var rootUrl = BABYLON.Tools.GetFolderPath(assetUrl);
  191. var fileName = BABYLON.Tools.GetFilename(assetUrl);
  192. BABYLON.SceneLoader.LoadAsync(rootUrl, fileName, engine).then(function (scene) {
  193. sceneLoaded({ name: fileName }, scene);
  194. currentScene = scene;
  195. scene.whenReadyAsync().then(function () {
  196. engine.runRenderLoop(function () {
  197. scene.render();
  198. });
  199. });
  200. }).catch(function (reason) {
  201. sceneError({ name: fileName }, null, reason.message || reason);
  202. });
  203. }
  204. else {
  205. filesInput = new BABYLON.FilesInput(engine, null, sceneLoaded, null, null, null, function () { BABYLON.Tools.ClearLogCache() }, null, sceneError);
  206. filesInput.onProcessFileCallback = (function (file, name, extension) {
  207. if (filesInput._filesToLoad && filesInput._filesToLoad.length === 1 && extension && extension.toLowerCase() === "dds") {
  208. BABYLON.FilesInput.FilesToLoad[name] = file;
  209. skyboxPath = "file:" + file.correctName;
  210. return false;
  211. }
  212. return true;
  213. }).bind(this);
  214. filesInput.monitorElementForDragNDrop(canvas);
  215. window.addEventListener("keydown", function (evt) {
  216. // Press R to reload
  217. if (evt.keyCode === 82 && !enableDebugLayer) {
  218. filesInput.reload();
  219. }
  220. });
  221. htmlInput.addEventListener('change', function (event) {
  222. var filestoLoad;
  223. // Handling data transfer via drag'n'drop
  224. if (event && event.dataTransfer && event.dataTransfer.files) {
  225. filesToLoad = event.dataTransfer.files;
  226. }
  227. // Handling files from input files
  228. if (event && event.target && event.target.files) {
  229. filesToLoad = event.target.files;
  230. }
  231. filesInput.loadFiles(event);
  232. }, false);
  233. }
  234. if (kiosk) {
  235. footer.style.display = "none";
  236. }
  237. btnFullScreen.addEventListener('click', function () {
  238. engine.switchFullscreen(true);
  239. }, false);
  240. btnInspector.addEventListener('click', function () {
  241. if (currentScene) {
  242. if (!enableDebugLayer) {
  243. currentScene.debugLayer.show();
  244. enableDebugLayer = true;
  245. } else {
  246. currentScene.debugLayer.hide();
  247. enableDebugLayer = false;
  248. }
  249. }
  250. }, false);
  251. window.addEventListener("keydown", function (evt) {
  252. // Press space to toggle footer
  253. if (evt.keyCode === 32 && !enableDebugLayer) {
  254. if (footer.style.display === "none") {
  255. footer.style.display = "block";
  256. }
  257. else {
  258. footer.style.display = "none";
  259. errorZone.style.display = "none";
  260. if (enableDebugLayer) {
  261. currentScene.debugLayer.hide();
  262. enableDebugLayer = false;
  263. }
  264. }
  265. }
  266. });
  267. sizeScene();
  268. window.onresize = function () {
  269. sizeScene();
  270. }
  271. }
  272. function sizeScene() {
  273. let divInspWrapper = document.getElementsByClassName('insp-wrapper')[0];
  274. if (divInspWrapper) {
  275. let divFooter = document.getElementsByClassName('footer')[0];
  276. divInspWrapper.style.height = (document.body.clientHeight - divFooter.clientHeight) + "px";
  277. divInspWrapper.style['max-width'] = document.body.clientWidth + "px";
  278. }
  279. }
  280. // animation
  281. // event on the dropdown
  282. function formatId(name){
  283. return "data-" + name.replace(/\s/g,'');
  284. }
  285. function displayDropdownContent(display) {
  286. if(display) {
  287. dropdownContent.style.display = "flex";
  288. chevronDown.style.display = "inline";
  289. chevronUp.style.display = "none";
  290. }
  291. else {
  292. dropdownContent.style.display = "none";
  293. chevronDown.style.display = "none";
  294. chevronUp.style.display = "inline";
  295. }
  296. }
  297. dropdownBtn.addEventListener("click", function() {
  298. if(dropdownContent.style.display === "flex") {
  299. displayDropdownContent(false);
  300. }
  301. else {
  302. displayDropdownContent(true);
  303. }
  304. });
  305. function createDropdownLink(group,index) {
  306. var animation = document.createElement("a");
  307. animation.innerHTML = group.name;
  308. animation.setAttribute("id", formatId(group.name+"-"+index));
  309. animation.addEventListener("click", function() {
  310. // stop the current animation group
  311. currentGroup.reset();
  312. currentGroup.stop();
  313. document.getElementById( formatId(currentGroup.name+"-"+currentGroupIndex)).classList.remove("active");
  314. playBtn.classList.remove("play");
  315. playBtn.classList.add("pause");
  316. // start the new animation group
  317. currentGroup = group;
  318. currentGroupIndex = index;
  319. currentGroup.start(true);
  320. this.classList.add("active");
  321. dropdownLabel.innerHTML = currentGroup.name;
  322. // set the slider
  323. slider.setAttribute("min", currentGroup.from);
  324. slider.setAttribute("max", currentGroup.to);
  325. currentSliderValue = currentGroup.from;
  326. slider.value = currentGroup.from;
  327. // hide the content of the dropdown
  328. displayDropdownContent(false);
  329. });
  330. dropdownContent.appendChild(animation);
  331. }
  332. // event on the play/pause button
  333. playBtn.addEventListener("click", function() {
  334. // click on the button to run the animation
  335. if( this.classList.contains("play") ) {
  336. this.classList.remove("play");
  337. this.classList.add("pause");
  338. var currentFrame = slider.value;
  339. currentGroup.play(true);
  340. }
  341. // click on the button to pause the animation
  342. else {
  343. this.classList.add("play");
  344. this.classList.remove("pause");
  345. currentGroup.pause();
  346. }
  347. });
  348. // event on the slider
  349. slider.addEventListener("input", function() {
  350. if( playBtn.classList.contains("play") ) {
  351. currentGroup.play(true);
  352. currentGroup.goToFrame(this.value);
  353. currentGroup.pause();
  354. } else {
  355. currentGroup.goToFrame(this.value);
  356. }
  357. });
  358. var sliderPause = false;
  359. slider.addEventListener("mousedown", function() {
  360. if( playBtn.classList.contains("pause") ) {
  361. sliderPause = true;
  362. playBtn.click();
  363. }
  364. });
  365. slider.addEventListener("mouseup", function() {
  366. if( sliderPause ) {
  367. sliderPause = false;
  368. playBtn.click();
  369. }
  370. });