threejs-cameras-z-fighting.html 4.3 KB

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