-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
435 lines (357 loc) · 13 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin-top: 0px !important;
margin-bottom: 0px !important;
}
</style>
</head>
<body>
<script src="build/PBD.wasm.js"></script>
<script>
let i = 0;
document.addEventListener("readystatechange", async () => {
if (i)
return;
i++;
window["P"] = await PBD();
window["r"]();
});
</script>
<H1>Wasm version</H1>
<a href="https://matthias-research.github.io/pages/challenges/bodyChainPBD.html">Js version</a> ,by Matthias Müller of Nvidia<br>
Grab the boxes if you can. Hit [F5] to restart. <br>
<div id="container"></div>
<script src="https://threejs.org/build/three.js"></script>
<script type="module">
// physics scene
var gravity = new THREE.Vector3(0.0, -10.0, 0.0);
var numSubsteps = 40;
var timeStep = 1.0 / 60.0;
var numObjects = 100;
var objectsSize = new THREE.Vector3(0.02, 0.04, 0.02);
var lastObjectsSize = new THREE.Vector3(0.2, 0.04, 0.2);
var rotDamping = 1000;
var posDamping = 1000;
var bodies = [];
var joints = [];
var boxes = [];
let simulator;
// visual scene
var scene;
var camera;
var renderer;
var container = document.getElementById('container');
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var grabDistance = 0.0;
var grabJoint = null;
var raycaster = new THREE.Raycaster();
raycaster.layers.set(1);
raycaster.params.Mesh.threshold = 3;
var mousePos = new THREE.Vector2();
var mouseDown = false;
var cameraLookAt = new THREE.Vector3(0.0, 3.0, 0.0);
var camPosition = new THREE.Vector3(0.0, 3.0, 4.0);
var cameraDirX = new THREE.Vector3(0.0, 0.0, 0.0);
var cameraDirY = new THREE.Vector3(0.0, 0.0, 0.0);
var cameraDirZ = new THREE.Vector3(0.0, 0.0, 0.0);
function init() {
scene = new THREE.Scene();
// Lights
scene.add(new THREE.AmbientLight(0x505050));
scene.fog = new THREE.Fog(0x000000, 0, 15);
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.angle = Math.PI / 5;
spotLight.penumbra = 0.2;
spotLight.position.set(2, 3, 3);
spotLight.castShadow = true;
spotLight.shadow.camera.near = 3;
spotLight.shadow.camera.far = 10;
spotLight.shadow.mapSize.width = 1024;
spotLight.shadow.mapSize.height = 1024;
scene.add(spotLight);
var dirLight = new THREE.DirectionalLight(0x55505a, 1);
dirLight.position.set(0, 3, 0);
dirLight.castShadow = true;
dirLight.shadow.camera.near = 1;
dirLight.shadow.camera.far = 10;
dirLight.shadow.camera.right = 1;
dirLight.shadow.camera.left = - 1;
dirLight.shadow.camera.top = 1;
dirLight.shadow.camera.bottom = - 1;
dirLight.shadow.mapSize.width = 1024;
dirLight.shadow.mapSize.height = 1024;
scene.add(dirLight);
// Geometry
var material = new THREE.MeshPhongMaterial({
color: 0x80ee10,
shininess: 100,
side: THREE.DoubleSide
});
var ground = new THREE.Mesh(
new THREE.PlaneBufferGeometry(20, 20, 1, 1),
new THREE.MeshPhongMaterial({ color: 0xa0adaf, shininess: 150 })
);
ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
ground.receiveShadow = true;
scene.add(ground);
var helper = new THREE.GridHelper(20, 20);
helper.material.opacity = 0.5;
helper.material.transparent = true;
helper.position.set(0, 0.001, 0);
scene.add(helper);
// Renderer
renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(0.9 * window.innerWidth, window.innerHeight - container.offsetTop);
window.addEventListener('resize', onWindowResize, false);
container.appendChild(renderer.domElement);
// Camera
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 100);
camera.position.set(camPosition.x, camPosition.y, camPosition.z);
camera.lookAt(cameraLookAt);
camera.updateMatrixWorld();
scene.add(camera);
createObjects();
// interaction
container.addEventListener('mousedown', onMouse, false);
container.addEventListener('mousemove', onMouse, false);
container.addEventListener('mouseup', onMouse, false);
container.addEventListener('wheel', onWheel, false);
container.addEventListener('touchstart', onTouch, false);
container.addEventListener('touchend', onTouch, false);
container.addEventListener('touchmove', onTouch, false);
}
// create objects -----------------------------------------------------------
function setQ(to, from) {
to.set_x(from.x);
to.set_y(from.y);
to.set_z(from.z);
to.set_w(from.w);
}
function setV(v, x, y, z) {
v.set_x(x);
v.set_y(y);
v.set_z(z);
}
function addV(addTo, addFrom) {
addTo.x += addFrom.x;
addTo.y += addFrom.y;
addTo.z += addFrom.z;
}
function tvToglmv(tv) {
let v = new P.vec3();
setV(v, tv.x, tv.y, tv.z);
return v;
}
function createObjects() {
simulator = new P.Simulator();
gravity = simulator.getG();
// setV(gravity, 0.0, -10.0, 0.0);
let pos = new THREE.Vector3(0.0, (numObjects * objectsSize.y + lastObjectsSize.y) * 1.4 + 0.2, 0.0);
let pose = new P.Pose();
let lastBody = null;
let jointPose0 = new P.Pose();
let jointPose1 = new P.Pose();
let q = new THREE.Quaternion(0.0, 0.0, 0.0, 1.0);
setQ(jointPose0.q, q.setFromAxisAngle(new THREE.Vector3(0.0, 0.0, 1.0), 0.5 * Math.PI))
setQ(jointPose1.q, q.setFromAxisAngle(new THREE.Vector3(0.0, 0.0, 1.0), 0.5 * Math.PI));
let lastSize = objectsSize.clone();
for (let i = 0; i < numObjects; i++) {
let size = i < numObjects - 1 ? objectsSize : lastObjectsSize;
// graphics
let boxVis = new THREE.Mesh(new THREE.BoxBufferGeometry(), new THREE.MeshPhongMaterial({ color: 0xf78a1d }));
boxVis.scale.set(size.x, size.y, size.z);
boxVis.name = "box " + i;
boxVis.layers.enable(1);
boxVis.castShadow = true;
boxVis.receiveShadow = true;
scene.add(boxVis);
// physics
setV(pose.p, pos.x, pos.y - i * objectsSize.y, pos.z);
let boxBody = new P.Body(pose);
boxBody.setBox(tvToglmv(size));
let s = i % 2 == 0 ? -0.5 : 0.5;
setV(jointPose0.p, s * size.x, 0.5 * size.y, s * size.z);
setV(jointPose1.p, s * lastSize.x, -0.5 * lastSize.y, s * lastSize.z);
if (!lastBody) {
jointPose1.copy(jointPose0);
addV(jointPose1.p, pose.p);
}
boxBody = simulator.addBody(boxBody);
boxVis.userData.body = boxBody;
boxes.push(boxVis);
bodies.push(boxBody);
let joint = new P.Joint(0, boxBody, lastBody, jointPose0, jointPose1);
joint.rotDamping = rotDamping;
joint.posDamping = posDamping;
joint = simulator.addJoint(joint);
joints.push(joint);
lastBody = boxBody;
lastSize.copy(size);
}
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate(ts) {
render(timeStep);
requestAnimationFrame(animate);
}
function render(ts) {
renderer.render(scene, camera);
simulator.simulate(ts, numSubsteps, gravity);
for (let i = 0; i < boxes.length; i++) {
let q = boxes[i].userData.body.pose.q;
let p = boxes[i].userData.body.pose.p;
boxes[i].quaternion.set(q.x, q.y, q.z, q.w);
boxes[i].position.set(p.x, p.y, p.z);
}
}
// scene interaction -----------------------------------------------------------
function handleCameraMotion(dx, dy) {
let scale = 0.01;
let prev = camera.position.distanceTo(cameraLookAt);
cameraDirZ.subVectors(cameraLookAt, camera.position);
cameraDirX.set(cameraDirZ.z, 0.0, -cameraDirZ.x, 0.0);
cameraDirX.normalize();
cameraDirY.crossVectors(cameraDirZ, cameraDirX);
cameraDirY.normalize();
camera.position.addScaledVector(cameraDirX, scale * dx);
camera.position.addScaledVector(cameraDirY, scale * dy);
cameraDirZ.subVectors(cameraLookAt, camera.position);
cameraDirZ.normalize();
let delta = camera.position.distanceTo(cameraLookAt) - prev;
camera.position.addScaledVector(cameraDirZ, -delta);
camera.lookAt(cameraLookAt);
}
function handleCameraZoom(d) {
let scale = 1.0 - 0.003 * d;
let dist = camera.position.distanceTo(cameraLookAt);
if (d > 0.0 && dist < 0.2 || d < 0.0 && dist > 5.0)
return;
camera.position.set(
cameraLookAt.x + (camera.position.x - cameraLookAt.x) * scale,
cameraLookAt.y + (camera.position.y - cameraLookAt.y) * scale,
cameraLookAt.z + (camera.position.z - cameraLookAt.z) * scale);
camera.updateMatrixWorld();
}
function startGrab(x, y) {
var rect = renderer.domElement.getBoundingClientRect();
mousePos.x = ((x - rect.left) / rect.width) * 2 - 1;
mousePos.y = -((y - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(mousePos, camera);
var intersects = raycaster.intersectObjects(scene.children);
if (intersects.length > 0) {
let body = intersects[0].object.userData.body;
grabDistance = intersects[0].distance;
let pose0 = new P.Pose();
let pose1 = new P.Pose();
let hit = raycaster.ray.origin.clone();
hit.addScaledVector(raycaster.ray.direction, grabDistance);
setV(pose1.p, hit.x, hit.y, hit.z);
body.pose.invTransform(hit);
setV(pose0.p, hit.x, hit.y, hit.z);
grabJoint = new P.Joint(0, body, null, pose0, pose1);
grabJoint.compliance = 10;
grabJoint = simulator.addJoint(grabJoint);
}
}
function addScaledVector(to, v, s) {
to.x += v.x * s;
to.y += v.y * s;
to.z += v.z * s;
}
function moveGrabbed(x, y) {
if (!grabJoint)
return;
var rect = renderer.domElement.getBoundingClientRect();
mousePos.x = ((x - rect.left) / rect.width) * 2 - 1;
mousePos.y = -((y - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(mousePos, camera);
setV(grabJoint.localPose1.p, raycaster.ray.origin.x, raycaster.ray.origin.y, raycaster.ray.origin.z);
addScaledVector(grabJoint.localPose1.p, raycaster.ray.direction, grabDistance);
}
function endGrab() {
if (grabJoint) {
simulator.popJoint();
grabJoint = null;
}
}
function onMouse(evt) {
event.preventDefault();
if (evt.type == "mousedown") {
startGrab(evt.clientX, evt.clientY);
mouseDown = true;
}
else if (evt.type == "mousemove" && mouseDown) {
if (grabJoint)
moveGrabbed(evt.clientX, evt.clientY);
else
handleCameraMotion(evt.movementX, evt.movementY);
}
else if (evt.type == "mouseup" || evt.type == "mouseout") {
mouseDown = false;
endGrab();
}
}
function onWheel(evt) {
handleCameraZoom(-evt.deltaY * 0.1);
}
var touchCoords = [{ x: 0.0, y: 0.0 }, { x: 0.0, y: 0.0 }];
function onTouch(evt) {
event.preventDefault();
if (evt.type == "touchdown") {
if (evt.touches.length == 1)
startGrab(evt.touches[0].clientX, evt.touches[0].clientY);
}
if (evt.type == "touchmove") {
if (grabJoint) {
moveGrabbed(evt.touches[0].clientX, evt.touches[0].clientY);
}
else {
if (evt.touches.length == 1) {
handleCameraMotion(
evt.touches[0].clientX - touchCoords[0].x,
evt.touches[0].clientY - touchCoords[0].y);
}
else if (evt.touches.length == 2) {
let px = (touchCoords[1].x - touchCoords[0].x);
let py = (touchCoords[1].y - touchCoords[0].y);
let p = Math.sqrt(px * px + py * py);
let dx = (evt.touches[1].clientX - evt.touches[0].clientX);
let dy = (evt.touches[1].clientY - evt.touches[0].clientY);
let d = Math.sqrt(dx * dx + dy * dy);
handleCameraZoom(d - p);
}
}
}
else if (evt.type == "touchup") {
if (evt.touches.length == 0)
endGrab();
}
if (evt.touches.length <= 2) {
for (let i = 0; i < evt.touches.length; i++) {
touchCoords[i].x = evt.touches[i].clientX;
touchCoords[i].y = evt.touches[i].clientY;
}
}
}
window["r"] = () => {
init();
animate(1 / 60);
}
</script>
</body>
</html>