-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_compute_points.html
More file actions
180 lines (118 loc) · 5.03 KB
/
webgpu_compute_points.html
File metadata and controls
180 lines (118 loc) · 5.03 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
<html lang="en">
<head>
<title>three.js webgpu - compute</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>Compute</span>
</div>
<small>
300k points animated using GPU.
</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 { Inspector } from 'three/addons/inspector/Inspector.js';
import { Fn, uniform, instancedArray, float, vec2, color, instanceIndex } from 'three/tsl';
let camera, scene, renderer;
let computeNode;
const pointerVector = new THREE.Vector2( - 10.0, - 10.0 ); // Out of bounds first
const scaleVector = new THREE.Vector2( 1, 1 );
init();
async function init() {
camera = new THREE.OrthographicCamera( - 1.0, 1.0, 1.0, - 1.0, 0, 1 );
camera.position.z = 1;
scene = new THREE.Scene();
// initialize particles
const particlesCount = 300000;
const particleArray = instancedArray( particlesCount, 'vec2' );
const velocityArray = instancedArray( particlesCount, 'vec2' );
// create function
const computeShaderFn = Fn( () => {
const particle = particleArray.element( instanceIndex );
const velocity = velocityArray.element( instanceIndex );
const pointer = uniform( pointerVector );
const limit = uniform( scaleVector );
const position = particle.add( velocity ).toVar();
velocity.x = position.x.abs().greaterThanEqual( limit.x ).select( velocity.x.negate(), velocity.x );
velocity.y = position.y.abs().greaterThanEqual( limit.y ).select( velocity.y.negate(), velocity.y );
position.assign( position.min( limit ).max( limit.negate() ) );
const pointerSize = 0.1;
const distanceFromPointer = pointer.sub( position ).length();
particle.assign( distanceFromPointer.lessThanEqual( pointerSize ).select( vec2(), position ) );
} );
// compute
computeNode = computeShaderFn().compute( particlesCount ).setName( 'Update Particles' );
computeNode.onInit( ( { renderer } ) => {
const precomputeShaderNode = Fn( () => {
const particleIndex = float( instanceIndex );
const randomAngle = particleIndex.mul( .005 ).mul( Math.PI * 2 );
const randomSpeed = particleIndex.mul( 0.00000001 ).add( 0.0000001 );
const velX = randomAngle.sin().mul( randomSpeed );
const velY = randomAngle.cos().mul( randomSpeed );
const velocity = velocityArray.element( instanceIndex );
velocity.xy = vec2( velX, velY );
} );
renderer.compute( precomputeShaderNode().compute( particlesCount ) );
} );
// use a compute shader to animate the point cloud's vertex data.
const pointsGeometry = new THREE.BufferGeometry();
pointsGeometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( 3 ), 3 ) ); // single vertex ( not triangle )
pointsGeometry.drawRange.count = 1; // force render points as instances ( not triangle )
const pointsMaterial = new THREE.PointsNodeMaterial();
pointsMaterial.colorNode = particleArray.element( instanceIndex ).add( color( 0xFFFFFF ) );
pointsMaterial.positionNode = particleArray.element( instanceIndex );
const mesh = new THREE.Points( pointsGeometry, pointsMaterial );
mesh.count = particlesCount;
scene.add( mesh );
renderer = new THREE.WebGPURenderer( { antialias: true, requiredLimits: { maxStorageBuffersInVertexStage: 1 } } );
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();
window.addEventListener( 'resize', onWindowResize );
window.addEventListener( 'mousemove', onMouseMove );
// gui
const gui = renderer.inspector.createParameters( 'Settings' );
gui.add( scaleVector, 'x', 0, 1, 0.01 );
gui.add( scaleVector, 'y', 0, 1, 0.01 );
}
function onWindowResize() {
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onMouseMove( event ) {
const x = event.clientX;
const y = event.clientY;
const width = window.innerWidth;
const height = window.innerHeight;
pointerVector.set(
( x / width - 0.5 ) * 2.0,
( - y / height + 0.5 ) * 2.0
);
}
function animate() {
renderer.compute( computeNode );
renderer.render( scene, camera );
}
</script>
</body>
</html>