cameras-logarithmic-depth-buffer.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 - Cameras - Logoarithmic Depth Buffer</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  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="module">
  24. import * as THREE from '../../build/three.module.js';
  25. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  26. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.js';
  27. function main() {
  28. const canvas = document.querySelector('#c');
  29. const renderer = new THREE.WebGLRenderer({
  30. canvas,
  31. logarithmicDepthBuffer: true,
  32. });
  33. const fov = 45;
  34. const aspect = 2; // the canvas default
  35. const near = 0.00001;
  36. const far = 100;
  37. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  38. camera.position.set(10, 6, 10);
  39. class MinMaxGUIHelper {
  40. constructor(obj, minProp, maxProp, minDif) {
  41. this.obj = obj;
  42. this.minProp = minProp;
  43. this.maxProp = maxProp;
  44. this.minDif = minDif;
  45. }
  46. get min() {
  47. return this.obj[this.minProp];
  48. }
  49. set min(v) {
  50. this.obj[this.minProp] = v;
  51. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  52. }
  53. get max() {
  54. return this.obj[this.maxProp];
  55. }
  56. set max(v) {
  57. this.obj[this.maxProp] = v;
  58. this.min = this.min; // this will call the min setter
  59. }
  60. }
  61. function updateCamera() {
  62. camera.updateProjectionMatrix();
  63. }
  64. const gui = new GUI();
  65. gui.add(camera, 'fov', 1, 180).onChange(updateCamera);
  66. const minMaxGUIHelper = new MinMaxGUIHelper(camera, 'near', 'far', 0.1);
  67. gui.add(minMaxGUIHelper, 'min', 0.00001, 50, 0.00001).name('near').onChange(updateCamera);
  68. gui.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  69. const controls = new OrbitControls(camera, canvas);
  70. controls.target.set(0, 5, 0);
  71. controls.update();
  72. const scene = new THREE.Scene();
  73. scene.background = new THREE.Color('black');
  74. {
  75. const planeSize = 40;
  76. const loader = new THREE.TextureLoader();
  77. const texture = loader.load('resources/images/checker.png');
  78. texture.wrapS = THREE.RepeatWrapping;
  79. texture.wrapT = THREE.RepeatWrapping;
  80. texture.magFilter = THREE.NearestFilter;
  81. const repeats = planeSize / 2;
  82. texture.repeat.set(repeats, repeats);
  83. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  84. const planeMat = new THREE.MeshPhongMaterial({
  85. map: texture,
  86. side: THREE.DoubleSide,
  87. });
  88. const mesh = new THREE.Mesh(planeGeo, planeMat);
  89. mesh.rotation.x = Math.PI * -.5;
  90. scene.add(mesh);
  91. }
  92. {
  93. const sphereRadius = 3;
  94. const sphereWidthDivisions = 32;
  95. const sphereHeightDivisions = 16;
  96. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  97. const numSpheres = 20;
  98. for (let i = 0; i < numSpheres; ++i) {
  99. const sphereMat = new THREE.MeshPhongMaterial();
  100. sphereMat.color.setHSL(i * .73, 1, 0.5);
  101. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  102. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, i * sphereRadius * -2.2);
  103. scene.add(mesh);
  104. }
  105. }
  106. {
  107. const color = 0xFFFFFF;
  108. const intensity = 1;
  109. const light = new THREE.DirectionalLight(color, intensity);
  110. light.position.set(0, 10, 0);
  111. light.target.position.set(-5, 0, 0);
  112. scene.add(light);
  113. scene.add(light.target);
  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() {
  126. if (resizeRendererToDisplaySize(renderer)) {
  127. const canvas = renderer.domElement;
  128. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  129. camera.updateProjectionMatrix();
  130. }
  131. renderer.render(scene, camera);
  132. requestAnimationFrame(render);
  133. }
  134. requestAnimationFrame(render);
  135. }
  136. main();
  137. </script>
  138. </html>