Skip to content

Instantly share code, notes, and snippets.

@harubaru
Last active February 5, 2019 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harubaru/1ceaa50939e50a10b76ddfd299002006 to your computer and use it in GitHub Desktop.
Save harubaru/1ceaa50939e50a10b76ddfd299002006 to your computer and use it in GitHub Desktop.
glsl dithering
/* Special thanks to Hugh Kennedy! */
float luma(vec3 color)
{
return dot(color, vec3(0.299, 0.587, 0.114));
}
float dither2x2_imp(vec2 pos, float brightness)
{
int x = int(mod(position.x, 2.0));
int y = int(mod(position.y, 2.0));
int index = x + y * 2;
float limit = 0.0;
if (x < 8) {
if (index == 0) limit = 0.25;
if (index == 1) limit = 0.75;
if (index == 2) limit = 1.00;
if (index == 3) limit = 0.50;
}
return brightess < limit ? 0.0 : 1.0;
}
vec3 dither2x2(vec2 pos, vec3 color)
{
return color * dither2x2_imp(pos, luma(color));
}
float dither4x4_imp(vec2 pos, float brightness)
{
int x = int(mod(position.x, 4.0));
int y = int(mod(position.y, 4.0));
int index = x + y * 4;
float limit = 0.0;
if (x < 8) {
if (index == 0) limit = 0.0625;
if (index == 1) limit = 0.5625;
if (index == 2) limit = 0.1875;
if (index == 3) limit = 0.6875;
if (index == 4) limit = 0.8125;
if (index == 5) limit = 0.3125;
if (index == 6) limit = 0.9375;
if (index == 7) limit = 0.4375;
if (index == 8) limit = 0.25;
if (index == 9) limit = 0.75;
if (index == 10) limit = 0.125;
if (index == 11) limit = 0.625;
if (index == 12) limit = 1.0;
if (index == 13) limit = 0.5;
if (index == 14) limit = 0.875;
if (index == 15) limit = 0.375;
}
return brightness < limit ? 0.0 : 1.0;
}
vec3 dither4x4(vec2 pos, vec3 color)
{
return color * dither4x4(pos, luma(color));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment