webgpu_postprocessing_ssgi.html 9.6 KB

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