webgpu_postprocessing_ssgi.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGPU - SSGI</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 - Post-Processing - SSGI<br />
  12. <a href="https://skfb.ly/YZoC" target="_blank" rel="noopener">Mirror's Edge Apartment</a> by
  13. <a href="https://sketchfab.com/aurelien_martel" target="_blank" rel="noopener">Aurélien Martel</a> is licensed under <a href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="noopener">Creative Commons Attribution-NonCommercial</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, diffuseColor, velocity, add, vec3, vec4, directionToColor, colorToDirection, sample } from 'three/tsl';
  28. import { ssgi } from 'three/addons/tsl/display/SSGINode.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 { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  33. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  34. import Stats from 'three/addons/libs/stats.module.js';
  35. let camera, scene, renderer, postProcessing, controls, stats;
  36. init();
  37. async function init() {
  38. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 50 );
  39. camera.position.set( 2.8, 1.8, 5 );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0xffffff );
  42. renderer = new THREE.WebGPURenderer();
  43. //renderer.setPixelRatio( window.devicePixelRatio ); // probably too costly for most hardware
  44. renderer.setSize( window.innerWidth, window.innerHeight );
  45. renderer.setAnimationLoop( animate );
  46. renderer.toneMapping = THREE.NeutralToneMapping;
  47. renderer.toneMappingExposure = 1.5;
  48. document.body.appendChild( renderer.domElement );
  49. stats = new Stats();
  50. document.body.appendChild( stats.domElement );
  51. //
  52. controls = new OrbitControls( camera, renderer.domElement );
  53. controls.target.set( 0, 1, 0 );
  54. controls.enablePan = true;
  55. controls.minDistance = 1;
  56. controls.maxDistance = 6;
  57. controls.update();
  58. //
  59. postProcessing = new THREE.PostProcessing( renderer );
  60. const scenePass = pass( scene, camera );
  61. scenePass.setMRT( mrt( {
  62. output: output,
  63. diffuseColor: diffuseColor,
  64. normal: directionToColor( normalView ),
  65. velocity: velocity
  66. } ) );
  67. const scenePassColor = scenePass.getTextureNode( 'output' );
  68. const scenePassDiffuse = scenePass.getTextureNode( 'diffuseColor' );
  69. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  70. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  71. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  72. // bandwidth optimization
  73. const diffuseTexture = scenePass.getTexture( 'diffuseColor' );
  74. diffuseTexture.type = THREE.UnsignedByteType;
  75. const normalTexture = scenePass.getTexture( 'normal' );
  76. normalTexture.type = THREE.UnsignedByteType;
  77. const sceneNormal = sample( ( uv ) => {
  78. return colorToDirection( scenePassNormal.sample( uv ) );
  79. } );
  80. // gi
  81. const giPass = ssgi( scenePassColor, scenePassDepth, sceneNormal, camera );
  82. giPass.sliceCount.value = 2;
  83. giPass.stepCount.value = 8;
  84. // composite
  85. const gi = giPass.rgb;
  86. const ao = giPass.a;
  87. const compositePass = vec4( add( scenePassColor.rgb.mul( ao ), ( scenePassDiffuse.rgb.mul( gi ) ) ), scenePassColor.a );
  88. // traa
  89. const traaPass = traa( compositePass, scenePassDepth, scenePassVelocity, camera );
  90. postProcessing.outputNode = traaPass;
  91. //
  92. const dracoLoader = new DRACOLoader();
  93. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  94. dracoLoader.setDecoderConfig( { type: 'js' } );
  95. const loader = new GLTFLoader();
  96. loader.setDRACOLoader( dracoLoader );
  97. loader.setPath( 'models/gltf/' );
  98. const gltf = await loader.loadAsync( 'mirrors_edge.glb' );
  99. const model = gltf.scene;
  100. scene.add( model );
  101. window.addEventListener( 'resize', onWindowResize );
  102. //
  103. const params = {
  104. output: 0
  105. };
  106. const types = { Default: 0, AO: 1, GI: 2, Beauty: 3 };
  107. const gui = new GUI();
  108. gui.title( 'SSGI settings' );
  109. gui.add( params, 'output', types ).onChange( updatePostprocessing );
  110. gui.add( giPass.sliceCount, 'value', 1, 4 ).step( 1 ).name( 'slice count' );
  111. gui.add( giPass.stepCount, 'value', 1, 32 ).step( 1 ).name( 'step count' );
  112. gui.add( giPass.radius, 'value', 1, 25 ).name( 'radius' );
  113. gui.add( giPass.expFactor, 'value', 1, 3 ).name( 'exp factor' );
  114. gui.add( giPass.thickness, 'value', 0.01, 10 ).name( 'thickness' );
  115. gui.add( giPass.backfaceLighting, 'value', 0, 1 ).name( 'backface lighting' );
  116. gui.add( giPass.aoIntensity, 'value', 0, 4 ).name( 'AO intenstiy' );
  117. gui.add( giPass.giIntensity, 'value', 0, 100 ).name( 'GI intenstiy' );
  118. gui.add( giPass.useLinearThickness, 'value' ).name( 'use linear thickness' );
  119. gui.add( giPass.useScreenSpaceSampling, 'value' ).name( 'screen-space sampling' );
  120. gui.add( giPass, 'useTemporalFiltering' ).name( 'temporal filtering' ).onChange( updatePostprocessing );
  121. function updatePostprocessing( value ) {
  122. if ( value === 1 ) {
  123. postProcessing.outputNode = vec4( vec3( ao ), 1 );
  124. } else if ( value === 2 ) {
  125. postProcessing.outputNode = vec4( gi, 1 );
  126. } else if ( value === 3 ) {
  127. postProcessing.outputNode = scenePassColor;
  128. } else {
  129. postProcessing.outputNode = giPass.useTemporalFiltering ? traaPass : compositePass;
  130. }
  131. postProcessing.needsUpdate = true;
  132. }
  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号