Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 4018b3b7

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 vec4 v_Color;                   // This is the color from the vertex shader interpolated across the triangle per fragment.
26
varying vec3 v_Normal;                  // Interpolated normal for this fragment.
27
varying vec2 v_TexCoordinate;           // Interpolated texture coordinate per fragment.
28 6a06a912 Leszek Koltunski
29
uniform int fNumEffects;                // total number of fragment effects
30
31
#if NUM_FRAGMENT>0
32
uniform int fType[NUM_FRAGMENT];        // their types.
33 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,
34
                                        // next describes the Region, i.e. area over which the effect is active.
35
36 4018b3b7 Leszek Koltunski
const vec3 LUMI3= vec3( 0.2125, 0.7154, 0.0721 );
37
const vec3 ZERO3= vec3(0.0,0.0,0.0);
38
const vec3 HALF3= vec3(0.5,0.5,0.5);
39
const vec2 ONE2 = vec2(1.0,1.0);
40
const vec2 HALF2= vec2(0.5,0.5);
41
42 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
43 341c803d Leszek Koltunski
// MACROBLOCK EFFECT
44 6a06a912 Leszek Koltunski
45 4018b3b7 Leszek Koltunski
void macroblock(float degree, int effect, inout vec4 pixel)
46 6a06a912 Leszek Koltunski
  {
47 4018b3b7 Leszek Koltunski
  vec2 a = degree*(fUniforms[effect].yz-ONE2)+ONE2;
48
  vec2 tex = ( max((1.0-degree)*v_TexCoordinate,floor(v_TexCoordinate*a)) + degree*HALF2 ) / a;
49
50
  pixel = texture2D(u_Texture, tex);
51 6a06a912 Leszek Koltunski
  }
52
53
//////////////////////////////////////////////////////////////////////////////////////////////
54 341c803d Leszek Koltunski
// CHROMA EFFECT
55 6a06a912 Leszek Koltunski
56 4018b3b7 Leszek Koltunski
void chroma(float degree, int effect, inout vec4 pixel)
57 6a06a912 Leszek Koltunski
  {
58 4018b3b7 Leszek Koltunski
  pixel = vec4( mix( pixel.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x), pixel.a);
59 6a06a912 Leszek Koltunski
  }
60
61
//////////////////////////////////////////////////////////////////////////////////////////////
62 341c803d Leszek Koltunski
// ALPHA EFFECT (change transparency level)
63 6a06a912 Leszek Koltunski
64 4018b3b7 Leszek Koltunski
void alpha(float degree, int effect, inout vec4 pixel)
65 6a06a912 Leszek Koltunski
  {
66 4018b3b7 Leszek Koltunski
  pixel.a *= (degree*(fUniforms[effect].x-1.0)+1.0);
67 6a06a912 Leszek Koltunski
  }
68
69
//////////////////////////////////////////////////////////////////////////////////////////////
70 341c803d Leszek Koltunski
// BRIGHTNESS EFFECT
71 6a06a912 Leszek Koltunski
72 4018b3b7 Leszek Koltunski
void brightness(float degree, int effect, inout vec4 pixel)
73 6a06a912 Leszek Koltunski
  {
74 4018b3b7 Leszek Koltunski
  pixel.rgb = mix(ZERO3, pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
75 6a06a912 Leszek Koltunski
  }
76 4018b3b7 Leszek Koltunski
77 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
78 341c803d Leszek Koltunski
// CONTRAST EFFECT
79 6a06a912 Leszek Koltunski
80 4018b3b7 Leszek Koltunski
void contrast(float degree, int effect, inout vec4 pixel)
81 6a06a912 Leszek Koltunski
  {
82 4018b3b7 Leszek Koltunski
  pixel.rgb = mix(HALF3, pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
83 6a06a912 Leszek Koltunski
  }
84
85
//////////////////////////////////////////////////////////////////////////////////////////////
86 341c803d Leszek Koltunski
// SATURATION EFFECT
87 6a06a912 Leszek Koltunski
88 4018b3b7 Leszek Koltunski
void saturation(float degree, int effect, inout vec4 pixel)
89 6a06a912 Leszek Koltunski
  {
90 4018b3b7 Leszek Koltunski
  float luminance = dot(LUMI3,pixel.rgb);
91
  pixel.rgb = mix(vec3(luminance,luminance,luminance), pixel.rgb, degree*(fUniforms[effect].x-1.0)+1.0 );
92 6a06a912 Leszek Koltunski
  }
93
94
#endif
95
96
//////////////////////////////////////////////////////////////////////////////////////////////
97
98
void main()                    		
99
  {  
100 4018b3b7 Leszek Koltunski
  vec4 pixel = texture2D(u_Texture, v_TexCoordinate);
101
102
#if NUM_FRAGMENT>0
103 6a06a912 Leszek Koltunski
  vec2 diff;
104
  float pointDegree;
105 4018b3b7 Leszek Koltunski
106 6a06a912 Leszek Koltunski
  for(int i=0; i<fNumEffects; i++)
107
    {
108 2fce34f4 Leszek Koltunski
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
109 6a06a912 Leszek Koltunski
    pointDegree = max(0.0,1.0-dot(diff,diff));
110 341c803d Leszek Koltunski
111 4018b3b7 Leszek Koltunski
         if( fType[i]==MACROBLOCK        ) macroblock(sign(pointDegree),2*i, pixel);
112
    else if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i, pixel);
113
    else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i, pixel);
114
    else if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),2*i, pixel);
115
    else if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,2*i, pixel);
116
    else if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),2*i, pixel);
117
    else if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,2*i, pixel);
118
    else if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),2*i, pixel);
119
    else if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,2*i, pixel);
120
    else if( fType[i]==SATURATION        ) saturation(sign(pointDegree),2*i, pixel);
121
    else if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,2*i, pixel);
122 6a06a912 Leszek Koltunski
    }
123
#endif
124
 
125 4018b3b7 Leszek Koltunski
  gl_FragColor = pixel*0.5*(v_Normal.z+1.0);
126 341c803d Leszek Koltunski
  }