fog-gui.html 3.8 KB

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