Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 2e75bd79

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 LUMI3= vec3( 0.2125, 0.7154, 0.0721 );
36
const vec3 ZERO3= vec3(0.0,0.0,0.0);
37
const vec3 HALF3= vec3(0.5,0.5,0.5);
38
const vec2 ONE2 = vec2(1.0,1.0);
39
const vec2 HALF2= vec2(0.5,0.5);
40

    
41
//////////////////////////////////////////////////////////////////////////////////////////////
42
// MACROBLOCK EFFECT
43

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

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

    
52
//////////////////////////////////////////////////////////////////////////////////////////////
53
// CHROMA EFFECT
54

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

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

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

    
68
//////////////////////////////////////////////////////////////////////////////////////////////
69
// BRIGHTNESS EFFECT
70

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

    
76
//////////////////////////////////////////////////////////////////////////////////////////////
77
// CONTRAST EFFECT
78

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

    
84
//////////////////////////////////////////////////////////////////////////////////////////////
85
// SATURATION EFFECT
86

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

    
93
#endif
94

    
95
//////////////////////////////////////////////////////////////////////////////////////////////
96

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

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

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

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