1
0

webgpu_postprocessing_ssr.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <head>
  5. <title>three.js webgpu - postprocessing - Screen Space Reflections (SSR)</title>
  6. <meta charset="utf-8">
  7. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  8. <link type="text/css" rel="stylesheet" href="main.css">
  9. </head>
  10. <body>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - postprocessing - screen space reflections<br />
  13. <a href="https://skfb.ly/6tqYD" target="_blank" rel="noopener">Steampunk Camera</a> by
  14. <a href="https://sketchfab.com/lumoize" target="_blank" rel="noopener">dylanheyes</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</a>.<br />
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/webgpu": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.tsl.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three/webgpu';
  28. import { pass, mrt, output, normalView, metalness, roughness, blendColor, screenUV, color, sample, directionToColor, colorToDirection, vec2 } from 'three/tsl';
  29. import { ssr } from 'three/addons/tsl/display/SSRNode.js';
  30. import { smaa } from 'three/addons/tsl/display/SMAANode.js';
  31. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  32. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. const params = {
  38. quality: 0.5,
  39. maxDistance: 0.5,
  40. opacity: 1,
  41. thickness: 0.015,
  42. enabled: true
  43. };
  44. let camera, scene, renderer, postProcessing, ssrPass;
  45. let gui, stats, controls;
  46. init();
  47. async function init() {
  48. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 50 );
  49. camera.position.set( 3, 2, 3 );
  50. scene = new THREE.Scene();
  51. scene.backgroundNode = screenUV.distance( .5 ).remap( 0, 0.5 ).mix( color( 0x888877 ), color( 0x776666 ) );
  52. const dracoLoader = new DRACOLoader();
  53. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  54. dracoLoader.setDecoderConfig( { type: 'js' } );
  55. const loader = new GLTFLoader();
  56. loader.setDRACOLoader( dracoLoader );
  57. loader.load( 'models/gltf/steampunk_camera.glb', function ( gltf ) {
  58. gltf.scene.traverse( function ( object ) {
  59. if ( object.material ) {
  60. if ( object.material.name === 'Lense_Casing' ) {
  61. object.material.transparent = true;
  62. }
  63. // Avoid overdrawing
  64. object.material.side = THREE.FrontSide;
  65. }
  66. } );
  67. gltf.scene.position.y = 0.1;
  68. scene.add( gltf.scene );
  69. } );
  70. //
  71. renderer = new THREE.WebGPURenderer();
  72. // renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.setAnimationLoop( animate );
  75. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  76. document.body.appendChild( renderer.domElement );
  77. await renderer.init();
  78. const environment = new RoomEnvironment();
  79. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  80. scene.environment = pmremGenerator.fromScene( environment ).texture;
  81. scene.environmentIntensity = 1.25;
  82. pmremGenerator.dispose();
  83. //
  84. postProcessing = new THREE.PostProcessing( renderer );
  85. const scenePass = pass( scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter } );
  86. scenePass.setMRT( mrt( {
  87. output: output,
  88. normal: directionToColor( normalView ),
  89. metalrough: vec2( metalness, roughness )
  90. } ) );
  91. const scenePassColor = scenePass.getTextureNode( 'output' );
  92. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  93. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  94. const scenePassMetalRough = scenePass.getTextureNode( 'metalrough' );
  95. // optimization bandwidth packing the normals and reducing the texture precision if possible
  96. const normalTexture = scenePass.getTexture( 'normal' );
  97. normalTexture.type = THREE.UnsignedByteType;
  98. const metalRoughTexture = scenePass.getTexture( 'metalrough' );
  99. metalRoughTexture.type = THREE.UnsignedByteType;
  100. const customNormal = sample( ( uv ) => {
  101. return colorToDirection( scenePassNormal.sample( uv ) );
  102. } );
  103. const customMetalness = sample( ( uv ) => {
  104. return scenePassMetalRough.sample( uv ).r;
  105. } );
  106. //
  107. ssrPass = ssr( scenePassColor, scenePassDepth, customNormal, customMetalness, camera );
  108. // blend SSR over beauty
  109. const outputNode = smaa( blendColor( scenePassColor, ssrPass.mul( scenePassMetalRough.g.oneMinus() ) ) );
  110. postProcessing.outputNode = outputNode;
  111. //
  112. controls = new OrbitControls( camera, renderer.domElement );
  113. controls.enableDamping = true;
  114. controls.update();
  115. // stats
  116. stats = new Stats();
  117. document.body.appendChild( stats.dom );
  118. window.addEventListener( 'resize', onWindowResize );
  119. // GUI
  120. gui = new GUI();
  121. gui.add( params, 'quality' ).min( 0 ).max( 1 ).onChange( updateParameters );
  122. gui.add( params, 'maxDistance' ).min( 0 ).max( 1 ).onChange( updateParameters );
  123. gui.add( params, 'opacity' ).min( 0 ).max( 1 ).onChange( updateParameters );
  124. gui.add( params, 'thickness' ).min( 0 ).max( 0.05 ).onChange( updateParameters );
  125. gui.add( params, 'enabled' ).onChange( () => {
  126. if ( params.enabled === true ) {
  127. postProcessing.outputNode = outputNode;
  128. } else {
  129. postProcessing.outputNode = scenePass;
  130. }
  131. postProcessing.needsUpdate = true;
  132. } );
  133. updateParameters();
  134. }
  135. function updateParameters() {
  136. ssrPass.quality.value = params.quality;
  137. ssrPass.maxDistance.value = params.maxDistance;
  138. ssrPass.opacity.value = params.opacity;
  139. ssrPass.thickness.value = params.thickness;
  140. }
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. function animate() {
  147. stats.begin();
  148. controls.update();
  149. postProcessing.render();
  150. stats.end();
  151. }
  152. </script>
  153. </body>
  154. </html>
粤ICP备19079148号