thebookofshaders/00/halftone.frag

64 lines
1.7 KiB
GLSL
Raw Normal View History

2015-03-15 15:21:47 +00:00
#ifdef GL_ES
precision mediump float;
#endif
2017-08-19 10:11:58 +00:00
uniform sampler2D u_tex0;
2015-03-15 15:21:47 +00:00
uniform vec2 u_resolution;
uniform vec2 u_mouse;
float PI = 3.1415926535897932384626433832795;
float PI180 = float(PI / 180.0);
float sind (float a) {
return sin(a * PI180);
}
float cosd (float a) {
return cos(a * PI180);
}
float added (vec2 sh, float sa, float ca, vec2 c, float d) {
return 0.5 + 0.25 * cos((sh.x * sa + sh.y * ca + c.x) * d) + 0.25 * cos((sh.x * ca - sh.y * sa + c.y) * d);
}
void main () {
float threshold = clamp(0.5, 0.0, 1.0);
vec2 st = gl_FragCoord.xy/u_resolution.xy;
if (st.x > 0.5) {
// Halftone dot matrix shader
// @author Tomek Augustyn 2010
2017-08-19 10:11:58 +00:00
2015-03-15 15:21:47 +00:00
// Ported from my old PixelBender experiment
// https://github.com/og2t/HiSlope/blob/master/src/hislope/pbk/fx/halftone/Halftone.pbk
float ratio = u_resolution.y / u_resolution.x;
vec2 dstCoord = vec2(st.x-0.5, st.y*0.5)*2.0;
vec2 srcCoord = vec2(st.x-0.5, st.y*0.5)*2.0;
vec2 rotationCenter = vec2(0.5);
vec2 shift = dstCoord - rotationCenter;
2017-08-19 10:11:58 +00:00
2015-03-15 15:21:47 +00:00
float dotSize = 5.0;
float angle = 45.0;
2017-08-19 10:11:58 +00:00
2015-03-15 15:21:47 +00:00
float rasterPattern = added(shift, sind(angle), cosd(angle), rotationCenter, PI / dotSize * 680.0);
vec4 srcPixel = texture2D(u_tex0, srcCoord);
2017-08-19 10:11:58 +00:00
2015-03-15 15:21:47 +00:00
float avg = 0.2125 * srcPixel.r + 0.7154 * srcPixel.g + 0.0721 * srcPixel.b;
float gray = (rasterPattern * threshold + avg - threshold) / (1.0 - threshold);
2017-08-19 10:11:58 +00:00
// uncomment to see how the raster pattern looks
2015-03-15 15:21:47 +00:00
// gray = rasterPattern;
2017-08-19 10:11:58 +00:00
2015-03-15 15:21:47 +00:00
gl_FragColor = vec4(gray, gray, gray, 1.0);
} else {
st = vec2(st.x,st.y*0.5)*2.0;
gl_FragColor = texture2D(u_tex0, st);
}
2017-08-19 10:11:58 +00:00
2015-03-15 15:21:47 +00:00
}