fog-gui.html 4.0 KB

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