webgpu_postprocessing_sss.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - SSS</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 - SSS">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_sss.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_sss.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>SSS</span>
  18. </div>
  19. <small>
  20. Screen-Space Shadows (SSS) combined with Shadow Maps.<br/>
  21. <a href="https://skfb.ly/pAQvU" target="_blank" rel="noopener">Nemetona_NatureBeauty</a> by
  22. <a href="https://sketchfab.com/JOJObrush" target="_blank" rel="noopener">JOJObrush</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</a>.<br />
  23. </small>
  24. </div>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.webgpu.js",
  29. "three/webgpu": "../build/three.webgpu.js",
  30. "three/tsl": "../build/three.tsl.js",
  31. "three/addons/": "./jsm/"
  32. }
  33. }
  34. </script>
  35. <script type="module">
  36. import * as THREE from 'three/webgpu';
  37. import { pass, vec3, vec4, mrt, screenUV, velocity, builtinShadowContext } from 'three/tsl';
  38. import { sss } from 'three/addons/tsl/display/SSSNode.js';
  39. import { traa } from 'three/addons/tsl/display/TRAANode.js';
  40. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  41. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  42. import { DRACOLoader, DRACO_GLTF_CONFIG } from 'three/addons/loaders/DRACOLoader.js';
  43. import { Inspector } from 'three/addons/inspector/Inspector.js';
  44. let camera, scene, renderer, renderPipeline, controls;
  45. init();
  46. async function init() {
  47. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  48. camera.position.set( 1, 2.5, - 3.5 );
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0xa0a0a0 );
  51. scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
  52. // lights
  53. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 2 );
  54. hemiLight.position.set( 0, 20, 0 );
  55. scene.add( hemiLight );
  56. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  57. dirLight.position.set( - 3, 10, - 10 );
  58. dirLight.castShadow = true;
  59. dirLight.shadow.bias = - 0.001; // remove self-shadowing artifacts
  60. dirLight.shadow.camera.top = 4;
  61. dirLight.shadow.camera.bottom = - 4;
  62. dirLight.shadow.camera.left = - 4;
  63. dirLight.shadow.camera.right = 4;
  64. dirLight.shadow.camera.near = 0.1;
  65. dirLight.shadow.camera.far = 40;
  66. dirLight.shadow.mapSize.width = 1024;
  67. dirLight.shadow.mapSize.height = 1024;
  68. scene.add( dirLight );
  69. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  70. // ground
  71. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  72. mesh.rotation.x = - Math.PI / 2;
  73. mesh.receiveShadow = true;
  74. scene.add( mesh );
  75. //
  76. const dracoLoader = new DRACOLoader();
  77. dracoLoader.setDecoderPath( DRACO_GLTF_CONFIG );
  78. const loader = new GLTFLoader();
  79. loader.setDRACOLoader( dracoLoader );
  80. loader.load( 'models/gltf/nemetona.glb', function ( gltf ) {
  81. const model = gltf.scene;
  82. model.rotation.y = Math.PI;
  83. model.scale.setScalar( 10 );
  84. model.position.y = 0.45;
  85. scene.add( model );
  86. model.traverse( function ( object ) {
  87. if ( object.isMesh ) {
  88. object.castShadow = true;
  89. object.receiveShadow = true;
  90. object.material.aoMap = null; // remove AO to better see the effect of shadows
  91. }
  92. } );
  93. } );
  94. //
  95. renderer = new THREE.WebGPURenderer();
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.setAnimationLoop( animate );
  99. renderer.shadowMap.enabled = true;
  100. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  101. renderer.inspector = new Inspector();
  102. document.body.appendChild( renderer.domElement );
  103. // post-processing
  104. renderPipeline = new THREE.RenderPipeline( renderer );
  105. // pre-pass
  106. const prePass = pass( scene, camera );
  107. prePass.name = 'Pre-Pass';
  108. prePass.transparent = false;
  109. prePass.setMRT( mrt( {
  110. output: velocity
  111. } ) );
  112. const prePassDepth = prePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => prePass.getLinearDepthNode() );
  113. const prePassVelocity = prePass.getTextureNode( 'output' ).toInspector( 'Velocity' );
  114. // scene pass
  115. const scenePass = pass( scene, camera ).toInspector( 'Color' );
  116. // sss
  117. const sssPass = sss( prePassDepth, camera, dirLight );
  118. sssPass.maxDistance.value = 0.2;
  119. sssPass.useTemporalFiltering = true;
  120. // scene context
  121. const sssSample = sssPass.getTextureNode().sample( screenUV ).r;
  122. const sssContext = builtinShadowContext( sssSample, dirLight );
  123. scenePass.contextNode = sssContext;
  124. // traa
  125. const traaPass = traa( scenePass, prePassDepth, prePassVelocity, camera );
  126. renderPipeline.outputNode = traaPass;
  127. //
  128. controls = new OrbitControls( camera, renderer.domElement );
  129. controls.minDistance = 1;
  130. controls.maxDistance = 20;
  131. controls.target.set( 0, 2, 0 );
  132. controls.enableDamping = true;
  133. controls.update();
  134. //
  135. const params = {
  136. output: 0
  137. };
  138. const types = { 'Scene with Shadow Maps + SSS': 0, 'Scene with Shadow Maps': 1, 'SSS': 2, };
  139. const gui = renderer.inspector.createParameters( 'SSS settings' );
  140. gui.add( params, 'output', types ).onChange( updatePostprocessing );
  141. gui.add( sssPass.shadowIntensity, 'value', 0, 1 ).name( 'shadow intensity' );
  142. gui.add( sssPass.maxDistance, 'value', 0.01, 1 ).name( 'max ray distance' );
  143. gui.add( sssPass.quality, 'value', 0, 1 ).name( 'quality' );
  144. gui.add( sssPass.thickness, 'value', 0.01, 0.1 ).name( 'thickness' );
  145. gui.add( sssPass, 'useTemporalFiltering' ).name( 'temporal filtering' ).onChange( updatePostprocessing );
  146. function updatePostprocessing() {
  147. scenePass.contextNode = params.output !== 1 ? sssContext : null;
  148. if ( params.output === 2 ) {
  149. renderPipeline.outputNode = vec4( vec3( sssPass.r ), 1 );
  150. } else {
  151. renderPipeline.outputNode = sssPass.useTemporalFiltering ? traaPass : scenePass;
  152. }
  153. renderPipeline.needsUpdate = true;
  154. }
  155. window.addEventListener( 'resize', onWindowResize );
  156. }
  157. function onWindowResize() {
  158. const width = window.innerWidth;
  159. const height = window.innerHeight;
  160. camera.aspect = width / height;
  161. camera.updateProjectionMatrix();
  162. renderer.setSize( width, height );
  163. }
  164. function animate() {
  165. controls.update();
  166. renderPipeline.render();
  167. }
  168. </script>
  169. </body>
  170. </html>
粤ICP备19079148号