forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgpu_mesh_batch.html
More file actions
364 lines (232 loc) · 7.58 KB
/
webgpu_mesh_batch.html
File metadata and controls
364 lines (232 loc) · 7.58 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
364
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - batch mesh</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>Batch Mesh</span>
</div>
<small>Mirror reflections using procedural effects.</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 { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { radixSort } from 'three/addons/utils/SortUtils.js';
import { normalView, directionToColor, diffuseColor, vec4 } from 'three/tsl';
let camera, scene, renderer;
let controls;
let gui;
let geometries, mesh, material;
const ids = [];
const matrix = new THREE.Matrix4();
//
const position = new THREE.Vector3();
const rotation = new THREE.Euler();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
//
const MAX_GEOMETRY_COUNT = 20000;
const api = {
webgpu: true,
count: 512,
dynamic: 16,
sortObjects: true,
perObjectFrustumCulled: true,
opacity: 1,
useCustomSort: true,
randomizeGeometry: ()=>{
for ( let i = 0; i < api.count; i ++ ) {
mesh.setGeometryIdAt( i, Math.floor( Math.random() * geometries.length ) );
}
}
};
init();
//
function randomizeMatrix( matrix ) {
position.x = Math.random() * 40 - 20;
position.y = Math.random() * 40 - 20;
position.z = Math.random() * 40 - 20;
rotation.x = Math.random() * 2 * Math.PI;
rotation.y = Math.random() * 2 * Math.PI;
rotation.z = Math.random() * 2 * Math.PI;
quaternion.setFromEuler( rotation );
scale.x = scale.y = scale.z = 0.5 + ( Math.random() * 0.5 );
return matrix.compose( position, quaternion, scale );
}
function randomizeRotationSpeed( rotation ) {
rotation.x = Math.random() * 0.01;
rotation.y = Math.random() * 0.01;
rotation.z = Math.random() * 0.01;
return rotation;
}
function initGeometries() {
geometries = [
new THREE.ConeGeometry( 1.0, 2.0 ),
new THREE.BoxGeometry( 2.0, 2.0, 2.0 ),
new THREE.SphereGeometry( 1.0, 16, 8 ),
];
}
function createMaterial() {
if ( ! material ) {
material = new THREE.MeshBasicNodeMaterial();
material.outputNode = vec4(
diffuseColor.mul( directionToColor( normalView ).y.add( 0.5 ) ).rgb,
diffuseColor.a,
);
}
return material;
}
function cleanup() {
if ( mesh ) {
mesh.parent.remove( mesh );
if ( mesh.dispose ) {
mesh.dispose();
}
}
}
function initMesh() {
cleanup();
initBatchedMesh();
}
function initBatchedMesh() {
const geometryCount = api.count;
const vertexCount = geometries.length * 512;
const indexCount = geometries.length * 1024;
const euler = new THREE.Euler();
const matrix = new THREE.Matrix4();
mesh = new THREE.BatchedMesh( geometryCount, vertexCount, indexCount, createMaterial() );
mesh.userData.rotationSpeeds = [];
// disable full-object frustum culling since all of the objects can be dynamic.
mesh.frustumCulled = false;
ids.length = 0;
const geometryIds = [
mesh.addGeometry( geometries[ 0 ] ),
mesh.addGeometry( geometries[ 1 ] ),
mesh.addGeometry( geometries[ 2 ] ),
];
for ( let i = 0; i < api.count; i ++ ) {
const id = mesh.addInstance( geometryIds[ i % geometryIds.length ] );
mesh.setMatrixAt( id, randomizeMatrix( matrix ) );
mesh.setColorAt( id, new THREE.Color( Math.random() * 0xffffff ) );
const rotationMatrix = new THREE.Matrix4();
rotationMatrix.makeRotationFromEuler( randomizeRotationSpeed( euler ) );
mesh.userData.rotationSpeeds.push( rotationMatrix );
ids.push( id );
}
scene.add( mesh );
}
function init( forceWebGL = false ) {
if ( renderer ) {
renderer.dispose();
controls.dispose();
document.body.removeChild( renderer.domElement );
}
// camera
const aspect = window.innerWidth / window.innerHeight;
camera = new THREE.PerspectiveCamera( 70, aspect, 1, 100 );
camera.position.z = 30;
// renderer
renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.inspector = new Inspector();
renderer.setAnimationLoop( animate );
// scene
scene = new THREE.Scene();
scene.background = forceWebGL ? new THREE.Color( 0xffc1c1 ) : new THREE.Color( 0xc1c1ff );
document.body.appendChild( renderer.domElement );
initGeometries();
initMesh();
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.autoRotate = true;
controls.autoRotateSpeed = 1.0;
// gui
gui = renderer.inspector.createParameters( 'Settings' );
gui.add( api, 'webgpu' ).onChange( () => {
init( ! api.webgpu );
} );
gui.add( api, 'count', 1, MAX_GEOMETRY_COUNT, 1 ).onChange( initMesh );
gui.add( api, 'dynamic', 0, MAX_GEOMETRY_COUNT, 1 );
gui.add( api, 'opacity', 0, 1 ).onChange( v => {
if ( v < 1 ) {
material.transparent = true;
material.depthWrite = false;
} else {
material.transparent = false;
material.depthWrite = true;
}
material.opacity = v;
material.needsUpdate = true;
} );
gui.add( api, 'sortObjects' );
gui.add( api, 'perObjectFrustumCulled' );
gui.add( api, 'useCustomSort' );
gui.add( api, 'randomizeGeometry' ).name( 'randomize geometry' );
// listeners
window.addEventListener( 'resize', onWindowResize );
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
}
async function animate() {
animateMeshes();
controls.update();
if ( mesh.isBatchedMesh ) {
mesh.sortObjects = api.sortObjects;
mesh.perObjectFrustumCulled = api.perObjectFrustumCulled;
mesh.setCustomSort( api.useCustomSort ? sortFunction : null );
}
renderer.render( scene, camera );
}
function animateMeshes() {
const loopNum = Math.min( api.count, api.dynamic );
for ( let i = 0; i < loopNum; i ++ ) {
const rotationMatrix = mesh.userData.rotationSpeeds[ i ];
const id = ids[ i ];
mesh.getMatrixAt( id, matrix );
matrix.multiply( rotationMatrix );
mesh.setMatrixAt( id, matrix );
}
}
}
//
function sortFunction( list, camera ) {
// initialize options
this._options = this._options || {
get: el => el.z,
aux: new Array( this.maxInstanceCount )
};
const options = this._options;
options.reversed = this.material.transparent;
// convert depth to unsigned 32 bit range
const factor = ( 2 ** 32 - 1 ) / camera.far; // UINT32_MAX / max_depth
for ( let i = 0, l = list.length; i < l; i ++ ) {
list[ i ].z *= factor;
}
// perform a fast-sort using the hybrid radix sort function
radixSort( list, options );
}
</script>
</body>
</html>