-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
310 lines (251 loc) · 8.68 KB
/
index.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
import fragmentShaderAlphaSrc from './lib/shaders/fragmentShaderAlpha'
import fragmentShaderPaintSrc from './lib/shaders/fragmentShaderPaint'
import vertexShaderSrc from './lib/shaders/vertexShader'
import ShaderProgram from './lib/ShaderProgram'
import FrameBuffer from './lib/FrameBuffer'
const nodeData = {
video : {
ready: 'readyState',
load: 'canplay',
width: 'videoWidth',
height: 'videoHeight'
},
img : {
ready: 'complete',
load: 'load',
width: 'width',
height: 'height'
},
canvas: {
ready: 'complete',
load: 'load',
width: 'width',
height: 'height'
}
}
function buildWebGlBuffers () {
// todo: change this to line_strip or fan for speed?
const gl = this._gl
const vertexPositionBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1, -1, 0,
1, -1, 0,
1, 1, 0,
-1, 1, 0
]), gl.STATIC_DRAW)
vertexPositionBuffer.itemSize = 3
vertexPositionBuffer.numItems = 4
const texCoordBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
0, 1,
1, 1,
1, 0,
0, 0
]), gl.STATIC_DRAW)
texCoordBuffer.itemSize = 2
texCoordBuffer.numItems = 4
const vertexIndexBuffer = gl.createBuffer()
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer)
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
0, 2, 1, 0, 3, 2 // Front face
]), gl.STATIC_DRAW)
vertexIndexBuffer.itemSize = 1
vertexIndexBuffer.numItems = 6
this._vertexPositionBuffer = vertexPositionBuffer
this._vertexIndexBuffer = vertexIndexBuffer
this._texCoordBuffer = texCoordBuffer
}
function setUpShaders () {
const gl = this._gl
let keyFunctions = ''
this._keys.forEach(k => {
const color = (k.color === 'auto') ? 'auto()' : `vec3(${k.color[0] / 255},${k.color[1] / 255},${k.color[2] / 255})`
const tolerance = isNaN(k.tolerance) ? 0.3 : k.tolerance.toFixed(3)
const amount = isNaN(k.amount) ? 1 : k.amount.toFixed(3)
keyFunctions += `pixel.a = distAlpha(${color}, ${tolerance}, ${amount});\n`
if (k.debug) keyFunctions += 'debug();\n'
})
if (this._alphaShader) this._alphaShader.unload()
if (this._paintShader) this._paintShader.unload()
if (this._alphaFramebuffer) this._alphaFramebuffer.unload()
this._alphaShader = new ShaderProgram(gl, vertexShaderSrc, fragmentShaderAlphaSrc)
this._paintShader = new ShaderProgram(gl, vertexShaderSrc, fragmentShaderPaintSrc.replace('%keys%', keyFunctions))
// holds downsampled image for auto-keying
this._alphaFramebuffer = new FrameBuffer(gl, 16, 16)
}
function initializeTextures () {
const gl = this._gl
// this assumes media has been loaded
function loadTexture (media) {
const texture = gl.createTexture()
texture.image = media
gl.bindTexture(gl.TEXTURE_2D, texture)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
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.bindTexture(gl.TEXTURE_2D, null)
texture.height = texture.image.height
texture.width = texture.image.width
return texture
}
this._mediaTexture = loadTexture(this._media)
}
function drawScreen (shader, sourceTexture, alphaTexture) {
const gl = this._gl
shader.useProgram()
/* todo: do this all only once at the beginning, since we only have one model */
gl.enableVertexAttribArray(shader.location_position)
gl.enableVertexAttribArray(shader.location_texCoord)
gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordBuffer)
gl.vertexAttribPointer(shader.location_texCoord, this._texCoordBuffer.itemSize, gl.FLOAT, false, 0, 0)
gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexPositionBuffer)
gl.vertexAttribPointer(shader.location_position, this._vertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0)
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._vertexIndexBuffer)
// set up textures
if (sourceTexture) {
shader.set_source(0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, sourceTexture)
}
if (alphaTexture) {
shader.set_alpha(1)
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, alphaTexture)
}
/* do this every time */
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.enable(gl.BLEND)
gl.disable(gl.DEPTH_TEST)
// draw!
gl.drawElements(gl.TRIANGLES, this._vertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0)
}
function checkReady (callback) {
if (!this._initialized) {
return
}
if (!this._data.ready || !this._data.load || this._media[this._data.ready] === this._data.readyTarget ||
(this._data.readyTarget === undefined && this._media[this._data.ready])) {
initializeTextures.apply(this)
setUpShaders.apply(this)
this.render()
if (typeof callback === 'function') {
callback()
}
} else {
setTimeout(() => {
checkReady.apply(this, callback)
}, 0)
}
}
class ChromaGL {
constructor (source, target) {
if (!this.hasWebGL2()) {
throw new Error('Browser does not support WebGL 2')
}
this._keys = []
this.source(source)
this.target(target)
buildWebGlBuffers.apply(this)
this._initialized = true
checkReady.call(this)
}
hasWebGL2 () {
try {
return !!window.WebGLRenderingContext && !!document.createElement('canvas').getContext('webgl2')
} catch (e) {
return false
}
}
source (source) {
if (!source || !source.tagName) {
throw new Error('Missing source element')
}
this._data = nodeData[source.tagName.toLowerCase()]
if (!this._data) {
throw new Error('Unsupported source media type')
}
this._media = source
checkReady.call(this)
return this
}
target (target) {
if (target instanceof HTMLCanvasElement) {
this._gl = target.getContext('webgl2')
} else if (target instanceof WebGLRenderingContext) {
this._gl = target
} else {
throw new Error('Target must be an HTMLCanvasElement (or its WebGLRenderingContext)')
}
setUpShaders.apply(this)
return this
}
render () {
if (!this._mediaTexture || !this._mediaTexture.image || !this._media[this._data.ready]) {
return
}
const gl = this._gl
// refresh source image
gl.bindTexture(gl.TEXTURE_2D, this._mediaTexture)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._mediaTexture.image)
gl.bindTexture(gl.TEXTURE_2D, null)
this.paint()
}
paint () {
if (!this._alphaShader || !this._media[this._data.ready]) {
return
}
const gl = this._gl
// downsample image to a 16x16 framebuffer used for auto-keying
gl.viewport(0, 0, 16, 16)
gl.bindFramebuffer(gl.FRAMEBUFFER, this._alphaFramebuffer.framebuffer)
gl.clear(gl.COLOR_BUFFER_BIT)
drawScreen.call(this, this._alphaShader, this._mediaTexture, null)
// draw final image to canvas
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
drawScreen.call(this, this._paintShader, this._mediaTexture, this._alphaFramebuffer.texture)
return this
}
key (...keys) {
this._keys = []
if (Array.isArray(keys[0]) && Array.isArray(keys[0][0])) {
keys = keys[0]
}
// validate
keys.forEach(key => {
if (Array.isArray(key)) {
if (key.length !== 3) throw new Error('Key color must be \'auto\' or an array like [r, g, b]')
if (key.some(c => isNaN(c))) throw new Error('Invalid key color component')
key = { color: key }
} else if (typeof key === 'object') {
if (Array.isArray(key.color) && key.color.length === 3) {
if (key.color.some(c => isNaN(c))) throw new Error('Invalid key color component')
} else if (key.color !== 'auto') {
throw new Error('Key color must be \'auto\' or an array like [r, g, b]')
}
} else if (key === 'auto') {
key = { color: 'auto' }
} else {
throw new Error('Unsupported chroma key type')
}
this._keys.push(key)
})
setUpShaders.apply(this)
this.render()
}
unload () {
if (!this._gl || !this._alphaShader || !this._paintShader || !this._alphaFramebuffer) return
this._alphaShader.unload()
this._paintShader.unload()
this._alphaFramebuffer.unload()
this._gl.deleteBuffer(this._vertexPositionBuffer)
this._gl.deleteBuffer(this._vertexIndexBuffer)
this._gl.deleteBuffer(this._texCoordBuffer)
this._gl.deleteTexture(this._mediaTexture)
}
}
export default ChromaGL