Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 4018b3b7

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 lowp 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 vec4 fUniforms[2*NUM_FRAGMENT]; // i-th effect is 2 consecutive vec4's: [2*i], [2*i+1]. First vec4 is the Interpolated values,
34
                                        // next describes the Region, i.e. area over which the effect is active.
35

    
36
const vec3 LUMI3= vec3( 0.2125, 0.7154, 0.0721 );
37
const vec3 ZERO3= vec3(0.0,0.0,0.0);
38
const vec3 HALF3= vec3(0.5,0.5,0.5);
39
const vec2 ONE2 = vec2(1.0,1.0);
40
const vec2 HALF2= vec2(0.5,0.5);
41

    
42
//////////////////////////////////////////////////////////////////////////////////////////////
43
// MACROBLOCK EFFECT
44

    
45
void macroblock(float degree, int effect, inout vec4 pixel)
46
  {
47
  vec2 a = degree*(fUniforms[effect].yz-ONE2)+ONE2;
48
  vec2 tex = ( max((1.0-degree)*v_TexCoordinate,floor(v_TexCoordinate*a)) + degree*HALF2 ) / a;
49

    
50
  pixel = texture2D(u_Texture, tex);
51
  }
52

    
53
//////////////////////////////////////////////////////////////////////////////////////////////
54
// CHROMA EFFECT
55

    
56
void chroma(float degree, int effect, inout vec4 pixel)
57
  {
58
  pixel = vec4( mix( pixel.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x), pixel.a);
59
  }
60

    
61
//////////////////////////////////////////////////////////////////////////////////////////////
62
// ALPHA EFFECT (change transparency level)
63

    
64
void alpha(float degree, int effect, inout vec4 pixel)
65
  {
66
  pixel.a *= (degree*(fUniforms[effect].x-1.0)+1.0);
67
  }
68

    
69
//////////////////////////////////////////////////////////////////////////////////////////////
70
// BRIGHTNESS EFFECT
71

    
72
void brightness(float degree, int effect, inout vec4 pixel)
73
  {
74
  pixel.rgb = mix(ZERO3, pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
75
  }
76

    
77
//////////////////////////////////////////////////////////////////////////////////////////////
78
// CONTRAST EFFECT
79

    
80
void contrast(float degree, int effect, inout vec4 pixel)
81
  {
82
  pixel.rgb = mix(HALF3, pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
83
  }
84

    
85
//////////////////////////////////////////////////////////////////////////////////////////////
86
// SATURATION EFFECT
87

    
88
void saturation(float degree, int effect, inout vec4 pixel)
89
  {
90
  float luminance = dot(LUMI3,pixel.rgb);
91
  pixel.rgb = mix(vec3(luminance,luminance,luminance), pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
92
  }
93

    
94
#endif
95

    
96
//////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
void main()                    		
99
  {  
100
  vec4 pixel = texture2D(u_Texture, v_TexCoordinate);
101

    
102
#if NUM_FRAGMENT>0
103
  vec2 diff;
104
  float pointDegree;
105

    
106
  for(int i=0; i<fNumEffects; i++)
107
    {
108
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
109
    pointDegree = max(0.0,1.0-dot(diff,diff));
110

    
111
         if( fType[i]==MACROBLOCK        ) macroblock(sign(pointDegree),2*i, pixel);
112
    else if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i, pixel);
113
    else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i, pixel);
114
    else if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),2*i, pixel);
115
    else if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,2*i, pixel);
116
    else if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),2*i, pixel);
117
    else if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,2*i, pixel);
118
    else if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),2*i, pixel);
119
    else if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,2*i, pixel);
120
    else if( fType[i]==SATURATION        ) saturation(sign(pointDegree),2*i, pixel);
121
    else if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,2*i, pixel);
122
    }
123
#endif
124
 
125
  gl_FragColor = pixel*0.5*(v_Normal.z+1.0);
126
  }
(1-1/2)