-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheadcoupledperspective.html
163 lines (137 loc) · 5.44 KB
/
headcoupledperspective.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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Head coupled perspective demo</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<video autoplay style="height:180px; width: 240px;"></video>
<script src="js/three.js"></script>
<script>
const cameraZ = 2000;
// Most of the three.js related code is adapted from
// https://www.auduno.com/headtrackr/examples, see also
// https://github.com/auduno/headtrackr
var camera, scene, renderer;
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(20,
window.innerWidth / window.innerHeight, 1, 1000000 );
camera.position.z = cameraZ;
scene.add( camera );
// Planes
//top wall
plane1 = new THREE.Mesh( new THREE.PlaneGeometry( 500, 3000, 5, 15 ),
new THREE.MeshBasicMaterial( { color: 0x2194ce, wireframe : true } ) );
plane1.rotation.x = Math.PI/2;
plane1.position.y = 250;
plane1.position.z = 50-1500;
scene.add( plane1 );
//left wall
plane2 = new THREE.Mesh( new THREE.PlaneGeometry( 3000, 500, 15, 5 ),
new THREE.MeshBasicMaterial( { color: 0x2194ce, wireframe : true } ) );
plane2.rotation.y = Math.PI/2;
plane2.position.x = -250;
plane2.position.z = 50-1500;
scene.add( plane2 );
//right wall
plane3 = new THREE.Mesh( new THREE.PlaneGeometry( 3000, 500, 15, 5 ),
new THREE.MeshBasicMaterial( { color: 0x2194ce, wireframe : true } ) );
plane3.rotation.y = -Math.PI/2;
plane3.position.x = 250;
plane3.position.z = 50-1500;
scene.add( plane3 );
//bottom wall
plane4 = new THREE.Mesh( new THREE.PlaneGeometry( 500, 3000, 5, 15 ),
new THREE.MeshBasicMaterial( { color: 0x2194ce, wireframe : true } ) );
plane4.rotation.x = -Math.PI/2;
plane4.position.y = -250;
plane4.position.z = 50-1500;
scene.add( plane4 );
// Create sprites with lines
var placeTarget = function(x,y,z) {
// Cylinder
var cylinder = new THREE.Mesh( new THREE.CylinderGeometry(30,30,1,20,1,false),
new THREE.MeshBasicMaterial( { color : 0xeeeeee} ) );
cylinder.position.x = x;
cylinder.rotation.x = Math.PI/2;
cylinder.position.y = y;
cylinder.position.z = z;
scene.add( cylinder );
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( 0, 0, -80000 ) );
geometry.vertices.push( new THREE.Vector3( 0, 0, z ) );
var line = new THREE.Line( geometry,
new THREE.LineBasicMaterial( { color: 0xeeeeee } ) );
line.position.x = x;
line.position.y = y;
scene.add( line );
}
placeTarget(-150,-150,-550);
placeTarget(0,-150,-200);
placeTarget(100,0,500);
placeTarget(-150,100,0);
placeTarget(150,-100,-1050);
placeTarget(50,0,1100);
placeTarget(-50,-50,600);
placeTarget(0,150,-2100);
placeTarget(-130,0,-700);
renderer = new THREE.WebGLRenderer({ clearAlpha: 1 });
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
init();
var theCapturer = null;
var faceDetector = new FaceDetector();
var interval;
function openCamera() {
const constraints = { "video": { width: { max: 320 }, height: { max: 240} } };
const width = 320;
const height = 240;
const halfWidth = width / 2;
const halfHeight = height / 2;
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
document.querySelector('video').src = URL.createObjectURL(stream);
theCapturer = new ImageCapture(stream.getVideoTracks()[0]);
interval = setInterval(function() {
theCapturer.grabFrame()
.then(bitmap => {
return faceDetector.detect(bitmap); })
.catch(e => { console.error('grabFrame() failed: ' + e); })
.then(faces => {
if (faces.length > 0) {
var center = new THREE.Vector3(faces[0].x + faces[0].width / 2,
faces[0].y + faces[0].height / 2 ,
0 );
// Move |camera.position.{x,y}| inversely to the face {x,y}.
camera.position.x = (halfWidth - center.x);
camera.position.y = (halfHeight - center.y);
// Getting closer if the face bounding box grows. Typical size
// of said box is ~= 90x90. The result is jittery :'(
camera.position.z = cameraZ + (90 - faces[0].width ) * 10;
// Shifting camera.position according to face is not enough to
// produce head coupled perspective,
// https://dev.opera.com/articles/head-tracking-with-webrtc/
camera.lookAt(
new THREE.Vector3(-camera.position.x, -camera.position.y, 0));
}
})
.catch((e) => { console.error("Face Detection failed: " + e); });
}, 75);
})
.catch(e => { console.error('getUserMedia() failed: ' + e) });
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
openCamera();
animate();
</script>
</body>
</html>