1
0

webgpu_postprocessing_ao.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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';
  27. import { pass, mrt, output, normalView } from 'three/tsl';
  28. import { ao } from 'three/addons/tsl/display/GTAONode.js';
  29. import { denoise } from 'three/addons/tsl/display/DenoiseNode.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. let camera, scene, renderer, postProcessing, controls;
  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( { antialias: true } );
  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. //
  76. postProcessing = new THREE.PostProcessing( renderer );
  77. const scenePass = pass( scene, camera );
  78. scenePass.setMRT( mrt( {
  79. output: output,
  80. normal: normalView
  81. } ) );
  82. scenePassColor = scenePass.getTextureNode( 'output' );
  83. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  84. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  85. // ao
  86. aoPass = ao( scenePassDepth, scenePassNormal, camera );
  87. aoPass.resolutionScale = 0.5; // running AO in half resolution is often sufficient
  88. blendPassAO = aoPass.getTextureNode().mul( scenePassColor );
  89. // denoise (optional, use it if you need best quality but is has a noticeable hit on performance)
  90. denoisePass = denoise( aoPass.getTextureNode(), scenePassDepth, scenePassNormal, camera );
  91. blendPassDenoise = denoisePass.mul( scenePassColor );
  92. postProcessing.outputNode = blendPassAO;
  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, 'distanceExponent' ).min( 1 ).max( 4 ).onChange( updateParameters );
  118. gui.add( params, 'distanceFallOff' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  119. gui.add( params, 'radius' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  120. gui.add( params, 'scale' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  121. gui.add( params, 'thickness' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  122. gui.add( params, 'enabled' ).onChange( updatePassChain );
  123. const folder = gui.addFolder( 'Denoise settings' );
  124. folder.add( params, 'denoiseRadius' ).min( 0.01 ).max( 10 ).name( 'radius' ).onChange( updateParameters );
  125. folder.add( params, 'lumaPhi' ).min( 0.01 ).max( 10 ).onChange( updateParameters );
  126. folder.add( params, 'depthPhi' ).min( 0.01 ).max( 10 ).onChange( updateParameters );
  127. folder.add( params, 'normalPhi' ).min( 0.01 ).max( 10 ).onChange( updateParameters );
  128. folder.add( params, 'denoised' ).name( 'enabled' ).onChange( updatePassChain );
  129. }
  130. function updatePassChain() {
  131. if ( params.enabled === true ) {
  132. if ( params.denoised === true ) {
  133. postProcessing.outputNode = blendPassDenoise;
  134. } else {
  135. postProcessing.outputNode = blendPassAO;
  136. }
  137. } else {
  138. postProcessing.outputNode = scenePassColor;
  139. }
  140. postProcessing.needsUpdate = true;
  141. }
  142. function updateParameters() {
  143. aoPass.distanceExponent.value = params.distanceExponent;
  144. aoPass.distanceFallOff.value = params.distanceFallOff;
  145. aoPass.radius.value = params.radius;
  146. aoPass.scale.value = params.scale;
  147. aoPass.thickness.value = params.thickness;
  148. denoisePass.radius.value = params.denoiseRadius;
  149. denoisePass.lumaPhi.value = params.lumaPhi;
  150. denoisePass.depthPhi.value = params.depthPhi;
  151. denoisePass.normalPhi.value = params.normalPhi;
  152. }
  153. function onWindowResize() {
  154. const width = window.innerWidth;
  155. const height = window.innerHeight;
  156. camera.aspect = width / height;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( width, height );
  159. }
  160. function animate() {
  161. controls.update();
  162. postProcessing.render();
  163. }
  164. </script>
  165. </body>
  166. </html>
粤ICP备19079148号