webgpu_postprocessing_ssgi.html 8.8 KB

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