-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_compute_particles_snow.html
More file actions
363 lines (243 loc) · 10.2 KB
/
webgpu_compute_particles_snow.html
File metadata and controls
363 lines (243 loc) · 10.2 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<html lang="en">
<head>
<title>three.js webgpu - compute snow</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">
<!-- <link type="text/css" rel="stylesheet" href="main.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>Compute Snow</span>
</div>
<small>
100k snow particles simulation.
</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 { Fn, texture, vec3, pass, color, uint, screenUV, instancedArray, positionWorld, positionLocal, time, vec2, hash, instanceIndex, If } from 'three/tsl';
import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
import { Inspector } from 'three/addons/inspector/Inspector.js';
import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const maxParticleCount = 100000;
let camera, scene, renderer;
let controls;
let computeParticles;
let renderPipeline;
let collisionCamera, collisionPosRT, collisionPosMaterial;
init();
async function init() {
const { innerWidth, innerHeight } = window;
camera = new THREE.PerspectiveCamera( 60, innerWidth / innerHeight, .1, 100 );
camera.position.set( 20, 2, 20 );
camera.layers.enable( 2 );
camera.lookAt( 0, 40, 0 );
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x0f3c37, 5, 40 );
const dirLight = new THREE.DirectionalLight( 0xf9ff9b, 9 );
dirLight.position.set( 10, 10, 0 );
scene.add( dirLight );
scene.add( new THREE.HemisphereLight( 0x0f3c37, 0x080d10, 100 ) );
//
collisionCamera = new THREE.OrthographicCamera( - 50, 50, 50, - 50, .1, 50 );
collisionCamera.position.y = 50;
collisionCamera.lookAt( 0, 0, 0 );
collisionCamera.layers.enable( 1 );
collisionPosRT = new THREE.RenderTarget( 1024, 1024 );
collisionPosRT.texture.type = THREE.HalfFloatType;
collisionPosRT.texture.magFilter = THREE.NearestFilter;
collisionPosRT.texture.minFilter = THREE.NearestFilter;
collisionPosRT.texture.generateMipmaps = false;
collisionPosMaterial = new THREE.MeshBasicNodeMaterial();
collisionPosMaterial.fog = false;
collisionPosMaterial.toneMapped = false;
collisionPosMaterial.colorNode = positionWorld.y;
//
const positionBuffer = instancedArray( maxParticleCount, 'vec3' );
const scaleBuffer = instancedArray( maxParticleCount, 'vec3' );
const staticPositionBuffer = instancedArray( maxParticleCount, 'vec3' );
const dataBuffer = instancedArray( maxParticleCount, 'vec4' );
// compute
const randUint = () => uint( Math.random() * 0xFFFFFF );
const computeInit = Fn( () => {
const position = positionBuffer.element( instanceIndex );
const scale = scaleBuffer.element( instanceIndex );
const particleData = dataBuffer.element( instanceIndex );
const randX = hash( instanceIndex );
const randY = hash( instanceIndex.add( randUint() ) );
const randZ = hash( instanceIndex.add( randUint() ) );
position.x = randX.mul( 100 ).add( - 50 );
position.y = randY.mul( 500 ).add( 3 );
position.z = randZ.mul( 100 ).add( - 50 );
scale.xyz = hash( instanceIndex.add( Math.random() ) ).mul( .8 ).add( .2 );
staticPositionBuffer.element( instanceIndex ).assign( vec3( 1000, 10000, 1000 ) );
particleData.y = randY.mul( - .1 ).add( - .02 );
particleData.x = position.x;
particleData.z = position.z;
particleData.w = randX;
} )().compute( maxParticleCount ).setName( 'Init Particles' );
//
const surfaceOffset = .2;
const speed = .4;
const computeUpdate = Fn( () => {
const getCoord = ( pos ) => pos.add( 50 ).div( 100 );
const position = positionBuffer.element( instanceIndex );
const scale = scaleBuffer.element( instanceIndex );
const particleData = dataBuffer.element( instanceIndex );
const velocity = particleData.y;
const random = particleData.w;
const rippleOnSurface = texture( collisionPosRT.texture, getCoord( position.xz ) ).toInspector( 'Collision Test', () => {
return texture( collisionPosRT.texture ).y; // .div( collisionCamera.position.y );
} );
const rippleFloorArea = rippleOnSurface.y.add( scale.x.mul( surfaceOffset ) );
If( position.y.greaterThan( rippleFloorArea ), () => {
position.x = particleData.x.add( time.mul( random.mul( random ) ).mul( speed ).sin().mul( 3 ) );
position.z = particleData.z.add( time.mul( random ).mul( speed ).cos().mul( random.mul( 10 ) ) );
position.y = position.y.add( velocity );
} ).Else( () => {
staticPositionBuffer.element( instanceIndex ).assign( position );
} );
} );
computeParticles = computeUpdate().compute( maxParticleCount );
computeParticles.name = 'Update Particles';
// rain
const geometry = new THREE.SphereGeometry( surfaceOffset, 5, 5 );
function particle( staticParticles ) {
const posBuffer = staticParticles ? staticPositionBuffer : positionBuffer;
const layer = staticParticles ? 1 : 2;
const staticMaterial = new THREE.MeshStandardNodeMaterial( {
color: 0xeeeeee,
roughness: .9,
metalness: 0
} );
staticMaterial.positionNode = positionLocal.mul( scaleBuffer.toAttribute() ).add( posBuffer.toAttribute() );
const rainParticles = new THREE.Mesh( geometry, staticMaterial );
rainParticles.count = maxParticleCount;
rainParticles.layers.disableAll();
rainParticles.layers.enable( layer );
return rainParticles;
}
const dynamicParticles = particle();
const staticParticles = particle( true );
scene.add( dynamicParticles );
scene.add( staticParticles );
// floor geometry
const floorGeometry = new THREE.PlaneGeometry( 100, 100 );
floorGeometry.rotateX( - Math.PI / 2 );
const plane = new THREE.Mesh( floorGeometry, new THREE.MeshStandardMaterial( {
color: 0x0c1e1e,
roughness: .5,
metalness: 0,
transparent: true
} ) );
plane.material.opacityNode = positionLocal.xz.mul( .05 ).distance( 0 ).saturate().oneMinus();
scene.add( plane );
// tree
function tree( count = 8 ) {
const coneMaterial = new THREE.MeshStandardNodeMaterial( {
color: 0x0d492c,
roughness: .6,
metalness: 0
} );
const object = new THREE.Group();
for ( let i = 0; i < count; i ++ ) {
const radius = 1 + i;
const coneGeometry = new THREE.ConeGeometry( radius * 0.95, radius * 1.25, 32 );
const cone = new THREE.Mesh( coneGeometry, coneMaterial );
cone.position.y = ( ( count - i ) * 1.5 ) + ( count * .6 );
object.add( cone );
}
const geometry = new THREE.CylinderGeometry( 1, 1, count, 32 );
const cone = new THREE.Mesh( geometry, coneMaterial );
cone.position.y = count / 2;
object.add( cone );
return object;
}
const teapotTree = new THREE.Mesh( new TeapotGeometry( .5, 18 ), new THREE.MeshBasicNodeMaterial( {
color: 0xfcfb9e
} ) );
teapotTree.name = 'Teapot Pass';
teapotTree.position.y = 18;
scene.add( tree() );
scene.add( teapotTree );
//
scene.backgroundNode = screenUV.distance( .5 ).mul( 2 ).mix( color( 0x0f4140 ), color( 0x060a0d ) );
//
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.inspector = new Inspector();
document.body.appendChild( renderer.domElement );
await renderer.init();
//
controls = new OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 10, 0 );
controls.minDistance = 25;
controls.maxDistance = 35;
controls.maxPolarAngle = Math.PI / 1.7;
controls.autoRotate = true;
controls.autoRotateSpeed = - 0.7;
controls.update();
// post processing
const scenePass = pass( scene, camera );
const scenePassColor = scenePass.getTextureNode();
const vignette = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus();
const teapotTreePass = pass( teapotTree, camera ).getTextureNode();
const teapotTreePassBlurred = gaussianBlur( teapotTreePass, vec2( 1 ), 6 );
teapotTreePassBlurred.resolutionScale = 0.2;
const scenePassColorBlurred = gaussianBlur( scenePassColor );
scenePassColorBlurred.resolutionScale = 0.5;
scenePassColorBlurred.directionNode = vec2( 1 );
// compose
let totalPass = scenePass.toInspector( 'Scene' );
totalPass = totalPass.add( scenePassColorBlurred.mul( .1 ) );
totalPass = totalPass.mul( vignette );
totalPass = totalPass.add( teapotTreePass.mul( 10 ).add( teapotTreePassBlurred ).toInspector( 'Teapot Blur' ) );
renderPipeline = new THREE.RenderPipeline( renderer );
renderPipeline.outputNode = totalPass;
//
renderer.compute( computeInit );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
const { innerWidth, innerHeight } = window;
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( innerWidth, innerHeight );
}
function animate() {
controls.update();
// position
scene.name = 'Collider Position';
scene.overrideMaterial = collisionPosMaterial;
renderer.setRenderTarget( collisionPosRT );
renderer.render( scene, collisionCamera );
// compute
renderer.compute( computeParticles );
// result
scene.name = 'Scene';
scene.overrideMaterial = null;
renderer.setRenderTarget( null );
renderPipeline.render();
}
</script>
</body>
</html>