index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /// <reference path="../../babylon.js" />
  2. document.addEventListener("DOMContentLoaded", startGame, false);
  3. function startGame() {
  4. if (BABYLON.Engine.isSupported()) {
  5. var canvas = document.getElementById("renderCanvas");
  6. var engine = new BABYLON.Engine(canvas, true);
  7. var divFps = document.getElementById("fps");
  8. var htmlInput = document.getElementById("files");
  9. var btnFullScreen = document.getElementById("btnFullscreen");
  10. var btnDownArrow = document.getElementById("btnDownArrow");
  11. var perffooter = document.getElementById("perf");
  12. var btnPerf = document.getElementById("btnPerf");
  13. var miscCounters = document.getElementById("miscCounters");
  14. var help01 = document.getElementById("help01");
  15. var help02 = document.getElementById("help02");
  16. var loadingText = document.getElementById("loadingText");
  17. var filesInput;
  18. var currentHelpCounter;
  19. var currentScene;
  20. var perffooterEnable = false;
  21. currentHelpCounter = localStorage.getItem("helpcounter");
  22. if (!currentHelpCounter) currentHelpCounter = 0;
  23. // Resize
  24. window.addEventListener("resize", function () {
  25. engine.resize();
  26. });
  27. var sceneLoaded = function (sceneFile, babylonScene) {
  28. currentScene = babylonScene;
  29. document.title = "BabylonJS - " + sceneFile.name;
  30. // Fix for IE, otherwise it will change the default filter for files selection after first use
  31. htmlInput.value = "";
  32. document.getElementById("logo").className = "hidden";
  33. loadingText.className = "loadingText";
  34. };
  35. var progressCallback = function (evt) {
  36. if (evt.lengthComputable) {
  37. loadingText.innerHTML = "Loading, please wait..." + (evt.loaded * 100 / evt.total).toFixed() + "%";
  38. } else {
  39. dlCount = evt.loaded / (1024 * 1024);
  40. loadingText.innerHTML = "Loading, please wait..." + Math.floor(dlCount * 100.0) / 100.0 + " MB already loaded.";
  41. }
  42. };
  43. var textureLoadingCallback = function (remainingTextures) {
  44. loadingText.innerHTML = "Streaming items..." + (remainingTextures ? (remainingTextures + " remaining") : "");
  45. };
  46. var startingProcessingFilesCallback = function () {
  47. loadingText.className = "";
  48. loadingText.innerHTML = "Loading, please wait...";
  49. };
  50. var additionnalRenderLoopLogic = function () {
  51. divFps.innerHTML = BABYLON.Tools.GetFps().toFixed() + " fps";
  52. if (currentScene) {
  53. miscCounters.innerHTML = "Total vertices: " + currentScene.getTotalVertices() + " <br />"
  54. + "Active vertices: " + currentScene.getActiveVertices() + " <br />"
  55. + "Active particles: " + currentScene.getActiveParticles() + " <br />"
  56. + "Frame duration: " + currentScene.getLastFrameDuration() + " ms" + " <br />"
  57. + "Evaluate Active Meshes duration: " + currentScene.getEvaluateActiveMeshesDuration() + " ms" + " <br />"
  58. + "Render Targets duration: " + currentScene.getRenderTargetsDuration() + " ms" + " <br />"
  59. + "Particles duration: " + currentScene.getParticlesDuration() + " ms" + " <br />"
  60. + "Sprites duration: " + currentScene.getSpritesDuration() + " ms" + " <br />"
  61. + "Render duration: " + currentScene.getRenderDuration() + " ms";
  62. }
  63. };
  64. filesInput = new BABYLON.FilesInput(engine, null, canvas, sceneLoaded, progressCallback, additionnalRenderLoopLogic, textureLoadingCallback, startingProcessingFilesCallback);
  65. filesInput.monitorElementForDragNDrop(canvas);
  66. htmlInput.addEventListener('change', filesInput.loadFiles, false);
  67. btnFullScreen.addEventListener('click', function () {
  68. engine.switchFullscreen(true);
  69. }, false);
  70. btnPerf.addEventListener('click', function () {
  71. perffooter.className = "perffooter shown";
  72. }, false);
  73. btnDownArrow.addEventListener('click', function () {
  74. perffooter.className = "perffooter";
  75. }, false);
  76. // The help tips will be displayed only 5 times
  77. if (currentHelpCounter < 5) {
  78. help01.className = "help shown";
  79. setTimeout(function () {
  80. help01.className = "help";
  81. help02.className = "help2 shown";
  82. setTimeout(function () {
  83. help02.className = "help2";
  84. localStorage.setItem("helpcounter", currentHelpCounter + 1);
  85. }, 5000);
  86. }, 5000);
  87. }
  88. }
  89. }