2
0

cameras-logarithmic-depth-buffer.html 4.6 KB

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