forked from marlon360/webxr-handtracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand-model-leap-motion.html
303 lines (261 loc) Β· 12.9 KB
/
hand-model-leap-motion.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic Example - Hand Tracking</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script src="js/three.js"></script>
<script type="module">
import {VRButton} from './js/VRButton.js';
// init scene with background
var scene = new THREE.Scene();
scene.background = new THREE.Color( 0x808080 );
// init camera
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.y = 0.5;
// init renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
var xrRefSpace;
let boxes_left = [];
let bones_left = [];
let boxes_right = [];
let bones_right = [];
// skeleton for hands
let boxes = { left: boxes_left, right: boxes_right, leftBones: bones_left, rightBones: bones_right};
let jointConnections = [
{startJoint: 2, endJoint: 3},
{startJoint: 3, endJoint: 4},
{startJoint: 6, endJoint: 7},
{startJoint: 7, endJoint: 8},
{startJoint: 8, endJoint: 9},
{startJoint: 11, endJoint: 12},
{startJoint: 12, endJoint: 13},
{startJoint: 13, endJoint: 14},
{startJoint: 16, endJoint: 17},
{startJoint: 17, endJoint: 18},
{startJoint: 18, endJoint: 19},
{startJoint: 21, endJoint: 22},
{startJoint: 22, endJoint: 23},
{startJoint: 23, endJoint: 24},
{startJoint: 6, endJoint: 11},
{startJoint: 11, endJoint: 16},
{startJoint: 16, endJoint: 21},
{startJoint: 0, endJoint: 1},
{startJoint: 0, endJoint: 20},
{startJoint: 1, endJoint: 2},
{startJoint: 1, endJoint: 6},
{startJoint: 20, endJoint: 1},
{startJoint: 20, endJoint: 21},
];
function addBox(x, y, z, box_list, offset) {
var geometry = new THREE.SphereGeometry(1, 16, 16);
var material = new THREE.MeshStandardMaterial( {
color: 0xeeeeee,
roughness: 0.8,
metalness: 0.4
});
material.color.setRGB(0, 0.1, 1.0);
var cube = new THREE.Mesh( geometry, material );
cube.castShadow = true;
box_list.push({
mesh: cube,
position: [x, y, z],
offset: offset
});
}
function addBone(startJoint, endJoint, box_list) {
var cylindergeometry = new THREE.CylinderGeometry( 0.005, 0.005, 0.1, 16 );
var cylindermaterial = new THREE.MeshStandardMaterial( {
color: 0xffffff,
roughness: 0.8,
metalness: 0.4
});
var cylinder = new THREE.Mesh( cylindergeometry, cylindermaterial );
box_list.push({
mesh: cylinder,
startJoint,
endJoint
});
}
var updateCylinderMesh = function(mesh, vstart, vend){
var HALF_PI = Math.PI * .5;
var distance = vstart.distanceTo(vend);
var position = vend.clone().add(vstart).divideScalar(2);
mesh.geometry.dispose();
// TODO: creating a new geometry every frame is very bad for the performance
mesh.geometry = new THREE.CylinderBufferGeometry(0.006,0.006,distance,16);
var orientation = new THREE.Matrix4();//a new orientation matrix to offset pivot
var offsetRotation = new THREE.Matrix4();//a matrix to fix pivot rotation
var offsetPosition = new THREE.Matrix4();//a matrix to fix pivot position
orientation.lookAt(vstart,vend,new THREE.Vector3(0,1,0));//look at destination
offsetRotation.makeRotationX(HALF_PI);//rotate 90 degs on X
orientation.multiply(offsetRotation);//combine orientation with rotation transformations
mesh.geometry.applyMatrix4(orientation)
mesh.position.copy(position);
}
function initHands() {
for (const box of boxes_left) {
scene.remove(box.mesh);
}
for (const box of boxes_right) {
scene.remove(box.mesh);
}
for (const bone of bones_left) {
scene.remove(bone.mesh);
}
for (const bone of bones_right) {
scene.remove(bone.mesh);
}
boxes_left = [];
bones_left = [];
boxes_right = [];
bones_right = [];
// skeleton for hands
boxes = { left: boxes_left, right: boxes_right, leftBones: bones_left, rightBones: bones_right};
if (XRHand) {
for (let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i++) {
addBox(0, 0, 0, boxes_left, i);
addBox(0, 0, 0, boxes_right, i);
}
for (const connection of jointConnections) {
addBone(connection.startJoint, connection.endJoint, bones_left);
addBone(connection.startJoint, connection.endJoint, bones_right);
}
}
}
renderer.xr.onSessionStartedCallback = (session) => {
// create skelton meshes
initHands();
// get xrRefSpace
session.requestReferenceSpace('local').then((refSpace) => {
xrRefSpace = refSpace.getOffsetReferenceSpace(new XRRigidTransform({x: 0, y: 1.5, z: 0}));
});
};
// add canvas to dom
document.body.appendChild( renderer.domElement );
// add enter vr button to dom
document.body.appendChild( VRButton.createButton( renderer ) );
// set color and shadows
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
// enable xr
renderer.xr.enabled = true;
var geometry = new THREE.SphereGeometry(0.2, 32, 32);
var material = new THREE.MeshStandardMaterial({color: 0xff4466, roughness: 0.8, metalness: 0.2});
var cube = new THREE.Mesh( geometry, material );
cube.position.z = -0.5;
cube.position.y = 1;
cube.castShadow = true;
scene.add( cube );
function createEnvironment() {
var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
var material = new THREE.MeshStandardMaterial( {
color: 0xeeeeee,
roughness: 1.0,
metalness: 0.0
});
var floor = new THREE.Mesh( geometry, material );
floor.rotation.x = - Math.PI / 2;
floor.receiveShadow = true;
scene.add( floor );
scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
var light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 6, 0 );
light.castShadow = true;
light.shadow.camera.top = 2;
light.shadow.camera.bottom = - 2;
light.shadow.camera.right = 2;
light.shadow.camera.left = - 2;
light.shadow.mapSize.set( 4096, 4096 );
scene.add( light );
}
function updateInputSources(session, frame, refSpace) {
for (let inputSource of session.inputSources) {
if (!inputSource.hand) {
continue;
} else {
let visible = false;
for (const box of boxes[inputSource.handedness]) {
let jointPose = null;
if (inputSource.hand[box.offset] !== null) {
jointPose = frame.getJointPose(inputSource.hand[box.offset], refSpace);
}
if (jointPose !== null) {
visible = true;
scene.add(box.mesh);
box.mesh.position.set(jointPose.transform.position.x, jointPose.transform.position.y + 1.5, jointPose.transform.position.z);
const q = new THREE.Quaternion(jointPose.transform.orientation.x, jointPose.transform.orientation.y, jointPose.transform.orientation.z, jointPose.transform.orientation.w);
box.mesh.quaternion.copy(q);
const radius = 0.008;
box.mesh.scale.set(radius, radius, radius);
} else {
scene.remove(box.mesh);
}
}
if (visible) {
for (const bone of boxes.leftBones) {
scene.add(bone.mesh);
updateCylinderMesh(bone.mesh, boxes.left[bone.startJoint].mesh.position, boxes.left[bone.endJoint].mesh.position);
}
for (const bone of boxes.rightBones) {
scene.add(bone.mesh);
updateCylinderMesh(bone.mesh, boxes.right[bone.startJoint].mesh.position, boxes.right[bone.endJoint].mesh.position);
}
} else {
for (const bone of boxes.leftBones) {
scene.remove(bone.mesh);
}
for (const bone of boxes.rightBones) {
scene.remove(bone.mesh);
}
}
}
}
}
function grabCheck(hand) {
const indexTip = hand[XRHand.INDEX_PHALANX_TIP].mesh;
const thumbTip = hand[XRHand.THUMB_PHALANX_TIP].mesh;
const distance = indexTip.position.distanceTo(thumbTip.position);
return distance < 0.01;
}
let offsetLeft = null;
let offsetRight = null;
renderer.setAnimationLoop( function (time, frame) {
if (frame != null && xrRefSpace != null) {
updateInputSources(renderer.xr.getSession(), frame, xrRefSpace);
cube.geometry.computeBoundingBox();
var sphereBBox = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
sphereBBox.setFromObject(cube);
if (grabCheck(boxes.left)) {
if (sphereBBox.containsPoint(boxes.left[XRHand.INDEX_PHALANX_TIP].mesh.position)) {
if (offsetLeft === null) {
offsetLeft = new THREE.Vector3().subVectors(boxes.left[XRHand.INDEX_PHALANX_TIP].mesh.position, cube.position);
}
cube.position.set(boxes.left[XRHand.INDEX_PHALANX_TIP].mesh.position.x - offsetLeft.x, boxes.left[XRHand.INDEX_PHALANX_TIP].mesh.position.y - offsetLeft.y, boxes.left[XRHand.INDEX_PHALANX_TIP].mesh.position.z - offsetLeft.z);
}
} else {
offsetLeft = null;
}
if (grabCheck(boxes.right)) {
if (sphereBBox.containsPoint(boxes.right[8].mesh.position)) {
if (offsetRight === null) {
offsetRight = new THREE.Vector3().subVectors(boxes.right[XRHand.INDEX_PHALANX_TIP].mesh.position, cube.position);
}
cube.position.set(boxes.right[XRHand.INDEX_PHALANX_TIP].mesh.position.x - offsetRight.x, boxes.right[XRHand.INDEX_PHALANX_TIP].mesh.position.y - offsetRight.y, boxes.right[XRHand.INDEX_PHALANX_TIP].mesh.position.z - offsetRight.z);
}
} else {
offsetRight = null;
}
}
renderer.render( scene, camera );
} );
createEnvironment();
</script>
</body>
</html>