index.js 17 KB

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