-
Notifications
You must be signed in to change notification settings - Fork 4
/
canvascapture9.html
687 lines (632 loc) · 22.4 KB
/
canvascapture9.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<!--
* Copyright 2011, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Color Adjust</title>
<style>
/*html, body {
width: 100%;
height: 100%;
border: 0px;
padding: 0px;
margin: 0px;
background-color: dark-red;
font-family: sans-serif;
overflow: hidden;
color: #fff;
}*/
a {
color: #fff;
}
CANVAS {
background-color: gray;
}
#advanced {
color: gray;
}
#uiContainer {
float:left;
color: white;
background-color: rgba(0,0,0,1.0);
border-radius: 10px;
padding: 10px;
font-size: small;
margin: 10px;
}
.clickable {
cursor: pointer;
}
#viewContainer {
float:left;
width: 640px;
height: 480px;
}
</style>
<link type="text/css" href="color-adjust/jquery-ui-1.8.2.custom/css/ui-lightness/jquery-ui-1.8.2.custom.css" rel="stylesheet" />
<script type="text/javascript" src="color-adjust/jquery-ui-1.8.2.custom/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="color-adjust/jquery-ui-1.8.2.custom/js/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript" src="color-adjust/tdl/base.js"></script>
<script type="text/javascript">
tdl.require('tdl.buffers');
tdl.require('tdl.fast');
tdl.require('tdl.framebuffers');
tdl.require('tdl.log');
tdl.require('tdl.math');
tdl.require('tdl.models');
tdl.require('tdl.particles');
tdl.require('tdl.primitives');
tdl.require('tdl.programs');
tdl.require('tdl.textures');
tdl.require('tdl.webgl');
// globals
var gl; // the gl context.
var canvas; // the canvas
var math; // the math lib.
var fast; // the fast math lib.
var g_logGLCalls = true; // whether or not to log webgl calls
var g_debug = false; // whether or not to debug.
var g_drawOnce = false; // draw just one frame.
var g_setCountElements = [];
var g_autoSet = true;
var g_autoSetting = 0;
var g_videoTexture;
var g_colorCube0Texture;
var g_colorCube1Texture;
var g_oldIndex = 0;
var g_mixTimer = 0;
var g_mixAmount = 0;
var g_eyeClock = 0;
var g_mixDuration = 1;
var g_applyColorAdjust = true;
var g_targetHeight = 0.0;
var g_targetRadius = 1;
var g_eyeHeight = 0;
var g_eyeRadius = 1.8;
var g_eyeSpeed = 0.0;
//g_drawOnce = true;
//g_debug = true;
var g_adjustmentImages = [
{ name: 'default', url: "color-adjust/adjustments/default.png" },
{ name: 'monochrome', url: "color-adjust/adjustments/monochrome.png" },
{ name: 'sepia', url: "color-adjust/adjustments/sepia.png" },
{ name: 'saturated', url: "color-adjust/adjustments/saturated.png", },
{ name: 'posterize', url: "color-adjust/adjustments/posterize.png", },
{ name: 'inverse', url: "color-adjust/adjustments/inverse.png", },
{ name: 'color negative', url: "color-adjust/adjustments/color-negative.png", },
{ name: 'high contrast', url: "color-adjust/adjustments/high-contrast-bw.png", },
{ name: 'funky contrast', url: "color-adjust/adjustments/funky-contrast.png", },
{ name: 'nightvision', url: "color-adjust/adjustments/nightvision.png", },
{ name: 'thermal', url: "color-adjust/adjustments/thermal.png", },
{ name: 'b/w', url: "color-adjust/adjustments/black-white.png", },
{ name: 'hue +60', url: "color-adjust/adjustments/hue-plus-60.png", },
{ name: 'hue +180', url: "color-adjust/adjustments/hue-plus-180.png", },
{ name: 'hue -60', url: "color-adjust/adjustments/hue-minus-60.png", },
{ name: 'red to cyan', url: "color-adjust/adjustments/red-to-cyan.png" },
{ name: 'blues', url: "color-adjust/adjustments/blues.png" },
{ name: 'infrared', url: "color-adjust/adjustments/infrared.png" },
{ name: 'radioactive', url: "color-adjust/adjustments/radioactive.png" },
{ name: 'goolgey', url: "color-adjust/adjustments/googley.png" },
{ name: 'bgy', url: "color-adjust/adjustments/bgy.png" }
];
function ValidateNoneOfTheArgsAreUndefined(functionName, args) {
for (var ii = 0; ii < args.length; ++ii) {
if (args[ii] === undefined) {
tdl.error("undefined passed to gl." + functionName + "(" +
tdl.webgl.glFunctionArgsToString(functionName, args) + ")");
}
}
}
function Log(msg) {
if (g_logGLCalls) {
tdl.log(msg);
}
}
function LogGLCall(functionName, args) {
if (g_logGLCalls) {
ValidateNoneOfTheArgsAreUndefined(functionName, args)
tdl.log("gl." + functionName + "(" +
tdl.webgl.glFunctionArgsToString(functionName, args) + ")");
}
}
/**
* Sets up plane
*/
function setupPlane() {
var texture = tdl.textures.loadTexture([0,100,200,255]);
g_videoTexture = texture;
var textures = {
texture: texture
};
var program = tdl.programs.loadProgramFromScriptTags(
'basicVertexShader',
'basicFragmentShader');
var arrays = tdl.primitives.createPlane(1, 1, 1, 1);
tdl.primitives.reorient(arrays,
[1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
0, 0, 0, 1]);
delete arrays.normal;
return new tdl.models.Model(program, arrays, textures);
}
/**
* Sets up RenderTarget plane
*/
function setupRTPlane(rtTexture) {
var textures = {
texture: rtTexture
};
var program = tdl.programs.loadProgramFromScriptTags(
'basicVertexShader',
'basicFragmentShader');
var arrays = tdl.primitives.createPlane(1, 1, 1, 1);
tdl.primitives.reorient(arrays,
[1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
0.5, 0.5, 0, 1]);
delete arrays.normal;
return new tdl.models.Model(program, arrays, textures);
}
/**
* Sets up colorAdjust Plane
*/
function setupColorAdjustPlane(inTexture) {
g_colorCubeTexture0 = tdl.textures.loadTexture(g_adjustmentImages[0].img);
g_colorCubeTexture1 = tdl.textures.loadTexture(g_adjustmentImages[1].img);
g_colorCubeTexture0.setParameter(gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
g_colorCubeTexture0.setParameter(gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
g_colorCubeTexture0.setParameter(gl.TEXTURE_MIN_FILTER, gl.LINEAR);
g_colorCubeTexture0.setParameter(gl.TEXTURE_MAG_FILTER, gl.LINEAR);
g_colorCubeTexture1.setParameter(gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
g_colorCubeTexture1.setParameter(gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
g_colorCubeTexture1.setParameter(gl.TEXTURE_MIN_FILTER, gl.LINEAR);
g_colorCubeTexture1.setParameter(gl.TEXTURE_MAG_FILTER, gl.LINEAR);
var textures = {
inTexture: inTexture,
colorCube0: g_colorCubeTexture0,
colorCube1: g_colorCubeTexture1
};
var program = tdl.programs.loadProgramFromScriptTags(
'colorAdjustVertexShader',
'colorAdjustFragmentShader');
var arrays = tdl.primitives.createPlane(2, 2, 1, 1);
tdl.primitives.reorient(arrays,
[1, 0, 0, 0,
0, 0,-1, 0,
0, 1, 0, 0,
0, 0, 0, 1]);
delete arrays.normal;
return new tdl.models.Model(program, arrays, textures);
}
function initialize() {
math = tdl.math;
fast = tdl.fast;
canvas = document.getElementById("canvas");
gl = tdl.webgl.setupWebGL(canvas);
if (!gl) {
return false;
}
if (g_debug) {
gl = tdl.webgl.makeDebugContext(gl, undefined, LogGLCall);
}
setupAdjustments();
}
function makeSimulatedVolumeImg(name, img, size, callback) {
var canvas = document.createElement("canvas");
canvas.width = size * size;
canvas.height = size;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var newImg = new Image();
newImg.onload = callback;
newImg.src = canvas.toDataURL();
return newImg;
}
function updateSelection(index) {
function updateTexture(tex, index) {
var info = g_adjustmentImages[index];
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
//gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, info.img);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, info.img);
}
updateTexture(g_colorCubeTexture1.texture, g_oldIndex);
updateTexture(g_colorCubeTexture0.texture, index);
g_adjustmentImages[g_oldIndex].div.style.color = "gray";
g_adjustmentImages[index].div.style.color = "red";
g_mixTimer = g_mixDuration;
g_oldIndex = index;
}
function setupAdjustments() {
tdl.log("setupAdjustments")
var loadingCount = 1;
var adjustmentsElement = document.getElementById("adjustments");
for (var ii = 0; ii < g_adjustmentImages.length; ++ii) {
++loadingCount;
var info = g_adjustmentImages[ii];
var div = document.createElement("div");
info.div = div;
div.innerHTML = info.name;
div.className = "clickable";
div.style.color = ii ? "gray" : "red";
div.onclick = function(index) {
return function() {
if (!loadingCount) {
updateSelection(index);
}
}
}(ii);
adjustmentsElement.appendChild(div);
var img = new Image();
img.src = info.url;
img.onload = function(img, info) {
return function() {
info.img = makeSimulatedVolumeImg(info.name, img, 8, function() {
--loadingCount;
if (loadingCount == 0) {
continueSetup();
}
});
}
}(img, info);
}
loadingCount--;
}
function continueSetup() {
var mainFBO = tdl.framebuffers.createFramebuffer(canvas.width, canvas.height, true);
var videoPlane = setupPlane();
var colorAdjustPlane = setupColorAdjustPlane(mainFBO.texture);
var rtPlane = setupRTPlane(mainFBO.texture);
var projection = new Float32Array(16);
var view = new Float32Array(16);
var identity = new Float32Array([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);
var world = new Float32Array(16);
var worldInverse = new Float32Array(16);
var worldInverseTranspose = new Float32Array(16);
var viewProjection = new Float32Array(16);
var worldViewProjection = new Float32Array(16);
var viewInverse = new Float32Array(16);
var viewDirectionProjectionInverse = new Float32Array(16);
var eyePosition = new Float32Array(3);
var target = new Float32Array(3);
var up = new Float32Array([0,1,0]);
var v3t0 = new Float32Array(3);
var v3t1 = new Float32Array(3);
var v3t2 = new Float32Array(3);
var v3t3 = new Float32Array(3);
var m4t0 = new Float32Array(16);
var m4t1 = new Float32Array(16);
var m4t2 = new Float32Array(16);
var m4t3 = new Float32Array(16);
var zero4 = new Float32Array(4);
var one4 = new Float32Array([1,1,1,1]);
var startTime = 0; // 196
var copyVideo = false;
var madeVideoTexture = false;
var video = document.getElementById("vid");
video.addEventListener("playing", function() {
copyVideo = true;
}, true);
video.addEventListener("ended", function() {
video.currentTime = startTime; video.play();
}, true);
video.addEventListener("error", function() {
tdl.log("could not play video");
}, true);
video.volume = 0;
video.play();
var showRT = false;
$(document).keypress(function(event) {
if (event.which == 'd'.charCodeAt(0) ||
event.which == 'D'.charCodeAt(0)) {
showRT = !showRT;
tdl.log(showRT);
}
});
var videoPlanePer = {
worldViewProjection: worldViewProjection
};
var rtPlanePer = {
worldViewProjection: worldViewProjection
};
var frameCount = 0;
var then = (new Date()).getTime() * 0.001;
var clock = 0.0;
function render() {
++frameCount;
if (!g_drawOnce) {
tdl.webgl.requestAnimationFrame(render, canvas);
}
var now = (new Date()).getTime() * 0.001;
var elapsedTime = now - then;
then = now;
clock += elapsedTime;
g_eyeClock += elapsedTime * g_eyeSpeed;
eyePosition[0] = Math.sin(g_eyeClock) * g_eyeRadius;
eyePosition[1] = g_eyeHeight;
eyePosition[2] = Math.cos(g_eyeClock) * g_eyeRadius;
target[0] = Math.sin(g_eyeClock + Math.PI) * g_targetRadius;
target[1] = g_targetHeight;
target[2] = Math.cos(g_eyeClock + Math.PI) * g_targetRadius;
if (copyVideo) {
if (video.currentTime < startTime) {
video.currentTime = startTime;
}
gl.bindTexture(gl.TEXTURE_2D, g_videoTexture.texture);
if (!madeVideoTexture) {
madeVideoTexture = true;
gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.BROWSER_DEFAULT_WEBGL);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video);
g_videoTexture.setParameter(gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
g_videoTexture.setParameter(gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
g_videoTexture.setParameter(gl.TEXTURE_MIN_FILTER, gl.LINEAR);
g_videoTexture.setParameter(gl.TEXTURE_MAG_FILTER, gl.LINEAR);
} else {
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, video);
}
}
// Draw to main fbo.
if (g_applyColorAdjust) {
mainFBO.bind();
}
gl.colorMask(true, true, true, true);
gl.depthMask(true);
gl.clearColor(0,0,0,1);
gl.clearDepth(1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
gl.enable(gl.DEPTH_TEST);
fast.matrix4.perspective(
projection,
math.degToRad(30),
canvas.clientWidth / canvas.clientHeight,
1,
5000);
fast.matrix4.lookAt(
view,
eyePosition,
target,
up);
fast.matrix4.mul(viewProjection, view, projection);
// view: view,
// projection: projection,
// viewProjection: viewProjection,
Log("--Draw Plane---------------------------------------");
videoPlane.drawPrep();
if (video.videoWidth) {
// this is a hack since I'm doing this in 3d.
// meaning I'm drawing to a polygon in 3d and based on
// where the view is this math just happesn to work.
var yAspect = 1;
var xAspect = video.videoWidth / video.videoHeight;
var width = canvas.clientHeight * xAspect / canvas.clientWidth;
if (width < 1) {
yAspect /= width;
xAspect /= width;
}
fast.matrix4.scaling(world, [xAspect, yAspect, 1]);
} else {
fast.matrix4.translation(world, [0, 0, 0]);
}
fast.matrix4.mul(worldViewProjection, world, viewProjection);
fast.matrix4.inverse(worldInverse, world);
fast.matrix4.transpose(worldInverseTranspose, worldInverse);
videoPlane.draw(videoPlanePer);
if (g_mixTimer > 0) {
g_mixTimer = Math.max(0, g_mixTimer - elapsedTime);
g_mixAmount = g_mixTimer / g_mixDuration;
}
if (g_applyColorAdjust) {
mainFBO.unbind();
gl.colorMask(true, true, true, true);
gl.depthMask(true);
gl.clearColor(0,1,0,1);
gl.clearDepth(1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
gl.disable(gl.BLEND);
gl.disable(gl.DEPTH_TEST);
colorAdjustPlane.drawPrep();
colorAdjustPlane.draw({
mixAmount: g_mixAmount
});
if (showRT) {
var orthoProjection = [
2 / canvas.width, 0, 0, 0,
0, -2 / canvas.height, 0, 0,
0, 0, 1, 0,
-1 + 1 / canvas.width, 1 - 1 / canvas.height, 0, 1
];
rtPlane.drawPrep();
if (video.videoWidth) {
var aspect = canvas.clientWidth / canvas.clientHeight;
fast.matrix4.scaling(m4t0, [video.videoWidth, video.videoHeight * aspect, 1]);
} else {
fast.matrix4.translation(mt40, [0, 0, 0]);
}
fast.matrix4.translation(m4t1, [canvas.width - video.videoWidth, 0, 0]);
fast.matrix4.mul(world, m4t0, m4t1);
fast.matrix4.mul(worldViewProjection, world, orthoProjection);
//fast.matrix4.mul(worldViewProjection, world, viewProjection);
rtPlane.draw(rtPlanePer);
}
}
// Set the alpha to 255.
gl.colorMask(false, false, false, true);
gl.clearColor(0,0,0,1);
gl.clear(gl.COLOR_BUFFER_BIT);
// turn off logging after 1 frame.
g_logGLCalls = false;
}
render();
return true;
}
$(function(){
initialize();
});
</script>
</head>
<body>
<div> Create Real-Time stream from < canvas > and play it back. WebGL effects on getUserMedia() demo.</div>
<div id="viewContainer">
<canvas id="canvas" width="640" height="480" style="width: 100%; height: 100%;"></canvas>
</div>
<div id="uiContainer">
<div>Adjustment</div>
<div id="adjustments">
</div>
</div>
<div style="display:none;">
<video id="vid">
</video>
</div>
<br style="clear:both" />
</body>
</br>
<button id="btn1" onclick="startStreaming()">Create Stream from < canvas ></button>
</body>
<script type="text/javascript" src="dimsum.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var v = document.getElementById('vid');
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
// Request access to video only
navigator.getUserMedia(
{
video:true,
audio:false
},
function(stream) {
var url = window.URL || window.webkitURL;
v.src = url ? url.createObjectURL(stream) : stream;
v.play();
},
function(error) {
alert('Something went wrong. (error code ' + error.code + ')');
return;
}
);
}
else {
alert('Sorry, the browser you are using doesn\'t support getUserMedia');
return;
}
},false);;
var theStream;
var thePlayback;
function startStreaming() {
document.getElementById("btn1").disabled = true;
// Create a MediaStream out of the <canvas> tag.
theStream = document.getElementById("canvas").captureStream();
document.body.appendChild(document.createElement("br"));
createButton("btn2", "Play back captured Stream to a <video>", startPlayback);
document.body.appendChild(document.createElement("br"));
}
function startPlayback() {
document.getElementById("btn2").disabled = true;
// And plug the created MediaStream into another <video> tag.
createVideoTag("playbackTag", 640, 480, theStream);
thePlayback = document.getElementById("playbackTag");
document.body.appendChild(document.createElement("br"));
createButton("btn3", "Stop theStream captured from <canvas>", stopStreaming);
}
function stopStreaming() {
document.getElementById("btn3").disabled = true;
theStream.getVideoTracks()[0].stop();
}
</script>
<!-- ===[ basic ]============================================== -->
<script id="basicVertexShader" type="text/something-not-javascript">
uniform mat4 worldViewProjection;
attribute vec4 position;
attribute vec2 texCoord;
varying vec2 v_texCoord;
void main() {
gl_Position = worldViewProjection * position;
v_texCoord = texCoord;
}
</script>
<script id="basicFragmentShader" type="text/something-not-javascript">
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D texture;
varying vec2 v_texCoord;
void main() {
gl_FragColor = texture2D(texture, v_texCoord);
}
</script>
<!-- ===[ colorAdjust ]============================================== -->
<script id="colorAdjustVertexShader" type="text/something-not-javascript">
attribute vec4 position;
attribute vec2 texCoord;
varying vec2 v_texCoord;
void main() {
gl_Position = position;
v_texCoord = texCoord;
}
</script>
<script id="colorAdjustFragmentShader" type="text/something-not-javascript">
#ifdef GL_ES
precision mediump float;
#endif
uniform float mixAmount;
uniform sampler2D inTexture;
uniform sampler2D colorCube0;
uniform sampler2D colorCube1;
varying vec2 v_texCoord;
vec4 sampleAs3DTexture(sampler2D tex, vec3 texCoord, float size) {
float sliceSize = 1.0 / size; // space of 1 slice
float slicePixelSize = sliceSize / size; // space of 1 pixel
float sliceInnerSize = slicePixelSize * (size - 1.0); // space of size pixels
float zSlice0 = min(floor(texCoord.z * size), size - 1.0);
float zSlice1 = min(zSlice0 + 1.0, size - 1.0);
float xOffset = slicePixelSize * 0.5 + texCoord.x * sliceInnerSize;
float s0 = xOffset + (zSlice0 * sliceSize);
float s1 = xOffset + (zSlice1 * sliceSize);
vec4 slice0Color = texture2D(tex, vec2(s0, texCoord.y));
vec4 slice1Color = texture2D(tex, vec2(s1, texCoord.y));
float zOffset = mod(texCoord.z * size, 1.0);
return mix(slice0Color, slice1Color, zOffset);
}
void main() {
vec4 originalColor = texture2D(inTexture, v_texCoord);
vec4 color0 = sampleAs3DTexture(colorCube0, originalColor.rgb, 8.0);
vec4 color1 = sampleAs3DTexture(colorCube1, originalColor.rgb, 8.0);
gl_FragColor = vec4(mix(color0, color1, mixAmount).rgb, originalColor.a);
}
</script>
</html>