threejs-fog-gui.html 3.8 KB

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