Project

General

Profile

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

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

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 4018b3b7 Leszek Koltunski
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 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
42 341c803d Leszek Koltunski
// MACROBLOCK EFFECT
43 6a06a912 Leszek Koltunski
44 4018b3b7 Leszek Koltunski
void macroblock(float degree, int effect, inout vec4 pixel)
45 6a06a912 Leszek Koltunski
  {
46 4018b3b7 Leszek Koltunski
  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 6a06a912 Leszek Koltunski
  }
51
52
//////////////////////////////////////////////////////////////////////////////////////////////
53 341c803d Leszek Koltunski
// CHROMA EFFECT
54 6a06a912 Leszek Koltunski
55 4018b3b7 Leszek Koltunski
void chroma(float degree, int effect, inout vec4 pixel)
56 6a06a912 Leszek Koltunski
  {
57 2e75bd79 Leszek Koltunski
  pixel.rgb = mix( pixel.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
58 6a06a912 Leszek Koltunski
  }
59
60
//////////////////////////////////////////////////////////////////////////////////////////////
61 341c803d Leszek Koltunski
// ALPHA EFFECT (change transparency level)
62 6a06a912 Leszek Koltunski
63 4018b3b7 Leszek Koltunski
void alpha(float degree, int effect, inout vec4 pixel)
64 6a06a912 Leszek Koltunski
  {
65 4018b3b7 Leszek Koltunski
  pixel.a *= (degree*(fUniforms[effect].x-1.0)+1.0);
66 6a06a912 Leszek Koltunski
  }
67
68
//////////////////////////////////////////////////////////////////////////////////////////////
69 341c803d Leszek Koltunski
// BRIGHTNESS EFFECT
70 6a06a912 Leszek Koltunski
71 4018b3b7 Leszek Koltunski
void brightness(float degree, int effect, inout vec4 pixel)
72 6a06a912 Leszek Koltunski
  {
73 4018b3b7 Leszek Koltunski
  pixel.rgb = mix(ZERO3, pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
74 6a06a912 Leszek Koltunski
  }
75 4018b3b7 Leszek Koltunski
76 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
77 341c803d Leszek Koltunski
// CONTRAST EFFECT
78 6a06a912 Leszek Koltunski
79 4018b3b7 Leszek Koltunski
void contrast(float degree, int effect, inout vec4 pixel)
80 6a06a912 Leszek Koltunski
  {
81 4018b3b7 Leszek Koltunski
  pixel.rgb = mix(HALF3, pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
82 6a06a912 Leszek Koltunski
  }
83
84
//////////////////////////////////////////////////////////////////////////////////////////////
85 341c803d Leszek Koltunski
// SATURATION EFFECT
86 6a06a912 Leszek Koltunski
87 4018b3b7 Leszek Koltunski
void saturation(float degree, int effect, inout vec4 pixel)
88 6a06a912 Leszek Koltunski
  {
89 4018b3b7 Leszek Koltunski
  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 6a06a912 Leszek Koltunski
  }
92
93
#endif
94
95
//////////////////////////////////////////////////////////////////////////////////////////////
96
97
void main()                    		
98
  {  
99 4018b3b7 Leszek Koltunski
  vec4 pixel = texture2D(u_Texture, v_TexCoordinate);
100
101
#if NUM_FRAGMENT>0
102 6a06a912 Leszek Koltunski
  vec2 diff;
103
  float pointDegree;
104 4018b3b7 Leszek Koltunski
105 6a06a912 Leszek Koltunski
  for(int i=0; i<fNumEffects; i++)
106
    {
107 2fce34f4 Leszek Koltunski
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
108 6a06a912 Leszek Koltunski
    pointDegree = max(0.0,1.0-dot(diff,diff));
109 341c803d Leszek Koltunski
110 4018b3b7 Leszek Koltunski
         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 6a06a912 Leszek Koltunski
    }
122
#endif
123
 
124 4018b3b7 Leszek Koltunski
  gl_FragColor = pixel*0.5*(v_Normal.z+1.0);
125 341c803d Leszek Koltunski
  }