index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 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. canvasZone.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. // Handling data transfer via drag'n'drop
  236. if (event && event.dataTransfer && event.dataTransfer.files) {
  237. filesToLoad = event.dataTransfer.files;
  238. }
  239. // Handling files from input files
  240. if (event && event.target && event.target.files) {
  241. filesToLoad = event.target.files;
  242. }
  243. filesInput.loadFiles(event);
  244. }, false);
  245. }
  246. window.addEventListener("keydown", function(event) {
  247. // Press R to reload
  248. if (event.keyCode === 82 && event.target.nodeName !== "INPUT" && currentScene) {
  249. if (assetUrl) {
  250. loadFromAssetUrl();
  251. }
  252. else {
  253. filesInput.reload();
  254. }
  255. }
  256. });
  257. btnInspector.addEventListener('click', function() {
  258. if (currentScene) {
  259. if (currentScene.debugLayer.isVisible()) {
  260. debugLayerEnabled = false;
  261. currentScene.debugLayer.hide();
  262. }
  263. else {
  264. currentScene.debugLayer.show();
  265. debugLayerEnabled = true;
  266. }
  267. }
  268. }, false);
  269. window.addEventListener("keydown", function(event) {
  270. // Press space to toggle footer
  271. if (event.keyCode === 32 && event.target.nodeName !== "INPUT") {
  272. if (footer.style.display === "none") {
  273. footer.style.display = "block";
  274. canvasZone.style.height = "calc(100% - 56px)";
  275. if (debugLayerEnabled) {
  276. currentScene.debugLayer.show();
  277. }
  278. engine.resize();
  279. }
  280. else {
  281. footer.style.display = "none";
  282. canvasZone.style.height = "100%";
  283. errorZone.style.display = "none";
  284. engine.resize();
  285. if (currentScene.debugLayer.isVisible()) {
  286. currentScene.debugLayer.hide();
  287. }
  288. }
  289. }
  290. });
  291. sizeScene();
  292. window.onresize = function() {
  293. sizeScene();
  294. }
  295. }
  296. function sizeScene() {
  297. let divInspWrapper = document.getElementsByClassName('insp-wrapper')[0];
  298. if (divInspWrapper) {
  299. let divFooter = document.getElementsByClassName('footer')[0];
  300. divInspWrapper.style.height = (document.body.clientHeight - divFooter.clientHeight) + "px";
  301. divInspWrapper.style['max-width'] = document.body.clientWidth + "px";
  302. }
  303. }
  304. // animation
  305. // event on the dropdown
  306. function formatId(name) {
  307. return "data-" + name.replace(/\s/g, '');
  308. }
  309. function displayDropdownContent(display) {
  310. if (display) {
  311. dropdownContent.style.display = "flex";
  312. chevronDown.style.display = "inline";
  313. chevronUp.style.display = "none";
  314. }
  315. else {
  316. dropdownContent.style.display = "none";
  317. chevronDown.style.display = "none";
  318. chevronUp.style.display = "inline";
  319. }
  320. }
  321. dropdownBtn.addEventListener("click", function() {
  322. if (dropdownContent.style.display === "flex") {
  323. displayDropdownContent(false);
  324. }
  325. else {
  326. displayDropdownContent(true);
  327. }
  328. });
  329. function selectCurrentGroup(group, index, animation) {
  330. if (currentGroupIndex !== undefined) {
  331. document.getElementById(formatId(currentGroup.name + "-" + currentGroupIndex)).classList.remove("active");
  332. }
  333. playBtn.classList.remove("play");
  334. playBtn.classList.add("pause");
  335. // start the new animation group
  336. currentGroup = group;
  337. currentGroupIndex = index;
  338. animation.classList.add("active");
  339. dropdownLabel.innerHTML = currentGroup.name;
  340. dropdownLabel.title = currentGroup.name;
  341. // set the slider
  342. slider.setAttribute("min", currentGroup.from);
  343. slider.setAttribute("max", currentGroup.to);
  344. currentSliderValue = currentGroup.from;
  345. slider.value = currentGroup.from;
  346. }
  347. function createDropdownLink(group, index) {
  348. var animation = document.createElement("a");
  349. animation.innerHTML = group.name;
  350. animation.title = group.name;
  351. animation.setAttribute("id", formatId(group.name + "-" + index));
  352. animation.addEventListener("click", function() {
  353. // stop the current animation group
  354. currentGroup.reset();
  355. currentGroup.stop();
  356. group.play(true);
  357. // hide the content of the dropdown
  358. displayDropdownContent(false);
  359. });
  360. dropdownContent.appendChild(animation);
  361. group.onAnimationGroupPlayObservable.add(function(grp) {
  362. selectCurrentGroup(grp, index, animation);
  363. });
  364. group.onAnimationGroupPauseObservable.add(function(grp) {
  365. playBtn.classList.add("play");
  366. playBtn.classList.remove("pause");
  367. });
  368. }
  369. // event on the play/pause button
  370. playBtn.addEventListener("click", function() {
  371. // click on the button to run the animation
  372. if (this.classList.contains("play")) {
  373. currentGroup.play(true);
  374. }
  375. // click on the button to pause the animation
  376. else {
  377. currentGroup.pause();
  378. }
  379. });
  380. // event on the slider
  381. slider.addEventListener("input", function() {
  382. var value = parseFloat(this.value);
  383. if (playBtn.classList.contains("play")) {
  384. currentGroup.play(true);
  385. currentGroup.goToFrame(value);
  386. currentGroup.pause();
  387. } else {
  388. currentGroup.goToFrame(value);
  389. }
  390. });
  391. var sliderPause = false;
  392. slider.addEventListener("mousedown", function() {
  393. if (playBtn.classList.contains("pause")) {
  394. sliderPause = true;
  395. playBtn.click();
  396. }
  397. });
  398. slider.addEventListener("mouseup", function() {
  399. if (sliderPause) {
  400. sliderPause = false;
  401. playBtn.click();
  402. }
  403. });