cameras-logarithmic-depth-buffer.html 4.6 KB

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