-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.mjs
107 lines (94 loc) · 2.93 KB
/
index.mjs
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
import { frag, vert } from './shaders.mjs';
import { grid } from './grid-geometry.mjs';
import GLea from '../lib/glea/glea.mjs';
let video = document.querySelector('video');
let fallbackImage = null;
let texture = null;
const mesh = grid(.05, .05);
const numVertices = mesh.length / 2;
const glea = new GLea({
glOptions: {
preserveDrawingBuffer: true
},
shaders: [
GLea.fragmentShader(frag),
GLea.vertexShader(vert)
],
buffers: {
'position': GLea.buffer(2, mesh),
'direction': GLea.buffer(1, Array(numVertices).fill(0).map(_ => Math.random()))
}
}).create();
function loop(time) {
const { gl } = glea;
// Upload the image into the texture.
// void gl.texImage2D(target, level, internalformat, format, type, HTMLVideoElement? pixels);
// void gl.texSubImage2D(target, level, xoffset, yoffset, format, type, HTMLVideoElement? pixels);
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video || fallbackImage);
// the use of texSubImage2D vs texImage2D is faster
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, video || fallbackImage);
glea.clear();
glea.uni('width', glea.width);
glea.uni('height', glea.height);
glea.uni('time', time * .005);
gl.drawArrays(gl.TRIANGLES, 0, numVertices);
requestAnimationFrame(loop);
}
function accessWebcam(video) {
return new Promise((resolve, reject) => {
const mediaConstraints = { audio: false, video: { width: 1280, height: 720 } };
navigator.mediaDevices.getUserMedia(mediaConstraints).then(mediaStream => {
video.srcObject = mediaStream;
video.onloadedmetadata = (e) => {
video.play();
resolve(video);
}
}).catch(err => {
reject(err);
});
});
}
function loadImage(url) {
return new Promise((resolve, reject) => {
const img = new Image();
img.crossOrigin = 'Anonymous';
img.src = url;
img.onload = () => {
resolve(img);
};
img.onerror = () => {
reject(img);
};
});
}
async function setup() {
const { gl } = glea;
try {
await accessWebcam(video);
} catch (ex) {
video = null;
console.error(ex.message);
}
if (! video) {
try {
fallbackImage = await loadImage('https://placekitten.com/1280/720')
} catch (ex) {
console.error(ex.message);
return false;
}
}
texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// Set the parameters so we can render any size image.
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
// Upload the image into the texture.
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video || fallbackImage);
window.addEventListener('resize', () => {
glea.resize();
});
loop(0);
}
setup();