-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchedStandardMaterial.html
410 lines (285 loc) · 9.81 KB
/
BatchedStandardMaterial.html
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - mesh - batch</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html, body {
padding: 0;
margin: 0;
overflow: hidden;
}
#info {
background: rgba( 0, 0, 0, 0.3 );
color: white;
position: absolute;
left: 0;
padding: 5px;
font-family: monospace;
white-space: pre;
}
</style>
</head>
<body>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<div id="info"></div>
<script type="module">
import * as THREE from 'three';
import { GUI } from 'three/addons/lil-gui.module.min.js';
import { OrbitControls } from 'three/addons/OrbitControls.js';
import { RGBELoader } from 'three/addons/RGBELoader.js';
import { BatchedStandardMaterial } from './garrettkjohnson/BatchedStandardMaterial.js';
let gui, infoEl;
let camera, controls, scene, renderer;
let geometries, mesh, material;
const ids = [];
const matrix = new THREE.Matrix4();
const color = new THREE.Color();
const clock = new THREE.Clock();
//
const position = new THREE.Vector3();
const rotation = new THREE.Euler();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
//
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
//
let frameTime = 0;
let frameSamples = 0;
let lastFrameStart = - 1;
let averageTime = 0;
let timeSamples = 0;
const MAX_GEOMETRY_COUNT = 5000;
const params = {
animationSpeed: 1,
metalness: true,
roughness: true,
emissive: true,
color: true,
checkShaderErrors: false,
};
init();
initGeometries();
initMesh();
animate();
//
function rand( min, max ) {
const delta = max - min;
return min + Math.random() * delta;
}
function randomizeMatrix( matrix ) {
position.randomDirection().multiplyScalar( 40 * Math.random() ** 1.5 );
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.35 + ( Math.random() * 1.25 );
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 ).toNonIndexed(),
new THREE.BoxGeometry( 2.0, 2.0, 2.0 ).toNonIndexed(),
new THREE.IcosahedronGeometry( 1, 1 ),//( 1.0, 20, 15 ),
new THREE.SphereGeometry( 1.0, 16, 13 ).toNonIndexed(),
];
geometries[2].computeVertexNormals();
}
function initMesh() {
const geometryCount = MAX_GEOMETRY_COUNT;
const vertexCount = geometryCount * 512;
const indexCount = geometryCount * 1024;
const euler = new THREE.Euler();
const matrix = new THREE.Matrix4();
mesh = new THREE.BatchedMesh( geometryCount, vertexCount, indexCount, null );
mesh.sortObjects = false;
mesh.perObjectFrustumCulled = false;
mesh.userData.info = [];
updateMaterial();
// disable full-object frustum culling since all of the objects can be dynamic.
mesh.frustumCulled = false;
ids.length = 0;
for ( let i = 0; i < geometryCount; i ++ ) {
const id = mesh.addGeometry( geometries[ i % geometries.length ] );
mesh.setMatrixAt( id, randomizeMatrix( matrix ) );
const rotationMatrix = new THREE.Matrix4();
rotationMatrix.makeRotationFromEuler( randomizeRotationSpeed( euler ) );
ids.push( id );
const c0 = new THREE.Color();
const c1 = new THREE.Color();
c0.setHSL( rand( 0, 0.05 ), rand( 1, 1 ), rand( 0.5, 0.7 ) );
c1.setHSL( rand( 0.5, 0.55 ), rand( 1, 1 ), rand( 0.5, 0.7 ) );
const roughness = 0.2 //rand( 0, 0.5 );
const metalness = 1 //rand( 0, 1 );
let emissiveIntensity = 0;
if ( rand( 0, 1 ) < 0.2 ) {
emissiveIntensity = 1// rand( 1, 5 );
}
mesh.userData.info.push( {
rotationMatrix,
c0,
c1,
emissiveIntensity,
roughness,
metalness,
offset: rand( 0, 2 * Math.PI ),
speed: rand( 1, 3 ),
value: 0,
} );
}
scene.add( mesh );
}
function updateMaterial() {
const props = [];
if ( params.metalness ) props.push( 'metalness' );
if ( params.color ) props.push( 'diffuse' );
if ( params.roughness ) props.push( 'roughness' );
if ( params.emissive ) props.push( 'emissive' );
if ( material ) material.dispose();
material = new BatchedStandardMaterial( {}, MAX_GEOMETRY_COUNT, props );
mesh.material = material;
frameSamples = 0;
}
function init() {
const width = window.innerWidth;
const height = window.innerHeight;
// camera
camera = new THREE.PerspectiveCamera( 70, width / height, 1, 500 );
camera.position.set( 50, 30, 30 ).multiplyScalar( 1.25 );
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( width, height );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild( renderer.domElement );
// scene
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x182122 );
scene.fog = new THREE.Fog( 0x222222, 60, 150 );
const url = 'https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/equirectangular/royal_esplanade_1k.hdr';
new RGBELoader()
.load( url, texture => {
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.background = texture;
scene.environment = texture;
} );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.autoRotate = true;
controls.autoRotateSpeed = 0.2;
// gui
gui = new GUI();
gui.add( params, 'animationSpeed', 0, 3 ).step( 0.1 );
gui.add( params, 'color' ).onChange( updateMaterial );
gui.add( params, 'metalness' ).onChange( updateMaterial );
gui.add( params, 'roughness' ).onChange( updateMaterial );
gui.add( params, 'emissive' ).onChange( updateMaterial );
gui.add( params, 'checkShaderErrors' );
infoEl = document.getElementById( 'info' );
// listeners
window.addEventListener( 'resize', onWindowResize );
window.addEventListener( 'mousemove', e => {
mouse.set(
( e.clientX / window.innerWidth ) * 2 - 1,
- ( e.clientY / window.innerHeight ) * 2 + 1
);
} );
}
//
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
}
function animate() {
requestAnimationFrame( animate );
animateMeshes();
updateHover();
controls.update();
render();
}
function updateHover() {
scene.updateMatrixWorld();
camera.updateMatrixWorld();
raycaster.setFromCamera( mouse, camera );
const hit = raycaster.intersectObject( mesh )[ 0 ];
if ( hit ) {
const batchId = hit.batchId;
material.setValue( batchId, 'diffuse', 0, 0, 0 );
material.setValue( batchId, 'emissive', 1, 0.2, 0.1 );
material.setValue( batchId, 'metalness', 0 );
material.setValue( batchId, 'roughness', 1 );
}
}
function animateMeshes() {
const delta = clock.getDelta();
for ( let i = 0; i < MAX_GEOMETRY_COUNT; i ++ ) {
const info = mesh.userData.info[ i ];
const {
rotationMatrix,
c0,
c1,
speed,
offset,
roughness,
metalness,
emissiveIntensity,
} = info;
const id = ids[ i ];
mesh.getMatrixAt( id, matrix );
matrix.multiply( rotationMatrix );
mesh.setMatrixAt( id, matrix );
info.value += delta * 2 * speed * params.animationSpeed;
color.lerpColors( c0, c1, 0.5 + 0.5 * Math.sin( offset + info.value ) );
material.setValue( i, 'diffuse', ...color );
material.setValue( i, 'roughness', roughness );
material.setValue( i, 'metalness', metalness );
material.setValue( i, 'emissive', color.r * emissiveIntensity, color.g * emissiveIntensity, color.b * emissiveIntensity );
}
scene.fog.near = Math.min( camera.position.length() - 30, 80 );
scene.fog.far = scene.fog.near + 60;
}
function render() {
renderer.info.checkShaderErrors = params.checkShaderErrors;
let frameDelta;
if ( lastFrameStart === - 1 ) {
lastFrameStart = window.performance.now();
} else {
frameDelta = window.performance.now() - lastFrameStart;
frameTime += ( frameDelta - frameTime ) / ( frameSamples + 1 );
if ( frameSamples < 60 ) {
frameSamples ++
}
lastFrameStart = window.performance.now();
}
const start = window.performance.now();
renderer.render( scene, camera );
const delta = window.performance.now() - start;
averageTime += ( delta - averageTime ) / ( timeSamples + 1 );
if ( timeSamples < 60 ) {
timeSamples ++;
}
infoEl.innerHTML = `draw calls : ${ renderer.info.render.calls }\n`;
infoEl.innerHTML += `render time : ${ averageTime.toFixed( 2 ) }ms\n`;
infoEl.innerHTML += `frame time : ${ frameTime.toFixed( 2 ) }ms`;
}
</script>
</body>
</html>