index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var onload = function () {
  2. var canvas = document.getElementById("renderCanvas");
  3. var divFps = document.getElementById("fps");
  4. var mode = "CAMERA";
  5. if (!BABYLON.Engine.isSupported()) {
  6. document.getElementById("notSupported").className = "";
  7. return;
  8. }
  9. // Babylon
  10. BABYLON.Engine.ShadersRepository = "/Babylon/Shaders/";
  11. var engine = new BABYLON.Engine(canvas, true);
  12. var scene = new BABYLON.Scene(engine);
  13. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
  14. var sun = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 100, 2), scene);
  15. camera.setPosition(new BABYLON.Vector3(20, 40, 20));
  16. camera.attachControl(canvas);
  17. // Skybox
  18. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  19. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  20. skyboxMaterial.backFaceCulling = false;
  21. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("Assets/skybox/skybox", scene);
  22. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  23. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  24. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  25. skybox.material = skyboxMaterial;
  26. // Grounds
  27. var ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "Assets/heightMap.png", 100, 100, 100, 0, 12, scene, true);
  28. var groundMaterial = new WORLDMONGER.GroundMaterial("ground", scene, sun);
  29. ground.material = groundMaterial;
  30. ground.position.y = -2.0;
  31. var extraGround = BABYLON.Mesh.CreateGround("extraGround", 1000, 1000, 1, scene, false);
  32. var extraGroundMaterial = new BABYLON.StandardMaterial("extraGround", scene);
  33. extraGroundMaterial.diffuseTexture = new BABYLON.Texture("Shaders/Ground/sand.jpg", scene);
  34. extraGroundMaterial.diffuseTexture.uScale = 60;
  35. extraGroundMaterial.diffuseTexture.vScale = 60;
  36. extraGround.position.y = -2.05;
  37. extraGround.material = extraGroundMaterial;
  38. // Water
  39. var water = BABYLON.Mesh.CreateGround("water", 1000, 1000, 1, scene, false);
  40. var waterMaterial = new WORLDMONGER.WaterMaterial("water", scene, sun);
  41. waterMaterial.refractionTexture.renderList.push(ground);
  42. waterMaterial.refractionTexture.renderList.push(extraGround);
  43. waterMaterial.reflectionTexture.renderList.push(ground);
  44. waterMaterial.reflectionTexture.renderList.push(skybox);
  45. water.isPickable = false;
  46. water.material = waterMaterial;
  47. // Elevation
  48. var elevationControl = new WORLDMONGER.ElevationControl(ground);
  49. // Bloom
  50. var blurWidth = 2.0;
  51. var postProcess0 = new BABYLON.PassPostProcess("Scene copy", 1.0, camera);
  52. var postProcess1 = new BABYLON.PostProcess("Down sample", "./postprocesses/downsample", ["screenSize", "highlightThreshold"], null, 0.5, camera, BABYLON.Texture.DEFAULT_SAMPLINGMODE);
  53. postProcess1.onApply = function (effect) {
  54. effect.setFloat2("screenSize", postProcess1.width, postProcess1.height);
  55. effect.setFloat("highlightThreshold", 0.85);
  56. };
  57. var postProcess2 = new BABYLON.BlurPostProcess("Horizontal blur", new BABYLON.Vector2(1.0, 0), blurWidth, 0.5, camera, BABYLON.Texture.DEFAULT_SAMPLINGMODE);
  58. var postProcess3 = new BABYLON.BlurPostProcess("Vertical blur", new BABYLON.Vector2(0, 1.0), blurWidth, 0.5, camera, BABYLON.Texture.DEFAULT_SAMPLINGMODE);
  59. var postProcess4 = new BABYLON.PostProcess("Final compose", "./postprocesses/compose", ["sceneIntensity", "glowIntensity", "highlightIntensity"], ["sceneSampler"], 1, camera);
  60. postProcess4.onApply = function (effect) {
  61. effect.setTextureFromPostProcess("sceneSampler", postProcess0);
  62. effect.setFloat("sceneIntensity", 0.8);
  63. effect.setFloat("glowIntensity", 0.6);
  64. effect.setFloat("highlightIntensity", 1.5);
  65. };
  66. // Render loop
  67. var renderFunction = function () {
  68. if (ground.isReady && ground.subMeshes.length == 1) {
  69. ground.subdivide(20); // Subdivide to optimize picking
  70. }
  71. // Camera
  72. if (camera.beta < 0.1)
  73. camera.beta = 0.1;
  74. else if (camera.beta > (Math.PI / 2) * 0.92)
  75. camera.beta = (Math.PI / 2) * 0.92;
  76. if (camera.radius > 70)
  77. camera.radius = 70;
  78. if (camera.radius < 5)
  79. camera.radius = 5;
  80. // Fps
  81. divFps.innerHTML = BABYLON.Tools.GetFps().toFixed() + " fps";
  82. // Render scene
  83. scene.render();
  84. // Animations
  85. skybox.rotation.y += 0.0001 * scene.getAnimationRatio();
  86. };
  87. // Launch render loop
  88. scene.executeWhenReady(function() {
  89. engine.runRenderLoop(renderFunction);
  90. });
  91. // Resize
  92. window.addEventListener("resize", function () {
  93. engine.resize();
  94. });
  95. // UI
  96. var cameraButton = document.getElementById("cameraButton");
  97. var elevationButton = document.getElementById("elevationButton");
  98. var digButton = document.getElementById("digButton");
  99. var help01 = document.getElementById("help01");
  100. var help02 = document.getElementById("help02");
  101. window.oncontextmenu = function () {
  102. return false;
  103. };
  104. cameraButton.addEventListener("pointerdown", function () {
  105. if (mode == "CAMERA")
  106. return;
  107. camera.attachControl(canvas);
  108. elevationControl.detachControl(canvas);
  109. mode = "CAMERA";
  110. cameraButton.className = "controlButton selected";
  111. digButton.className = "controlButton";
  112. elevationButton.className = "controlButton";
  113. });
  114. elevationButton.addEventListener("pointerdown", function () {
  115. help01.className = "help";
  116. help02.className = "help";
  117. if (mode == "ELEVATION")
  118. return;
  119. if (mode == "CAMERA") {
  120. camera.detachControl(canvas);
  121. elevationControl.attachControl(canvas);
  122. }
  123. mode = "ELEVATION";
  124. elevationControl.direction = 1;
  125. elevationButton.className = "controlButton selected";
  126. digButton.className = "controlButton";
  127. cameraButton.className = "controlButton";
  128. });
  129. digButton.addEventListener("pointerdown", function () {
  130. help01.className = "help";
  131. help02.className = "help";
  132. if (mode == "DIG")
  133. return;
  134. if (mode == "CAMERA") {
  135. camera.detachControl(canvas);
  136. elevationControl.attachControl(canvas);
  137. }
  138. mode = "DIG";
  139. elevationControl.direction = -1;
  140. digButton.className = "controlButton selected";
  141. elevationButton.className = "controlButton";
  142. cameraButton.className = "controlButton";
  143. });
  144. // Sliders
  145. $("#slider-vertical").slider({
  146. orientation: "vertical",
  147. range: "min",
  148. min: 2,
  149. max: 15,
  150. value: 5,
  151. slide: function (event, ui) {
  152. elevationControl.radius = ui.value;
  153. }
  154. });
  155. $("#slider-range").slider({
  156. orientation: "vertical",
  157. range: true,
  158. min: 0,
  159. max: 12,
  160. values: [0, 11],
  161. slide: function (event, ui) {
  162. elevationControl.heightMin = ui.values[0];
  163. elevationControl.heightMax = ui.values[1];
  164. }
  165. });
  166. $("#qualitySlider").slider({
  167. orientation: "vertical",
  168. range: "min",
  169. min: 0,
  170. max: 3,
  171. value: 3,
  172. slide: function (event, ui) {
  173. switch (ui.value) {
  174. case 3:
  175. waterMaterial.refractionTexture.resize(512, true);
  176. waterMaterial.reflectionTexture.resize(512, true);
  177. scene.getEngine().setHardwareScalingLevel(1);
  178. scene.particlesEnabled = true;
  179. scene.postProcessesEnabled = true;
  180. break;
  181. case 2:
  182. waterMaterial.refractionTexture.resize(256, true);
  183. waterMaterial.reflectionTexture.resize(256, true);
  184. scene.getEngine().setHardwareScalingLevel(1);
  185. scene.particlesEnabled = false;
  186. scene.postProcessesEnabled = false;
  187. break;
  188. case 1:
  189. waterMaterial.refractionTexture.resize(256, true);
  190. waterMaterial.reflectionTexture.resize(256, true);
  191. scene.getEngine().setHardwareScalingLevel(2);
  192. scene.particlesEnabled = false;
  193. scene.postProcessesEnabled = false;
  194. break;
  195. case 0:
  196. waterMaterial.refractionTexture.resize(256, true);
  197. waterMaterial.reflectionTexture.resize(256, true);
  198. scene.getEngine().setHardwareScalingLevel(3);
  199. scene.particlesEnabled = false;
  200. scene.postProcessesEnabled = false;
  201. break;
  202. }
  203. }
  204. });
  205. help01.className = "help shown";
  206. setTimeout(function() {
  207. help01.className = "help";
  208. help02.className = "help shown";
  209. setTimeout(function() {
  210. help02.className = "help";
  211. }, 5000);
  212. }, 5000);
  213. };