webgpu_postprocessing_ssgi.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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, 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. renderer = new THREE.WebGPURenderer();
  42. //renderer.setPixelRatio( window.devicePixelRatio ); // probably too costly for most hardware
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. renderer.toneMapping = THREE.NeutralToneMapping;
  46. renderer.toneMappingExposure = 1.5;
  47. document.body.appendChild( renderer.domElement );
  48. stats = new Stats();
  49. document.body.appendChild( stats.domElement );
  50. //
  51. controls = new OrbitControls( camera, renderer.domElement );
  52. controls.target.set( 0, 1, 0 );
  53. controls.enablePan = true;
  54. controls.minDistance = 1;
  55. controls.maxDistance = 6;
  56. controls.update();
  57. //
  58. postProcessing = new THREE.PostProcessing( renderer );
  59. const scenePass = pass( scene, camera );
  60. scenePass.setMRT( mrt( {
  61. output: output,
  62. normal: normalView,
  63. velocity: velocity
  64. } ) );
  65. const scenePassColor = scenePass.getTextureNode( 'output' );
  66. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  67. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  68. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  69. // gi
  70. const giPass = ssgi( scenePassColor, scenePassDepth, scenePassNormal, camera );
  71. giPass.sliceCount.value = 2;
  72. giPass.stepCount.value = 8;
  73. // composite
  74. const gi = giPass.rgb;
  75. const ao = giPass.a;
  76. const compositePass = vec4( add( scenePassColor.rgb, gi ).mul( ao ), scenePassColor.a );
  77. // traa
  78. const traaPass = traa( compositePass, scenePassDepth, scenePassVelocity, camera );
  79. postProcessing.outputNode = traaPass;
  80. //
  81. const dracoLoader = new DRACOLoader();
  82. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  83. dracoLoader.setDecoderConfig( { type: 'js' } );
  84. const loader = new GLTFLoader();
  85. loader.setDRACOLoader( dracoLoader );
  86. loader.setPath( 'models/gltf/' );
  87. const gltf = await loader.loadAsync( 'mirrors_edge.glb' );
  88. const model = gltf.scene;
  89. scene.add( model );
  90. window.addEventListener( 'resize', onWindowResize );
  91. //
  92. const gui = new GUI();
  93. gui.title( 'SSGI settings' );
  94. gui.add( giPass.sliceCount, 'value', 1, 4 ).step( 1 ).name( 'slice count' );
  95. gui.add( giPass.stepCount, 'value', 1, 32 ).step( 1 ).name( 'step count' );
  96. gui.add( giPass.radius, 'value', 1, 25 ).name( 'radius' );
  97. gui.add( giPass.expFactor, 'value', 1, 3 ).name( 'exp factor' );
  98. gui.add( giPass.thickness, 'value', 0.01, 10 ).name( 'thickness' );
  99. gui.add( giPass.backfaceLighting, 'value', 0, 1 ).name( 'backface lighting' );
  100. gui.add( giPass.aoIntensity, 'value', 0, 4 ).name( 'AO intenstiy' );
  101. gui.add( giPass.giIntensity, 'value', 0, 100 ).name( 'GI intenstiy' );
  102. gui.add( giPass.useLinearThickness, 'value' ).name( 'use linear thickness' );
  103. gui.add( giPass.useScreenSpaceSampling, 'value' ).name( 'screen-space sampling' );
  104. gui.add( giPass, 'useTemporalFiltering' ).name( 'temporal filtering' );
  105. const params = {
  106. output: 0
  107. };
  108. const types = { Default: 0, AO: 1, GI: 2, Beauty: 3 };
  109. gui.add( params, 'output', types ).onChange( value => {
  110. if ( value === 1 ) {
  111. postProcessing.outputNode = vec4( vec3( ao ), 1 );
  112. } else if ( value === 2 ) {
  113. postProcessing.outputNode = vec4( gi, 1 );
  114. } else if ( value === 3 ) {
  115. postProcessing.outputNode = scenePassColor;
  116. } else {
  117. postProcessing.outputNode = traaPass;
  118. }
  119. postProcessing.needsUpdate = true;
  120. } );
  121. }
  122. function onWindowResize() {
  123. const width = window.innerWidth;
  124. const height = window.innerHeight;
  125. camera.aspect = width / height;
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( width, height );
  128. }
  129. function animate() {
  130. controls.update();
  131. stats.update();
  132. postProcessing.render();
  133. }
  134. </script>
  135. </body>
  136. </html>
粤ICP备19079148号