Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 03cb451d

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 vec3 v_Normal;                  // Interpolated normal for this fragment.
26
varying vec2 v_TexCoordinate;           // Interpolated texture coordinate per fragment.
27

    
28
uniform int fNumEffects;                // total number of fragment effects
29

    
30
#if NUM_FRAGMENT>0
31
uniform int fType[NUM_FRAGMENT];        // their types.
32
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,
33
                                        // next describes the Region, i.e. area over which the effect is active.
34

    
35
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
36

    
37
//////////////////////////////////////////////////////////////////////////////////////////////
38
// CHROMA EFFECT
39

    
40
#if defined(CHROMA) || defined(SMOOTH_CHROMA)
41
void chroma(float degree, int effect, inout vec4 color)
42
  {
43
  color.rgb = mix(color.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
44
  }
45
#endif
46

    
47
//////////////////////////////////////////////////////////////////////////////////////////////
48
// ALPHA EFFECT (change transparency level)
49

    
50
#if defined(ALPHA) || defined(SMOOTH_ALPHA)
51
void alpha(float degree, int effect, inout vec4 color)
52
  {
53
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
54
  }
55
#endif
56

    
57
//////////////////////////////////////////////////////////////////////////////////////////////
58
// BRIGHTNESS EFFECT
59

    
60
#if defined(BRIGHTNESS) || defined(SMOOTH_BRIGHTNESS)
61
void brightness(float degree, int effect, inout vec4 color)
62
  {
63
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
64
  }
65
#endif
66

    
67
//////////////////////////////////////////////////////////////////////////////////////////////
68
// CONTRAST EFFECT
69

    
70
#if defined(CONTRAST) || defined(SMOOTH_CONTRAST)
71
void contrast(float degree, int effect, inout vec4 color)
72
  {
73
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
74
  }
75
#endif
76

    
77
//////////////////////////////////////////////////////////////////////////////////////////////
78
// SATURATION EFFECT
79

    
80
#if defined(SATURATION) || defined(SMOOTH_SATURATION)
81
void saturation(float degree, int effect, inout vec4 color)
82
  {
83
  float luminance = dot(LUMI,color.rgb);
84
  color.rgb = mix(vec3(luminance,luminance,luminance), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
85
  }
86
#endif
87

    
88
#endif    // NUM_FRAGMENT>0
89

    
90
//////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
void main()                    		
93
  {  
94
  vec4 pixel = texture2D(u_Texture,v_TexCoordinate);
95

    
96
#if NUM_FRAGMENT>0
97
  vec2 diff;
98
  float pointDegree;
99
  int j=0;
100

    
101
  for(int i=0; i<fNumEffects; i++)
102
    {
103
    diff = (v_Position.xy - fUniforms[j+1].xy)/fUniforms[j+1].zw;
104
    pointDegree = max(0.0,1.0-dot(diff,diff));
105

    
106
#ifdef CHROMA
107
    if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),j,pixel); else
108
#endif
109
#ifdef SMOOTH_CHROMA
110
    if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,j,pixel); else
111
#endif
112
#ifdef ALPHA
113
    if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),j,pixel); else
114
#endif
115
#ifdef SMOOTH_ALPHA
116
    if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,j,pixel); else
117
#endif
118
#ifdef BRIGHTNESS
119
    if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),j,pixel); else
120
#endif
121
#ifdef SMOOTH_BRIGHTNESS
122
    if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,j,pixel); else
123
#endif
124
#ifdef CONTRAST
125
    if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),j,pixel); else
126
#endif
127
#ifdef SMOOTH_CONTRAST
128
    if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,j,pixel); else
129
#endif
130
#ifdef SATURATION
131
    if( fType[i]==SATURATION        ) saturation(sign(pointDegree),j,pixel); else
132
#endif
133
#ifdef SMOOTH_SATURATION
134
    if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,j,pixel); else
135
#endif
136
    {}
137

    
138
    j+=2;
139
    }
140
#endif
141

    
142
  gl_FragColor = vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
143
  }
(3-3/6)