webgpu_postprocessing_ao.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, metalness, normalView, roughness, velocity, blendColor, sample, vec2 } from 'three/tsl';
  28. import { ao } from 'three/addons/tsl/display/GTAONode.js';
  29. import { ssr } from 'three/addons/tsl/display/SSRNode.js';
  30. import { traa } from 'three/addons/tsl/display/TRAANode.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  33. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. let camera, scene, renderer, postProcessing, controls, stats;
  38. let aoPass, ssrPass;
  39. const params = {
  40. distanceExponent: 1,
  41. distanceFallOff: 1,
  42. radius: 0.25,
  43. scale: 1,
  44. thickness: 1,
  45. denoiseRadius: 5,
  46. lumaPhi: 5,
  47. depthPhi: 5,
  48. normalPhi: 5
  49. };
  50. init();
  51. async function init() {
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 50 );
  53. camera.position.set( 1, 1.3, 5 );
  54. scene = new THREE.Scene();
  55. renderer = new THREE.WebGPURenderer();
  56. // renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. renderer.setAnimationLoop( animate );
  59. renderer.toneMapping = THREE.NeutralToneMapping;
  60. renderer.toneMappingExposure = 3.0;
  61. document.body.appendChild( renderer.domElement );
  62. stats = new Stats();
  63. document.body.appendChild( stats.domElement );
  64. await renderer.init();
  65. const environment = new RoomEnvironment();
  66. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  67. scene.background = new THREE.Color( 0x666666 );
  68. scene.environment = pmremGenerator.fromScene( environment ).texture;
  69. environment.dispose();
  70. pmremGenerator.dispose();
  71. //
  72. controls = new OrbitControls( camera, renderer.domElement );
  73. controls.target.set( 0, 0.5, - 1 );
  74. controls.update();
  75. controls.enableDamping = true;
  76. //
  77. postProcessing = new THREE.PostProcessing( renderer );
  78. const scenePass = pass( scene, camera );
  79. scenePass.setMRT( mrt( {
  80. output: output,
  81. normal: normalView,
  82. metalrough: vec2( metalness, roughness ),
  83. velocity: velocity
  84. } ) );
  85. const scenePassColor = scenePass.getTextureNode( 'output' );
  86. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  87. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  88. const scenePassMetalRough = scenePass.getTextureNode( 'metalrough' );
  89. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  90. const metalRoughTexture = scenePass.getTexture( 'metalrough' );
  91. metalRoughTexture.type = THREE.UnsignedByteType;
  92. // ao
  93. aoPass = ao( scenePassDepth, scenePassNormal, camera );
  94. aoPass.resolutionScale = 0.5;
  95. // ssr
  96. const customMetalness = sample( ( uv ) => {
  97. const metalRough = scenePassMetalRough.sample( uv );
  98. return metalRough.r.mul( metalRough.g.add( 0.25 ) );
  99. } );
  100. ssrPass = ssr( scenePassColor, scenePassDepth, scenePassNormal, customMetalness, camera );
  101. ssrPass.resolutionScale = 1.0;
  102. postProcessing.outputNode = traa(
  103. blendColor( scenePassColor.mul( aoPass ), ssrPass ),
  104. scenePassDepth, scenePassVelocity, camera
  105. );
  106. //
  107. const dracoLoader = new DRACOLoader();
  108. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  109. dracoLoader.setDecoderConfig( { type: 'js' } );
  110. const loader = new GLTFLoader();
  111. loader.setDRACOLoader( dracoLoader );
  112. loader.setPath( 'models/gltf/' );
  113. const gltf = await loader.loadAsync( 'minimalistic_modern_bedroom.glb' );
  114. const model = gltf.scene;
  115. model.position.set( 0, 1, 0 );
  116. scene.add( model );
  117. // Fix scene
  118. const duvet = model.getObjectByName( 'Bedroom_Duvet_0' );
  119. duvet.material.metalness = 0;
  120. const carpet = model.getObjectByName( 'Bedroom_Carpet_0' );
  121. carpet.material.metalness = 0;
  122. const windows = model.getObjectByName( 'Bedroom_Windows_0' );
  123. windows.material.emissive = new THREE.Color( 0xffffff );
  124. windows.material.emissiveIntensity = 1.0;
  125. model.traverse( ( o ) => {
  126. if ( o.material ) {
  127. if ( o.material.map ) o.material.map.anisotropy = 8;
  128. if ( o.material.roughnessMap ) o.material.roughnessMap.anisotropy = 8;
  129. // Remove emissive from all materials
  130. o.material.emissive = new THREE.Color( 0x000000 );
  131. o.material.emissiveMap = null;
  132. // Transparent objects (e.g. loaded via GLTFLoader) might have "depthWrite" set to "false".
  133. // This is wanted when rendering the beauty pass however it produces wrong results when computing
  134. // AO since depth and normal data are out of sync. Computing normals from depth by not using MRT
  135. // can mitigate the issue although the depth information (and thus the normals) are not correct in
  136. // first place. Besides, normal estimation is computationally more expensive than just sampling a
  137. // normal texture. So depending on your scene, consider to enable "depthWrite" for all transparent objects.
  138. o.material.depthWrite = true;
  139. }
  140. } );
  141. //
  142. window.addEventListener( 'resize', onWindowResize );
  143. //
  144. const gui = new GUI();
  145. gui.title( 'AO settings' );
  146. gui.add( params, 'distanceExponent' ).min( 1 ).max( 4 ).onChange( updateParameters );
  147. gui.add( params, 'distanceFallOff' ).min( 0.01 ).max( 1 ).onChange( updateParameters );
  148. gui.add( params, 'radius' ).min( 0.1 ).max( 1 ).onChange( updateParameters );
  149. gui.add( params, 'scale' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  150. gui.add( params, 'thickness' ).min( 0.01 ).max( 2 ).onChange( updateParameters );
  151. }
  152. function updateParameters() {
  153. aoPass.distanceExponent.value = params.distanceExponent;
  154. aoPass.distanceFallOff.value = params.distanceFallOff;
  155. aoPass.radius.value = params.radius;
  156. aoPass.scale.value = params.scale;
  157. aoPass.thickness.value = params.thickness;
  158. }
  159. function onWindowResize() {
  160. const width = window.innerWidth;
  161. const height = window.innerHeight;
  162. camera.aspect = width / height;
  163. camera.updateProjectionMatrix();
  164. renderer.setSize( width, height );
  165. }
  166. function animate() {
  167. controls.update();
  168. stats.update();
  169. postProcessing.render();
  170. }
  171. </script>
  172. </body>
  173. </html>
粤ICP备19079148号