1
0

webgpu_postprocessing_ao.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>AO</span>
  14. </div>
  15. <small>
  16. Ambient Occlusion based on GTAO.<br />
  17. <a href="https://skfb.ly/oCnNx" target="_blank" rel="noopener">Minimalistic Modern Bedroom</a> by
  18. <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>.
  19. </small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { pass, mrt, output, normalView, velocity, vec3, vec4 } from 'three/tsl';
  34. import { ao } from 'three/addons/tsl/display/GTAONode.js';
  35. import { traa } from 'three/addons/tsl/display/TRAANode.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  38. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  39. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  40. import { Inspector } from 'three/addons/inspector/Inspector.js';
  41. let camera, scene, renderer, postProcessing, controls;
  42. let aoPass, traaPass, blendPassAO, scenePassColor;
  43. const params = {
  44. samples: 16,
  45. distanceExponent: 1,
  46. distanceFallOff: 1,
  47. radius: 0.25,
  48. scale: 1,
  49. thickness: 1,
  50. aoOnly: false
  51. };
  52. init();
  53. async function init() {
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 50 );
  55. camera.position.set( 1, 1.3, 5 );
  56. scene = new THREE.Scene();
  57. renderer = new THREE.WebGPURenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setAnimationLoop( animate );
  61. renderer.inspector = new Inspector();
  62. document.body.appendChild( renderer.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' ).toInspector( 'Color' );
  87. const scenePassDepth = scenePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => {
  88. return scenePass.getLinearDepthNode();
  89. } );
  90. const scenePassNormal = scenePass.getTextureNode( 'normal' ).toInspector( 'Normal' );
  91. const scenePassVelocity = scenePass.getTextureNode( 'velocity' ).toInspector( 'Velocity' );
  92. // ao
  93. aoPass = ao( scenePassDepth, scenePassNormal, camera ).toInspector( 'AO' );
  94. aoPass.resolutionScale = 0.5; // running AO in half resolution is often sufficient
  95. blendPassAO = vec4( scenePassColor.rgb.mul( aoPass.r ), scenePassColor.a ); // the AO is stored only in the red channel
  96. // traa
  97. traaPass = traa( blendPassAO, scenePassDepth, scenePassVelocity, camera );
  98. postProcessing.outputNode = traaPass;
  99. //
  100. const dracoLoader = new DRACOLoader();
  101. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  102. dracoLoader.setDecoderConfig( { type: 'js' } );
  103. const loader = new GLTFLoader();
  104. loader.setDRACOLoader( dracoLoader );
  105. loader.setPath( 'models/gltf/' );
  106. const gltf = await loader.loadAsync( 'minimalistic_modern_bedroom.glb' );
  107. const model = gltf.scene;
  108. model.position.set( 0, 1, 0 );
  109. scene.add( model );
  110. model.traverse( ( o ) => {
  111. // Transparent objects (e.g. loaded via GLTFLoader) might have "depthWrite" set to "false".
  112. // This is wanted when rendering the beauty pass however it produces wrong results when computing
  113. // AO since depth and normal data are out of sync. Computing normals from depth by not using MRT
  114. // can mitigate the issue although the depth information (and thus the normals) are not correct in
  115. // first place. Besides, normal estimation is computationally more expensive than just sampling a
  116. // normal texture. So depending on your scene, consider to enable "depthWrite" for all transparent objects.
  117. if ( o.material ) o.material.depthWrite = true;
  118. } );
  119. window.addEventListener( 'resize', onWindowResize );
  120. //
  121. const gui = renderer.inspector.createParameters( 'Settings' );
  122. gui.add( params, 'samples', 4, 32, 1 ).onChange( updateParameters );
  123. gui.add( params, 'distanceExponent', 1, 2 ).onChange( updateParameters );
  124. gui.add( params, 'distanceFallOff', 0.01, 1 ).onChange( updateParameters );
  125. gui.add( params, 'radius', 0.1, 1 ).onChange( updateParameters );
  126. gui.add( params, 'scale', 0.01, 2 ).onChange( updateParameters );
  127. gui.add( params, 'thickness', 0.01, 2 ).onChange( updateParameters );
  128. gui.add( params, 'aoOnly' ).onChange( ( value ) => {
  129. if ( value === true ) {
  130. postProcessing.outputNode = vec4( vec3( aoPass.r ), 1 );
  131. } else {
  132. postProcessing.outputNode = traaPass;
  133. }
  134. postProcessing.needsUpdate = true;
  135. } );
  136. }
  137. function updateParameters() {
  138. aoPass.samples.value = params.samples;
  139. aoPass.distanceExponent.value = params.distanceExponent;
  140. aoPass.distanceFallOff.value = params.distanceFallOff;
  141. aoPass.radius.value = params.radius;
  142. aoPass.scale.value = params.scale;
  143. aoPass.thickness.value = params.thickness;
  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. controls.update();
  154. postProcessing.render();
  155. }
  156. </script>
  157. </body>
  158. </html>
粤ICP备19079148号