cameras-logarithmic-depth-buffer.html 4.7 KB

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