webgpu_postprocessing_ao.html 7.2 KB

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