index.js 16 KB

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