1
0

webgpu_postprocessing_ao.html 7.4 KB

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