webgl_reversed_depth_buffer.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #info_note {
  59. position: absolute;
  60. top: 24px;
  61. width: 100%;
  62. padding: 10px;
  63. box-sizing: border-box;
  64. text-align: center;
  65. color: rgba(255, 255, 255, 0.8);
  66. z-index: 2;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <div id="container">
  72. <div id="container_normal"><h2 class="container_label">normal z-buffer</h2></div>
  73. <div id="container_logarithmic"><h2 class="container_label">logarithmic z-buffer</h2></div>
  74. <div id="container_reverse"><h2 class="container_label">reverse z-buffer</h2></div>
  75. </div>
  76. <div id="info">
  77. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - reverse depth buffer<br/>
  78. </div>
  79. <div id="info_note">
  80. Note: For best results, a floating-point depth buffer should be used with post-processing. See <a href="https://developer.nvidia.com/blog/visualizing-depth-precision" target="_blank" rel="noopener">Visualizing Depth Precision</a>.
  81. </div>
  82. <script type="importmap">
  83. {
  84. "imports": {
  85. "three": "../build/three.module.js",
  86. "three/addons/": "./jsm/"
  87. }
  88. }
  89. </script>
  90. <script type="module">
  91. // https://webgpu.github.io/webgpu-samples/?sample=reversedZ
  92. import * as THREE from 'three';
  93. import Stats from 'three/addons/libs/stats.module.js';
  94. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  95. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  96. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  97. let stats, camera, reversedCamera, scene;
  98. let normalRenderer, logarithmicRenderer, reverseRenderer;
  99. let normalComposer, logarithmicComposer, reverseComposer;
  100. const meshes = [];
  101. const renderTarget = new THREE.WebGLRenderTarget();
  102. renderTarget.depthTexture = new THREE.DepthTexture();
  103. renderTarget.depthTexture.type = THREE.FloatType;
  104. init();
  105. animate();
  106. function init() {
  107. const container = document.getElementById( 'container' );
  108. stats = new Stats();
  109. container.appendChild( stats.dom );
  110. camera = new THREE.PerspectiveCamera( 72, 0.33 * window.innerWidth / window.innerHeight, 5, 9999 );
  111. camera.position.z = 12;
  112. reversedCamera = camera.clone();
  113. scene = new THREE.Scene();
  114. const xCount = 1;
  115. const yCount = 5;
  116. const numInstances = xCount * yCount;
  117. const d = 0.0001; // half distance between two planes
  118. const o = 0.5; // half x offset to shift planes so they are only partially overlapping
  119. const positions = new Float32Array( [
  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. - 1 - o, 1, d,
  126. - 1 + o, - 1, - d,
  127. 1 + o, - 1, - d,
  128. - 1 + o, 1, - d,
  129. 1 + o, - 1, - d,
  130. 1 + o, 1, - d,
  131. - 1 + o, 1, - d,
  132. ] );
  133. const colors = new Float32Array( [
  134. 1, 0, 0,
  135. 1, 0, 0,
  136. 1, 0, 0,
  137. 1, 0, 0,
  138. 1, 0, 0,
  139. 1, 0, 0,
  140. 0, 1, 0,
  141. 0, 1, 0,
  142. 0, 1, 0,
  143. 0, 1, 0,
  144. 0, 1, 0,
  145. 0, 1, 0,
  146. ] );
  147. const geometry = new THREE.BufferGeometry();
  148. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  149. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  150. const material = new THREE.MeshBasicMaterial( { vertexColors: true } );
  151. for ( let i = 0; i < numInstances; i ++ ) {
  152. const mesh = new THREE.Mesh( geometry, material );
  153. meshes.push( mesh );
  154. scene.add( mesh );
  155. }
  156. let i = 0;
  157. for ( let x = 0; x < xCount; x ++ ) {
  158. for ( let y = 0; y < yCount; y ++ ) {
  159. const z = - 800 * i;
  160. const s = 1 + 50 * i;
  161. const mesh = meshes[ i ];
  162. mesh.position.set(
  163. x - xCount / 2 + 0.5,
  164. ( 4.0 - 0.2 * z ) * ( y - yCount / 2 + 1.0 ),
  165. z
  166. );
  167. mesh.scale.setScalar( s );
  168. i ++;
  169. }
  170. }
  171. const normalContainer = document.getElementById( 'container_normal' );
  172. normalRenderer = new THREE.WebGLRenderer();
  173. normalRenderer.setPixelRatio( window.devicePixelRatio );
  174. normalRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  175. normalRenderer.domElement.style.position = 'relative';
  176. normalContainer.appendChild( normalRenderer.domElement );
  177. normalComposer = new EffectComposer( normalRenderer, renderTarget );
  178. normalComposer.addPass( new RenderPass( scene, camera ) );
  179. normalComposer.addPass( new OutputPass() );
  180. const logarithmicContainer = document.getElementById( 'container_logarithmic' );
  181. logarithmicRenderer = new THREE.WebGLRenderer( { logarithmicDepthBuffer: true } );
  182. logarithmicRenderer.setPixelRatio( window.devicePixelRatio );
  183. logarithmicRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  184. logarithmicRenderer.domElement.style.position = 'relative';
  185. logarithmicContainer.appendChild( logarithmicRenderer.domElement );
  186. logarithmicComposer = new EffectComposer( logarithmicRenderer, renderTarget );
  187. logarithmicComposer.addPass( new RenderPass( scene, camera ) );
  188. logarithmicComposer.addPass( new OutputPass() );
  189. const reverseContainer = document.getElementById( 'container_reverse' );
  190. reverseRenderer = new THREE.WebGLRenderer( { reversedDepthBuffer: true } );
  191. reverseRenderer.setPixelRatio( window.devicePixelRatio );
  192. reverseRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  193. reverseRenderer.domElement.style.position = 'relative';
  194. reverseContainer.appendChild( reverseRenderer.domElement );
  195. reverseComposer = new EffectComposer( reverseRenderer, renderTarget );
  196. reverseComposer.addPass( new RenderPass( scene, reversedCamera ) );
  197. reverseComposer.addPass( new OutputPass() );
  198. window.addEventListener( 'resize', onWindowResize );
  199. onWindowResize();
  200. }
  201. function animate() {
  202. requestAnimationFrame( animate );
  203. const now = performance.now() / 1000;
  204. for ( let i = 0; i < meshes.length; i ++ ) {
  205. const angle = THREE.MathUtils.degToRad( 30 );
  206. const axis = new THREE.Vector3( Math.sin( now ), Math.cos( now ), 0 );
  207. meshes[ i ].quaternion.setFromAxisAngle( axis, angle );
  208. }
  209. render();
  210. }
  211. function render() {
  212. normalComposer.render();
  213. logarithmicComposer.render();
  214. reverseComposer.render();
  215. stats.update();
  216. }
  217. function onWindowResize() {
  218. const width = 0.33 * window.innerWidth;
  219. const height = window.innerHeight;
  220. const dpr = window.devicePixelRatio;
  221. normalComposer.setSize( width * dpr, height * dpr );
  222. logarithmicComposer.setSize( width * dpr, height * dpr );
  223. reverseComposer.setSize( width * dpr, height * dpr );
  224. normalRenderer.setSize( width, height );
  225. logarithmicRenderer.setSize( width, height );
  226. reverseRenderer.setSize( width, height );
  227. camera.aspect = 0.33 * window.innerWidth / window.innerHeight;
  228. camera.updateProjectionMatrix();
  229. reversedCamera.aspect = 0.33 * window.innerWidth / window.innerHeight;
  230. reversedCamera.updateProjectionMatrix();
  231. }
  232. </script>
  233. </body>
  234. </html>
粤ICP备19079148号