webgpu_postprocessing_ao.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import { pass, mrt, output, transformedNormalView, texture, ao, denoise } from 'three/tsl';
  22. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  23. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  24. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import { SimplexNoise } from 'three/addons/math/SimplexNoise.js';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. let camera, scene, renderer, postProcessing, controls, clock, stats, mixer;
  30. let aoPass, denoisePass, blendPassAO, blendPassDenoise, scenePassColor;
  31. const params = {
  32. distanceExponent: 1,
  33. distanceFallOff: 1,
  34. radius: 0.25,
  35. scale: 1,
  36. thickness: 1,
  37. denoised: true,
  38. enabled: true,
  39. denoiseRadius: 5,
  40. lumaPhi: 5,
  41. depthPhi: 5,
  42. normalPhi: 5
  43. };
  44. init();
  45. async function init() {
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  47. camera.position.set( 5, 2, 8 );
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0xbfe3dd );
  50. clock = new THREE.Clock();
  51. const hdrloader = new RGBELoader();
  52. const envMap = await hdrloader.loadAsync( 'textures/equirectangular/quarry_01_1k.hdr' );
  53. envMap.mapping = THREE.EquirectangularReflectionMapping;
  54. scene.environment = envMap;
  55. renderer = new THREE.WebGPURenderer();
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.setAnimationLoop( animate );
  58. document.body.appendChild( renderer.domElement );
  59. controls = new OrbitControls( camera, renderer.domElement );
  60. controls.target.set( 0, 0.5, 0 );
  61. controls.update();
  62. controls.enablePan = false;
  63. controls.enableDamping = true;
  64. stats = new Stats();
  65. document.body.appendChild( stats.dom );
  66. //
  67. postProcessing = new THREE.PostProcessing( renderer );
  68. const scenePass = pass( scene, camera );
  69. scenePass.setMRT( mrt( {
  70. output: output,
  71. normal: transformedNormalView
  72. } ) );
  73. scenePassColor = scenePass.getTextureNode( 'output' );
  74. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  75. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  76. // ao
  77. aoPass = ao( scenePassDepth, scenePassNormal, camera );
  78. blendPassAO = aoPass.getTextureNode().mul( scenePassColor );
  79. // denoise (optional)
  80. const noiseTexture = texture( generateNoise() );
  81. denoisePass = denoise( aoPass.getTextureNode(), scenePassDepth, scenePassNormal, noiseTexture, camera );
  82. blendPassDenoise = denoisePass.mul( scenePassColor );
  83. postProcessing.outputNode = blendPassDenoise;
  84. //
  85. const dracoLoader = new DRACOLoader();
  86. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  87. dracoLoader.setDecoderConfig( { type: 'js' } );
  88. const loader = new GLTFLoader();
  89. loader.setDRACOLoader( dracoLoader );
  90. loader.setPath( 'models/gltf/' );
  91. const gltf = await loader.loadAsync( 'LittlestTokyo.glb' );
  92. const model = gltf.scene;
  93. model.position.set( 1, 1, 0 );
  94. model.scale.set( 0.01, 0.01, 0.01 );
  95. scene.add( model );
  96. mixer = new THREE.AnimationMixer( model );
  97. mixer.clipAction( gltf.animations[ 0 ] ).play();
  98. window.addEventListener( 'resize', onWindowResize );
  99. //
  100. const gui = new GUI();
  101. gui.title( 'AO settings' );
  102. gui.add( params, 'distanceExponent' ).min( 1 ).max( 4 ).onChange( updateParameters );
  103. gui.add( params, 'distanceFallOff' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  104. gui.add( params, 'radius' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  105. gui.add( params, 'scale' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  106. gui.add( params, 'thickness' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  107. gui.add( params, 'denoised' ).onChange( updatePassChain );
  108. gui.add( params, 'enabled' ).onChange( updatePassChain );
  109. const folder = gui.addFolder( 'Denoise settings' );
  110. folder.add( params, 'denoiseRadius' ).min( 0.01 ).max( 10 ).name( 'radius' ).onChange( updateParameters );
  111. folder.add( params, 'lumaPhi' ).min( 0.01 ).max( 10 ).onChange( updateParameters );
  112. folder.add( params, 'depthPhi' ).min( 0.01 ).max( 10 ).onChange( updateParameters );
  113. folder.add( params, 'normalPhi' ).min( 0.01 ).max( 10 ).onChange( updateParameters );
  114. }
  115. function updatePassChain() {
  116. if ( params.enabled === true ) {
  117. if ( params.denoised === true ) {
  118. postProcessing.outputNode = blendPassDenoise;
  119. } else {
  120. postProcessing.outputNode = blendPassAO;
  121. }
  122. } else {
  123. postProcessing.outputNode = scenePassColor;
  124. }
  125. postProcessing.needsUpdate = true;
  126. }
  127. function updateParameters() {
  128. aoPass.distanceExponent.value = params.distanceExponent;
  129. aoPass.distanceFallOff.value = params.distanceFallOff;
  130. aoPass.radius.value = params.radius;
  131. aoPass.scale.value = params.scale;
  132. aoPass.thickness.value = params.thickness;
  133. denoisePass.radius.value = params.denoiseRadius;
  134. denoisePass.lumaPhi.value = params.lumaPhi;
  135. denoisePass.depthPhi.value = params.depthPhi;
  136. denoisePass.normalPhi.value = params.normalPhi;
  137. }
  138. function generateNoise( size = 64 ) {
  139. const simplex = new SimplexNoise();
  140. const arraySize = size * size * 4;
  141. const data = new Uint8Array( arraySize );
  142. for ( let i = 0; i < size; i ++ ) {
  143. for ( let j = 0; j < size; j ++ ) {
  144. const x = i;
  145. const y = j;
  146. data[ ( i * size + j ) * 4 ] = ( simplex.noise( x, y ) * 0.5 + 0.5 ) * 255;
  147. data[ ( i * size + j ) * 4 + 1 ] = ( simplex.noise( x + size, y ) * 0.5 + 0.5 ) * 255;
  148. data[ ( i * size + j ) * 4 + 2 ] = ( simplex.noise( x, y + size ) * 0.5 + 0.5 ) * 255;
  149. data[ ( i * size + j ) * 4 + 3 ] = ( simplex.noise( x + size, y + size ) * 0.5 + 0.5 ) * 255;
  150. }
  151. }
  152. const noiseTexture = new THREE.DataTexture( data, size, size );
  153. noiseTexture.wrapS = THREE.RepeatWrapping;
  154. noiseTexture.wrapT = THREE.RepeatWrapping;
  155. noiseTexture.needsUpdate = true;
  156. return noiseTexture;
  157. }
  158. function onWindowResize() {
  159. const width = window.innerWidth;
  160. const height = window.innerHeight;
  161. camera.aspect = width / height;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( width, height );
  164. }
  165. function animate() {
  166. const delta = clock.getDelta();
  167. if ( mixer ) {
  168. mixer.update( delta );
  169. }
  170. controls.update();
  171. postProcessing.render();
  172. stats.update();
  173. }
  174. </script>
  175. </body>
  176. </html>