webgpu_postprocessing_sss.html 6.8 KB

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