1
0

webgpu_postprocessing_ssgi.html 8.9 KB

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