webgpu_postprocessing_ao.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - ambient occlusion</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>AO</span>
  14. </div>
  15. <small>
  16. Ambient Occlusion based on GTAO.<br />
  17. <a href="https://skfb.ly/oCnNx" target="_blank" rel="noopener">Minimalistic Modern Bedroom</a> by
  18. <a href="https://sketchfab.com/dylanheyes" target="_blank" rel="noopener">dylanheyes</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</a>.
  19. </small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { pass, mrt, output, normalView, velocity, vec3, vec4 } from 'three/tsl';
  34. import { ao } from 'three/addons/tsl/display/GTAONode.js';
  35. import { traa } from 'three/addons/tsl/display/TRAANode.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  38. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  39. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  40. import { Inspector } from 'three/addons/inspector/Inspector.js';
  41. let camera, scene, renderer, postProcessing, controls;
  42. let aoPass, traaPass, blendPassAO, scenePassColor;
  43. const params = {
  44. samples: 16,
  45. distanceExponent: 1,
  46. distanceFallOff: 1,
  47. radius: 0.25,
  48. scale: 1,
  49. thickness: 1,
  50. aoOnly: false
  51. };
  52. init();
  53. async function init() {
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 50 );
  55. camera.position.set( 1, 1.3, 5 );
  56. scene = new THREE.Scene();
  57. renderer = new THREE.WebGPURenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setAnimationLoop( animate );
  61. renderer.inspector = new Inspector();
  62. document.body.appendChild( renderer.domElement );
  63. await renderer.init();
  64. const environment = new RoomEnvironment();
  65. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  66. scene.background = new THREE.Color( 0x666666 );
  67. scene.environment = pmremGenerator.fromScene( environment ).texture;
  68. environment.dispose();
  69. pmremGenerator.dispose();
  70. //
  71. controls = new OrbitControls( camera, renderer.domElement );
  72. controls.target.set( 0, 0.5, - 1 );
  73. controls.update();
  74. controls.enablePan = false;
  75. controls.enableDamping = true;
  76. controls.minDistance = 2;
  77. controls.maxDistance = 8;
  78. //
  79. postProcessing = new THREE.PostProcessing( renderer );
  80. const scenePass = pass( scene, camera );
  81. scenePass.setMRT( mrt( {
  82. output: output,
  83. normal: normalView,
  84. velocity: velocity
  85. } ) );
  86. scenePassColor = scenePass.getTextureNode( 'output' ).toInspector( 'Color' );
  87. const scenePassDepth = scenePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => {
  88. return scenePass.getLinearDepthNode();
  89. } );
  90. const scenePassNormal = scenePass.getTextureNode( 'normal' ).toInspector( 'Normal' );
  91. const scenePassVelocity = scenePass.getTextureNode( 'velocity' ).toInspector( 'Velocity' );
  92. // ao
  93. aoPass = ao( scenePassDepth, scenePassNormal, camera ).toInspector( 'AO' );
  94. aoPass.resolutionScale = 0.5; // running AO in half resolution is often sufficient
  95. aoPass.useTemporalFiltering = true;
  96. blendPassAO = vec4( scenePassColor.rgb.mul( aoPass.r ), scenePassColor.a ); // the AO is stored only in the red channel
  97. // traa
  98. traaPass = traa( blendPassAO, scenePassDepth, scenePassVelocity, camera );
  99. postProcessing.outputNode = traaPass;
  100. //
  101. const dracoLoader = new DRACOLoader();
  102. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  103. dracoLoader.setDecoderConfig( { type: 'js' } );
  104. const loader = new GLTFLoader();
  105. loader.setDRACOLoader( dracoLoader );
  106. loader.setPath( 'models/gltf/' );
  107. const gltf = await loader.loadAsync( 'minimalistic_modern_bedroom.glb' );
  108. const model = gltf.scene;
  109. model.position.set( 0, 1, 0 );
  110. scene.add( model );
  111. model.traverse( ( o ) => {
  112. // Transparent objects (e.g. loaded via GLTFLoader) might have "depthWrite" set to "false".
  113. // This is wanted when rendering the beauty pass however it produces wrong results when computing
  114. // AO since depth and normal data are out of sync. Computing normals from depth by not using MRT
  115. // can mitigate the issue although the depth information (and thus the normals) are not correct in
  116. // first place. Besides, normal estimation is computationally more expensive than just sampling a
  117. // normal texture. So depending on your scene, consider to enable "depthWrite" for all transparent objects.
  118. if ( o.material ) o.material.depthWrite = true;
  119. } );
  120. window.addEventListener( 'resize', onWindowResize );
  121. //
  122. const gui = renderer.inspector.createParameters( 'Settings' );
  123. gui.add( params, 'samples', 4, 32, 1 ).onChange( updateParameters );
  124. gui.add( params, 'distanceExponent', 1, 2 ).onChange( updateParameters );
  125. gui.add( params, 'distanceFallOff', 0.01, 1 ).onChange( updateParameters );
  126. gui.add( params, 'radius', 0.1, 1 ).onChange( updateParameters );
  127. gui.add( params, 'scale', 0.01, 2 ).onChange( updateParameters );
  128. gui.add( params, 'thickness', 0.01, 2 ).onChange( updateParameters );
  129. gui.add( aoPass, 'useTemporalFiltering' ).name( 'temporal filtering' );
  130. gui.add( params, 'aoOnly' ).onChange( ( value ) => {
  131. if ( value === true ) {
  132. postProcessing.outputNode = vec4( vec3( aoPass.r ), 1 );
  133. } else {
  134. postProcessing.outputNode = traaPass;
  135. }
  136. postProcessing.needsUpdate = true;
  137. } );
  138. }
  139. function updateParameters() {
  140. aoPass.samples.value = params.samples;
  141. aoPass.distanceExponent.value = params.distanceExponent;
  142. aoPass.distanceFallOff.value = params.distanceFallOff;
  143. aoPass.radius.value = params.radius;
  144. aoPass.scale.value = params.scale;
  145. aoPass.thickness.value = params.thickness;
  146. }
  147. function onWindowResize() {
  148. const width = window.innerWidth;
  149. const height = window.innerHeight;
  150. camera.aspect = width / height;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( width, height );
  153. }
  154. function animate() {
  155. controls.update();
  156. postProcessing.render();
  157. }
  158. </script>
  159. </body>
  160. </html>
粤ICP备19079148号