forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgpu_postprocessing_motion_blur.html
More file actions
245 lines (168 loc) · 6.64 KB
/
webgpu_postprocessing_motion_blur.html
File metadata and controls
245 lines (168 loc) · 6.64 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
241
242
243
244
245
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - motion blur</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>Motion Blur</span>
</div>
<small>Motion Blur based on Velocity (Motion Vectors).</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, texture, uniform, output, mrt, velocity, uv, screenUV, vec4 } from 'three/tsl';
import { motionBlur } from 'three/addons/tsl/display/MotionBlur.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { Inspector } from 'three/addons/inspector/Inspector.js';
let camera, scene, renderer;
let boxLeft, boxRight, model, mixer, timer;
let renderPipeline;
let controls;
const params = {
speed: 1.0
};
init();
function init() {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
camera.position.set( 0, 1.5, 4.5 );
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x0487e2, 7, 25 );
const sunLight = new THREE.DirectionalLight( 0xFFE499, 5 );
sunLight.castShadow = true;
sunLight.shadow.camera.near = .1;
sunLight.shadow.camera.far = 10;
sunLight.shadow.camera.right = 2;
sunLight.shadow.camera.left = - 2;
sunLight.shadow.camera.top = 2;
sunLight.shadow.camera.bottom = - 2;
sunLight.shadow.mapSize.width = 1024;
sunLight.shadow.mapSize.height = 1024;
sunLight.position.set( 4, 4, 2 );
const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
scene.add( sunLight );
scene.add( skyAmbientLight );
scene.add( waterAmbientLight );
timer = new THREE.Timer();
timer.connect( document );
// animated model
const loader = new GLTFLoader();
loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
model = gltf.scene;
model.rotation.y = Math.PI / 2;
model.traverse( function ( child ) {
if ( child.isMesh ) {
child.castShadow = true;
child.receiveShadow = true;
}
} );
mixer = new THREE.AnimationMixer( model );
const action = mixer.clipAction( gltf.animations[ 3 ] );
action.play();
scene.add( model );
} );
// textures
const textureLoader = new THREE.TextureLoader();
const floorColor = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
floorColor.wrapS = THREE.RepeatWrapping;
floorColor.wrapT = THREE.RepeatWrapping;
floorColor.colorSpace = THREE.SRGBColorSpace;
const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
floorNormal.wrapS = THREE.RepeatWrapping;
floorNormal.wrapT = THREE.RepeatWrapping;
// floor
const floorUV = uv().mul( 5 );
const floorMaterial = new THREE.MeshPhongNodeMaterial();
floorMaterial.colorNode = texture( floorColor, floorUV );
const floor = new THREE.Mesh( new THREE.BoxGeometry( 15, .001, 15 ), floorMaterial );
floor.receiveShadow = true;
floor.position.set( 0, 0, 0 );
scene.add( floor );
const walls = new THREE.Mesh( new THREE.BoxGeometry( 15, 15, 15 ), new THREE.MeshPhongNodeMaterial( { colorNode: floorMaterial.colorNode, side: THREE.BackSide } ) );
scene.add( walls );
const map = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
map.colorSpace = THREE.SRGBColorSpace;
const geometry = new THREE.TorusGeometry( .8 );
const material = new THREE.MeshBasicMaterial( { map } );
boxRight = new THREE.Mesh( geometry, material );
boxRight.position.set( 3.5, 1.5, - 4 );
scene.add( boxRight );
boxLeft = new THREE.Mesh( geometry, material );
boxLeft.position.set( - 3.5, 1.5, - 4 );
scene.add( boxLeft );
// renderer
renderer = new THREE.WebGPURenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.shadowMap.enabled = true;
renderer.inspector = new Inspector();
document.body.appendChild( renderer.domElement );
controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 1;
controls.maxDistance = 10;
controls.maxPolarAngle = Math.PI / 2;
controls.autoRotate = true;
controls.autoRotateSpeed = 1;
controls.target.set( 0, 1, 0 );
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.update();
// post-processing
const blurAmount = uniform( 1 );
const scenePass = pass( scene, camera );
scenePass.setMRT( mrt( {
output,
velocity
} ) );
const beauty = scenePass.getTextureNode().toInspector( 'Color' );
const vel = scenePass.getTextureNode( 'velocity' ).toInspector( 'Velocity' ).mul( blurAmount );
const mBlur = motionBlur( beauty, vel );
const vignette = screenUV.distance( .5 ).remap( .6, 1 ).mul( 2 ).clamp().oneMinus();
renderPipeline = new THREE.RenderPipeline( renderer );
renderPipeline.outputNode = vec4( mBlur.mul( vignette ).rgb, mBlur.a );
//
const gui = renderer.inspector.createParameters( 'Motion Blur Settings' );
gui.add( controls, 'autoRotate' );
gui.add( blurAmount, 'value', 0, 3 ).name( 'blur amount' );
gui.add( params, 'speed', 0, 2 );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
timer.update();
controls.update();
const delta = timer.getDelta();
const speed = params.speed;
boxRight.rotation.y += delta * 4 * speed;
boxLeft.scale.setScalar( 1 + Math.sin( timer.getElapsed() * 10 * speed ) * .2 );
if ( model ) {
mixer.update( delta * speed );
}
renderPipeline.render();
}
</script>
</body>
</html>