forked from lettier/3d-game-shaders-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelize.frag
44 lines (30 loc) · 873 Bytes
/
pixelize.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
/*
(C) 2019 David Lettier
lettier.com
*/
#version 150
uniform sampler2D colorTexture;
uniform sampler2D positionTexture;
uniform vec2 parameters;
uniform vec2 enabled;
out vec4 fragColor;
void main() {
// Must be odd.
int pixelSize = int(parameters.x);
vec2 texSize = textureSize(colorTexture, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
// Avoid the background.
vec4 position = texture(positionTexture, texCoord);
if (enabled.x != 1 || position.a <= 0.0) {
fragColor = texture(colorTexture, texCoord);
return;
}
float x = int(gl_FragCoord.x) % pixelSize;
float y = int(gl_FragCoord.y) % pixelSize;
x = floor(pixelSize / 2.0) - x;
y = floor(pixelSize / 2.0) - y;
x = gl_FragCoord.x + x;
y = gl_FragCoord.y + y;
vec2 uv = vec2(x, y) / texSize;
fragColor = texture(colorTexture, uv);
}