threejs-cameras-logarithmic-depth-buffer.html 4.4 KB

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