webgpu_postprocessing_ssgi.html 9.2 KB

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