index.js 16 KB

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