1
0

webgpu_reversed_depth_buffer.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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="example.css">
  8. <style>
  9. #container {
  10. display: flex;
  11. }
  12. #container_normal,
  13. #container_logarithmic,
  14. #container_reverse {
  15. width: 33%;
  16. display: inline-block;
  17. position: relative;
  18. }
  19. .container_label {
  20. position: absolute;
  21. bottom: 1em;
  22. width: 100%;
  23. color: white;
  24. z-index: 10;
  25. display: block;
  26. text-align: center;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="container">
  32. <div id="container_normal"><h2 class="container_label">normal z-buffer</h2></div>
  33. <div id="container_logarithmic"><h2 class="container_label">logarithmic z-buffer</h2></div>
  34. <div id="container_reverse"><h2 class="container_label">reverse z-buffer</h2></div>
  35. </div>
  36. <div id="info">
  37. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  38. <div class="title-wrapper">
  39. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Reverse Depth Buffer</span>
  40. </div>
  41. <small>
  42. 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>.
  43. </small>
  44. </div>
  45. <script type="importmap">
  46. {
  47. "imports": {
  48. "three": "../build/three.webgpu.js",
  49. "three/webgpu": "../build/three.webgpu.js",
  50. "three/tsl": "../build/three.tsl.js",
  51. "three/addons/": "./jsm/"
  52. }
  53. }
  54. </script>
  55. <script type="module">
  56. import * as THREE from 'three/webgpu';
  57. import { pass } from 'three/tsl';
  58. let camera, reversedCamera, scene;
  59. let normalRenderer, logarithmicRenderer, reverseRenderer;
  60. let normalRenderPipeline, logarithmicRenderPipeline, reverseRenderPipeline;
  61. const meshes = [];
  62. init().then( animate );
  63. async function init() {
  64. camera = new THREE.PerspectiveCamera( 72, 0.33 * window.innerWidth / window.innerHeight, 5, 9999 );
  65. camera.position.z = 12;
  66. reversedCamera = camera.clone();
  67. scene = new THREE.Scene();
  68. const xCount = 1;
  69. const yCount = 5;
  70. const numInstances = xCount * yCount;
  71. const d = 0.0001; // half distance between two planes
  72. const o = 0.5; // half x offset to shift planes so they are only partially overlapping
  73. const positions = new Float32Array( [
  74. - 1 - o, - 1, d,
  75. 1 - o, - 1, d,
  76. - 1 - o, 1, d,
  77. 1 - o, - 1, d,
  78. 1 - o, 1, d,
  79. - 1 - o, 1, d,
  80. - 1 + o, - 1, - d,
  81. 1 + o, - 1, - d,
  82. - 1 + o, 1, - d,
  83. 1 + o, - 1, - d,
  84. 1 + o, 1, - d,
  85. - 1 + o, 1, - d,
  86. ] );
  87. const colors = new Float32Array( [
  88. 1, 0, 0,
  89. 1, 0, 0,
  90. 1, 0, 0,
  91. 1, 0, 0,
  92. 1, 0, 0,
  93. 1, 0, 0,
  94. 0, 1, 0,
  95. 0, 1, 0,
  96. 0, 1, 0,
  97. 0, 1, 0,
  98. 0, 1, 0,
  99. 0, 1, 0,
  100. ] );
  101. const geometry = new THREE.BufferGeometry();
  102. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  103. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  104. const material = new THREE.MeshBasicMaterial( { vertexColors: true } );
  105. for ( let i = 0; i < numInstances; i ++ ) {
  106. const mesh = new THREE.Mesh( geometry, material );
  107. meshes.push( mesh );
  108. scene.add( mesh );
  109. }
  110. let i = 0;
  111. for ( let x = 0; x < xCount; x ++ ) {
  112. for ( let y = 0; y < yCount; y ++ ) {
  113. const z = - 800 * i;
  114. const s = 1 + 50 * i;
  115. const mesh = meshes[ i ];
  116. mesh.position.set(
  117. x - xCount / 2 + 0.5,
  118. ( 4.0 - 0.2 * z ) * ( y - yCount / 2 + 1.0 ),
  119. z
  120. );
  121. mesh.scale.setScalar( s );
  122. i ++;
  123. }
  124. }
  125. const normalContainer = document.getElementById( 'container_normal' );
  126. normalRenderer = new THREE.WebGPURenderer();
  127. normalRenderer.setPixelRatio( window.devicePixelRatio );
  128. normalRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  129. normalRenderer.domElement.style.position = 'relative';
  130. normalContainer.appendChild( normalRenderer.domElement );
  131. const logarithmicContainer = document.getElementById( 'container_logarithmic' );
  132. logarithmicRenderer = new THREE.WebGPURenderer( { logarithmicDepthBuffer: true } );
  133. logarithmicRenderer.setPixelRatio( window.devicePixelRatio );
  134. logarithmicRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  135. logarithmicRenderer.domElement.style.position = 'relative';
  136. logarithmicContainer.appendChild( logarithmicRenderer.domElement );
  137. const reverseContainer = document.getElementById( 'container_reverse' );
  138. reverseRenderer = new THREE.WebGPURenderer( { reversedDepthBuffer: true } );
  139. reverseRenderer.setPixelRatio( window.devicePixelRatio );
  140. reverseRenderer.setSize( 0.33 * window.innerWidth, window.innerHeight );
  141. reverseRenderer.domElement.style.position = 'relative';
  142. reverseContainer.appendChild( reverseRenderer.domElement );
  143. //
  144. normalRenderPipeline = new THREE.RenderPipeline( normalRenderer );
  145. normalRenderPipeline.outputNode = pass( scene, camera );
  146. logarithmicRenderPipeline = new THREE.RenderPipeline( logarithmicRenderer );
  147. logarithmicRenderPipeline.outputNode = pass( scene, camera );
  148. reverseRenderPipeline = new THREE.RenderPipeline( reverseRenderer );
  149. reverseRenderPipeline.outputNode = pass( scene, reversedCamera );
  150. await Promise.all( [ normalRenderer.init(), logarithmicRenderer.init(), reverseRenderer.init() ] );
  151. window.addEventListener( 'resize', onWindowResize );
  152. onWindowResize();
  153. }
  154. function animate() {
  155. requestAnimationFrame( animate );
  156. const now = performance.now() / 1000;
  157. for ( let i = 0; i < meshes.length; i ++ ) {
  158. const angle = THREE.MathUtils.degToRad( 30 );
  159. const axis = new THREE.Vector3( Math.sin( now ), Math.cos( now ), 0 );
  160. meshes[ i ].quaternion.setFromAxisAngle( axis, angle );
  161. }
  162. render();
  163. }
  164. function render() {
  165. normalRenderPipeline.render();
  166. logarithmicRenderPipeline.render();
  167. reverseRenderPipeline.render();
  168. }
  169. function onWindowResize() {
  170. const width = 0.33 * window.innerWidth;
  171. const height = window.innerHeight;
  172. normalRenderer.setSize( width, height );
  173. logarithmicRenderer.setSize( width, height );
  174. reverseRenderer.setSize( width, height );
  175. camera.aspect = 0.33 * window.innerWidth / window.innerHeight;
  176. camera.updateProjectionMatrix();
  177. reversedCamera.aspect = 0.33 * window.innerWidth / window.innerHeight;
  178. reversedCamera.updateProjectionMatrix();
  179. }
  180. </script>
  181. </body>
  182. </html>
粤ICP备19079148号