-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
209 lines (162 loc) · 6.08 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
<html>
<head>
<title>Leap Motion Web SDK</title>
<script src="script/three.js"></script>
<script src="leap-motion.js"></script>
</head>
<body>
<leap-motion></leap-motion>
<canvas></canvas>
</body>
<style>
html, body {
margin: 0;
height: 100%;
}
canvas {
width: 100%;
height: 100%;
display: block;
}
</style>
<script>
// ThreeJS
const canvas = document.getElementsByTagName("canvas")[0];
const renderer = new THREE.WebGLRenderer({canvas});
renderer.setPixelRatio(devicePixelRatio);
const camera = new THREE.PerspectiveCamera(
75, // Field of View (degrees)
2, // aspect ratio (width / height)
0.1, // near plane
1000, // far plane
);
camera.position.z = 250;
camera.position.y = 200;
camera.rotateX(-0.5);
const scene = new THREE.Scene();
const light = new THREE.DirectionalLight(
0xFFFFFF, // color
1, // intensity
);
light.position.set(-1, 2, 4);
scene.add(light);
const geometry = new THREE.BoxGeometry(
30, // width
30, // height
30, // depth
);
const material = new THREE.MeshPhongMaterial({
color : 0x44aa88,
});
const cube = new THREE.Mesh(geometry, material);
cube.position.set(0, 125, 0)
scene.add(cube);
function resize() {
const width = canvas.clientWidth;
const height = canvas.clientHeight;
camera.aspect = width/height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
var needsResizing = true;
window.addEventListener("resize", event => {
needsResizing = true;
});
function renderCallback() {
if(needsResizing) {
resize();
needsResizing = false;
}
cube.rotation.x = Date.now() / 1000;
cube.rotation.y = Date.now() / 500;
if(leapMotionElement.leapMotion !== undefined) {
const frame = leapMotionElement.leapMotion.frame;
if(frame !== undefined) {
handPoints.setFromHands(...frame.hands);
frame.hands.forEach(hand => {
if(hand.pinch.strength > 0.5 && cube.position.distanceTo(hand.fingers[1].btipPosition) < 45) {
const midpoint = new THREE.Vector3();
midpoint.add(hand.fingers[0].btipPosition);
midpoint.add(hand.fingers[1].btipPosition);
midpoint.multiplyScalar(0.5);
cube.position.copy(midpoint);
}
});
}
}
renderer.render(scene, camera);
requestAnimationFrame(renderCallback);
}
requestAnimationFrame(renderCallback);
</script>
<script>
// Drawing hands
class HandPoints {
constructor() {
this.geometry = new THREE.BufferGeometry();
this.material = new THREE.PointsMaterial({
size : 15,
vertexColors : THREE.VertexColors,
});
const positions = [];
const colors = [];
this.geometry.addAttribute(
"position",
new THREE.Float32BufferAttribute(this.positions, 3)
);
this.geometry.addAttribute(
"color",
new THREE.Float32BufferAttribute(this.colors, 3)
);
this.geometry.colorsNeedUpdate = true;
this.points = new THREE.Points(
this.geometry,
this.material
);
this.points.frustumCulled = false;
scene.add(this.points);
}
setFromHands(...hands) {
const positions = [];
const colors = [];
hands.forEach(hand => {
positions.push(...hand.elbow.toArray());
colors.push(1, 1, 1);
positions.push(...hand.wrist.toArray());
colors.push(1, 1, 1);
positions.push(...hand.palm.position.toArray());
colors.push(0.5, 0.5, 0.5);
hand.fingers.forEach((finger, index) => {
positions.push(...finger.carpPosition.toArray());
colors.push(1, 1, 1);
positions.push(...finger.mcpPosition.toArray());
colors.push(1, 1, 102/255);
positions.push(...finger.pipPosition.toArray());
colors.push(51/255, 204/255, 51/255);
positions.push(...finger.dipPosition.toArray());
colors.push(0, 153/255, 1);
positions.push(...finger.btipPosition.toArray());
colors.push(1, 102/255, 102/255);
});
});
this.geometry.addAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
this.geometry.addAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
}
}
var handPoints = new HandPoints();
</script>
<script>
// Leap Motion
const leapMotionElement = document.getElementsByTagName("leap-motion")[0];
leapMotionElement.leapMotion.open();
leapMotionElement.addEventListener("load", event => {
leapMotionElement.leapMotion.open();
});
</script>
</html>