cameras-z-fighting.html 4.4 KB

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