1
0

webgpu_postprocessing_ao.html 7.4 KB

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