Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ ad33f883

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

    
34
#if defined(SATURATION) || defined(SMOOTH_SATURATION)
35
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
36
#endif
37

    
38
//////////////////////////////////////////////////////////////////////////////////////////////
39
// CHROMA EFFECT
40

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

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

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

    
58
//////////////////////////////////////////////////////////////////////////////////////////////
59
// BRIGHTNESS EFFECT
60

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

    
68
//////////////////////////////////////////////////////////////////////////////////////////////
69
// CONTRAST EFFECT
70

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

    
78
//////////////////////////////////////////////////////////////////////////////////////////////
79
// SATURATION EFFECT
80

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

    
89
#endif    // NUM_FRAGMENT>0
90

    
91
//////////////////////////////////////////////////////////////////////////////////////////////
92

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

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

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

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

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

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