webgpu_postprocessing_ssgi.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 } 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: 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. // gi
  73. const giPass = ssgi( scenePassColor, scenePassDepth, scenePassNormal, camera );
  74. giPass.sliceCount.value = 2;
  75. giPass.stepCount.value = 8;
  76. // composite
  77. const gi = giPass.rgb;
  78. const ao = giPass.a;
  79. const compositePass = vec4( add ( scenePassColor.rgb.mul( ao ), ( scenePassDiffuse.rgb.mul( gi ) ) ), scenePassColor.a );
  80. // traa
  81. const traaPass = traa( compositePass, scenePassDepth, scenePassVelocity, camera );
  82. postProcessing.outputNode = traaPass;
  83. //
  84. const dracoLoader = new DRACOLoader();
  85. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  86. dracoLoader.setDecoderConfig( { type: 'js' } );
  87. const loader = new GLTFLoader();
  88. loader.setDRACOLoader( dracoLoader );
  89. loader.setPath( 'models/gltf/' );
  90. const gltf = await loader.loadAsync( 'mirrors_edge.glb' );
  91. const model = gltf.scene;
  92. scene.add( model );
  93. window.addEventListener( 'resize', onWindowResize );
  94. //
  95. const params = {
  96. output: 0
  97. };
  98. const types = { Default: 0, AO: 1, GI: 2, Beauty: 3 };
  99. const gui = new GUI();
  100. gui.title( 'SSGI settings' );
  101. let outputDropdown = gui.add( params, 'output', types );
  102. outputDropdown.onChange( value => {
  103. if ( value === 1 ) {
  104. postProcessing.outputNode = vec4( vec3( ao ), 1 );
  105. } else if ( value === 2 ) {
  106. postProcessing.outputNode = vec4( gi, 1 );
  107. } else if ( value === 3 ) {
  108. postProcessing.outputNode = scenePassColor;
  109. } else {
  110. postProcessing.outputNode = giPass.useTemporalFiltering ? traaPass : compositePass;
  111. }
  112. postProcessing.needsUpdate = true;
  113. } );
  114. gui.add( giPass.sliceCount, 'value', 1, 4 ).step( 1 ).name( 'slice count' );
  115. gui.add( giPass.stepCount, 'value', 1, 32 ).step( 1 ).name( 'step count' );
  116. gui.add( giPass.radius, 'value', 1, 25 ).name( 'radius' );
  117. gui.add( giPass.expFactor, 'value', 1, 3 ).name( 'exp factor' );
  118. gui.add( giPass.thickness, 'value', 0.01, 10 ).name( 'thickness' );
  119. gui.add( giPass.backfaceLighting, 'value', 0, 1 ).name( 'backface lighting' );
  120. gui.add( giPass.aoIntensity, 'value', 0, 4 ).name( 'AO intenstiy' );
  121. gui.add( giPass.giIntensity, 'value', 0, 100 ).name( 'GI intenstiy' );
  122. gui.add( giPass.useLinearThickness, 'value' ).name( 'use linear thickness' );
  123. gui.add( giPass.useScreenSpaceSampling, 'value' ).name( 'screen-space sampling' );
  124. gui.add( giPass, 'useTemporalFiltering' ).name( 'temporal filtering' ).onChange( outputDropdown._onChange );
  125. }
  126. function onWindowResize() {
  127. const width = window.innerWidth;
  128. const height = window.innerHeight;
  129. camera.aspect = width / height;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( width, height );
  132. }
  133. function animate() {
  134. controls.update();
  135. stats.update();
  136. postProcessing.render();
  137. }
  138. </script>
  139. </body>
  140. </html>
粤ICP备19079148号