threejs-fog-gui.html 3.7 KB

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