forked from lettier/3d-game-shaders-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
posterize.frag
47 lines (30 loc) · 1.05 KB
/
posterize.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
(C) 2019 David Lettier
lettier.com
*/
#version 150
uniform vec2 gamma;
uniform sampler2D colorTexture;
uniform sampler2D positionTexture;
uniform vec2 enabled;
out vec4 fragColor;
void main() {
float levels = 6.0;
vec2 texSize = textureSize(colorTexture, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
// Avoid the background.
vec4 position = texture(positionTexture, texCoord);
if (position.a <= 0) { fragColor = vec4(0); return; }
fragColor = texture(colorTexture, texCoord);
if (enabled.x != 1) { return; }
fragColor.rgb = pow(fragColor.rgb, vec3(gamma.y));
float greyscale = max(fragColor.r, max(fragColor.g, fragColor.b));
float lower = floor(greyscale * levels) / levels;
float lowerDiff = abs(greyscale - lower);
float upper = ceil(greyscale * levels) / levels;
float upperDiff = abs(upper - greyscale);
float level = lowerDiff <= upperDiff ? lower : upper;
float adjustment = level / greyscale;
fragColor.rgb = fragColor.rgb * adjustment;
fragColor.rgb = pow(fragColor.rgb, vec3(gamma.x));
}