1
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
precision highp float;
|
21
|
|
22
|
uniform sampler2D u_Texture; // The input texture.
|
23
|
|
24
|
varying vec3 v_Position; // Interpolated position for this fragment.
|
25
|
varying vec4 v_Color; // This is the color from the vertex shader interpolated across the triangle per fragment.
|
26
|
varying vec3 v_Normal; // Interpolated normal for this fragment.
|
27
|
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
|
28
|
|
29
|
uniform int fNumEffects; // total number of fragment effects
|
30
|
|
31
|
#if NUM_FRAGMENT>0
|
32
|
uniform int fType[NUM_FRAGMENT]; // their types.
|
33
|
uniform vec3 fUniforms[3*NUM_FRAGMENT]; // i-th effect is 3 consecutive vec3's: [3*i], [3*i+1], [3*i+2]. first 4 floats are the Interpolated values,
|
34
|
// next 5 describe the Region, i.e. area over which the effect is active.
|
35
|
// Important note: here the Region is written in a different order than in the Vertex shader.
|
36
|
|
37
|
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );
|
38
|
|
39
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
// macroblocks
|
41
|
|
42
|
void macroblock(float degree, int effect, inout vec2 tex)
|
43
|
{
|
44
|
vec2 one = vec2(1.0,1.0);
|
45
|
vec2 a = degree*(fUniforms[effect].yz-one)+one;
|
46
|
tex = ( max((1.0-degree)*tex,floor(tex*a)) + degree*vec2(0.5,0.5) ) / a;
|
47
|
}
|
48
|
|
49
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
// change the RGB values
|
51
|
|
52
|
void chroma(float degree, int effect, inout vec4 color)
|
53
|
{
|
54
|
color.rgb = mix(color.rgb, vec3(fUniforms[effect].yz,fUniforms[effect+1].x), degree*fUniforms[effect].x);
|
55
|
}
|
56
|
|
57
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
// change the transparency level
|
59
|
|
60
|
void alpha(float degree, int effect, inout vec4 color)
|
61
|
{
|
62
|
color.a *= (degree*(fUniforms[effect].x-1.0)+1.0);
|
63
|
}
|
64
|
|
65
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
// change the brightness level
|
67
|
|
68
|
void brightness(float degree, int effect, inout vec4 color)
|
69
|
{
|
70
|
color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
|
71
|
}
|
72
|
|
73
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
// change the contrast level
|
75
|
|
76
|
void contrast(float degree, int effect, inout vec4 color)
|
77
|
{
|
78
|
color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
|
79
|
}
|
80
|
|
81
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
// change the saturation level
|
83
|
|
84
|
void saturation(float degree, int effect, inout vec4 color)
|
85
|
{
|
86
|
float luminance = dot(LUMI,color.rgb);
|
87
|
color.rgb = mix(vec3(luminance,luminance,luminance), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
|
88
|
}
|
89
|
|
90
|
#endif
|
91
|
|
92
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
void main()
|
95
|
{
|
96
|
vec2 tex = v_TexCoordinate;
|
97
|
vec4 col = v_Color;
|
98
|
vec2 diff;
|
99
|
float pointDegree;
|
100
|
|
101
|
#if NUM_FRAGMENT>0
|
102
|
for(int i=0; i<fNumEffects; i++)
|
103
|
{
|
104
|
diff = (v_Position.xy - fUniforms[3*i+2].yz)/fUniforms[3*i+1].yz;
|
105
|
pointDegree = max(0.0,1.0-dot(diff,diff));
|
106
|
|
107
|
//switch(fType[i])
|
108
|
// {
|
109
|
// case MACROBLOCK : macroblock(sign(pointDegree),3*i,tex); break;
|
110
|
// case CHROMA : chroma (sign(pointDegree),3*i,col); break;
|
111
|
// case SMOOTH_CHROMA : chroma ( pointDegree ,3*i,col); break;
|
112
|
// case ALPHA : alpha (sign(pointDegree),3*i,col); break;
|
113
|
// case SMOOTH_ALPHA : alpha ( pointDegree ,3*i,col); break;
|
114
|
// case BRIGHTNESS : brightness(sign(pointDegree),3*i,col); break;
|
115
|
// case SMOOTH_BRIGHTNESS : brightness( pointDegree ,3*i,col); break;
|
116
|
// case CONTRAST : contrast (sign(pointDegree),3*i,col); break;
|
117
|
// case SMOOTH_CONTRAST : contrast ( pointDegree ,3*i,col); break;
|
118
|
// case SATURATION : saturation(sign(pointDegree),3*i,col); break;
|
119
|
// case SMOOTH_SATURATION : saturation( pointDegree ,3*i,col); break;
|
120
|
// }
|
121
|
|
122
|
if( fType[i]==MACROBLOCK ) macroblock(sign(pointDegree),3*i,tex);
|
123
|
else if( fType[i]==CHROMA ) chroma (sign(pointDegree),3*i,col);
|
124
|
else if( fType[i]==SMOOTH_CHROMA ) chroma ( pointDegree ,3*i,col);
|
125
|
else if( fType[i]==ALPHA ) alpha (sign(pointDegree),3*i,col);
|
126
|
else if( fType[i]==SMOOTH_ALPHA ) alpha ( pointDegree ,3*i,col);
|
127
|
else if( fType[i]==BRIGHTNESS ) brightness(sign(pointDegree),3*i,col);
|
128
|
else if( fType[i]==SMOOTH_BRIGHTNESS ) brightness( pointDegree ,3*i,col);
|
129
|
else if( fType[i]==CONTRAST ) contrast (sign(pointDegree),3*i,col);
|
130
|
else if( fType[i]==SMOOTH_CONTRAST ) contrast ( pointDegree ,3*i,col);
|
131
|
else if( fType[i]==SATURATION ) saturation(sign(pointDegree),3*i,col);
|
132
|
else if( fType[i]==SMOOTH_SATURATION ) saturation( pointDegree ,3*i,col);
|
133
|
}
|
134
|
#endif
|
135
|
|
136
|
gl_FragColor = (col * 0.5 * (v_Normal.z+1.0) * texture2D(u_Texture, tex));
|
137
|
}
|