-
Notifications
You must be signed in to change notification settings - Fork 10
/
blit_shader.js
77 lines (70 loc) · 2.12 KB
/
blit_shader.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
/*
* Copyright Olli Etuaho 2012-2013.
*/
'use strict';
var blitShader = {};
/**
* Fragment shader source for converting a float monochrome raster to color.
*/
blitShader.convertSimpleSrc = ` precision highp float;
uniform sampler2D uSrcTex;
varying vec2 vTexCoord;
uniform vec4 uColor;
void main(void) {
vec4 src = texture2D(uSrcTex, vTexCoord);
float a = src.w * uColor.w;
gl_FragColor = vec4(uColor.xyz * a, a); // premultiply
}`;
/**
* Fragment shader source for converting a monochrome raster stored in red and
* green channels to color.
*/
blitShader.convertRedGreenSrc = ` precision highp float;
uniform sampler2D uSrcTex;
varying vec2 vTexCoord;
uniform vec4 uColor;
void main(void) {
vec4 src = texture2D(uSrcTex, vTexCoord);
float a = (src.x + src.y / 256.0) * uColor.w;
gl_FragColor = vec4(uColor.xyz * a, a); // premultiply
}`;
/**
* Fragment shader source for a straight-up blit.
*/
blitShader.blitSrc = ` precision highp float;
uniform sampler2D uSrcTex;
varying vec2 vTexCoord;
void main(void) {
gl_FragColor = texture2D(uSrcTex, vTexCoord);
}`;
/**
* Vertex shader source for blitting/conversion/compositing.
*/
blitShader.blitVertSrc = ` precision highp float;
attribute vec2 aVertexPosition;
varying vec2 vTexCoord;
void main(void) {
vTexCoord = vec2((aVertexPosition.x + 1.0) * 0.5, (aVertexPosition.y + 1.0) * 0.5);
gl_Position = vec4(aVertexPosition, 0.0, 1.0);
}`;
/**
* Vertex shader source for drawing scaled/translated image.
*/
blitShader.blitScaledTranslatedVertSrc = ` precision highp float;
attribute vec2 aVertexPosition;
uniform vec2 uScale;
uniform vec2 uTranslate;
varying vec2 vTexCoord;
void main(void) {
vTexCoord = vec2((aVertexPosition.x + 1.0) * 0.5, (aVertexPosition.y + 1.0) * 0.5);
vec2 vertexPosition = aVertexPosition * uScale + uTranslate;
gl_Position = vec4(vertexPosition, 0.0, 1.0);
}`;
/**
* Uniform parameters for the conversion shaders.
* @constructor
*/
blitShader.ConversionUniformParameters = function() {
this['uSrcTex'] = null;
this['uColor'] = new Float32Array([0.0, 0.0, 0.0, 0.0]);
};