forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebgpu_shadowmap.html
More file actions
220 lines (160 loc) · 5.9 KB
/
webgpu_shadowmap.html
File metadata and controls
220 lines (160 loc) · 5.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - shadow map</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="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - webgpu shadow map
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/tsl": "../build/three.webgpu.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { mx_fractal_noise_vec3, positionWorld, vec4, Fn, color, vertexIndex, hash } from 'three/tsl';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
let camera, scene, renderer, clock;
let dirLight, spotLight;
let torusKnot, dirGroup;
init();
function init() {
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 10, 20 );
scene = new THREE.Scene();
scene.backgroundNode = color( 0x222244 );
scene.fog = new THREE.Fog( 0x222244, 50, 100 );
// lights
scene.add( new THREE.AmbientLight( 0x444444, 2 ) );
spotLight = new THREE.SpotLight( 0xff8888, 400 );
spotLight.angle = Math.PI / 5;
spotLight.penumbra = 0.3;
spotLight.position.set( 8, 10, 5 );
spotLight.castShadow = true;
spotLight.shadow.camera.near = 8;
spotLight.shadow.camera.far = 200;
spotLight.shadow.mapSize.width = 2048;
spotLight.shadow.mapSize.height = 2048;
spotLight.shadow.bias = - 0.002;
spotLight.shadow.radius = 4;
scene.add( spotLight );
dirLight = new THREE.DirectionalLight( 0x8888ff, 3 );
dirLight.position.set( 3, 12, 17 );
dirLight.castShadow = true;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 500;
dirLight.shadow.camera.right = 17;
dirLight.shadow.camera.left = - 17;
dirLight.shadow.camera.top = 17;
dirLight.shadow.camera.bottom = - 17;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
dirLight.shadow.radius = 4;
dirLight.shadow.bias = - 0.0005;
dirGroup = new THREE.Group();
dirGroup.add( dirLight );
scene.add( dirGroup );
// geometry
const geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 80 );
const material = new THREE.MeshPhongNodeMaterial( {
color: 0x999999,
shininess: 0,
specular: 0x222222
} );
const materialCustomShadow = material.clone();
materialCustomShadow.transparent = true;
const materialColor = vec4( 1, 0, 1, .5 );
const discardNode = hash( vertexIndex ).greaterThan( 0.5 );
materialCustomShadow.colorNode = Fn( () => {
discardNode.discard();
return materialColor;
} )();
materialCustomShadow.shadowNode = Fn( () => {
discardNode.discard();
return materialColor;
} )();
torusKnot = new THREE.Mesh( geometry, materialCustomShadow );
torusKnot.scale.multiplyScalar( 1 / 18 );
torusKnot.position.y = 3;
torusKnot.castShadow = true;
torusKnot.receiveShadow = true;
scene.add( torusKnot );
const cylinderGeometry = new THREE.CylinderGeometry( 0.75, 0.75, 7, 32 );
const pillar1 = new THREE.Mesh( cylinderGeometry, material );
pillar1.position.set( 8, 3.5, 8 );
pillar1.castShadow = true;
const pillar2 = pillar1.clone();
pillar2.position.set( 8, 3.5, - 8 );
const pillar3 = pillar1.clone();
pillar3.position.set( - 8, 3.5, 8 );
const pillar4 = pillar1.clone();
pillar4.position.set( - 8, 3.5, - 8 );
scene.add( pillar1 );
scene.add( pillar2 );
scene.add( pillar3 );
scene.add( pillar4 );
const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
const planeMaterial = new THREE.MeshPhongMaterial( {
color: 0x999999,
shininess: 0,
specular: 0x111111
} );
planeMaterial.shadowPositionNode = Fn( () => {
const pos = positionWorld.toVar();
pos.xz.addAssign( mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().xz );
return pos;
} )();
planeMaterial.colorNode = Fn( () => {
const pos = positionWorld.toVar();
pos.xz.addAssign( mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().xz );
return mx_fractal_noise_vec3( positionWorld.mul( 2 ) ).saturate().zzz.mul( 0.2 ).add( .5 );
} )();
const ground = new THREE.Mesh( planeGeometry, planeMaterial );
ground.rotation.x = - Math.PI / 2;
ground.scale.multiplyScalar( 3 );
ground.castShadow = true;
ground.receiveShadow = true;
scene.add( ground );
// renderer
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.shadowMap.enabled = true;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild( renderer.domElement );
// Mouse control
const controls = new OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 2, 0 );
controls.minDistance = 7;
controls.maxDistance = 40;
controls.update();
clock = new THREE.Clock();
window.addEventListener( 'resize', resize );
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate( time ) {
const delta = clock.getDelta();
torusKnot.rotation.x += 0.25 * delta;
torusKnot.rotation.y += 0.5 * delta;
torusKnot.rotation.z += 1 * delta;
dirGroup.rotation.y += 0.7 * delta;
dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
renderer.render( scene, camera );
}
</script>
</body>
</html>