This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
eyetracking_ml.html
154 lines (130 loc) · 4.85 KB
/
eyetracking_ml.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
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#008000"/>
<title>eyetracking_ml</title>
<link rel="manifest" href="hello_ml_manifest.json">
<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('/hello_ml_sw.js')
.then(function() {
console.log('Service Worker Registered');
});
}
</script>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<h1>eyetracking_ml</h1>
<script src="three.js"></script>
<script>
let container, scene, camera, session, eyeMesh;
const controllerGeometry = new THREE.BoxBufferGeometry(0.1, 0.2, 0.01);
const controllerMaterial = new THREE.MeshPhongMaterial({
color: 0x4caf50,
});
const _makeControllerMesh = (x = 0, y = 0, z = 0, qx = 0, qy = 0, qz = 0, qw = 1) => {
const mesh = new THREE.Mesh(controllerGeometry, controllerMaterial);
mesh.position.set(x, y, z);
mesh.quaternion.set(qx, qy, qz, qw);
// mesh.matrix.compose(mesh.position, mesh.quaternion, mesh.scale);
mesh.updateMatrix();
mesh.updateMatrixWorld();
mesh.matrixAutoUpdate = false;
mesh.frustumCulled = false;
return mesh;
};
const _getControllerIndex = inputSource => inputSource.handedness === 'left' ? 0 : 1;
function init() {
container = document.createElement('div');
document.body.appendChild(container);
scene = new THREE.Scene();
scene.matrixAutoUpdate = false;
// scene.background = new THREE.Color(0x3B3961);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
// camera.position.set(0, 1, 0);
camera.lookAt(new THREE.Vector3());
scene.add(camera);
const ambientLight = new THREE.AmbientLight(0x808080);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
eyeMesh = (() => {
const geometry = new THREE.BoxBufferGeometry(0.05, 0.05, 0.05);
const material = new THREE.MeshPhongMaterial({
color: 0x0000FF,
});
const mesh = new THREE.Mesh(geometry, material);
// mesh.visible = false;
mesh.frustumCulled = false;
return mesh;
})();
scene.add(eyeMesh);
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// window.browser.magicleap.RequestDepthPopulation(true);
// renderer.autoClear = false;
container.appendChild(renderer.domElement);
renderer.setAnimationLoop(animate);
}
function animate(time, frame) {
const _updateEyeMesh = () => {
if (session && frame) {
const eyeInputSources = session.getInputSources().filter(inputSource => inputSource.targetRayMode === 'gaze');
const eyeGamepads = navigator.getGamepads().filter(gamepad => gamepad.id === 'eye');
if (eyeInputSources.length > 0 && eyeGamepads.length > 0) {
const eyeInputSource = eyeInputSources[0];
const eyeGamepad = eyeGamepads[0];
const pose = frame.getPose(eyeInputSource.targetRaySpace);
const {axes} = eyeGamepad;
eyeMesh.matrix.fromArray(pose.transform.inverse.matrix);
eyeMesh.matrix.decompose(eyeMesh.position, eyeMesh.quaternion, eyeMesh.scale);
const scaleFactor = axes.some(axis => axis < 0) ? 0.2 : 1;
eyeMesh.scale.set(1, scaleFactor, 1);
eyeMesh.updateMatrixWorld();
}
}
};
_updateEyeMesh();
renderer.render(scene, renderer.vr.enabled ? renderer.vr.getCamera(camera) : camera);
}
init();
(async () => {
console.log('request session');
session = await navigator.xr.requestSession({
exclusive: true,
extensions: {
eyeTracking: true,
},
});
// console.log('request first frame');
session.requestAnimationFrame((timestamp, frame) => {
renderer.vr.setSession(session, {
frameOfReferenceType: 'stage',
});
const {views} = frame.getViewerPose();
const viewport = session.baseLayer.getViewport(views[0]);
const width = viewport.width;
const height = viewport.height;
renderer.setSize(width * 2, height);
renderer.setAnimationLoop(null);
renderer.vr.enabled = true;
renderer.vr.setAnimationLoop(animate);
// eyeTracker = session.requestEyeTracking();
console.log('running!');
});
})();
</script>
</body>
</html>