webgpu_postprocessing_sss.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, output, velocity } 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. postProcessing = new THREE.PostProcessing( renderer );
  100. const scenePass = pass( scene, camera );
  101. scenePass.setMRT( mrt( {
  102. output: output,
  103. velocity: velocity
  104. } ) );
  105. const scenePassColor = scenePass.getTextureNode( 'output' );
  106. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  107. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  108. // sss
  109. const sssPass = sss( scenePassDepth, camera, dirLight );
  110. sssPass.shadowIntensity.value = 0.3;
  111. sssPass.maxDistance.value = 0.2;
  112. sssPass.useTemporalFiltering = true;
  113. // composite
  114. const compositePass = vec4( scenePassColor.rgb.mul( sssPass.r ), scenePassColor.a );
  115. compositePass.name = 'Composite';
  116. // traa
  117. const traaPass = traa( compositePass, scenePassDepth, scenePassVelocity, camera );
  118. postProcessing.outputNode = traaPass;
  119. //
  120. controls = new OrbitControls( camera, renderer.domElement );
  121. controls.minDistance = 1;
  122. controls.maxDistance = 20;
  123. controls.target.set( 0, 2, 0 );
  124. controls.enableDamping = true;
  125. controls.update();
  126. //
  127. const params = {
  128. output: 0
  129. };
  130. const types = { 'Scene with Shadow Maps + SSS': 0, 'Scene with Shadow Maps': 1, 'SSS': 2, };
  131. const gui = renderer.inspector.createParameters( 'SSS settings' );
  132. gui.add( params, 'output', types ).onChange( updatePostprocessing );
  133. gui.add( sssPass.shadowIntensity, 'value', 0, 1 ).name( 'shadow intensity' );
  134. gui.add( sssPass.maxDistance, 'value', 0.01, 1 ).name( 'max ray distance' );
  135. gui.add( sssPass.quality, 'value', 0, 1 ).name( 'quality' );
  136. gui.add( sssPass.thickness, 'value', 0.01, 0.1 ).name( 'thickness' );
  137. gui.add( sssPass, 'useTemporalFiltering' ).name( 'temporal filtering' ).onChange( updatePostprocessing );
  138. function updatePostprocessing() {
  139. if ( params.output === 1 ) {
  140. postProcessing.outputNode = scenePassColor;
  141. } else if ( params.output === 2 ) {
  142. postProcessing.outputNode = vec4( vec3( sssPass.r ), 1 );
  143. } else {
  144. postProcessing.outputNode = sssPass.useTemporalFiltering ? traaPass : compositePass;
  145. }
  146. postProcessing.needsUpdate = true;
  147. }
  148. window.addEventListener( 'resize', onWindowResize );
  149. }
  150. function onWindowResize() {
  151. const width = window.innerWidth;
  152. const height = window.innerHeight;
  153. camera.aspect = width / height;
  154. camera.updateProjectionMatrix();
  155. renderer.setSize( width, height );
  156. }
  157. function animate() {
  158. controls.update();
  159. postProcessing.render();
  160. }
  161. </script>
  162. </body>
  163. </html>
粤ICP备19079148号