1
0

webgl_reverse_depth_buffer.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - reverse depth buffer</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. margin: 0;
  11. background-color: #000;
  12. color: #fff;
  13. font-family: Monospace;
  14. font-size: 13px;
  15. line-height: 24px;
  16. overscroll-behavior: none;
  17. }
  18. a {
  19. color: #ff0;
  20. text-decoration: none;
  21. }
  22. a:hover {
  23. text-decoration: underline;
  24. }
  25. #info {
  26. position: absolute;
  27. top: 0px;
  28. width: 100%;
  29. padding: 10px;
  30. box-sizing: border-box;
  31. text-align: center;
  32. -moz-user-select: none;
  33. -webkit-user-select: none;
  34. -ms-user-select: none;
  35. user-select: none;
  36. pointer-events: none;
  37. z-index: 1; /* TODO Solve this in HTML */
  38. }
  39. #container {
  40. display: flex;
  41. }
  42. #container_normal,
  43. #container_logarithmic,
  44. #container_reverse {
  45. width: 33%;
  46. display: inline-block;
  47. position: relative;
  48. }
  49. .container_label {
  50. position: absolute;
  51. bottom: 1em;
  52. width: 100%;
  53. color: white;
  54. z-index: 10;
  55. display: block;
  56. text-align: center;
  57. }
  58. #depth-warning {
  59. position: absolute;
  60. top: 40px;
  61. width: 100%;
  62. padding: 10px;
  63. box-sizing: border-box;
  64. text-align: center;
  65. color: #ff5555;
  66. font-weight: bold;
  67. z-index: 2;
  68. display: none;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <div id="container">
  74. <div id="container_normal"><h2 class="container_label">normal z-buffer</h2></div>
  75. <div id="container_logarithmic"><h2 class="container_label">logarithmic z-buffer</h2></div>
  76. <div id="container_reverse"><h2 class="container_label">reverse z-buffer</h2></div>
  77. </div>
  78. <div id="info">
  79. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - reverse depth buffer<br/>
  80. </div>
  81. <div id="depth-warning">
  82. Warning: Your browser's WebGL provides less than 24-bit depth buffer precision. This example may not display correctly.
  83. </div>
  84. <script type="importmap">
  85. {
  86. "imports": {
  87. "three": "../build/three.module.js",
  88. "three/addons/": "./jsm/"
  89. }
  90. }
  91. </script>
  92. <script type="module">
  93. // https://webgpu.github.io/webgpu-samples/?sample=reversedZ
  94. import * as THREE from 'three';
  95. import Stats from 'three/addons/libs/stats.module.js';
  96. let stats, camera, scene, normalRenderer, logarithmicRenderer, reverseRenderer;
  97. const meshes = [];
  98. init();
  99. animate();
  100. function init() {
  101. const container = document.getElementById( 'container' );
  102. stats = new Stats();
  103. container.appendChild( stats.dom );
  104. camera = new THREE.PerspectiveCamera( 72, 0.33 * window.innerWidth / window.innerHeight, 5, 9999 );
  105. camera.position.z = 12;
  106. scene = new THREE.Scene();
  107. const xCount = 1;
  108. const yCount = 5;
  109. const numInstances = xCount * yCount;
  110. const d = 0.0001; // half distance between two planes
  111. const o = 0.5; // half x offset to shift planes so they are only partially overlapping
  112. const positions = new Float32Array( [
  113. - 1 - o, - 1, d,
  114. 1 - o, - 1, d,
  115. - 1 - o, 1, d,
  116. 1 - o, - 1, d,
  117. 1 - o, 1, d,
  118. - 1 - o, 1, d,
  119. - 1 + o, - 1, - d,
  120. 1 + o, - 1, - d,
  121. - 1 + o, 1, - d,
  122. 1 + o, - 1, - d,
  123. 1 + o, 1, - d,
  124. - 1 + o, 1, - d,
  125. ] );
  126. const colors = new Float32Array( [
  127. 1, 0, 0,
  128. 1, 0, 0,
  129. 1, 0, 0,
  130. 1, 0, 0,
  131. 1, 0, 0,
  132. 1, 0, 0,
  133. 0, 1, 0,
  134. 0, 1, 0,
  135. 0, 1, 0,
  136. 0, 1, 0,
  137. 0, 1, 0,
  138. 0, 1, 0,
  139. ] );
  140. const geometry = new THREE.BufferGeometry();
  141. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  142. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  143. const material = new THREE.MeshBasicMaterial( { vertexColors: true } );
  144. for ( let i = 0; i < numInstances; i ++ ) {
  145. const mesh = new THREE.Mesh( geometry, material );
  146. meshes.push( mesh );
  147. scene.add( mesh );
  148. }
  149. let i = 0;
  150. for ( let x = 0; x < xCount; x ++ ) {
  151. for ( let y = 0; y < yCount; y ++ ) {
  152. const z = - 800 * i;
  153. const s = 1 + 50 * i;
  154. const mesh = meshes[ i ];
  155. mesh.position.set(
  156. x - xCount / 2 + 0.5,
  157. ( 4.0 - 0.2 * z ) * ( y - yCount / 2 + 1.0 ),
  158. z
  159. );
  160. mesh.scale.setScalar( s );
  161. i ++;
  162. }
  163. }
  164. const normalContainer = document.getElementById( 'container_normal' );
  165. normalRenderer = new THREE.WebGLRenderer();
  166. normalRenderer.setPixelRatio( window.devicePixelRatio );
  167. normalRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  168. normalRenderer.domElement.style.position = 'relative';
  169. normalContainer.appendChild( normalRenderer.domElement );
  170. const logarithmicContainer = document.getElementById( 'container_logarithmic' );
  171. logarithmicRenderer = new THREE.WebGLRenderer( { logarithmicDepthBuffer: true } );
  172. logarithmicRenderer.setPixelRatio( window.devicePixelRatio );
  173. logarithmicRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  174. logarithmicRenderer.domElement.style.position = 'relative';
  175. logarithmicContainer.appendChild( logarithmicRenderer.domElement );
  176. const reverseContainer = document.getElementById( 'container_reverse' );
  177. reverseRenderer = new THREE.WebGLRenderer( { reverseDepthBuffer: true } );
  178. reverseRenderer.setPixelRatio( window.devicePixelRatio );
  179. reverseRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  180. reverseRenderer.domElement.style.position = 'relative';
  181. reverseContainer.appendChild( reverseRenderer.domElement );
  182. // Check depth buffer precision
  183. const gl = normalRenderer.getContext();
  184. const depthBits = gl.getParameter( gl.DEPTH_BITS );
  185. if ( depthBits < 24 ) {
  186. document.getElementById( 'depth-warning' ).style.display = 'block';
  187. }
  188. window.addEventListener( 'resize', onWindowResize );
  189. }
  190. function animate() {
  191. requestAnimationFrame( animate );
  192. const now = performance.now() / 1000;
  193. for ( let i = 0; i < meshes.length; i ++ ) {
  194. const angle = THREE.MathUtils.degToRad( 30 );
  195. const axis = new THREE.Vector3( Math.sin( now ), Math.cos( now ), 0 );
  196. meshes[ i ].quaternion.setFromAxisAngle( axis, angle );
  197. }
  198. render();
  199. }
  200. function render() {
  201. normalRenderer.render( scene, camera );
  202. logarithmicRenderer.render( scene, camera );
  203. reverseRenderer.render( scene, camera );
  204. stats.update();
  205. }
  206. function onWindowResize() {
  207. normalRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  208. logarithmicRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  209. reverseRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  210. camera.aspect = 0.33 * window.innerWidth / window.innerHeight;
  211. camera.updateProjectionMatrix();
  212. }
  213. </script>
  214. </body>
  215. </html>
粤ICP备19079148号