| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - SSS</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="example.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
- <div class="title-wrapper">
- <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>SSS</span>
- </div>
- <small>
- Screen-Space Shadows (SSS) combined with Shadow Maps.<br/>
- <a href="https://skfb.ly/pAQvU" target="_blank" rel="noopener">Nemetona_NatureBeauty</a> by
- <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 />
- </small>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/webgpu": "../build/three.webgpu.js",
- "three/tsl": "../build/three.tsl.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three/webgpu';
- import { pass, vec3, vec4, mrt, screenUV, velocity, builtinShadowContext } from 'three/tsl';
- import { sss } from 'three/addons/tsl/display/SSSNode.js';
- import { traa } from 'three/addons/tsl/display/TRAANode.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- let camera, scene, renderer, postProcessing, controls;
- init();
- async function init() {
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( 1, 2.5, - 3.5 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xa0a0a0 );
- scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
- // lights
- const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 2 );
- hemiLight.position.set( 0, 20, 0 );
- scene.add( hemiLight );
- const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
- dirLight.position.set( - 3, 10, - 10 );
- dirLight.castShadow = true;
- dirLight.shadow.camera.top = 4;
- dirLight.shadow.camera.bottom = - 4;
- dirLight.shadow.camera.left = - 4;
- dirLight.shadow.camera.right = 4;
- dirLight.shadow.camera.near = 0.1;
- dirLight.shadow.camera.far = 40;
- dirLight.shadow.bias = - 0.001;
- dirLight.shadow.mapSize.width = 1024;
- dirLight.shadow.mapSize.height = 1024;
- scene.add( dirLight );
- // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
- // ground
- const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
- mesh.rotation.x = - Math.PI / 2;
- mesh.receiveShadow = true;
- scene.add( mesh );
- //
- const dracoLoader = new DRACOLoader();
- dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
- const loader = new GLTFLoader();
- loader.setDRACOLoader( dracoLoader );
- loader.load( 'models/gltf/nemetona.glb', function ( gltf ) {
- const model = gltf.scene;
- model.rotation.y = Math.PI;
- model.scale.setScalar( 10 );
- model.position.y = 0.45;
-
- scene.add( model );
- model.traverse( function ( object ) {
- if ( object.isMesh ) {
-
- object.castShadow = true;
- object.receiveShadow = true;
- object.material.aoMap = null; // remove AO to better see the effect of shadows
- }
- } );
- } );
- //
- renderer = new THREE.WebGPURenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- renderer.inspector = new Inspector();
- document.body.appendChild( renderer.domElement );
- // post-processing
- postProcessing = new THREE.PostProcessing( renderer );
- // pre-pass
- const prePass = pass( scene, camera );
- prePass.name = 'Pre-Pass';
- prePass.transparent = false;
- prePass.setMRT( mrt( {
- output: velocity
- } ) );
- const prePassDepth = prePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => prePass.getLinearDepthNode() );
- const prePassVelocity = prePass.getTextureNode( 'output' ).toInspector( 'Velocity' );
- // scene pass
- const scenePass = pass( scene, camera ).toInspector( 'Color' );
- // sss
- const sssPass = sss( prePassDepth, camera, dirLight );
- sssPass.maxDistance.value = 0.2;
- sssPass.useTemporalFiltering = true;
- // scene context
- const sssSample = sssPass.getTextureNode().sample( screenUV ).r;
- const sssContext = builtinShadowContext( sssSample, dirLight );
- scenePass.contextNode = sssContext;
- // traa
- const traaPass = traa( scenePass, prePassDepth, prePassVelocity, camera );
- postProcessing.outputNode = traaPass;
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 1;
- controls.maxDistance = 20;
- controls.target.set( 0, 2, 0 );
- controls.enableDamping = true;
- controls.update();
- //
- const params = {
- output: 0
- };
- const types = { 'Scene with Shadow Maps + SSS': 0, 'Scene with Shadow Maps': 1, 'SSS': 2, };
- const gui = renderer.inspector.createParameters( 'SSS settings' );
- gui.add( params, 'output', types ).onChange( updatePostprocessing );
- gui.add( sssPass.shadowIntensity, 'value', 0, 1 ).name( 'shadow intensity' );
- gui.add( sssPass.maxDistance, 'value', 0.01, 1 ).name( 'max ray distance' );
- gui.add( sssPass.quality, 'value', 0, 1 ).name( 'quality' );
- gui.add( sssPass.thickness, 'value', 0.01, 0.1 ).name( 'thickness' );
- gui.add( sssPass, 'useTemporalFiltering' ).name( 'temporal filtering' ).onChange( updatePostprocessing );
- function updatePostprocessing() {
- scenePass.contextNode = params.output !== 1 ? sssContext : null;
- if ( params.output === 2 ) {
- postProcessing.outputNode = vec4( vec3( sssPass.r ), 1 );
- } else {
- postProcessing.outputNode = sssPass.useTemporalFiltering ? traaPass : scenePass;
- }
- postProcessing.needsUpdate = true;
- }
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- function animate() {
- controls.update();
- postProcessing.render();
- }
- </script>
- </body>
- </html>
|