webgpu_postprocessing_ao.html 6.7 KB

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