cameras-z-fighting.html 4.3 KB

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