webgpu_postprocessing_ao.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - ambient occlusion</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/webgpu';
  27. import { pass, mrt, output, normalView, velocity } from 'three/tsl';
  28. import { ao } from 'three/addons/tsl/display/GTAONode.js';
  29. import { traa } from 'three/addons/tsl/display/TRAANode.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 { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. import Stats from 'three/addons/libs/stats.module.js';
  36. let camera, scene, renderer, postProcessing, controls, stats;
  37. let aoPass, traaPass, blendPassAO, 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. stats = new Stats();
  62. document.body.appendChild( stats.domElement );
  63. await renderer.init();
  64. const environment = new RoomEnvironment();
  65. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  66. scene.background = new THREE.Color( 0x666666 );
  67. scene.environment = pmremGenerator.fromScene( environment ).texture;
  68. environment.dispose();
  69. pmremGenerator.dispose();
  70. //
  71. controls = new OrbitControls( camera, renderer.domElement );
  72. controls.target.set( 0, 0.5, - 1 );
  73. controls.update();
  74. controls.enablePan = false;
  75. controls.enableDamping = true;
  76. controls.minDistance = 2;
  77. controls.maxDistance = 8;
  78. //
  79. postProcessing = new THREE.PostProcessing( renderer );
  80. const scenePass = pass( scene, camera );
  81. scenePass.setMRT( mrt( {
  82. output: output,
  83. normal: normalView,
  84. velocity: velocity
  85. } ) );
  86. scenePassColor = scenePass.getTextureNode( 'output' );
  87. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  88. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  89. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  90. // ao
  91. aoPass = ao( scenePassDepth, scenePassNormal, camera );
  92. aoPass.resolutionScale = 0.5; // running AO in half resolution is often sufficient
  93. blendPassAO = aoPass.getTextureNode().mul( scenePassColor );
  94. // traa
  95. traaPass = traa( blendPassAO, scenePassDepth, scenePassVelocity, camera );
  96. postProcessing.outputNode = traaPass;
  97. //
  98. const dracoLoader = new DRACOLoader();
  99. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  100. dracoLoader.setDecoderConfig( { type: 'js' } );
  101. const loader = new GLTFLoader();
  102. loader.setDRACOLoader( dracoLoader );
  103. loader.setPath( 'models/gltf/' );
  104. const gltf = await loader.loadAsync( 'minimalistic_modern_bedroom.glb' );
  105. const model = gltf.scene;
  106. model.position.set( 0, 1, 0 );
  107. scene.add( model );
  108. model.traverse( ( o ) => {
  109. // Transparent objects (e.g. loaded via GLTFLoader) might have "depthWrite" set to "false".
  110. // This is wanted when rendering the beauty pass however it produces wrong results when computing
  111. // AO since depth and normal data are out of sync. Computing normals from depth by not using MRT
  112. // can mitigate the issue although the depth information (and thus the normals) are not correct in
  113. // first place. Besides, normal estimation is computationally more expensive than just sampling a
  114. // normal texture. So depending on your scene, consider to enable "depthWrite" for all transparent objects.
  115. if ( o.material ) o.material.depthWrite = true;
  116. } );
  117. window.addEventListener( 'resize', onWindowResize );
  118. //
  119. const gui = new GUI();
  120. gui.title( 'AO settings' );
  121. gui.add( params, 'distanceExponent' ).min( 1 ).max( 4 ).onChange( updateParameters );
  122. gui.add( params, 'distanceFallOff' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  123. gui.add( params, 'radius' ).min( 0.1 ).max( 1 ).onChange( updateParameters );
  124. gui.add( params, 'scale' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  125. gui.add( params, 'thickness' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  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. }
  134. function onWindowResize() {
  135. const width = window.innerWidth;
  136. const height = window.innerHeight;
  137. camera.aspect = width / height;
  138. camera.updateProjectionMatrix();
  139. renderer.setSize( width, height );
  140. }
  141. function animate() {
  142. controls.update();
  143. stats.update();
  144. postProcessing.render();
  145. }
  146. </script>
  147. </body>
  148. </html>
粤ICP备19079148号