OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <!-- |
| 6 |
| 7 /* |
| 8 ** Copyright (c) 2012 The Khronos Group Inc. |
| 9 ** |
| 10 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 11 ** copy of this software and/or associated documentation files (the |
| 12 ** "Materials"), to deal in the Materials without restriction, including |
| 13 ** without limitation the rights to use, copy, modify, merge, publish, |
| 14 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 15 ** permit persons to whom the Materials are furnished to do so, subject to |
| 16 ** the following conditions: |
| 17 ** |
| 18 ** The above copyright notice and this permission notice shall be included |
| 19 ** in all copies or substantial portions of the Materials. |
| 20 ** |
| 21 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 24 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 25 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 26 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 27 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 28 */ |
| 29 |
| 30 --> |
| 31 <script type="application/x-javascript" src="../util.js"></script> |
| 32 <script> |
| 33 var processor = { |
| 34 lastTime : new Date, |
| 35 timerCallback: function() { |
| 36 if (this.video.paused || this.video.ended) { |
| 37 return; |
| 38 } |
| 39 this.computeFrame(); |
| 40 var t = new Date; |
| 41 var elapsed = t - this.lastTime; |
| 42 this.lastTime = t; |
| 43 var self = this; |
| 44 setTimeout(function () { |
| 45 self.timerCallback(); |
| 46 }, Math.max(0, 33-elapsed)); |
| 47 }, |
| 48 |
| 49 init: function() { |
| 50 if (this.initDone) return; |
| 51 this.c2 = document.getElementById("c2"); |
| 52 this.ctx2 = this.c2.getContext(GL_CONTEXT_ID); |
| 53 this.greenScreen = new Filter(this.ctx2, 'identity-flip-vert', 'greenScreen'
); |
| 54 this.ctx2.activeTexture(this.ctx2.TEXTURE0); |
| 55 this.tex = loadTexture(this.ctx2, this.c1, false); |
| 56 this.ctx2.activeTexture(this.ctx2.TEXTURE1); |
| 57 this.tex2 = loadTexture(this.ctx2, document.getElementById('i'), false); |
| 58 this.ctx2.activeTexture(this.ctx2.TEXTURE0); |
| 59 this.initDone = true; |
| 60 }, |
| 61 |
| 62 doLoad: function() { |
| 63 this.video = document.getElementById("video"); |
| 64 this.c1 = document.getElementById("c1"); |
| 65 this.ctx1 = this.c1.getContext("2d"); |
| 66 this.ctx1.globalCompositeOperation = 'copy'; |
| 67 this.ctx1.fillRect(0,0,this.c1.width, this.c1.height); |
| 68 var self = this; |
| 69 this.video.addEventListener("play", function() { |
| 70 self.init(); |
| 71 self.width = self.video.videoWidth; |
| 72 self.height = self.video.videoHeight; |
| 73 self.lastTime = new Date; |
| 74 self.timerCallback(); |
| 75 }, false); |
| 76 }, |
| 77 |
| 78 computeFrame: function() { |
| 79 // this.ctx1.drawImage(this.video, 0, 0, this.width, this.height); |
| 80 this.ctx2.texImage2D(this.ctx2.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_
BYTE, this.video); |
| 81 this.greenScreen.apply(function(s) { |
| 82 s.uniform1i('Texture', 0); |
| 83 s.uniform1i('Texture2', 1); |
| 84 }); |
| 85 |
| 86 return; |
| 87 } |
| 88 }; |
| 89 |
| 90 </script> |
| 91 <script id="identity-flip-vert" type="x-shader/x-vertex"> |
| 92 attribute vec3 Vertex; |
| 93 attribute vec2 Tex; |
| 94 |
| 95 varying vec4 texCoord0; |
| 96 void main() |
| 97 { |
| 98 texCoord0 = vec4(Tex.s,1.0-Tex.t,0.0,0.0); |
| 99 gl_Position = vec4(Vertex, 1.0); |
| 100 } |
| 101 </script> |
| 102 <script id="greenScreen" type="x-shader/x-fragment"> |
| 103 precision mediump float; |
| 104 |
| 105 uniform sampler2D Texture; |
| 106 uniform sampler2D Texture2; |
| 107 |
| 108 varying vec4 texCoord0; |
| 109 void main() |
| 110 { |
| 111 vec4 c = texture2D(Texture, texCoord0.st); |
| 112 float r = c.r * 0.5; |
| 113 float a = c.g * 6.28; |
| 114 float x = cos(a) * r; |
| 115 float y = sin(a) * r; |
| 116 vec4 c2 = texture2D(Texture2, vec2(0.7+x, 0.5+y)); |
| 117 // Shaders with branches are not allowed when dealing with non-SOP conte
nt |
| 118 // so instead of "if (b) c2.a = 0;", we use mix() |
| 119 bool b = (c.g > 120.0/255.0 && c.r > 120.0/255.0 && c.b < 50.0/255.0); |
| 120 c2.a = mix(c2.a, 0.0, float(b)); |
| 121 gl_FragColor = c2; |
| 122 } |
| 123 </script> |
| 124 <style> |
| 125 body { |
| 126 background: black; |
| 127 color:#CCCCCC; |
| 128 } |
| 129 a { |
| 130 color: white; |
| 131 } |
| 132 #c2 { |
| 133 background-image: url(http://www.mozbox.org/pub/green/foo.png); |
| 134 background-repeat: repeat; |
| 135 } |
| 136 div { |
| 137 float: left; |
| 138 border :1px solid #444444; |
| 139 padding:10px; |
| 140 margin: 10px; |
| 141 background:#3B3B3B; |
| 142 } |
| 143 img { display: none; } |
| 144 </style> |
| 145 </head> |
| 146 <body onload="processor.doLoad()"> |
| 147 <div> |
| 148 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. |
| 149 </div> |
| 150 <div> |
| 151 <video id="video" controls="true"> |
| 152 <source src="http://www.mozbox.org/pub/green/video.ogv"></source> |
| 153 <source src="http://cs.helsinki.fi/u/ilmarihe/c3d/demos/video.mp4"></sou
rce> |
| 154 </video> |
| 155 <canvas id="c1" width="320" height="192"></canvas> |
| 156 </div> |
| 157 <div> |
| 158 <canvas id="c2" width="640" height="384"></canvas> |
| 159 </div> |
| 160 <img id="i" src="http://www.mozbox.org/pub/green/foo.png"> |
| 161 </body> |
| 162 </html> |
OLD | NEW |