-
Notifications
You must be signed in to change notification settings - Fork 0
/
headtrackr_test.html
237 lines (198 loc) · 7.02 KB
/
headtrackr_test.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
<html>
<head>
<title>Headoriented perspective using Headtrackr</title>
<script src="three.js-master/build/three.js"></script>
<!--<script src="three.js-master/examples/js/controls/OrbitControls.js"></script>-->
<script src="libs/headtrackr.js"></script>
<script src="dat.gui-master/build/dat.gui.js"></script>
</head>
<body>
<script>
"use strict";
/*
See:
https://www.auduno.com/headtrackr/documentation/reference.html
https://www.auduno.com/2012/06/15/head-tracking-with-webrtc/
http://web.mit.edu/ebfox/Public/main.pdf
Plan: Improve headtrackr library, then improve demo. The goal is to have a cool (scary) demo with eye-oriented anaglyph rendering.
Add better calibration control to library and to demo. (Modifiable camera offsets, fov, assumed head size etc. during use.)
Try to adapt camshift to use hue,saturation and ignore brightness.
Try to do detection and tracking of colored 3D glasses instead of head. Should be easier, really.
*/
//https needed for webcam access
if (window.location.protocol === "http") {
window.location.protocol = "https";
}
//Globals
var /*clock, */renderer, scene, camera, camContainer, gui, ht;//, controls;
var ceilLight;
(function main() {
//Renderer setup
//clock = new THREE.Clock(/*false*/); //false vil skru av autostart
document.body.style = "overflow: hidden;";
var container = document.createElement("div");
container.style = "position: absolute; top: 0; left: 0;"
document.body.appendChild(container);
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setClearColor(0xaaaaff);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
//Scene setup:
scene = new THREE.Scene();
scene.add(new THREE.AxisHelper(1000));
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
//ROOM
let B = 1500;
let H = 2500;
let L = 5000;
let wallMat = new THREE.MeshPhongMaterial({color: 0xbb8833, side: THREE.FrontSide});
let floorMat = new THREE.MeshPhongMaterial({color: 0xbbbbbb, side: THREE.FrontSide});
let ceilMat = new THREE.MeshPhongMaterial({color: 0xdddddd, side: THREE.FrontSide});
let roomGeom = new THREE.BoxBufferGeometry(1,1,1,1,1,1);
roomGeom.translate(0,0.5,0);
let room = new THREE.Mesh(
roomGeom,
[wallMat, wallMat, ceilMat, floorMat, wallMat, new THREE.MeshBasicMaterial({side: THREE.FrontSide})]);
room.scale.set(B,H,L);
scene.add(room);
//Lights
ceilLight = new THREE.Group();
ceilLight.position.y = H;
let lightSocket = new THREE.Mesh(
new THREE.ConeBufferGeometry(100, 170, 32),
new THREE.MeshPhongMaterial({color: 0xffffff})
);
lightSocket.position.y = -85;
lightSocket.rotation.x = Math.PI;
ceilLight.add(lightSocket);
let bulb = new THREE.Mesh(
new THREE.SphereBufferGeometry(80, 32, 16),
new THREE.MeshPhongMaterial({color: 0xffff77})
);
bulb.position.y = -100;
ceilLight.add(bulb);
let pl = new THREE.PointLight(0xffff77, 4, 10000);
ceilLight.add(pl);
scene.add(ceilLight);
let standLight = new THREE.Group();
pl = new THREE.PointLight(0xffff77, 1, 10000);
pl.position.set(-0.35*B, 0.6*H, 0.35*L);
scene.add(standLight);
//Window
let depth = 50;
let winCont = new THREE.Group();
winCont.position.set(0.3*B, 0.4*H, -0.5*L-depth);
let wMat = new THREE.MeshPhongMaterial({color: 0xffffff, transparent: true, opacity: 0.1, side: THREE.FrontSide});
let concrete = new THREE.MeshLambertMaterial({color: 0xbbbbbb, side: THREE.FrontSide});
let winGeom = new THREE.BoxBufferGeometry(1,1,1,1,1,1);
winGeom.translate(0,0,0.5);
let win = new THREE.Mesh(
//Should match the renderer size
winGeom,
[concrete, concrete, concrete, concrete,
wMat, new THREE.MeshBasicMaterial({side: THREE.FrontSide})]
);
win.scale.set(200,200,depth);
winCont.add(win);
scene.add(winCont);
//Camera setup
camera = new THREE.PerspectiveCamera(15, window.innerWidth/window.innerHeight, 1, 100000);
camera.position.z = 600;
window.addEventListener('resize', function() {
//camera.aspect = window.innerWidth / window.innerHeight;
//camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
//document.documentElement.webkitRequestFullscreen();
camContainer = new THREE.Group();
camContainer.add(camera);
camContainer.scale.z = -1;
//camContainer.lookAt(new THREE.Vector3(0, 0.5*H, 0));
winCont.add(camContainer);
//DEBUG
/*camera.fov = 50;
camera.updateProjectionMatrix();
camera.rotation.set(0.5, 6.1, 3.6);*/
let vid = document.createElement("video");
vid.autoplay = true;
vid.loop = true; //right?
let canv = document.createElement("canvas");
canv.width = 640; //dimensions necessary??
canv.height = 480;
/*Object.assign(canv.style, {
position: "fixed",
top: 0,
left: 0,
"z-index": 10,
display: "None"
});
document.body.appendChild(canv);
canv.appendChild(vid);*/
/*document.addEventListener("headtrackrStatus", function(e) {
console.log(e.status);
});*/
//DEBUG
let headDisp = document.createElement("p");
Object.assign(headDisp.style, {
position: "fixed",
top: "1vh",
left: "1vw",
"z-index": 100,
display: "block",
color: "yellow",
"font-size": "2vh"
});
document.body.appendChild(headDisp);
//Do I need some params anyway?
ht = new headtrackr.Tracker({});
ht.init(vid, canv);
ht.start();
document.addEventListener("headtrackingEvent", function(e) {
//I will use millimeters:
let headX = 10*e.x;//removed inversion
let headY = 10*e.y;
let headZ = 10*e.z;
//console.log("head position=(%d,%d,%d)", headX,headY, headZ);
//DEBUG
headX = Math.min(Math.max(headX, -500), 500);
headY = Math.min(Math.max(headY, -500), 500);
headZ = Math.min(Math.max(headZ, 0), 1000);
headDisp.innerHTML = ["x: ", headX.toFixed(0), ", y: ", headY.toFixed(0), ", z: ", headZ.toFixed(0)].join("");
//camera.position.z = headZ;
//Testing:
camera.position.set(headX, headY, headZ);
let {width: w, height: h} = renderer.getSize();
//Example: (96 csspx / 25.4 mm (1 in)) * 1.25 px/csspx ~= 4.72 px/mm
let pxpermm = (96/25.4)*window.devicePixelRatio;
//console.log("pxpermm: ", pxpermm);
//Size of renderer (screen) in mm:
let rw = w/pxpermm;
let rh = h/pxpermm;
//Scale window frame to fit screen:
win.scale.set(rw,rh,depth);
//Proceed by imagining a bigger camera and screen that
//we will use only a part of:
//Set width, height, fov and aspect of "full" camera:
let W = rw + 2*Math.abs(headX);
let H = rh + 2*Math.abs(headY);
camera.fov = 2*Math.atan(0.5*H/Math.abs(headZ))*180/Math.PI;
camera.aspect = W/H;
//Set view within "full" camera:
//I think I get this very wrong, currently.
let offsetX = headX <= 0 ? -2*headX: 0;
let offsetY = headY <= 0 ? 0: 2*headY;
camera.setViewOffset(W, H, offsetX, offsetY, rw, rh);
camera.updateProjectionMatrix();
//console.log("headtrackingEvent: camera.position.z=%2f, camera.fov=%.2f ", camera.position.z, camera.fov, "camera.view=", camera.view);
});
animate();
})();
function animate() {
requestAnimationFrame(animate);
//var dt = clock.getDelta();
//var t = clock.getElapsedTime();
renderer.render(scene, camera);
}
</script>
</body>
</html>