1
0

cameras-logarithmic-depth-buffer.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. function main() {
  36. const canvas = document.querySelector( '#c' );
  37. const renderer = new THREE.WebGLRenderer( {
  38. canvas,
  39. logarithmicDepthBuffer: true,
  40. antialias: true
  41. } );
  42. const fov = 45;
  43. const aspect = 2; // the canvas default
  44. const near = 0.00001;
  45. const far = 100;
  46. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  47. camera.position.set( 10, 6, 10 );
  48. class MinMaxGUIHelper {
  49. constructor( obj, minProp, maxProp, minDif ) {
  50. this.obj = obj;
  51. this.minProp = minProp;
  52. this.maxProp = maxProp;
  53. this.minDif = minDif;
  54. }
  55. get min() {
  56. return this.obj[ this.minProp ];
  57. }
  58. set min( v ) {
  59. this.obj[ this.minProp ] = v;
  60. this.obj[ this.maxProp ] = Math.max( this.obj[ this.maxProp ], v + this.minDif );
  61. }
  62. get max() {
  63. return this.obj[ this.maxProp ];
  64. }
  65. set max( v ) {
  66. this.obj[ this.maxProp ] = v;
  67. this.min = this.min; // this will call the min setter
  68. }
  69. }
  70. function updateCamera() {
  71. camera.updateProjectionMatrix();
  72. }
  73. const gui = new GUI();
  74. gui.add( camera, 'fov', 1, 180 ).onChange( updateCamera );
  75. const minMaxGUIHelper = new MinMaxGUIHelper( camera, 'near', 'far', 0.1 );
  76. gui.add( minMaxGUIHelper, 'min', 0.00001, 50, 0.00001 ).name( 'near' ).onChange( updateCamera );
  77. gui.add( minMaxGUIHelper, 'max', 0.1, 50, 0.1 ).name( 'far' ).onChange( updateCamera );
  78. const controls = new OrbitControls( camera, canvas );
  79. controls.target.set( 0, 5, 0 );
  80. controls.update();
  81. const scene = new THREE.Scene();
  82. scene.background = new THREE.Color( 'black' );
  83. {
  84. const planeSize = 40;
  85. const loader = new THREE.TextureLoader();
  86. const texture = loader.load( 'resources/images/checker.png' );
  87. texture.wrapS = THREE.RepeatWrapping;
  88. texture.wrapT = THREE.RepeatWrapping;
  89. texture.magFilter = THREE.NearestFilter;
  90. texture.colorSpace = THREE.SRGBColorSpace;
  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 = 3;
  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>