This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.html
457 lines (394 loc) · 18.1 KB
/
controller.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<!DOCTYPE html>
<html lang="en-GB" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Controller</title>
<link rel="stylesheet" type="text/css" href="/css/controller.css">
<script type="text/javascript" src="/js/transforms3d.js"></script>
<script type="text/javascript" src="/js/webgl-utils.js"></script>
<script type="text/javascript" src="/js/sender.js"></script>
<script type="text/javascript" src="/js/detection.js"></script>
<script type="text/javascript" src="/js/rotation.js"></script>
<script type="text/javascript" src="/js/motion.js"></script>
<script type="text/javascript" src="/js/helpers.js"></script>
<script id="shader-fs1" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D texture;
uniform float width;//w4
uniform float height;//h4
varying vec2 textureCoord;
void main(void) {
// get current real coordinates
float x = floor(textureCoord.x * width);
float y = floor(textureCoord.y * height);
// base top left coordinates of given square
// taking every fourth pixel
float baseX = x - mod(x, 4.0);
float baseY = y - mod(y, 4.0);
// distance between 2 pixels in general
vec2 diff = vec2(1.0/width, 1.0/height);
// actual texture coordinates of top left pixel
// + diff/2 because textureCoord is always between numbers
vec2 baseTextureCoord = vec2(baseX / width + diff.x / 2.0, baseY / height + diff.y / 2.0);
// get all 16 pixels in given square
// done this way because when making the texture smaller, browsers were returning different sampled coordinates (Firefox and Chrome)
// doing it like this means all 16 pixels have the same values and it doesn't matter anymore which one is picked by browser
vec4 pix00 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y));
vec4 pix10 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y));
vec4 pix20 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y));
vec4 pix30 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y));
//vec4 pix40 = texture2D(texture, vec2(baseTextureCoord.x + 4.0*diff.x, baseTextureCoord.y));
vec4 pix01 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y + diff.y));
vec4 pix11 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y + diff.y));
vec4 pix21 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y + diff.y));
vec4 pix31 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y + diff.y));
//vec4 pix41 = texture2D(texture, vec2(baseTextureCoord.x + 4.0*diff.x, baseTextureCoord.y + diff.y));
vec4 pix02 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y + 2.0*diff.y));
vec4 pix12 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y + 2.0*diff.y));
vec4 pix22 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y + 2.0*diff.y));
vec4 pix32 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y + 2.0*diff.y));
//vec4 pix42 = texture2D(texture, vec2(baseTextureCoord.x + 4.0*diff.x, baseTextureCoord.y + 2.0*diff.y));
vec4 pix03 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y + 3.0*diff.y));
vec4 pix13 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y + 3.0*diff.y));
vec4 pix23 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y + 3.0*diff.y));
vec4 pix33 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y + 3.0*diff.y));
//vec4 pix43 = texture2D(texture, vec2(baseTextureCoord.x + 4.0*diff.x, baseTextureCoord.y + 3.0*diff.y));
//vec4 pix04 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y + 4.0*diff.y));
//vec4 pix14 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y + 4.0*diff.y));
//vec4 pix24 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y + 4.0*diff.y));
//vec4 pix34 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y + 4.0*diff.y));
//vec4 pix44 = texture2D(texture, vec2(baseTextureCoord.x + 4.0*diff.x, baseTextureCoord.y + 4.0*diff.y));
float thresholdG = 0.6;
float thresholdRB = 0.4;
float count = 0.0;
// count it as pixel of interest only if green is high enough and red & blue not too much
if (pix00.g > thresholdG && pix00.r < thresholdRB && pix00.b < thresholdRB) count += 1.0;
if (pix10.g > thresholdG && pix10.r < thresholdRB && pix10.b < thresholdRB) count += 1.0;
if (pix20.g > thresholdG && pix20.r < thresholdRB && pix20.b < thresholdRB) count += 1.0;
if (pix30.g > thresholdG && pix30.r < thresholdRB && pix30.b < thresholdRB) count += 1.0;
//if (pix40.g > thresholdG && pix40.r < thresholdRB && pix40.b < thresholdRB) count += 1.0;
if (pix01.g > thresholdG && pix01.r < thresholdRB && pix01.b < thresholdRB) count += 1.0;
if (pix11.g > thresholdG && pix11.r < thresholdRB && pix11.b < thresholdRB) count += 1.0;
if (pix21.g > thresholdG && pix21.r < thresholdRB && pix21.b < thresholdRB) count += 1.0;
if (pix31.g > thresholdG && pix31.r < thresholdRB && pix31.b < thresholdRB) count += 1.0;
//if (pix41.g > thresholdG && pix41.r < thresholdRB && pix41.b < thresholdRB) count += 1.0;
if (pix02.g > thresholdG && pix02.r < thresholdRB && pix02.b < thresholdRB) count += 1.0;
if (pix12.g > thresholdG && pix12.r < thresholdRB && pix12.b < thresholdRB) count += 1.0;
if (pix22.g > thresholdG && pix22.r < thresholdRB && pix22.b < thresholdRB) count += 1.0;
if (pix32.g > thresholdG && pix32.r < thresholdRB && pix32.b < thresholdRB) count += 1.0;
//if (pix42.g > thresholdG && pix42.r < thresholdRB && pix42.b < thresholdRB) count += 1.0;
if (pix03.g > thresholdG && pix03.r < thresholdRB && pix03.b < thresholdRB) count += 1.0;
if (pix13.g > thresholdG && pix13.r < thresholdRB && pix13.b < thresholdRB) count += 1.0;
if (pix23.g > thresholdG && pix23.r < thresholdRB && pix23.b < thresholdRB) count += 1.0;
if (pix33.g > thresholdG && pix33.r < thresholdRB && pix33.b < thresholdRB) count += 1.0;
//if (pix43.g > thresholdG && pix43.r < thresholdRB && pix43.b < thresholdRB) count += 1.0;
//if (pix04.g > thresholdG && pix04.r < thresholdRB && pix04.b < thresholdRB) count += 1.0;
//if (pix14.g > thresholdG && pix14.r < thresholdRB && pix14.b < thresholdRB) count += 1.0;
//if (pix24.g > thresholdG && pix24.r < thresholdRB && pix24.b < thresholdRB) count += 1.0;
//if (pix34.g > thresholdG && pix34.r < thresholdRB && pix34.b < thresholdRB) count += 1.0;
//if (pix44.g > thresholdG && pix44.r < thresholdRB && pix44.b < thresholdRB) count += 1.0;
// write count, and top left coordinates
gl_FragColor = vec4(count, baseX, baseY, 1.0);
}
</script>
<script id="shader-fs2" type="x-shader/x-fragment">
precision highp float;
uniform sampler2D texture;
uniform float width;//w12
uniform float height;//h12
varying vec2 textureCoord;
void main(void) {
// current coordinates
float x = floor(textureCoord.x * width);
float y = floor(textureCoord.y * height);
// base top left synthetic coordinates of given square
// now working with smaller picture already so real coordinates are saved inside pixels
float baseX = x - mod(x, 3.0);
float baseY = y - mod(y, 3.0);
// distance between 2 pixels
vec2 diff = vec2(1.0/width, 1.0/height);
// actual texture coordinates of top left pixel
// + diff/2 because textureCoord is always between numbers
vec2 baseTextureCoord = vec2(baseX / width + diff.x / 2.0, baseY / height + diff.y / 2.0);
vec4 pix00 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y));
vec4 pix10 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y));
vec4 pix20 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y));
//vec4 pix30 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y));
vec4 pix01 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y + diff.y));
vec4 pix11 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y + diff.y));
vec4 pix21 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y + diff.y));
//vec4 pix31 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y + diff.y));
vec4 pix02 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y + 2.0*diff.y));
vec4 pix12 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y + 2.0*diff.y));
vec4 pix22 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y + 2.0*diff.y));
//vec4 pix32 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y + 2.0*diff.y));
//vec4 pix03 = texture2D(texture, vec2(baseTextureCoord.x, baseTextureCoord.y + 3.0*diff.y));
//vec4 pix13 = texture2D(texture, vec2(baseTextureCoord.x + diff.x, baseTextureCoord.y + 3.0*diff.y));
//vec4 pix23 = texture2D(texture, vec2(baseTextureCoord.x + 2.0*diff.x, baseTextureCoord.y + 3.0*diff.y));
//vec4 pix33 = texture2D(texture, vec2(baseTextureCoord.x + 3.0*diff.x, baseTextureCoord.y + 3.0*diff.y));
float sumCount = pix00.r + pix10.r + pix20.r + pix01.r + pix11.r + pix21.r + pix02.r + pix12.r + pix22.r;
//+ pix30.r + pix31.r + pix32.r + pix03.r + pix13.r + pix23.r + pix33.r;
// no interesting pixels here
// discard squares with one interesting pixel?
if (sumCount < 2.0) discard;
// weighted arithmetic mean of coordinates
float sumX =
pix00.r * pix00.g + pix10.r * pix10.g + pix20.r * pix20.g + //pix30.r * pix30.g +
pix01.r * pix01.g + pix11.r * pix11.g + pix21.r * pix21.g + //pix31.r * pix31.g +
pix02.r * pix02.g + pix12.r * pix12.g + pix22.r * pix22.g;// + pix32.r * pix32.g +
//pix03.r * pix03.g + pix13.r * pix13.g + pix23.r * pix23.g + pix33.r * pix33.b;
float meanX = sumX / sumCount;
float sumY =
pix00.r * pix00.b + pix10.r * pix10.b + pix20.r * pix20.b + //pix30.r * pix30.b +
pix01.r * pix01.b + pix11.r * pix11.b + pix21.r * pix21.b + //pix31.r * pix31.b +
pix02.r * pix02.b + pix12.r * pix12.b + pix22.r * pix22.b;// + pix32.r * pix32.b +
//pix03.r * pix03.b + pix13.r * pix13.b + pix23.r * pix23.b + pix33.r * pix33.b;
float meanY = sumY / sumCount;
gl_FragColor = vec4(sumCount, meanX, meanY, 1.0);
}
</script>
<script id="shader-fs-draw" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D texture;
varying vec2 textureCoord;
void main(void) {
gl_FragColor = texture2D(texture, textureCoord);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 rotation;
varying vec2 textureCoord;
void main(void) {
textureCoord = aTextureCoord;
gl_Position = rotation * vec4(aVertexPosition, 1.0);
}
</script>
<script type="text/javascript">
"use strict";
// HTMLVideoElement
var video;
// info if detection is being performed - if animation is running
// also is used to make sure that animation is running only once
var isAnimating = false;
window.onload = function() {
let initResult = Detection.init();
if (!initResult) {
alert("Your browser does not support WebGL.")
document.querySelector(".loading").innerHTML = "Your browser does not support WebGL or some of its extensions.";
return;
}
// set video listener, important for starting animation
video = document.querySelector("video");
video.addEventListener("canplaythrough", setupAfterVideoStreamIsReady, false);
video.addEventListener("click", sendTouch, false);
// set change and click listeners
document.querySelector("label#all").addEventListener("click", doAll, false);
document.querySelector("label#none").addEventListener("click", doNone, false);
document.querySelector("label#position input").addEventListener("change", doPosition, false);
document.querySelector("label#rotation input").addEventListener("change", doRotation, false);
document.querySelector("label#motion input").addEventListener("change", doMotion, false);
// window size listener for changing layout
// checks which one of height or width is bigger and changes the layout appropriately
window.addEventListener("resize", windowSizeChanged, false);
windowSizeChanged();
// init requestAnimFrame function
Utils.initRequestAnimationFrame();
// init Rotation object
Rotation.init();
// let the user know that the rotation of device is correct
Rotation.setCustomFunction(function(data) {
Helpers.checkRotation(data, function rotationToRight() {
document.querySelector(".controls").style.backgroundColor = "lightblue";
}, function rotationToLeft() {
document.querySelector(".controls").style.backgroundColor = "lightblue";
}, function noRotation() {
document.querySelector(".controls").style.backgroundColor = "white";
});
});
// init Motion object
Motion.init();
// https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
// get access to camera
navigator.mediaDevices.getUserMedia({
audio: false,
video: { width: 1280, height: 720, facingMode: "environment" }
/*video: {
width: { min: 720, ideal: 1024, max: 1280 },
height: { min: 540, max: 720 },
//prefer rear camera
facingMode: "environment"
}*/
}).then(function(stream) {
video.srcObject = stream;
}).catch(function(error) {
let msg = "";
if (error.name === "ConstraintNotSatisfiedError" || error.name === "NotFoundError") {
msg = "Your camera does not meet required resolution for this application to work.";
} else if (error.name === "TrackStartError") {
msg = "Your camera is probably used by different application at this time.";
} else {
msg = "There was an unknown error when accessing your camera.";
}
document.querySelector(".loading").innerHTML = msg;
alert(msg);
console.log("%cVideo Error: " + error, 'color: red');
console.log(msg);
});
};
/**
* Main animation function that triggers detection in the image from camera
*/
function animate() {
if (isAnimating) {
Detection.updateTexture(video);
Detection.repaint();
requestAnimFrame(animate);
}
}
/**
* Event handler when camera is ready to give pictures
*/
function setupAfterVideoStreamIsReady() {
Detection.setupAfterVideoStreamIsReady(video.videoWidth, video.videoHeight);
// hide loading message
document.querySelector(".loading-container").classList.add("hidden");
document.querySelector(".main").classList.remove("hidden");
}
/**
* Event handler for starting all operations
*/
function doAll() {
document.querySelector("#all").classList.add("pulse");
setTimeout(function() {
document.querySelector("#all").classList.remove("pulse");
}, 600);
document.querySelector("label#position input").checked = true;
document.querySelector("label#rotation input").checked = true;
document.querySelector("label#motion input").checked = true;
doPosition();
doRotation();
doMotion();
}
/**
* Event handler for stopping all operations
*/
function doNone() {
document.querySelector("#none").classList.add("pulse");
setTimeout(function() {
document.querySelector("#none").classList.remove("pulse");
}, 600);
document.querySelector("label#position input").checked = false;
document.querySelector("label#rotation input").checked = false;
document.querySelector("label#motion input").checked = false;
doPosition();
doRotation();
doMotion();
}
/**
* Event handler when position button is clicked
*/
function doPosition() {
if (document.querySelector("label#position input").checked) {
document.querySelector("label#position").classList.add("active");
if (!isAnimating) {
isAnimating = true;
Detection.restart();
animate();
}
} else {
document.querySelector("label#position").classList.remove("active");
Detection.finish();
isAnimating = false;
}
}
/**
* Event handler when rotation button is clicked
*/
function doRotation() {
if (document.querySelector("label#rotation input").checked) {
document.querySelector("label#rotation").classList.add("active");
Rotation.start();
} else {
document.querySelector("label#rotation").classList.remove("active");
Rotation.finish();
}
}
/**
* Event handler when motion button is clicked
*/
function doMotion() {
if (document.querySelector("label#motion input").checked) {
document.querySelector("label#motion").classList.add("active");
Motion.start();
} else {
document.querySelector("label#motion").classList.remove("active");
Motion.finish();
}
}
/**
* Resize event handler
* Changes GUI so that buttons are either right or bottom
*/
function windowSizeChanged() {
if (window.innerHeight < window.innerWidth) {
// window is wider than higher
document.querySelector(".main").classList.remove("bottom");
document.querySelector(".main").classList.add("right");
} else {
// window is higher than wider
document.querySelector(".main").classList.remove("right");
document.querySelector(".main").classList.add("bottom");
}
}
/**
* Click and touch event handler
* Sends information that the video has been clicked or touched
*/
function sendTouch() {
let obj = {
type: "touch",
time: new Date().getTime()
};
Sender.add(obj);
}
/**
* Helper method for sending test data
*/
function testSend() {
let request = new XMLHttpRequest();
request.open('POST', '/ajax/data');
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
let data = [{
type: "test",
time: new Date().getTime()
}];
request.send(JSON.stringify(data));
}
</script>
</head>
<body>
<div class="loading-container">
<div class="loading">
Loading, please wait...<br>
Access to your camera is required for this appplication to work.<br>
You might be asked for giving a permissison to access it.
</div>
</div>
<div class="main hidden">
<div class="center">
<canvas></canvas>
<video src="" autoplay muted></video>
</div>
<div class="controls">
<label id="all">Check all</label><br>
<label id="none">Uncheck all</label><br>
<hr>
<label id="position"><input type="checkbox"> Position</label><br>
<label id="rotation"><input type="checkbox"> Rotation</label><br>
<label id="motion"><input type="checkbox"> Motion</label>
</div>
</div>
</body>
</html>