Project

General

Profile

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

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

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
void chroma(float degree, int effect, inout vec4 color)
41
  {
42
  color.rgb = mix(color.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
43
  }
44

    
45
//////////////////////////////////////////////////////////////////////////////////////////////
46
// ALPHA EFFECT (change transparency level)
47

    
48
void alpha(float degree, int effect, inout vec4 color)
49
  {
50
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
51
  }
52

    
53
//////////////////////////////////////////////////////////////////////////////////////////////
54
// BRIGHTNESS EFFECT
55

    
56
void brightness(float degree, int effect, inout vec4 color)
57
  {
58
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
59
  }
60
  
61
//////////////////////////////////////////////////////////////////////////////////////////////
62
// CONTRAST EFFECT
63

    
64
void contrast(float degree, int effect, inout vec4 color)
65
  {
66
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
67
  }
68

    
69
//////////////////////////////////////////////////////////////////////////////////////////////
70
// SATURATION EFFECT
71

    
72
void saturation(float degree, int effect, inout vec4 color)
73
  {
74
  float luminance = dot(LUMI,color.rgb);
75
  color.rgb = mix(vec3(luminance,luminance,luminance), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
76
  }
77

    
78
#endif
79

    
80
//////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
void main()                    		
83
  {  
84
  vec4 pixel = texture2D(u_Texture,v_TexCoordinate);
85

    
86
#if NUM_FRAGMENT>0
87
  vec2 diff;
88
  float pointDegree;
89
  
90
  for(int i=0; i<fNumEffects; i++)
91
    {
92
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
93
    pointDegree = max(0.0,1.0-dot(diff,diff));
94

    
95
         if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i,pixel);
96
    else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i,pixel);
97
    else if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),2*i,pixel);
98
    else if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,2*i,pixel);
99
    else if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),2*i,pixel);
100
    else if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,2*i,pixel);
101
    else if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),2*i,pixel);
102
    else if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,2*i,pixel);
103
    else if( fType[i]==SATURATION        ) saturation(sign(pointDegree),2*i,pixel);
104
    else if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,2*i,pixel);
105
    }
106
#endif
107

    
108
  gl_FragColor = vec4(pixel.rgb * v_Normal.z, pixel.a);
109
  }
(1-1/4)