Project

General

Profile

Download (5.07 KB) Statistics
| Branch: | Revision:

library / src / main / res / raw / main_fragment_shader.glsl @ 6a06a912

1

    
2
precision highp float;
3
  
4
uniform sampler2D u_Texture;    // The input texture.
5
    
6
varying vec3 v_Position;		// Interpolated position for this fragment.
7
varying vec4 v_Color;          	// This is the color from the vertex shader interpolated across the triangle per fragment.
8
varying vec3 v_Normal;         	// Interpolated normal for this fragment.
9
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
10

    
11
uniform int fNumEffects;                // total number of fragment effects
12

    
13
#if NUM_FRAGMENT>0
14
uniform int fType[NUM_FRAGMENT];        // their types.
15
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,
16
                                        // next 5 describe the Region, i.e. area over which the effect is active.
17
                                        // Important note: here the Region is written in a different order than in the Vertex shader.
18
 
19
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
20
 
21
//////////////////////////////////////////////////////////////////////////////////////////////
22
// macroblocks
23

    
24
void macroblock(float degree, int effect, inout vec2 tex)
25
  {
26
  vec2 one = vec2(1.0,1.0);  
27
  vec2 a = degree*(fUniforms[effect].yz-one)+one;
28
  tex = ( max((1.0-degree)*tex,floor(tex*a)) + degree*vec2(0.5,0.5) ) / a;
29
  }
30

    
31
//////////////////////////////////////////////////////////////////////////////////////////////
32
// change the RGB values
33

    
34
void chroma(float degree, int effect, inout vec4 color)
35
  {
36
  color.rgb = mix(color.rgb, vec3(fUniforms[effect].yz,fUniforms[effect+1].x), degree*fUniforms[effect].x); 
37
  }
38

    
39
//////////////////////////////////////////////////////////////////////////////////////////////
40
// change the transparency level
41

    
42
void alpha(float degree, int effect, inout vec4 color)
43
  {
44
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
45
  }
46

    
47
//////////////////////////////////////////////////////////////////////////////////////////////
48
// change the brightness level
49

    
50
void brightness(float degree, int effect, inout vec4 color)
51
  {
52
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
53
  }
54
  
55
//////////////////////////////////////////////////////////////////////////////////////////////
56
// change the contrast level
57

    
58
void contrast(float degree, int effect, inout vec4 color)
59
  {
60
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
61
  }
62

    
63
//////////////////////////////////////////////////////////////////////////////////////////////
64
// change the saturation level
65

    
66
void saturation(float degree, int effect, inout vec4 color)
67
  {
68
  float luminance = dot(LUMI,color.rgb);
69
  color.rgb = mix(vec3(luminance,luminance,luminance), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
70
  }
71

    
72
#endif
73

    
74
//////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
void main()                    		
77
  {  
78
  vec2 tex = v_TexCoordinate;
79
  vec4 col = v_Color;
80
  vec2 diff;
81
  float pointDegree;
82
  
83
#if NUM_FRAGMENT>0  
84
  for(int i=0; i<fNumEffects; i++)
85
    {
86
    diff = (v_Position.xy - fUniforms[3*i+2].yz)/fUniforms[3*i+1].yz;
87
    pointDegree = max(0.0,1.0-dot(diff,diff));
88
  
89
    //switch(fType[i])
90
    //  {
91
    //  case MACROBLOCK        : macroblock(sign(pointDegree),3*i,tex); break;
92
    //  case CHROMA            : chroma    (sign(pointDegree),3*i,col); break;
93
    //  case SMOOTH_CHROMA     : chroma    (     pointDegree ,3*i,col); break;
94
    //  case ALPHA             : alpha     (sign(pointDegree),3*i,col); break;
95
    //  case SMOOTH_ALPHA      : alpha     (     pointDegree ,3*i,col); break;
96
    //  case BRIGHTNESS        : brightness(sign(pointDegree),3*i,col); break;
97
    //  case SMOOTH_BRIGHTNESS : brightness(     pointDegree ,3*i,col); break;
98
    //  case CONTRAST          : contrast  (sign(pointDegree),3*i,col); break;
99
    //  case SMOOTH_CONTRAST   : contrast  (     pointDegree ,3*i,col); break;
100
    //  case SATURATION        : saturation(sign(pointDegree),3*i,col); break;
101
    //  case SMOOTH_SATURATION : saturation(     pointDegree ,3*i,col); break;
102
    //  }
103
    
104
         if( fType[i]==MACROBLOCK        ) macroblock(sign(pointDegree),3*i,tex);
105
    else if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),3*i,col);
106
    else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,3*i,col);
107
    else if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),3*i,col);
108
    else if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,3*i,col);
109
    else if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),3*i,col);
110
    else if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,3*i,col);
111
    else if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),3*i,col);
112
    else if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,3*i,col);
113
    else if( fType[i]==SATURATION        ) saturation(sign(pointDegree),3*i,col);
114
    else if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,3*i,col);
115
    }
116
#endif
117
 
118
  gl_FragColor = (col * 0.5 * (v_Normal.z+1.0) * texture2D(u_Texture, tex));
119
  }
(1-1/2)