index.js 15 KB

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