fog-gui.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Fog w/GUI</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="module">
  24. import * as THREE from '../../build/three.module.js';
  25. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.js';
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas});
  29. const gui = new GUI();
  30. const fov = 75;
  31. const aspect = 2; // the canvas default
  32. const near = 0.1;
  33. const far = 5;
  34. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  35. camera.position.z = 2;
  36. const scene = new THREE.Scene();
  37. // We use this class to pass to lil-gui
  38. // so when it manipulates near or far
  39. // near is never > far and far is never < near
  40. // Also when lil-gui maniplates color we'll
  41. // update both the fog and background colors.
  42. class FogGUIHelper {
  43. constructor(fog, backgroundColor) {
  44. this.fog = fog;
  45. this.backgroundColor = backgroundColor;
  46. }
  47. get near() {
  48. return this.fog.near;
  49. }
  50. set near(v) {
  51. this.fog.near = v;
  52. this.fog.far = Math.max(this.fog.far, v);
  53. }
  54. get far() {
  55. return this.fog.far;
  56. }
  57. set far(v) {
  58. this.fog.far = v;
  59. this.fog.near = Math.min(this.fog.near, v);
  60. }
  61. get color() {
  62. return `#${this.fog.color.getHexString()}`;
  63. }
  64. set color(hexString) {
  65. this.fog.color.set(hexString);
  66. this.backgroundColor.set(hexString);
  67. }
  68. }
  69. {
  70. const near = 1;
  71. const far = 2;
  72. const color = 'lightblue';
  73. scene.fog = new THREE.Fog(color, near, far);
  74. scene.background = new THREE.Color(color);
  75. const fogGUIHelper = new FogGUIHelper(scene.fog, scene.background);
  76. gui.add(fogGUIHelper, 'near', near, far).listen();
  77. gui.add(fogGUIHelper, 'far', near, far).listen();
  78. gui.addColor(fogGUIHelper, 'color');
  79. }
  80. {
  81. const color = 0xFFFFFF;
  82. const intensity = 1;
  83. const light = new THREE.DirectionalLight(color, intensity);
  84. light.position.set(-1, 2, 4);
  85. scene.add(light);
  86. }
  87. const boxWidth = 1;
  88. const boxHeight = 1;
  89. const boxDepth = 1;
  90. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  91. function makeInstance(geometry, color, x) {
  92. const material = new THREE.MeshPhongMaterial({color});
  93. const cube = new THREE.Mesh(geometry, material);
  94. scene.add(cube);
  95. cube.position.x = x;
  96. return cube;
  97. }
  98. const cubes = [
  99. makeInstance(geometry, 0x44aa88, 0),
  100. makeInstance(geometry, 0x8844aa, -2),
  101. makeInstance(geometry, 0xaa8844, 2),
  102. ];
  103. function resizeRendererToDisplaySize(renderer) {
  104. const canvas = renderer.domElement;
  105. const width = canvas.clientWidth;
  106. const height = canvas.clientHeight;
  107. const needResize = canvas.width !== width || canvas.height !== height;
  108. if (needResize) {
  109. renderer.setSize(width, height, false);
  110. }
  111. return needResize;
  112. }
  113. function render(time) {
  114. time *= 0.001;
  115. if (resizeRendererToDisplaySize(renderer)) {
  116. const canvas = renderer.domElement;
  117. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  118. camera.updateProjectionMatrix();
  119. }
  120. cubes.forEach((cube, ndx) => {
  121. const speed = 1 + ndx * .1;
  122. const rot = time * speed;
  123. cube.rotation.x = rot;
  124. cube.rotation.y = rot;
  125. });
  126. renderer.render(scene, camera);
  127. requestAnimationFrame(render);
  128. }
  129. requestAnimationFrame(render);
  130. }
  131. main();
  132. </script>
  133. </html>