-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_postprocessing_sss.html
More file actions
240 lines (166 loc) · 6.86 KB
/
webgpu_postprocessing_sss.html
File metadata and controls
240 lines (166 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!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.bias = - 0.001; // remove self-shadowing artifacts
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.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>