webgpu_postprocessing_ssgi.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three/webgpu';
  25. import { pass, mrt, output, normalView, diffuseColor, velocity, add, vec3, vec4, directionToColor, colorToDirection, sample } from 'three/tsl';
  26. import { ssgi } from 'three/addons/tsl/display/SSGINode.js';
  27. import { traa } from 'three/addons/tsl/display/TRAANode.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import Stats from 'three/addons/libs/stats.module.js';
  31. let camera, scene, renderer, postProcessing, controls, stats;
  32. init();
  33. async function init() {
  34. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  35. camera.position.set( 0, 10, 30 );
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0xaaaaaa );
  38. renderer = new THREE.WebGPURenderer();
  39. //renderer.setPixelRatio( window.devicePixelRatio ); // probably too costly for most hardware
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. renderer.setAnimationLoop( animate );
  42. renderer.shadowMap.enabled = true;
  43. document.body.appendChild( renderer.domElement );
  44. stats = new Stats();
  45. document.body.appendChild( stats.domElement );
  46. //
  47. controls = new OrbitControls( camera, renderer.domElement );
  48. controls.target.set( 0, 7, 0 );
  49. controls.enablePan = true;
  50. controls.minDistance = 1;
  51. controls.maxDistance = 100;
  52. controls.update();
  53. //
  54. postProcessing = new THREE.PostProcessing( renderer );
  55. const scenePass = pass( scene, camera );
  56. scenePass.setMRT( mrt( {
  57. output: output,
  58. diffuseColor: diffuseColor,
  59. normal: directionToColor( normalView ),
  60. velocity: velocity
  61. } ) );
  62. const scenePassColor = scenePass.getTextureNode( 'output' );
  63. const scenePassDiffuse = scenePass.getTextureNode( 'diffuseColor' );
  64. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  65. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  66. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  67. // bandwidth optimization
  68. const diffuseTexture = scenePass.getTexture( 'diffuseColor' );
  69. diffuseTexture.type = THREE.UnsignedByteType;
  70. const normalTexture = scenePass.getTexture( 'normal' );
  71. normalTexture.type = THREE.UnsignedByteType;
  72. const sceneNormal = sample( ( uv ) => {
  73. return colorToDirection( scenePassNormal.sample( uv ) );
  74. } );
  75. // gi
  76. const giPass = ssgi( scenePassColor, scenePassDepth, sceneNormal, camera );
  77. giPass.sliceCount.value = 2;
  78. giPass.stepCount.value = 8;
  79. // composite
  80. const gi = giPass.rgb;
  81. const ao = giPass.a;
  82. const compositePass = vec4( add( scenePassColor.rgb.mul( ao ), ( scenePassDiffuse.rgb.mul( gi ) ) ), scenePassColor.a );
  83. // traa
  84. const traaPass = traa( compositePass, scenePassDepth, scenePassVelocity, camera );
  85. postProcessing.outputNode = traaPass;
  86. // Cornell Box inspired scene
  87. // Walls
  88. const wallGeometry = new THREE.PlaneGeometry( 1, 1 );
  89. // Left wall - red
  90. const redWallMaterial = new THREE.MeshPhysicalMaterial( { color: '#ff0000' } );
  91. const leftWall = new THREE.Mesh( wallGeometry, redWallMaterial );
  92. leftWall.scale.set( 20, 15, 1 );
  93. leftWall.rotation.y = Math.PI * 0.5;
  94. leftWall.position.set( - 10, 7.5, 0 );
  95. leftWall.receiveShadow = true;
  96. scene.add( leftWall );
  97. // Right wall - green
  98. const greenWallMaterial = new THREE.MeshPhysicalMaterial( { color: '#00ff00' } );
  99. const rightWall = new THREE.Mesh( wallGeometry, greenWallMaterial );
  100. rightWall.scale.set( 20, 15, 1 );
  101. rightWall.rotation.y = Math.PI * - 0.5;
  102. rightWall.position.set( 10, 7.5, 0 );
  103. rightWall.receiveShadow = true;
  104. scene.add( rightWall );
  105. // White walls and boxes
  106. const whiteMaterial = new THREE.MeshPhysicalMaterial( { color: '#fff' } );
  107. // Floor
  108. const floor = new THREE.Mesh( wallGeometry, whiteMaterial );
  109. floor.scale.set( 20, 20, 1 );
  110. floor.rotation.x = Math.PI * - .5;
  111. floor.receiveShadow = true;
  112. scene.add( floor );
  113. // Back wall
  114. const backWall = new THREE.Mesh( wallGeometry, whiteMaterial );
  115. backWall.scale.set( 15, 20, 1 );
  116. backWall.rotation.z = Math.PI * - 0.5;
  117. backWall.position.set( 0, 7.5, - 10 );
  118. backWall.receiveShadow = true;
  119. scene.add( backWall );
  120. // Ceiling
  121. const ceiling = new THREE.Mesh( wallGeometry, whiteMaterial );
  122. ceiling.scale.set( 20, 20, 1 );
  123. ceiling.rotation.x = Math.PI * 0.5;
  124. ceiling.position.set( 0, 15, 0 );
  125. ceiling.receiveShadow = true;
  126. scene.add( ceiling );
  127. // Boxes
  128. const tallBoxGeometry = new THREE.BoxGeometry( 5, 7, 5 );
  129. const tallBox = new THREE.Mesh( tallBoxGeometry, whiteMaterial );
  130. tallBox.rotation.y = Math.PI * 0.25;
  131. tallBox.position.set( - 3, 3.5, - 2 );
  132. tallBox.castShadow = true;
  133. tallBox.receiveShadow = true;
  134. scene.add( tallBox );
  135. const shortBoxGeometry = new THREE.BoxGeometry( 4, 4, 4 );
  136. const shortBox = new THREE.Mesh( shortBoxGeometry, whiteMaterial );
  137. shortBox.rotation.y = Math.PI * - 0.1;
  138. shortBox.position.set( 4, 2, 4 );
  139. shortBox.castShadow = true;
  140. shortBox.receiveShadow = true;
  141. scene.add( shortBox );
  142. // Light source geometry
  143. const lightSourceGeometry = new THREE.CylinderGeometry( 2.5, 2.5, 1, 64 );
  144. const lightSourceMaterial = new THREE.MeshBasicMaterial();
  145. const lightSource = new THREE.Mesh( lightSourceGeometry, lightSourceMaterial );
  146. lightSource.position.y = 15;
  147. scene.add( lightSource );
  148. // Point light
  149. const pointLight = new THREE.PointLight( '#ffffff', 100 );
  150. pointLight.position.set( 0, 13, 0 );
  151. pointLight.distance = 100;
  152. pointLight.castShadow = true;
  153. pointLight.shadow.mapSize.width = 1024;
  154. pointLight.shadow.mapSize.height = 1024;
  155. pointLight.shadow.bias = - 0.0025;
  156. scene.add( pointLight );
  157. // Ambient light
  158. const ambientLight = new THREE.AmbientLight( '#0c0c0c' );
  159. scene.add( ambientLight );
  160. window.addEventListener( 'resize', onWindowResize );
  161. //
  162. const params = {
  163. output: 0
  164. };
  165. const types = { Combined: 0, Direct: 3, AO: 1, GI: 2 };
  166. const gui = new GUI();
  167. gui.title( 'SSGI settings' );
  168. gui.add( params, 'output', types ).onChange( updatePostprocessing );
  169. gui.add( giPass.sliceCount, 'value', 1, 4 ).step( 1 ).name( 'slice count' );
  170. gui.add( giPass.stepCount, 'value', 1, 32 ).step( 1 ).name( 'step count' );
  171. gui.add( giPass.radius, 'value', 1, 25 ).name( 'radius' );
  172. gui.add( giPass.expFactor, 'value', 1, 3 ).name( 'exp factor' );
  173. gui.add( giPass.thickness, 'value', 0.01, 10 ).name( 'thickness' );
  174. gui.add( giPass.backfaceLighting, 'value', 0, 1 ).name( 'backface lighting' );
  175. gui.add( giPass.aoIntensity, 'value', 0, 4 ).name( 'AO intenstiy' );
  176. gui.add( giPass.giIntensity, 'value', 0, 100 ).name( 'GI intenstiy' );
  177. gui.add( giPass.useLinearThickness, 'value' ).name( 'use linear thickness' );
  178. gui.add( giPass.useScreenSpaceSampling, 'value' ).name( 'screen-space sampling' );
  179. gui.add( giPass, 'useTemporalFiltering' ).name( 'temporal filtering' ).onChange( updatePostprocessing );
  180. function updatePostprocessing() {
  181. const value = params.output;
  182. if ( value === 1 ) {
  183. postProcessing.outputNode = vec4( vec3( ao ), 1 );
  184. } else if ( value === 2 ) {
  185. postProcessing.outputNode = vec4( gi, 1 );
  186. } else if ( value === 3 ) {
  187. postProcessing.outputNode = scenePassColor;
  188. } else {
  189. postProcessing.outputNode = giPass.useTemporalFiltering ? traaPass : compositePass;
  190. }
  191. postProcessing.needsUpdate = true;
  192. }
  193. }
  194. function onWindowResize() {
  195. const width = window.innerWidth;
  196. const height = window.innerHeight;
  197. camera.aspect = width / height;
  198. camera.updateProjectionMatrix();
  199. renderer.setSize( width, height );
  200. }
  201. function animate() {
  202. controls.update();
  203. stats.update();
  204. postProcessing.render();
  205. }
  206. </script>
  207. </body>
  208. </html>
粤ICP备19079148号