fog-gui.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js",
  30. "three/addons/": "../../examples/jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import {GUI} from 'three/addons/libs/lil-gui.module.min.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  40. const gui = new GUI();
  41. const fov = 75;
  42. const aspect = 2; // the canvas default
  43. const near = 0.1;
  44. const far = 5;
  45. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  46. camera.position.z = 2;
  47. const scene = new THREE.Scene();
  48. // We use this class to pass to lil-gui
  49. // so when it manipulates near or far
  50. // near is never > far and far is never < near
  51. // Also when lil-gui maniplates color we'll
  52. // update both the fog and background colors.
  53. class FogGUIHelper {
  54. constructor(fog, backgroundColor) {
  55. this.fog = fog;
  56. this.backgroundColor = backgroundColor;
  57. }
  58. get near() {
  59. return this.fog.near;
  60. }
  61. set near(v) {
  62. this.fog.near = v;
  63. this.fog.far = Math.max(this.fog.far, v);
  64. }
  65. get far() {
  66. return this.fog.far;
  67. }
  68. set far(v) {
  69. this.fog.far = v;
  70. this.fog.near = Math.min(this.fog.near, v);
  71. }
  72. get color() {
  73. return `#${this.fog.color.getHexString()}`;
  74. }
  75. set color(hexString) {
  76. this.fog.color.set(hexString);
  77. this.backgroundColor.set(hexString);
  78. }
  79. }
  80. {
  81. const near = 1;
  82. const far = 2;
  83. const color = 'lightblue';
  84. scene.fog = new THREE.Fog(color, near, far);
  85. scene.background = new THREE.Color(color);
  86. const fogGUIHelper = new FogGUIHelper(scene.fog, scene.background);
  87. gui.add(fogGUIHelper, 'near', near, far).listen();
  88. gui.add(fogGUIHelper, 'far', near, far).listen();
  89. gui.addColor(fogGUIHelper, 'color');
  90. }
  91. {
  92. const color = 0xFFFFFF;
  93. const intensity = 1;
  94. const light = new THREE.DirectionalLight(color, intensity);
  95. light.position.set(-1, 2, 4);
  96. scene.add(light);
  97. }
  98. const boxWidth = 1;
  99. const boxHeight = 1;
  100. const boxDepth = 1;
  101. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  102. function makeInstance(geometry, color, x) {
  103. const material = new THREE.MeshPhongMaterial({color});
  104. const cube = new THREE.Mesh(geometry, material);
  105. scene.add(cube);
  106. cube.position.x = x;
  107. return cube;
  108. }
  109. const cubes = [
  110. makeInstance(geometry, 0x44aa88, 0),
  111. makeInstance(geometry, 0x8844aa, -2),
  112. makeInstance(geometry, 0xaa8844, 2),
  113. ];
  114. function resizeRendererToDisplaySize(renderer) {
  115. const canvas = renderer.domElement;
  116. const width = canvas.clientWidth;
  117. const height = canvas.clientHeight;
  118. const needResize = canvas.width !== width || canvas.height !== height;
  119. if (needResize) {
  120. renderer.setSize(width, height, false);
  121. }
  122. return needResize;
  123. }
  124. function render(time) {
  125. time *= 0.001;
  126. if (resizeRendererToDisplaySize(renderer)) {
  127. const canvas = renderer.domElement;
  128. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  129. camera.updateProjectionMatrix();
  130. }
  131. cubes.forEach((cube, ndx) => {
  132. const speed = 1 + ndx * .1;
  133. const rot = time * speed;
  134. cube.rotation.x = rot;
  135. cube.rotation.y = rot;
  136. });
  137. renderer.render(scene, camera);
  138. requestAnimationFrame(render);
  139. }
  140. requestAnimationFrame(render);
  141. }
  142. main();
  143. </script>
  144. </html>