Project

General

Profile

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

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

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 6a06a912 Leszek Koltunski
37
//////////////////////////////////////////////////////////////////////////////////////////////
38 341c803d Leszek Koltunski
// CHROMA EFFECT
39 6a06a912 Leszek Koltunski
40 ffbf279e Leszek Koltunski
void chroma(float degree, int effect, inout vec4 color)
41 6a06a912 Leszek Koltunski
  {
42 ffbf279e Leszek Koltunski
  color.rgb = mix(color.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
43 6a06a912 Leszek Koltunski
  }
44
45
//////////////////////////////////////////////////////////////////////////////////////////////
46 341c803d Leszek Koltunski
// ALPHA EFFECT (change transparency level)
47 6a06a912 Leszek Koltunski
48 ffbf279e Leszek Koltunski
void alpha(float degree, int effect, inout vec4 color)
49 6a06a912 Leszek Koltunski
  {
50 ffbf279e Leszek Koltunski
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
51 6a06a912 Leszek Koltunski
  }
52
53
//////////////////////////////////////////////////////////////////////////////////////////////
54 341c803d Leszek Koltunski
// BRIGHTNESS EFFECT
55 6a06a912 Leszek Koltunski
56 ffbf279e Leszek Koltunski
void brightness(float degree, int effect, inout vec4 color)
57 6a06a912 Leszek Koltunski
  {
58 ffbf279e Leszek Koltunski
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
59 6a06a912 Leszek Koltunski
  }
60 ffbf279e Leszek Koltunski
  
61 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
62 341c803d Leszek Koltunski
// CONTRAST EFFECT
63 6a06a912 Leszek Koltunski
64 ffbf279e Leszek Koltunski
void contrast(float degree, int effect, inout vec4 color)
65 6a06a912 Leszek Koltunski
  {
66 ffbf279e Leszek Koltunski
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
67 6a06a912 Leszek Koltunski
  }
68
69
//////////////////////////////////////////////////////////////////////////////////////////////
70 341c803d Leszek Koltunski
// SATURATION EFFECT
71 6a06a912 Leszek Koltunski
72 ffbf279e Leszek Koltunski
void saturation(float degree, int effect, inout vec4 color)
73 6a06a912 Leszek Koltunski
  {
74 ffbf279e Leszek Koltunski
  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 6a06a912 Leszek Koltunski
  }
77
78
#endif
79
80
//////////////////////////////////////////////////////////////////////////////////////////////
81
82
void main()                    		
83
  {  
84 42e08626 Leszek Koltunski
  vec4 pixel = texture2D(u_Texture,v_TexCoordinate);
85 4018b3b7 Leszek Koltunski
86
#if NUM_FRAGMENT>0
87 6a06a912 Leszek Koltunski
  vec2 diff;
88
  float pointDegree;
89 ffbf279e Leszek Koltunski
  
90 6a06a912 Leszek Koltunski
  for(int i=0; i<fNumEffects; i++)
91
    {
92 2fce34f4 Leszek Koltunski
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
93 6a06a912 Leszek Koltunski
    pointDegree = max(0.0,1.0-dot(diff,diff));
94 341c803d Leszek Koltunski
95 42e08626 Leszek Koltunski
         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 6a06a912 Leszek Koltunski
    }
106
#endif
107 ff8ad0a7 Leszek Koltunski
108 a7067deb Leszek Koltunski
  gl_FragColor = vec4(pixel.rgb * v_Normal.z, pixel.a);
109 341c803d Leszek Koltunski
  }