webgpu_postprocessing_ssr.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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';
  28. import { pass, mrt, output, normalView, metalness, blendColor, screenUV, color, sample, directionToColor, colorToDirection } 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. maxDistance: 0.5,
  39. opacity: 1,
  40. thickness: 0.015,
  41. enabled: true
  42. };
  43. let camera, scene, renderer, postProcessing, ssrPass;
  44. let gui, stats, controls;
  45. init();
  46. async function init() {
  47. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 50 );
  48. camera.position.set( 3, 2, 3 );
  49. scene = new THREE.Scene();
  50. scene.backgroundNode = screenUV.distance( .5 ).remap( 0, 0.5 ).mix( color( 0x888877 ), color( 0x776666 ) );
  51. const dracoLoader = new DRACOLoader();
  52. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  53. dracoLoader.setDecoderConfig( { type: 'js' } );
  54. const loader = new GLTFLoader();
  55. loader.setDRACOLoader( dracoLoader );
  56. loader.load( 'models/gltf/steampunk_camera.glb', function ( gltf ) {
  57. gltf.scene.traverse( function ( object ) {
  58. if ( object.material ) {
  59. // Avoid overdrawing
  60. object.material.side = THREE.FrontSide;
  61. }
  62. } );
  63. gltf.scene.position.y = 0.1;
  64. scene.add( gltf.scene );
  65. } );
  66. //
  67. renderer = new THREE.WebGPURenderer();
  68. // renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.setAnimationLoop( animate );
  71. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  72. document.body.appendChild( renderer.domElement );
  73. await renderer.init();
  74. const environment = new RoomEnvironment();
  75. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  76. scene.environment = pmremGenerator.fromScene( environment ).texture;
  77. scene.environmentIntensity = 1.25;
  78. pmremGenerator.dispose();
  79. //
  80. postProcessing = new THREE.PostProcessing( renderer );
  81. const scenePass = pass( scene, camera, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter } );
  82. scenePass.setMRT( mrt( {
  83. output: output,
  84. normal: directionToColor( normalView ),
  85. metalness: metalness
  86. } ) );
  87. const scenePassColor = scenePass.getTextureNode( 'output' );
  88. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  89. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  90. const scenePassMetalness = scenePass.getTextureNode( 'metalness' );
  91. // optimization bandwidth packing the normals and reducing the texture precision if possible
  92. const metalnessTexture = scenePass.getTexture( 'metalness' );
  93. metalnessTexture.type = THREE.UnsignedByteType;
  94. const normalTexture = scenePass.getTexture( 'normal' );
  95. normalTexture.type = THREE.UnsignedByteType;
  96. const customNormal = sample( ( uv ) => {
  97. return colorToDirection( scenePassNormal.sample( uv ) );
  98. } );
  99. //
  100. ssrPass = ssr( scenePassColor, scenePassDepth, customNormal, scenePassMetalness, camera );
  101. ssrPass.resolutionScale = 1.0;
  102. // blend SSR over beauty
  103. const outputNode = smaa( blendColor( scenePassColor, ssrPass ) );
  104. postProcessing.outputNode = outputNode;
  105. //
  106. controls = new OrbitControls( camera, renderer.domElement );
  107. controls.enableDamping = true;
  108. controls.update();
  109. // stats
  110. stats = new Stats();
  111. document.body.appendChild( stats.dom );
  112. window.addEventListener( 'resize', onWindowResize );
  113. // GUI
  114. gui = new GUI();
  115. gui.add( params, 'maxDistance' ).min( 0 ).max( 1 ).onChange( updateParameters );
  116. gui.add( params, 'opacity' ).min( 0 ).max( 1 ).onChange( updateParameters );
  117. gui.add( params, 'thickness' ).min( 0 ).max( 0.05 ).onChange( updateParameters );
  118. gui.add( params, 'enabled' ).onChange( () => {
  119. if ( params.enabled === true ) {
  120. postProcessing.outputNode = outputNode;
  121. } else {
  122. postProcessing.outputNode = scenePass;
  123. }
  124. postProcessing.needsUpdate = true;
  125. } );
  126. updateParameters();
  127. }
  128. function updateParameters() {
  129. ssrPass.maxDistance.value = params.maxDistance;
  130. ssrPass.opacity.value = params.opacity;
  131. ssrPass.thickness.value = params.thickness;
  132. }
  133. function onWindowResize() {
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. function animate() {
  139. stats.begin();
  140. controls.update();
  141. postProcessing.render();
  142. stats.end();
  143. }
  144. </script>
  145. </body>
  146. </html>
粤ICP备19079148号