fog-gui.html 4.0 KB

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