OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <head> |
| 3 <meta charset="utf-8"> |
| 4 <script type="application/x-javascript" src="../util.js"></script> |
| 5 <script> |
| 6 var processor = { |
| 7 lastTime : new Date, |
| 8 timerCallback: function() { |
| 9 if (this.video.paused || this.video.ended) { |
| 10 return; |
| 11 } |
| 12 this.computeFrame(); |
| 13 var t = new Date; |
| 14 var elapsed = t - this.lastTime; |
| 15 this.lastTime = t; |
| 16 var self = this; |
| 17 setTimeout(function () { |
| 18 self.timerCallback(); |
| 19 }, Math.max(0, 33-elapsed)); |
| 20 }, |
| 21 |
| 22 init: function() { |
| 23 if (this.initDone) return; |
| 24 this.c2 = document.getElementById("c2"); |
| 25 this.ctx2 = this.c2.getContext(GL_CONTEXT_ID); |
| 26 this.greenScreen = new Filter(this.ctx2, 'identity-flip-vert', 'greenScreen'
); |
| 27 this.ctx2.activeTexture(this.ctx2.TEXTURE0); |
| 28 this.tex = loadTexture(this.ctx2, this.c1, false); |
| 29 this.ctx2.activeTexture(this.ctx2.TEXTURE1); |
| 30 this.tex2 = loadTexture(this.ctx2, document.getElementById('i'), false); |
| 31 this.ctx2.activeTexture(this.ctx2.TEXTURE0); |
| 32 this.initDone = true; |
| 33 }, |
| 34 |
| 35 doLoad: function() { |
| 36 this.video = document.getElementById("video"); |
| 37 this.c1 = document.getElementById("c1"); |
| 38 this.ctx1 = this.c1.getContext("2d"); |
| 39 this.ctx1.globalCompositeOperation = 'copy'; |
| 40 this.ctx1.fillRect(0,0,this.c1.width, this.c1.height); |
| 41 var self = this; |
| 42 this.video.addEventListener("play", function() { |
| 43 self.init(); |
| 44 self.width = self.video.videoWidth; |
| 45 self.height = self.video.videoHeight; |
| 46 self.lastTime = new Date; |
| 47 self.timerCallback(); |
| 48 }, false); |
| 49 }, |
| 50 |
| 51 computeFrame: function() { |
| 52 // this.ctx1.drawImage(this.video, 0, 0, this.width, this.height); |
| 53 this.ctx2.texImage2D(this.ctx2.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_
BYTE, this.video); |
| 54 this.greenScreen.apply(function(s) { |
| 55 s.uniform1i('Texture', 0); |
| 56 s.uniform1i('Texture2', 1); |
| 57 }); |
| 58 |
| 59 return; |
| 60 } |
| 61 }; |
| 62 |
| 63 </script> |
| 64 <script id="identity-flip-vert" type="x-shader/x-vertex"> |
| 65 attribute vec3 Vertex; |
| 66 attribute vec2 Tex; |
| 67 |
| 68 varying vec4 texCoord0; |
| 69 void main() |
| 70 { |
| 71 texCoord0 = vec4(Tex.s,1.0-Tex.t,0.0,0.0); |
| 72 gl_Position = vec4(Vertex, 1.0); |
| 73 } |
| 74 </script> |
| 75 <script id="greenScreen" type="x-shader/x-fragment"> |
| 76 precision mediump float; |
| 77 |
| 78 uniform sampler2D Texture; |
| 79 uniform sampler2D Texture2; |
| 80 |
| 81 varying vec4 texCoord0; |
| 82 void main() |
| 83 { |
| 84 vec4 c = texture2D(Texture, texCoord0.st); |
| 85 float r = c.r * 0.5; |
| 86 float a = c.g * 6.28; |
| 87 float x = cos(a) * r; |
| 88 float y = sin(a) * r; |
| 89 vec4 c2 = texture2D(Texture2, vec2(0.7+x, 0.5+y)); |
| 90 // Shaders with branches are not allowed when dealing with non-SOP conte
nt |
| 91 // so instead of "if (b) c2.a = 0;", we use mix() |
| 92 bool b = (c.g > 120.0/255.0 && c.r > 120.0/255.0 && c.b < 50.0/255.0); |
| 93 c2.a = mix(c2.a, 0.0, float(b)); |
| 94 gl_FragColor = c2; |
| 95 } |
| 96 </script> |
| 97 <style> |
| 98 body { |
| 99 background: black; |
| 100 color:#CCCCCC; |
| 101 } |
| 102 a { |
| 103 color: white; |
| 104 } |
| 105 #c2 { |
| 106 background-image: url(http://www.mozbox.org/pub/green/foo.png); |
| 107 background-repeat: repeat; |
| 108 } |
| 109 div { |
| 110 float: left; |
| 111 border :1px solid #444444; |
| 112 padding:10px; |
| 113 margin: 10px; |
| 114 background:#3B3B3B; |
| 115 } |
| 116 img { display: none; } |
| 117 </style> |
| 118 </head> |
| 119 <body onload="processor.doLoad()"> |
| 120 <div> |
| 121 This is a port of Paul Rouget's <a href="http://blog.mozbox.org/post/200
9/02/25/video-canvas%3A-special-effects">Canvas+Video green screen demo</a>, plu
s a texture lookup from the Firefox logo based on the values of the green and re
d color channels. |
| 122 </div> |
| 123 <div> |
| 124 <video id="video" controls="true"> |
| 125 <source src="http://www.mozbox.org/pub/green/video.ogv"></source> |
| 126 <source src="http://cs.helsinki.fi/u/ilmarihe/c3d/demos/video.mp4"></sou
rce> |
| 127 </video> |
| 128 <canvas id="c1" width="320" height="192"></canvas> |
| 129 </div> |
| 130 <div> |
| 131 <canvas id="c2" width="640" height="384"></canvas> |
| 132 </div> |
| 133 <img id="i" src="http://www.mozbox.org/pub/green/foo.png"> |
| 134 </body> |
| 135 </html> |
OLD | NEW |