webgpu_postprocessing_ao.html 6.4 KB

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