-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.js
469 lines (420 loc) · 17.4 KB
/
main.js
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
(function(){
var scene, ambientLight, light, camera, controls, renderer;
var world;
var debugMesh, debugGridMesh;
var controller;
var boxSize;
var numParticles;
init();
animate();
function init(){
var ySpread = 0.1;
var query = parseParams();
numParticles = query.n ? parseInt(query.n,10) : 64;
var gridResolution = new THREE.Vector3();
switch(numParticles){
default:
case 64:
numParticles = 64;
gridResolution.set(numParticles/2, numParticles/8, numParticles/2);
break;
case 128:
gridResolution.set(numParticles/2, numParticles/16, numParticles/2);
break;
case 256:
gridResolution.set(numParticles/2, numParticles/32, numParticles/2);
ySpread = 0.05;
break;
case 512:
gridResolution.set(numParticles/2, numParticles/64, numParticles/2);
ySpread = 0.01;
break;
case 1024:
gridResolution.set(numParticles/2, numParticles/64, numParticles/2);
ySpread = 0.001;
break;
}
var numBodies = numParticles / 2;
var radius = 1/numParticles * 0.5;
boxSize = new THREE.Vector3(0.25, 1, 0.25);
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( 1 );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
var container = document.getElementById( 'container' );
container.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
scene = window.mainScene = new THREE.Scene();
light = new THREE.DirectionalLight();
light.castShadow = true;
light.shadow.mapSize.width = light.shadow.mapSize.height = 1024;
var d = 0.5;
light.shadow.camera.left = - d;
light.shadow.camera.right = d;
light.shadow.camera.top = d;
light.shadow.camera.bottom = - d;
light.shadow.camera.far = 100;
light.position.set(1,1,1);
scene.add(light);
ambientLight = new THREE.AmbientLight( 0x222222 );
scene.add( ambientLight );
renderer.setClearColor(ambientLight.color, 1.0);
camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 0.01, 100 );
camera.position.set(0,0.6,1.4);
var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x000000 } );
groundMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), groundMaterial );
groundMesh.rotation.x = - Math.PI / 2;
groundMesh.receiveShadow = true;
scene.add( groundMesh );
// Add controls
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableZoom = true;
controls.target.set(0.0, 0.1, 0.0);
controls.maxPolarAngle = Math.PI * 0.5;
// Physics
world = window.world = new gp.World({
maxSubSteps: 1, // TODO: fix
gravity: new THREE.Vector3(0,-2,0),
renderer: renderer,
maxBodies: numBodies * numBodies,
maxParticles: numParticles * numParticles,
radius: radius,
stiffness: 1700,
damping: 6,
fixedTimeStep: 1/120,
friction: 2,
drag: 0.3,
boxSize: boxSize,
gridPosition: new THREE.Vector3(-boxSize.x,0,-boxSize.z),
gridResolution: gridResolution
});
// Interaction sphere
world.setSphereRadius(0, 0.05);
world.setSpherePosition(0, 0,0,0);
// Add bodies
for(var bodyId=0; bodyId<world.maxBodies; bodyId++){
var x = -boxSize.x + 2*boxSize.x*Math.random();
var y = ySpread*Math.random();
var z = -boxSize.z + 2*boxSize.z*Math.random();
var q = new THREE.Quaternion();
var axis = new THREE.Vector3(
Math.random()-0.5,
Math.random()-0.5,
Math.random()-0.5
);
axis.normalize();
q.setFromAxisAngle(axis, Math.random() * Math.PI * 2);
var inertia = new THREE.Vector3();
var mass = 1;
if(bodyId < world.maxBodies / 2){
calculateBoxInertia(inertia, mass, new THREE.Vector3(radius*2*4,radius*2,radius*2));
} else {
calculateBoxInertia(inertia, mass, new THREE.Vector3(radius*4,radius*4,radius*2));
}
world.addBody(x,y,z, q.x, q.y, q.z, q.w, mass, inertia.x, inertia.y, inertia.z);
}
// Add particles to bodies
for(var particleId=0; particleId<world.maxParticles; ++particleId){
var bodyId = Math.floor(particleId / 4);
var x=0, y=0; z=0;
if(bodyId < world.maxBodies / 2){
x = (particleId % 4 - 1.5) * radius * 2.01;
} else {
var i = particleId - bodyId * 4;
x = ((i % 2)-0.5) * radius * 2.01;
y = (Math.floor(i / 2)-0.5) * radius * 2.01;
}
world.addParticle(bodyId, x,y,z);
}
// Create an instanced mesh for debug spheres
var sphereGeometry = new THREE.SphereBufferGeometry(world.radius, 8, 8);
var instances = world.maxParticles;
var debugGeometry = new THREE.InstancedBufferGeometry();
debugGeometry.maxInstancedCount = instances;
for(var attributeName in sphereGeometry.attributes){
debugGeometry.addAttribute( attributeName, sphereGeometry.attributes[attributeName].clone() );
}
debugGeometry.setIndex( sphereGeometry.index.clone() );
var particleIndices = new THREE.InstancedBufferAttribute( new Float32Array( instances * 1 ), 1, 1 );
for ( var i = 0, ul = particleIndices.count; i < ul; i++ ) {
particleIndices.setX( i, i );
}
debugGeometry.addAttribute( 'particleIndex', particleIndices );
debugGeometry.boundingSphere = null;
// Particle spheres material / debug material - extend the phong shader in three.js
var phongShader = THREE.ShaderLib.phong;
var uniforms = THREE.UniformsUtils.clone(phongShader.uniforms);
uniforms.particleWorldPosTex = { value: null };
uniforms.quatTex = { value: null };
var debugMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: sharedShaderCode.innerText + renderParticlesVertex.innerText,
fragmentShader: phongShader.fragmentShader,
lights: true,
defines: {
USE_MAP: true,
bodyTextureResolution: 'vec2(' + world.bodyTextureSize.toFixed(1) + ',' + world.bodyTextureSize.toFixed(1) + ')',
resolution: 'vec2(' + world.particleTextureSize.toFixed(1) + ',' + world.particleTextureSize.toFixed(1) + ')'
}
});
debugMesh = new THREE.Mesh( debugGeometry, debugMaterial );
debugMesh.frustumCulled = false;
var checkerTexture = new THREE.DataTexture(new Uint8Array([255,0,0,255, 255,255,255,255]), 2, 1, THREE.RGBAFormat, THREE.UnsignedByteType, THREE.UVMapping);
checkerTexture.needsUpdate = true;
debugMaterial.uniforms.map.value = checkerTexture;
initDebugGrid();
// Create an instanced mesh for cylinders
var cylinderGeometry = new THREE.CylinderBufferGeometry(radius, radius, 2*4*radius, 8);
cylinderGeometry.rotateZ(Math.PI / 2);// particles are spread along x, not y
var bodyInstances = numBodies*numBodies / 2;
var meshGeometry = new THREE.InstancedBufferGeometry();
meshGeometry.maxInstancedCount = bodyInstances;
for(var attributeName in cylinderGeometry.attributes){
meshGeometry.addAttribute( attributeName, cylinderGeometry.attributes[attributeName].clone() );
}
meshGeometry.setIndex( cylinderGeometry.index.clone() );
var bodyIndices = new THREE.InstancedBufferAttribute( new Float32Array( bodyInstances * 1 ), 1, 1 );
for ( var i = 0, ul = bodyIndices.count; i < ul; i++ ) {
bodyIndices.setX( i, i ); // one index per instance
}
meshGeometry.addAttribute( 'bodyIndex', bodyIndices );
meshGeometry.boundingSphere = null;
// Create an instanced mesh for boxes
var boxGeometry = new THREE.BoxBufferGeometry(4*radius, 4*radius, 2*radius, 8);
var bodyInstances = numBodies*numBodies / 2;
var boxMeshGeometry = new THREE.InstancedBufferGeometry();
boxMeshGeometry.maxInstancedCount = bodyInstances;
for(var attributeName in boxGeometry.attributes){
boxMeshGeometry.addAttribute( attributeName, boxGeometry.attributes[attributeName].clone() );
}
boxMeshGeometry.setIndex( boxGeometry.index.clone() );
var bodyIndices2 = new THREE.InstancedBufferAttribute( new Float32Array( bodyInstances * 1 ), 1, 1 );
for ( var i = 0, ul = bodyIndices2.count; i < ul; i++ ) {
bodyIndices2.setX( i, i + numBodies*numBodies / 2 ); // one index per instance
}
boxMeshGeometry.addAttribute( 'bodyIndex', bodyIndices2 );
boxMeshGeometry.boundingSphere = null;
// Mesh material - extend the phong shader
var meshUniforms = THREE.UniformsUtils.clone(phongShader.uniforms);
meshUniforms.bodyQuatTex = { value: null };
meshUniforms.bodyPosTex = { value: null };
var meshMaterial = new THREE.ShaderMaterial({
uniforms: meshUniforms,
vertexShader: sharedShaderCode.innerText + renderBodiesVertex.innerText,
fragmentShader: phongShader.fragmentShader,
lights: true,
defines: {
bodyTextureResolution: 'vec2(' + world.bodyTextureSize.toFixed(1) + ',' + world.bodyTextureSize.toFixed(1) + ')',
resolution: 'vec2(' + world.particleTextureSize.toFixed(1) + ',' + world.particleTextureSize.toFixed(1) + ')'
}
});
meshMesh = new THREE.Mesh( meshGeometry, meshMaterial );
meshMesh.frustumCulled = false; // Instances can't be culled like normal meshes
// Create a depth material for rendering instances to shadow map
meshMesh.customDepthMaterial = new THREE.ShaderMaterial({
uniforms: THREE.UniformsUtils.merge([
THREE.ShaderLib.depth.uniforms,
meshUniforms
]),
vertexShader: sharedShaderCode.innerText + renderBodiesDepth.innerText,
fragmentShader: THREE.ShaderLib.depth.fragmentShader,
defines: {
DEPTH_PACKING: 3201,
bodyTextureResolution: 'vec2(' + world.bodyTextureSize.toFixed(1) + ',' + world.bodyTextureSize.toFixed(1) + ')',
resolution: 'vec2(' + world.particleTextureSize.toFixed(1) + ',' + world.particleTextureSize.toFixed(1) + ')'
}
});
meshMesh.castShadow = true;
meshMesh.receiveShadow = true;
scene.add( meshMesh );
meshMesh2 = new THREE.Mesh( boxMeshGeometry, meshMaterial );
meshMesh2.customDepthMaterial = meshMesh.customDepthMaterial;
meshMesh2.frustumCulled = false; // Instances can't be culled like normal meshes
meshMesh2.castShadow = true;
meshMesh2.receiveShadow = true;
scene.add( meshMesh2 );
// interaction
interactionSphereMesh = new THREE.Mesh(new THREE.SphereBufferGeometry(1,16,16), new THREE.MeshPhongMaterial({ color: 0xffffff }));
scene.add(interactionSphereMesh);
gizmo = new THREE.TransformControls( camera, renderer.domElement );
gizmo.addEventListener( 'change', function(){
if(this.object === interactionSphereMesh){
world.setSpherePosition(
0,
interactionSphereMesh.position.x,
interactionSphereMesh.position.y,
interactionSphereMesh.position.z
);
} else if(this.object === debugGridMesh){
world.broadphase.position.copy(debugGridMesh.position);
}
});
scene.add(gizmo);
gizmo.attach(interactionSphereMesh);
interactionSphereMesh.castShadow = true;
interactionSphereMesh.receiveShadow = true;
initGUI();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate( time ) {
requestAnimationFrame( animate );
updatePhysics( time );
render();
stats.update();
}
var prevTime, prevSpawnedBody=0;
function updatePhysics(time){
var deltaTime = prevTime === undefined ? 0 : (time - prevTime) / 1000;
if(!controller.paused){
if( controller.interaction === 'none') {
// Animate sphere
var introSweepPos = Math.max(0, 1 - world.fixedTime);
var x = 0.12*Math.sin(2 * 1.9 * world.fixedTime);
var y = 0.05*(Math.cos(2 * 2 * world.fixedTime)+0.5) + introSweepPos;
var z = 0.12*Math.cos(2 * 2.1 * world.fixedTime) + introSweepPos;
interactionSphereMesh.position.set( x, y, z );
world.setSpherePosition( 0, x, y, z );
}
/*
// Dynamic spawning
var x = 0.1*Math.sin(9 * world.fixedTime) + Math.random()*0.05;
var z = 0.1*Math.cos(10 * world.fixedTime) + Math.random()*0.05;
var y = 0.3 + 0.05*Math.cos(11 * world.fixedTime);
world.setBodyPositions([(prevSpawnedBody++) % world.maxBodies], [new THREE.Vector3(x,y,z)]);
*/
world.step( deltaTime );
}
prevTime = time;
}
function initDebugGrid(){
var w = world.broadphase.resolution.x * world.radius * 2;
var h = world.broadphase.resolution.y * world.radius * 2;
var d = world.broadphase.resolution.z * world.radius * 2;
var boxGeom = new THREE.BoxGeometry( w, h, d );
var wireframeMaterial = new THREE.MeshBasicMaterial({ wireframe: true });
debugGridMesh = new THREE.Object3D();
var mesh = new THREE.Mesh(boxGeom,wireframeMaterial);
debugGridMesh.add(mesh);
debugGridMesh.position.copy(world.broadphase.position);
mesh.position.set(w/2, h/2, d/2);
scene.add(debugGridMesh);
}
function updateDebugGrid(){
debugGridMesh.position.copy(world.broadphase.position);
}
function render() {
controls.update();
// Render main scene
updateDebugGrid();
meshMesh.material.uniforms.bodyPosTex.value = world.bodyPositionTexture;
meshMesh.material.uniforms.bodyQuatTex.value = world.bodyQuaternionTexture;
meshMesh.customDepthMaterial.uniforms.bodyPosTex.value = world.bodyPositionTexture;
meshMesh.customDepthMaterial.uniforms.bodyQuatTex.value = world.bodyQuaternionTexture;
debugMesh.material.uniforms.particleWorldPosTex.value = world.particlePositionTexture;
debugMesh.material.uniforms.quatTex.value = world.bodyQuaternionTexture;
renderer.render( scene, camera );
debugMesh.material.uniforms.particleWorldPosTex.value = null;
debugMesh.material.uniforms.quatTex.value = null;
}
function calculateBoxInertia(out, mass, extents){
var c = 1 / 12 * mass;
out.set(
c * ( 2 * extents.y * 2 * extents.y + 2 * extents.z * 2 * extents.z ),
c * ( 2 * extents.x * 2 * extents.x + 2 * extents.z * 2 * extents.z ),
c * ( 2 * extents.y * 2 * extents.y + 2 * extents.x * 2 * extents.x )
);
}
function initGUI(){
controller = {
moreObjects: function(){ location.href = "?n=" + (numParticles*2); },
lessObjects: function(){ location.href = "?n=" + Math.max(2,numParticles/2); },
paused: false,
renderParticles: false,
renderShadows: true,
gravity: world.gravity.y,
interaction: 'none',
sphereRadius: world.getSphereRadius(0)
};
function guiChanged() {
world.gravity.y = controller.gravity;
if(controller.renderParticles){
scene.remove(meshMesh, meshMesh2);
scene.add(debugMesh);
} else {
scene.remove(debugMesh);
scene.add(meshMesh, meshMesh2);
}
// Shadow rendering
renderer.shadowMap.autoUpdate = controller.renderShadows;
if(!controller.renderShadows){
renderer.clearTarget(light.shadow.map);
}
// Interaction
gizmo.detach(gizmo.object);
scene.remove(debugGridMesh);
switch(controller.interaction){
case 'sphere':
gizmo.attach(interactionSphereMesh);
break;
case 'broadphase':
scene.add(debugGridMesh);
gizmo.attach(debugGridMesh);
break;
}
var r = controller.sphereRadius;
interactionSphereMesh.scale.set(r,r,r);
world.setSphereRadius(0,r);
}
gui = new dat.GUI();
gui.add( world, "stiffness", 0, 5000, 0.1 );
gui.add( world, "damping", 0, 100, 0.1 );
gui.add( world, "drag", 0, 1, 0.01 );
gui.add( world, "friction", 0, 10, 0.001 );
gui.add( world, "fixedTimeStep", 0, 0.1, 0.001 );
gui.add( controller, "paused" ).onChange( guiChanged );
gui.add( controller, "gravity", -2, 2, 0.1 ).onChange( guiChanged );
gui.add( controller, "moreObjects" );
gui.add( controller, "lessObjects" );
gui.add( controller, "renderParticles" ).onChange( guiChanged );
gui.add( controller, "renderShadows" ).onChange( guiChanged );
gui.add( controller, 'interaction', [ 'none', 'sphere', 'broadphase' ] ).onChange( guiChanged );
gui.add( controller, 'sphereRadius', boxSize.x/10, boxSize.x/2 ).onChange( guiChanged );
guiChanged();
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
document.addEventListener('click', function( event ) {
mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( [interactionSphereMesh] );
if ( intersects.length > 0 ) {
controller.interaction = 'sphere';
gui.updateDisplay();
guiChanged();
}
});
}
function parseParams(){
return location.search
.substr(1)
.split("&")
.map(function(pair){
var a = pair.split("=");
var o = {};
o[a[0]] = a[1];
return o;
})
.reduce(function(a,b){
for(var key in b) a[key] = b[key];
return a;
});
}
})();