-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bvh_Colision_Sample.html
598 lines (423 loc) · 19.7 KB
/
Bvh_Colision_Sample.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bvh_Colision_Sample</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>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
<body>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/",
"three-mesh-bvh": "https://cdn.jsdelivr.net/npm/[email protected]/build/index.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { MeshBVH, MeshBVHHelper, StaticGeometryGenerator } from 'three-mesh-bvh';
import Stats from 'three/addons/stats.module.js';
import { GLTFLoader } from 'three/addons/GLTFLoader.js';
import { OrbitControls } from 'three/addons/OrbitControls.js';
import { GUI } from 'three/addons/lil-gui.module.min.js';
const params = {
displayCollider: false,
displayBVH: false,
displayParents: false,
visualizeDepth: 10,
gravity: - 9.8,
physicsSteps: 5,
// TODO: support steps based on given sphere velocity / radius
simulationSpeed: 1,
sphereSize: 1,
pause: false,
step: () => {
const steps = params.physicsSteps;
for (let i = 0; i < steps; i++) {
update(0.016 / steps);
}
},
explode: explodeSpheres,
reset: reset,
};
let renderer, camera, scene, clock, gui, stats;
let environment, collider, visualizer;
const spheres = [];
const hits = [];
const tempSphere = new THREE.Sphere();
const deltaVec = new THREE.Vector3();
const tempVec = new THREE.Vector3();
const forwardVector = new THREE.Vector3(0, 0, 1);
init();
render();
function init() {
const bgColor = 0x263238 / 2;
// renderer setup
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(bgColor, 1);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild(renderer.domElement);
// scene setup
scene = new THREE.Scene();
scene.fog = new THREE.Fog(bgColor, 30, 70);
// lights
const light = new THREE.DirectionalLight(0xaaccff, 1);
light.position.set(1, 1.5, 1).multiplyScalar(50);
light.intensity = 0.25;
const shadowCam = light.shadow.camera;
shadowCam.bottom = shadowCam.left = - 10;
shadowCam.top = shadowCam.right = 10;
scene.add(light);
scene.add(new THREE.HemisphereLight(0x4488ff, 0x223344, 0.3));
// camera setup
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 50);
camera.position.set(10, 10, - 10);
camera.far = 100;
camera.updateProjectionMatrix();
window.camera = camera;
clock = new THREE.Clock();
new OrbitControls(camera, renderer.domElement);
// stats setup
stats = new Stats();
document.body.appendChild(stats.dom);
loadColliderEnvironment();
// dat.gui
gui = new GUI();
const visFolder = gui.addFolder('Visualization');
visFolder.add(params, 'displayCollider');
visFolder.add(params, 'displayBVH');
visFolder.add(params, 'displayParents').onChange(v => {
visualizer.displayParents = v;
visualizer.update();
});
visFolder.add(params, 'visualizeDepth', 1, 20, 1).onChange(v => {
visualizer.depth = v;
visualizer.update();
});
visFolder.open();
const physicsFolder = gui.addFolder('Physics');
physicsFolder.add(params, 'physicsSteps', 0, 30, 1);
physicsFolder.add(params, 'gravity', - 100, 100, 0.01).onChange(v => {
params.gravity = parseFloat(v);
});
physicsFolder.add(params, 'simulationSpeed', 0, 5, 0.01);
physicsFolder.add(params, 'sphereSize', 0.2, 5, 0.1);
physicsFolder.add(params, 'pause');
physicsFolder.add(params, 'step');
physicsFolder.open();
gui.add(params, 'explode');
gui.add(params, 'reset');
gui.open();
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
let x = 0;
let y = 0;
renderer.domElement.addEventListener('pointerdown', e => {
x = e.clientX;
y = e.clientY;
});
renderer.domElement.addEventListener('pointerup', e => {
const totalDelta = Math.abs(e.clientX - x) + Math.abs(e.clientY - y);
if (totalDelta > 2) return;
mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
mouse.y = - (e.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const sphere = createSphere();
sphere.position.copy(camera.position).addScaledVector(raycaster.ray.direction, 3);
sphere
.velocity
.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5)
.addScaledVector(raycaster.ray.direction, 10 * Math.random() + 15)
.multiplyScalar(0.5);
});
window.addEventListener('resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
window.createSphere = createSphere;
}
function loadColliderEnvironment() {
new GLTFLoader()
.load('https://raw.githubusercontent.com/gkjohnson/3d-demo-data/main/models/low-poly-jungle-scene/scene.gltf', res => {
environment = res.scene;
environment.scale.setScalar(0.05);
const pointLight = new THREE.PointLight(0x00ffff);
pointLight.distance = 7;
pointLight.position.set(- 100, - 40, 100);
environment.add(pointLight);
const porchLight = new THREE.PointLight(0xffdd66);
porchLight.distance = 15;
porchLight.intensity = 5;
porchLight.position.set(80, 80, 135);
porchLight.shadow.normalBias = 1e-2;
porchLight.shadow.bias = - 1e-3;
porchLight.shadow.mapSize.setScalar(1024);
porchLight.castShadow = true;
environment.add(porchLight);
// collect all geometries to merge
environment.updateMatrixWorld(true);
const staticGenerator = new StaticGeometryGenerator(environment);
staticGenerator.attributes = ['position'];
const mergedGeometry = staticGenerator.generate();
mergedGeometry.boundsTree = new MeshBVH(mergedGeometry);
collider = new THREE.Mesh(mergedGeometry);
collider.material.wireframe = true;
collider.material.opacity = 0.5;
collider.material.transparent = true;
visualizer = new MeshBVHHelper(collider, params.visualizeDepth);
scene.add(visualizer);
scene.add(collider);
scene.add(environment);
environment.traverse(c => {
if (c.material) {
c.castShadow = true;
c.receiveShadow = true;
c.material.shadowSide = 2;
}
});
});
}
function onCollide(object1, object2, point, normal, velocity, offset = 0) {
if (velocity < Math.max(Math.abs(0.04 * params.gravity), 5)) {
return;
}
// Create an animation when objects collide
const effectScale = Math.max(
object2 ?
Math.max(object1.collider.radius, object2.collider.radius) :
object1.collider.radius,
0.4
) * 2.0;
const plane = new THREE.Mesh(
new THREE.RingGeometry(0, 1, 30),
new THREE.MeshBasicMaterial({ side: 2, transparent: true, depthWrite: false })
);
plane.lifetime = 0;
plane.maxLifetime = 0.4;
plane.maxScale = effectScale * Math.max(Math.sin(Math.min(velocity / 200, 1) * Math.PI / 2), 0.35);
plane.position.copy(point).addScaledVector(normal, offset);
plane.quaternion.setFromUnitVectors(forwardVector, normal);
scene.add(plane);
hits.push(plane);
}
function createSphere() {
const white = new THREE.Color(0xffffff);
const color = new THREE.Color(0x263238 / 2).lerp(white, Math.random() * 0.5 + 0.5).convertSRGBToLinear();
const sphere = new THREE.Mesh(
new THREE.SphereGeometry(1, 20, 20),
new THREE.MeshStandardMaterial({ color })
);
scene.add(sphere);
sphere.castShadow = true;
sphere.receiveShadow = true;
sphere.material.shadowSide = 2;
const radius = 0.5 * params.sphereSize * (Math.random() * .2 + 0.6);
sphere.scale.setScalar(radius);
sphere.collider = new THREE.Sphere(sphere.position, radius);
sphere.velocity = new THREE.Vector3(0, 0, 0);
sphere.mass = Math.pow(radius, 3) * Math.PI * 4 / 3;
spheres.push(sphere);
return sphere;
}
function updateSphereCollisions(deltaTime) {
// TODO: Add visualization for velocity vector, collision vector, all intersection vectors
const bvh = collider.geometry.boundsTree;
for (let i = 0, l = spheres.length; i < l; i++) {
const sphere = spheres[i];
const sphereCollider = sphere.collider;
// move the sphere
sphere.velocity.y += params.gravity * deltaTime;
sphereCollider.center.addScaledVector(sphere.velocity, deltaTime);
// remove the spheres if they've left the world
if (sphereCollider.center.y < - 80) {
spheres.splice(i, 1);
i--;
l--;
sphere.material.dispose();
sphere.geometry.dispose();
scene.remove(sphere);
continue;
}
// get the sphere position in world space
tempSphere.copy(sphere.collider);
let collided = false;
bvh.shapecast({
intersectsBounds: box => {
return box.intersectsSphere(tempSphere);
},
intersectsTriangle: tri => {
// get delta between closest point and center
tri.closestPointToPoint(tempSphere.center, deltaVec);
deltaVec.sub(tempSphere.center);
const distance = deltaVec.length();
if (distance < tempSphere.radius) {
// move the sphere position to be outside the triangle
const radius = tempSphere.radius;
const depth = distance - radius;
deltaVec.multiplyScalar(1 / distance);
tempSphere.center.addScaledVector(deltaVec, depth);
collided = true;
}
},
boundsTraverseOrder: box => {
return box.distanceToPoint(tempSphere.center) - tempSphere.radius;
},
});
if (collided) {
// get the delta direction and reflect the velocity across it
deltaVec.subVectors(tempSphere.center, sphereCollider.center).normalize();
sphere.velocity.reflect(deltaVec);
// dampen the velocity and apply some drag
const dot = sphere.velocity.dot(deltaVec);
sphere.velocity.addScaledVector(deltaVec, - dot * 0.5);
sphere.velocity.multiplyScalar(Math.max(1.0 - deltaTime, 0));
// update the sphere collider position
sphereCollider.center.copy(tempSphere.center);
// find the point on the surface that was hit
tempVec
.copy(tempSphere.center)
.addScaledVector(deltaVec, - tempSphere.radius);
onCollide(sphere, null, tempVec, deltaVec, dot, 0.05);
}
}
// Handle sphere collisions
for (let i = 0, l = spheres.length; i < l; i++) {
const s1 = spheres[i];
const c1 = s1.collider;
for (let j = i + 1; j < l; j++) {
const s2 = spheres[j];
const c2 = s2.collider;
// If they actually intersected
deltaVec.subVectors(c1.center, c2.center);
const depth = deltaVec.length() - (c1.radius + c2.radius);
if (depth < 0) {
deltaVec.normalize();
// get the magnitude of the velocity in the hit direction
const v1dot = s1.velocity.dot(deltaVec);
const v2dot = s2.velocity.dot(deltaVec);
// distribute how much to offset the spheres based on how
// quickly they were going relative to each other. The ball
// that was moving should move back the most. Add a max value
// to avoid jitter.
const offsetRatio1 = Math.max(v1dot, 0.2);
const offsetRatio2 = Math.max(v2dot, 0.2);
const total = offsetRatio1 + offsetRatio2;
const ratio1 = offsetRatio1 / total;
const ratio2 = offsetRatio2 / total;
// correct the positioning of the spheres
c1.center.addScaledVector(deltaVec, - ratio1 * depth);
c2.center.addScaledVector(deltaVec, ratio2 * depth);
// Use the momentum formula to adjust velocities
const velocityDifference = new THREE.Vector3();
velocityDifference
.addScaledVector(deltaVec, - v1dot)
.addScaledVector(deltaVec, v2dot);
const velDiff = velocityDifference.length();
const m1 = s1.mass;
const m2 = s2.mass;
// Compute new velocities in the moving frame of the sphere that
// moved into the other.
let newVel1, newVel2;
const damping = 0.5;
if (velocityDifference.dot(s1.velocity) > velocityDifference.dot(s2.velocity)) {
newVel1 = damping * velDiff * (m1 - m2) / (m1 + m2);
newVel2 = damping * velDiff * 2 * m1 / (m1 + m2);
// remove any existing relative velocity from the moving sphere
newVel1 -= velDiff;
} else {
newVel1 = damping * velDiff * 2 * m2 / (m1 + m2);
newVel2 = damping * velDiff * (m2 - m1) / (m1 + m2);
// remove any existing relative velocity from the moving sphere
newVel2 -= velDiff;
}
// Apply new velocities
velocityDifference.normalize();
s1.velocity.addScaledVector(velocityDifference, newVel1);
s2.velocity.addScaledVector(velocityDifference, newVel2);
tempVec.copy(c1.center).addScaledVector(deltaVec, - c1.radius);
onCollide(s1, s2, tempVec, deltaVec, velDiff, 0);
}
}
s1.position.copy(c1.center);
}
}
function reset() {
spheres.forEach(s => {
s.material.dispose();
s.geometry.dispose();
scene.remove(s);
});
spheres.length = 0;
hits.forEach(h => {
h.material.dispose();
h.geometry.dispose();
scene.remove(h);
});
hits.length = 0;
}
function explodeSpheres() {
const temp = new THREE.Vector3();
spheres.forEach(s => {
temp.copy(s.position);
temp.y += 10;
temp.normalize();
s.velocity.addScaledVector(temp, 120);
});
}
// Update physics and animation
function update(delta) {
if (collider) {
const steps = params.physicsSteps;
for (let i = 0; i < steps; i++) {
updateSphereCollisions(delta / steps);
}
}
// Update collision animations
for (let i = 0, l = hits.length; i < l; i++) {
const hit = hits[i];
hit.lifetime += delta;
const ratio = hit.lifetime / hit.maxLifetime;
let scale = Math.sin(ratio * 4.5 * Math.PI / 4);
scale = 1.0 - Math.pow(1.0 - scale, 2);
hit.scale.setScalar(scale * hit.maxScale);
hit.material.opacity = 1.0 - Math.sin(ratio * 2 * Math.PI / 4);
if (ratio >= 1) {
hits.splice(i, 1);
hit.parent.remove(hit);
hit.geometry.dispose();
hit.material.dispose();
i--;
l--;
}
}
}
function render() {
stats.update();
requestAnimationFrame(render);
const delta = Math.min(clock.getDelta(), 0.1);
if (collider) {
collider.visible = params.displayCollider;
visualizer.visible = params.displayBVH;
if (!params.pause) {
update(params.simulationSpeed * delta);
}
}
renderer.render(scene, camera);
}
</script>
</body>
</html>