forked from marlon360/webxr-handtracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.html
197 lines (167 loc) Β· 8.12 KB
/
basic.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
<!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 boxes_right = [];
// skeleton for hands
let boxes = { left: boxes_left, right: boxes_right};
function addBox(x, y, z, box_list, offset) {
var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial();
material.color.setRGB(0, 0.2, 0.7);
var cube = new THREE.Mesh( geometry, material );
cube.castShadow = true;
box_list.push({
mesh: cube,
position: [x, y, z],
offset: offset
});
}
function initHands() {
for (const box of boxes_left) {
scene.remove(box.mesh);
}
for (const box of boxes_right) {
scene.remove(box.mesh);
}
boxes_left = [];
boxes_right = [];
boxes = { left: boxes_left, right: boxes_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);
}
}
}
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 {
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) {
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);
box.mesh.scale.set(jointPose.radius, jointPose.radius, jointPose.radius);
} else {
scene.remove(box.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>