webgpu_postprocessing_ao.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 } 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 Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. let camera, scene, renderer, postProcessing, controls, clock, stats, mixer;
  29. let aoPass;
  30. const params = {
  31. blendIntensity: 1,
  32. distanceExponent: 1,
  33. distanceFallOff: 1,
  34. radius: 0.25,
  35. scale: 1,
  36. thickness: 1,
  37. enabled: true
  38. };
  39. init();
  40. async function init() {
  41. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  42. camera.position.set( 5, 2, 8 );
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0xbfe3dd );
  45. clock = new THREE.Clock();
  46. const hdrloader = new RGBELoader();
  47. const texture = await hdrloader.loadAsync( 'textures/equirectangular/quarry_01_1k.hdr' );
  48. texture.mapping = THREE.EquirectangularReflectionMapping;
  49. scene.environment = texture;
  50. renderer = new THREE.WebGPURenderer();
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  52. renderer.setAnimationLoop( animate );
  53. document.body.appendChild( renderer.domElement );
  54. controls = new OrbitControls( camera, renderer.domElement );
  55. controls.target.set( 0, 0.5, 0 );
  56. controls.update();
  57. controls.enablePan = false;
  58. controls.enableDamping = true;
  59. stats = new Stats();
  60. document.body.appendChild( stats.dom );
  61. //
  62. postProcessing = new THREE.PostProcessing( renderer );
  63. const scenePass = pass( scene, camera );
  64. scenePass.setMRT( mrt( {
  65. output: output,
  66. normal: transformedNormalView
  67. } ) );
  68. const scenePassColor = scenePass.getTextureNode( 'output' );
  69. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  70. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  71. aoPass = scenePassColor.ao( scenePassDepth, scenePassNormal, camera );
  72. postProcessing.outputNode = aoPass;
  73. //
  74. const dracoLoader = new DRACOLoader();
  75. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  76. dracoLoader.setDecoderConfig( { type: 'js' } );
  77. const loader = new GLTFLoader();
  78. loader.setDRACOLoader( dracoLoader );
  79. loader.setPath( 'models/gltf/' );
  80. const gltf = await loader.loadAsync( 'LittlestTokyo.glb' );
  81. const model = gltf.scene;
  82. model.position.set( 1, 1, 0 );
  83. model.scale.set( 0.01, 0.01, 0.01 );
  84. scene.add( model );
  85. mixer = new THREE.AnimationMixer( model );
  86. mixer.clipAction( gltf.animations[ 0 ] ).play();
  87. window.addEventListener( 'resize', onWindowResize );
  88. //
  89. const gui = new GUI();
  90. gui.title( 'AO settings' );
  91. gui.add( params, 'blendIntensity' ).min( 0 ).max( 1 ).onChange( updateParameters );
  92. gui.add( params, 'distanceExponent' ).min( 1 ).max( 4 ).onChange( updateParameters );
  93. gui.add( params, 'distanceFallOff' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  94. gui.add( params, 'radius' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  95. gui.add( params, 'scale' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  96. gui.add( params, 'thickness' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  97. gui.add( params, 'enabled' ).onChange( ( value ) => {
  98. if ( value === true ) {
  99. postProcessing.outputNode = aoPass;
  100. } else {
  101. postProcessing.outputNode = scenePassColor;
  102. }
  103. postProcessing.needsUpdate = true;
  104. } );
  105. }
  106. function updateParameters() {
  107. aoPass.blendIntensity.value = params.blendIntensity;
  108. aoPass.distanceExponent.value = params.distanceExponent;
  109. aoPass.distanceFallOff.value = params.distanceFallOff;
  110. aoPass.radius.value = params.radius;
  111. aoPass.scale.value = params.scale;
  112. aoPass.thickness.value = params.thickness;
  113. }
  114. function onWindowResize() {
  115. const width = window.innerWidth;
  116. const height = window.innerHeight;
  117. camera.aspect = width / height;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( width, height );
  120. }
  121. function animate() {
  122. const delta = clock.getDelta();
  123. if ( mixer ) {
  124. mixer.update( delta );
  125. }
  126. controls.update();
  127. postProcessing.render();
  128. stats.update();
  129. }
  130. </script>
  131. </body>
  132. </html>