webgpu_postprocessing_ao.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 { sample, pass, mrt, context, screenUV, normalView, velocity, vec3, vec4, directionToColor, colorToDirection, colorSpaceToWorking } 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;
  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. // controls
  65. controls = new OrbitControls( camera, renderer.domElement );
  66. controls.target.set( 0, 0.5, - 1 );
  67. controls.update();
  68. controls.enablePan = false;
  69. controls.enableDamping = true;
  70. controls.minDistance = 2;
  71. controls.maxDistance = 8;
  72. // environment
  73. const environment = new RoomEnvironment();
  74. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  75. scene.background = new THREE.Color( 0x666666 );
  76. scene.environment = pmremGenerator.fromScene( environment ).texture;
  77. environment.dispose();
  78. pmremGenerator.dispose();
  79. // post-processing
  80. postProcessing = new THREE.PostProcessing( renderer );
  81. // pre-pass
  82. const prePass = pass( scene, camera ).toInspector( 'Normal', ( inspectNode ) => colorSpaceToWorking( inspectNode, THREE.SRGBColorSpace ) );
  83. prePass.name = 'Pre-Pass';
  84. prePass.transparent = false;
  85. prePass.setMRT( mrt( {
  86. output: directionToColor( normalView ),
  87. velocity: velocity
  88. } ) );
  89. const prePassNormal = sample( ( uv ) => {
  90. return colorToDirection( prePass.getTextureNode().sample( uv ) );
  91. } );
  92. const prePassDepth = prePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => prePass.getLinearDepthNode() );
  93. const prePassVelocity = prePass.getTextureNode( 'velocity' ).toInspector( 'Velocity' );
  94. // pre-pass - bandwidth optimization
  95. const normalTexture = prePass.getTexture( 'output' );
  96. normalTexture.type = THREE.UnsignedByteType;
  97. // scene pass
  98. const scenePass = pass( scene, camera ).toInspector( 'Color' );
  99. // ao
  100. aoPass = ao( prePassDepth, prePassNormal, camera ).toInspector( 'GTAO', ( inspectNode ) => inspectNode.r );
  101. aoPass.resolutionScale = 0.5; // running AO in half resolution is often sufficient
  102. aoPass.useTemporalFiltering = true;
  103. const aoPassOutput = aoPass.getTextureNode();
  104. // scene context
  105. scenePass.contextNode = context( {
  106. ao: aoPassOutput.sample( screenUV ).r
  107. } );
  108. // final output + traa
  109. traaPass = traa( scenePass, prePassDepth, prePassVelocity, camera );
  110. traaPass.useSubpixelCorrection = false;
  111. postProcessing.outputNode = traaPass;
  112. // models
  113. const dracoLoader = new DRACOLoader();
  114. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  115. dracoLoader.setDecoderConfig( { type: 'js' } );
  116. const loader = new GLTFLoader();
  117. loader.setDRACOLoader( dracoLoader );
  118. loader.setPath( 'models/gltf/' );
  119. const gltf = await loader.loadAsync( 'minimalistic_modern_bedroom.glb' );
  120. const model = gltf.scene;
  121. model.position.set( 0, 1, 0 );
  122. scene.add( model );
  123. //
  124. const transparentMesh = new THREE.Mesh( new THREE.PlaneGeometry( 1.8, 2 ), new THREE.MeshStandardNodeMaterial( { transparent: true, opacity: .1 } ) );
  125. transparentMesh.material.transparent = true;
  126. transparentMesh.position.z = 0;
  127. transparentMesh.position.y = 0.5;
  128. transparentMesh.visible = false;
  129. scene.add( transparentMesh );
  130. // events
  131. window.addEventListener( 'resize', onWindowResize );
  132. //
  133. const gui = renderer.inspector.createParameters( 'Settings' );
  134. gui.add( params, 'samples', 4, 32, 1 ).onChange( updateParameters );
  135. gui.add( params, 'distanceExponent', 1, 2 ).onChange( updateParameters );
  136. gui.add( params, 'distanceFallOff', 0.01, 1 ).onChange( updateParameters );
  137. gui.add( params, 'radius', 0.1, 1 ).onChange( updateParameters );
  138. gui.add( params, 'scale', 0.01, 2 ).onChange( updateParameters );
  139. gui.add( params, 'thickness', 0.01, 2 ).onChange( updateParameters );
  140. gui.add( aoPass, 'useTemporalFiltering' ).name( 'temporal filtering' );
  141. gui.add( transparentMesh, 'visible' ).name( 'show transparent mesh' );
  142. gui.add( params, 'aoOnly' ).onChange( ( value ) => {
  143. if ( value === true ) {
  144. postProcessing.outputNode = vec4( vec3( aoPass.r ), 1 );
  145. } else {
  146. postProcessing.outputNode = traaPass;
  147. }
  148. postProcessing.needsUpdate = true;
  149. } );
  150. }
  151. function updateParameters() {
  152. aoPass.samples.value = params.samples;
  153. aoPass.distanceExponent.value = params.distanceExponent;
  154. aoPass.distanceFallOff.value = params.distanceFallOff;
  155. aoPass.radius.value = params.radius;
  156. aoPass.scale.value = params.scale;
  157. aoPass.thickness.value = params.thickness;
  158. }
  159. function onWindowResize() {
  160. const width = window.innerWidth;
  161. const height = window.innerHeight;
  162. camera.aspect = width / height;
  163. camera.updateProjectionMatrix();
  164. renderer.setSize( width, height );
  165. }
  166. function animate() {
  167. controls.update();
  168. postProcessing.render();
  169. }
  170. </script>
  171. </body>
  172. </html>
粤ICP备19079148号