Project

General

Profile

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

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

1 d333eb6b Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
20 8cefe6a3 Leszek Koltunski
precision lowp float;
21 6a06a912 Leszek Koltunski
  
22 d333eb6b Leszek Koltunski
uniform sampler2D u_Texture;            // The input texture.
23 6a06a912 Leszek Koltunski
    
24 2fce34f4 Leszek Koltunski
varying vec3 v_Position;                // Interpolated position for this fragment.
25 d333eb6b Leszek Koltunski
varying vec3 v_Normal;                  // Interpolated normal for this fragment.
26
varying vec2 v_TexCoordinate;           // Interpolated texture coordinate per fragment.
27 6a06a912 Leszek Koltunski
28
uniform int fNumEffects;                // total number of fragment effects
29
30
#if NUM_FRAGMENT>0
31
uniform int fType[NUM_FRAGMENT];        // their types.
32 2fce34f4 Leszek Koltunski
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 ffbf279e Leszek Koltunski
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
36
 
37 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
38 341c803d Leszek Koltunski
// MACROBLOCK EFFECT
39 6a06a912 Leszek Koltunski
40 ffbf279e Leszek Koltunski
void macroblock(float degree, int effect, inout vec2 tex)
41 6a06a912 Leszek Koltunski
  {
42 ffbf279e Leszek Koltunski
  vec2 one = vec2(1.0,1.0);  
43
  vec2 a = degree*(fUniforms[effect].yz-one)+one;
44
  tex = ( max((1.0-degree)*tex,floor(tex*a)) + degree*vec2(0.5,0.5) ) / a;
45 6a06a912 Leszek Koltunski
  }
46
47
//////////////////////////////////////////////////////////////////////////////////////////////
48 341c803d Leszek Koltunski
// CHROMA EFFECT
49 6a06a912 Leszek Koltunski
50 ffbf279e Leszek Koltunski
void chroma(float degree, int effect, inout vec4 color)
51 6a06a912 Leszek Koltunski
  {
52 ffbf279e Leszek Koltunski
  color.rgb = mix(color.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
53 6a06a912 Leszek Koltunski
  }
54
55
//////////////////////////////////////////////////////////////////////////////////////////////
56 341c803d Leszek Koltunski
// ALPHA EFFECT (change transparency level)
57 6a06a912 Leszek Koltunski
58 ffbf279e Leszek Koltunski
void alpha(float degree, int effect, inout vec4 color)
59 6a06a912 Leszek Koltunski
  {
60 ffbf279e Leszek Koltunski
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
61 6a06a912 Leszek Koltunski
  }
62
63
//////////////////////////////////////////////////////////////////////////////////////////////
64 341c803d Leszek Koltunski
// BRIGHTNESS EFFECT
65 6a06a912 Leszek Koltunski
66 ffbf279e Leszek Koltunski
void brightness(float degree, int effect, inout vec4 color)
67 6a06a912 Leszek Koltunski
  {
68 ffbf279e Leszek Koltunski
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
69 6a06a912 Leszek Koltunski
  }
70 ffbf279e Leszek Koltunski
  
71 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
72 341c803d Leszek Koltunski
// CONTRAST EFFECT
73 6a06a912 Leszek Koltunski
74 ffbf279e Leszek Koltunski
void contrast(float degree, int effect, inout vec4 color)
75 6a06a912 Leszek Koltunski
  {
76 ffbf279e Leszek Koltunski
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
77 6a06a912 Leszek Koltunski
  }
78
79
//////////////////////////////////////////////////////////////////////////////////////////////
80 341c803d Leszek Koltunski
// SATURATION EFFECT
81 6a06a912 Leszek Koltunski
82 ffbf279e Leszek Koltunski
void saturation(float degree, int effect, inout vec4 color)
83 6a06a912 Leszek Koltunski
  {
84 ffbf279e Leszek Koltunski
  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 6a06a912 Leszek Koltunski
  }
87
88
#endif
89
90
//////////////////////////////////////////////////////////////////////////////////////////////
91
92
void main()                    		
93
  {  
94 ffbf279e Leszek Koltunski
  vec2 tex = v_TexCoordinate;
95 ff8ad0a7 Leszek Koltunski
  vec4 col = texture2D(u_Texture, tex);
96 4018b3b7 Leszek Koltunski
97
#if NUM_FRAGMENT>0
98 6a06a912 Leszek Koltunski
  vec2 diff;
99
  float pointDegree;
100 ffbf279e Leszek Koltunski
  
101 6a06a912 Leszek Koltunski
  for(int i=0; i<fNumEffects; i++)
102
    {
103 2fce34f4 Leszek Koltunski
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
104 6a06a912 Leszek Koltunski
    pointDegree = max(0.0,1.0-dot(diff,diff));
105 341c803d Leszek Koltunski
106 ffbf279e Leszek Koltunski
         if( fType[i]==MACROBLOCK        ) macroblock(sign(pointDegree),2*i,tex);
107
    else if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i,col);
108
    else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i,col);
109
    else if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),2*i,col);
110
    else if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,2*i,col);
111
    else if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),2*i,col);
112
    else if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,2*i,col);
113
    else if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),2*i,col);
114
    else if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,2*i,col);
115
    else if( fType[i]==SATURATION        ) saturation(sign(pointDegree),2*i,col);
116
    else if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,2*i,col);
117 6a06a912 Leszek Koltunski
    }
118
#endif
119 ff8ad0a7 Leszek Koltunski
120
  gl_FragColor = vec4(col.rgb * 0.5 * (v_Normal.z+1.0), col.a);
121 341c803d Leszek Koltunski
  }