-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl-demo.js
342 lines (272 loc) · 11.1 KB
/
webgl-demo.js
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
// will set to true when video can be copied to texture
var copyVideo = false;
// UI
var textureChanged = false;
var radius_input = document.getElementById('radius_input');
var radius_value = document.getElementById('radius_value');
radius_input.oninput = function() {
radius_value.value = this.value;
};
var rounds_input = document.getElementById('rounds_input');
var rounds_value = document.getElementById('rounds_value');
rounds_input.oninput = function() {
rounds_value.value = this.value;
};
var downscale_input = document.getElementById('downscale_input');
var downscale_value = document.getElementById('downscale_value');
var downscalePrev = downscale_value; // tracking changes, to avoid reinitializing stuff every frame
downscale_input.oninput = function() {
downscale_value.value = this.value;
textureChanged = true;
};
const fps_field = document.getElementById('fps');
var linearSampling_checkbox = document.getElementById('ls_checkbox');
linearSampling_checkbox.onchange = function() {
textureChanged = true;
}
// Start
main();
function main() {
const video = document.createElement('video');
var url = 'video.mp4';
var playing = false;
var timeupdate = false;
video.autoplay = true;
video.muted = true;
video.loop = true;
// Waiting for these 2 events ensures
// there is data in the video
video.addEventListener('playing', function() {
playing = true;
checkReady();
}, true);
video.addEventListener('timeupdate', function() {
timeupdate = true;
checkReady();
}, true);
// Can start rendering when we know
// resolution of the video
video.addEventListener('loadedmetadata', function(e) {
processVideo(video);
}, true);
video.src = url;
video.play();
function checkReady() {
if (playing && timeupdate) {
copyVideo = true;
}
}
return video;
}
function processVideo(video) {
// Performance indicator
var elapsedTime = 0;
var frameCount = 0;
var lastTime = 0;
const canvas = document.querySelector('#glcanvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// Preserving the original ratio using CSS
canvas.style.width = "100vw";
canvas.style.height = 100 * video.videoHeight / video.videoWidth + "vw";
canvas.style.maxHeight = "100vh";
canvas.style.maxWidth = 100 * video.videoWidth / video.videoHeight + "vh";
const gl = canvas.getContext('webgl');
// If we don't have a GL context, give up now
if (!gl) {
alert('Unable to initialize WebGL. Your browser or machine may not support it.');
return;
}
// Initialize a shader program
const vertexSource = document.getElementById("vertex-shader").text;
const fragmentSource = document.getElementById("fragment-shader").text;
shaderProgram = linkShaders(gl, vertexSource, fragmentSource);
// Define attributes & uniform variables
// for our shader program
const attributeLocations = {
vertexPosition: gl.getAttribLocation(shaderProgram, 'vPosition'),
}
const uniformLocations = {
videoTexture: gl.getUniformLocation(shaderProgram, 'videoTexture'),
pixelSize: gl.getUniformLocation(shaderProgram, 'pixelSize'),
stage: gl.getUniformLocation(shaderProgram, 'stage'),
flipY: gl.getUniformLocation(shaderProgram, 'flipY'),
offsets: gl.getUniformLocation(shaderProgram, 'offsets'),
weights: gl.getUniformLocation(shaderProgram, 'weights'),
}
// Binding buffers
const positions = [1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0];
const indices = [0, 1, 3, 0, 3, 2];
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
// Tell WebGL how to pull out the positions from the position
// buffer into the vertexPosition attribute
{
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.vertexAttribPointer(attributeLocations.vertexPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(attributeLocations.vertexPosition);
}
// Tell WebGL which indices to use to index the vertices
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.useProgram(shaderProgram);
// Make 2 temporary framebuffers to store intermidiate results
// Explained here: https://webglfundamentals.org/webgl/lessons/webgl-image-processing-continued.html
var x = video.videoWidth / downscale_value.value;
var y = video.videoHeight / downscale_value.value;
var X = video.videoWidth;
var Y = video.videoHeight;
var originalTexture = initTexture(gl, X, Y);
var tempTextures = [];
var framebuffers = [];
for (var ii = 0; ii < 2; ++ii) {
var texture = initTexture(gl, x, y);
tempTextures.push(texture);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.RGBA, x, y, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
// create a framebuffer
var fbo = gl.createFramebuffer();
framebuffers.push(fbo);
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
// attach a texture to it.
gl.framebufferTexture2D(
gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
}
function drawToFrameBuffer(frameBufferID) {
gl.uniform1i(uniformLocations.stage, frameBufferID % 2 == 0 ? 0 : 1);
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffers[frameBufferID]);
gl.viewport(0, 0, x, y);
gl.uniform2fv(uniformLocations.pixelSize, [radius_value.value * 1.0 / x, radius_value.value * 1.0 / y]);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
gl.bindTexture(gl.TEXTURE_2D, tempTextures[frameBufferID]);
}
// These produce different blurs
var kernels = {
linear: {
offsets: [0.0, 1.43478, 3.34783, 5.26087, 7.17391],
weights: [0.16819, 0.27277, 0.116901, 0.0240679, 0.00211122],
},
discrete: {
offsets: [0.0, 1.0, 2.0, 3.0, 4.0],
weights: [0.20236, 0.179044, 0.124009, 0.067234, 0.028532],
}
}
gl.uniform1fv(uniformLocations.offsets, kernels.discrete.offsets);
gl.uniform1fv(uniformLocations.weights, kernels.discrete.weights);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
function render() {
if (textureChanged) {
downscalePrev = downscale_value.value;
x = video.videoWidth / downscale_value.value;
y = video.videoHeight / downscale_value.value;
gl.bindTexture(gl.TEXTURE_2D, tempTextures[0]);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.RGBA, x, y, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.bindTexture(gl.TEXTURE_2D, tempTextures[1]);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.RGBA, x, y, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
if (linearSampling_checkbox.checked) {
gl.uniform1fv(uniformLocations.offsets, kernels.linear.offsets);
gl.uniform1fv(uniformLocations.weights, kernels.linear.weights);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
} else {
gl.uniform1fv(uniformLocations.offsets, kernels.discrete.offsets);
gl.uniform1fv(uniformLocations.weights, kernels.discrete.weights);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
}
textureChanged = false;
}
if (copyVideo) {
// 1. Update original texture
gl.bindTexture(gl.TEXTURE_2D, originalTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video);
// 2. Ping-ponging data from framebuffer[0] to framebuffer[1]
gl.uniform1i(uniformLocations.flipY, 1);
var n = (radius_value.value == 0) ? 1 : rounds_value.value;
for (var i = 1; i <= n; ++i) {
if (i == n) {
drawToFrameBuffer(0);
} else {
drawToFrameBuffer(0);
drawToFrameBuffer(1);
}
}
// 3. Drawing to canvas
gl.uniform1i(uniformLocations.flipY, -1);
gl.uniform1i(uniformLocations.stage, 1);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, X, Y);
gl.uniform2fv(uniformLocations.pixelSize, [radius_value.value * 1.0 / X, radius_value.value * 1.0 / Y]);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
// FPS
var now = new Date().getTime();
frameCount++;
elapsedTime += (now - lastTime);
lastTime = now;
if (elapsedTime >= 1000) {
fps = frameCount;
frameCount = 0;
elapsedTime -= 1000;
fps_field.innerHTML = fps;
}
requestAnimationFrame(render);
}
lastTime = new Date().getTime();
requestAnimationFrame(render);
}
//
// Initialize a texture.
//
function initTexture(gl, width, height) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
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.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
return texture;
}
//
// Initialize a shader program, so WebGL knows how to draw our data
//
function linkShaders(gl, vsSource, fsSource) {
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
return null;
}
return shaderProgram;
}
//
// creates a shader of the given type, uploads the source and
// compiles it.
//
function loadShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}