webgpu_reversed_depth_buffer.html 7.0 KB

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