webgpu_postprocessing_ao.html 7.3 KB

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