threejs-fog.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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</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="resources/threejs/r102/three.min.js"></script>
  23. <script>
  24. 'use strict';
  25. /* global THREE */
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  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. {
  37. const near = 1;
  38. const far = 2;
  39. const color = 'lightblue';
  40. scene.fog = new THREE.Fog(color, near, far);
  41. scene.background = new THREE.Color(color);
  42. }
  43. {
  44. const color = 0xFFFFFF;
  45. const intensity = 1;
  46. const light = new THREE.DirectionalLight(color, intensity);
  47. light.position.set(-1, 2, 4);
  48. scene.add(light);
  49. }
  50. const boxWidth = 1;
  51. const boxHeight = 1;
  52. const boxDepth = 1;
  53. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  54. function makeInstance(geometry, color, x) {
  55. const material = new THREE.MeshPhongMaterial({color});
  56. const cube = new THREE.Mesh(geometry, material);
  57. scene.add(cube);
  58. cube.position.x = x;
  59. return cube;
  60. }
  61. const cubes = [
  62. makeInstance(geometry, 0x44aa88, 0),
  63. makeInstance(geometry, 0x8844aa, -2),
  64. makeInstance(geometry, 0xaa8844, 2),
  65. ];
  66. function resizeRendererToDisplaySize(renderer) {
  67. const canvas = renderer.domElement;
  68. const width = canvas.clientWidth;
  69. const height = canvas.clientHeight;
  70. const needResize = canvas.width !== width || canvas.height !== height;
  71. if (needResize) {
  72. renderer.setSize(width, height, false);
  73. }
  74. return needResize;
  75. }
  76. function render(time) {
  77. time *= 0.001;
  78. if (resizeRendererToDisplaySize(renderer)) {
  79. const canvas = renderer.domElement;
  80. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  81. camera.updateProjectionMatrix();
  82. }
  83. cubes.forEach((cube, ndx) => {
  84. const speed = 1 + ndx * .1;
  85. const rot = time * speed;
  86. cube.rotation.x = rot;
  87. cube.rotation.y = rot;
  88. });
  89. renderer.render(scene, camera);
  90. requestAnimationFrame(render);
  91. }
  92. requestAnimationFrame(render);
  93. }
  94. main();
  95. </script>
  96. </html>