index.js 16 KB

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