-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
webgl_instancing_scatter.html
305 lines (190 loc) · 7.28 KB
/
webgl_instancing_scatter.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - instancing - scatter</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="main.css">
</head>
<body>
<script type="module">
import * as THREE from '../build/three.module.js';
import { MeshSurfaceSampler } from './jsm/math/MeshSurfaceSampler.js';
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
import Stats from './jsm/libs/stats.module.js';
import { GUI } from './jsm/libs/dat.gui.module.js';
var camera, scene, renderer, stats;
var api = {
count: 2000,
distribution: 'random',
resample: resample,
surfaceColor: 0xFFF784,
backgroundColor: 0xE39469,
};
var stemMesh, blossomMesh;
var stemGeometry, blossomGeometry;
var stemMaterial, blossomMaterial;
var sampler;
var count = api.count;
var ages = new Float32Array( count );
var scales = new Float32Array( count );
var dummy = new THREE.Object3D();
var _position = new THREE.Vector3();
var _normal = new THREE.Vector3();
var _scale = new THREE.Vector3();
// var surfaceGeometry = new THREE.BoxBufferGeometry( 10, 10, 10 ).toNonIndexed();
var surfaceGeometry = new THREE.TorusKnotBufferGeometry( 10, 3, 100, 16 ).toNonIndexed();
var surfaceMaterial = new THREE.MeshLambertMaterial( { color: api.surfaceColor, wireframe: false } );
var surface = new THREE.Mesh( surfaceGeometry, surfaceMaterial );
// Source: https://gist.github.com/gre/1650294
var easeOutCubic = function ( t ) { return ( -- t ) * t * t + 1; };
// Scaling curve causes particles to grow quickly, ease gradually into full scale, then
// disappear quickly. More of the particle's lifetime is spent around full scale.
var scaleCurve = function ( t ) {
return Math.abs( easeOutCubic( ( t > 0.5 ? 1 - t : t ) * 2 ) );
};
var loader = new GLTFLoader();
loader.load( './models/gltf/Flower/Flower.glb', function ( gltf ) {
var _stemMesh = gltf.scene.getObjectByName('Stem');
var _blossomMesh = gltf.scene.getObjectByName('Blossom');
stemGeometry = _stemMesh.geometry;
blossomGeometry = _blossomMesh.geometry;
var defaultTransform = new THREE.Matrix4()
.makeRotationX( Math.PI )
.multiply( new THREE.Matrix4().makeScale( 7, 7, 7 ) )
stemGeometry.applyMatrix( defaultTransform );
blossomGeometry.applyMatrix( defaultTransform );
stemMaterial = _stemMesh.material;
blossomMaterial = _blossomMesh.material;
// Assign random colors to the blossoms.
var _color = new THREE.Color();
var color = new Float32Array( count * 3 );
var blossomPalette = [ 0xF20587, 0xF2D479, 0xF2C879, 0xF2B077, 0xF24405 ];
for ( var i = 0; i < count; i ++ ) {
_color.setHex( blossomPalette[ Math.floor( Math.random() * blossomPalette.length ) ] );
_color.toArray( color, i * 3 );
}
blossomGeometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( color, 3 ) );
blossomMaterial.vertexColors = THREE.VertexColors;
stemMesh = new THREE.InstancedMesh( stemGeometry, stemMaterial, count );
blossomMesh = new THREE.InstancedMesh( blossomGeometry, blossomMaterial, count );
resample();
init();
animate();
} );
function init() {
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.set( 25, 25, 25 );
camera.lookAt( 0, 0, 0 );
//
scene = new THREE.Scene();
scene.background = new THREE.Color( api.backgroundColor );
var pointLight = new THREE.PointLight( 0xAA8899, 0.75 );
pointLight.position.set( 50, -25, 75 );
scene.add( pointLight );
scene.add( new THREE.HemisphereLight() );
//
scene.add( stemMesh );
scene.add( blossomMesh );
scene.add( surface );
//
var gui = new GUI();
gui.add( api, 'count', 0, count ).onChange( function () {
stemMesh.count = api.count;
blossomMesh.count = api.count;
} );
// gui.addColor( api, 'backgroundColor' ).onChange( function () {
// scene.background.setHex( api.backgroundColor );
// } );
// gui.addColor( api, 'surfaceColor' ).onChange( function () {
// surfaceMaterial.color.setHex( api.surfaceColor );
// } );
gui.add( api, 'distribution' ).options( [ 'random', 'weighted' ] ).onChange( resample );
gui.add( api, 'resample' );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
stats = new Stats();
document.body.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function resample() {
var vertexCount = surface.geometry.getAttribute( 'position' ).count;
console.info( 'Sampling ' + count + ' points from a surface with ' + vertexCount + ' vertices...' );
//
console.time( '.build()' );
sampler = new MeshSurfaceSampler( surface )
.setWeightAttribute( api.distribution === 'weighted' ? 'uv' : null )
.build();
console.timeEnd( '.build()' );
//
console.time( '.sample()' );
for ( var i = 0; i < count; i ++ ) {
ages[ i ] = Math.random();
scales[ i ] = scaleCurve( ages[ i ] );
resampleParticle( i );
}
console.timeEnd( '.sample()' );
stemMesh.instanceMatrix.needsUpdate = true;
blossomMesh.instanceMatrix.needsUpdate = true;
}
function resampleParticle ( i ) {
sampler.sample( _position, _normal );
_normal.add( _position );
dummy.position.copy( _position );
dummy.scale.set( scales[ i ], scales[ i ], scales[ i ] );
dummy.lookAt( _normal );
dummy.updateMatrix();
stemMesh.setMatrixAt( i, dummy.matrix );
blossomMesh.setMatrixAt( i, dummy.matrix );
}
function updateParticle ( i ) {
// Update lifecycle.
ages[ i ] += 0.005;
if ( ages[ i ] >= 1 ) {
ages[ i ] = 0.001;
scales[ i ] = scaleCurve( ages[ i ] );
resampleParticle( i );
return;
}
// Update scale.
var prevScale = scales[ i ];
scales[ i ] = scaleCurve( ages[ i ] );
_scale.set( scales[ i ] / prevScale, scales[ i ] / prevScale, scales[ i ] / prevScale );
// Update transform.
stemMesh.getMatrixAt( i, dummy.matrix );
dummy.matrix.scale( _scale );
stemMesh.setMatrixAt( i, dummy.matrix );
blossomMesh.setMatrixAt( i, dummy.matrix );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
if ( stemMesh && blossomMesh ) {
var time = Date.now() * 0.001;
scene.rotation.x = Math.sin( time / 4 );
scene.rotation.y = Math.sin( time / 2 );
for ( var i = 0; i < api.count; i ++ ) {
updateParticle( i );
}
stemMesh.instanceMatrix.needsUpdate = true;
blossomMesh.instanceMatrix.needsUpdate = true;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>