Color reduction and dither

Reduces the values per RGB-channel.

Can use a checkerboard-dither with variable intensity.

 

Original image in screenshots

Shader code
shader_type canvas_item;

uniform float colors : hint_range(1.0, 16.0);
uniform float dither : hint_range(0.0, 0.5);

void fragment()
{
	vec4 color = texture(TEXTURE, UV);
	
	float a = floor(mod(UV.x / TEXTURE_PIXEL_SIZE.x, 2.0));
	float b = floor(mod(UV.y / TEXTURE_PIXEL_SIZE.y, 2.0));	
	float c = mod(a + b, 2.0);
	
	COLOR.r = (round(color.r * colors + dither) / colors) * c;
	COLOR.g = (round(color.g * colors + dither) / colors) * c;
	COLOR.b = (round(color.b * colors + dither) / colors) * c;
	c = 1.0 - c;
	COLOR.r += (round(color.r * colors - dither) / colors) * c;
	COLOR.g += (round(color.g * colors - dither) / colors) * c;
	COLOR.b += (round(color.b * colors - dither) / colors) * c;
}
Tags
dither color 8bit
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Arbitrary Color Reduction and Palette Ordered Dithering

16 Bit Color (C64-Like) with Dither

Color Dither

Subscribe
Notify of
guest

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Hgo03
Hgo03
5 months ago

I applied this to a ColorRect and it just looks white, how do you make it work?

Big Gum Guy
Big Gum Guy
4 months ago
Reply to  Hgo03

The shader is being applied to your colorrect which is probably white, thus is why its white.

If you want to apply it to the screen as an effect then you should replace the color rect with a texture rect and set its texture to a viewport texture.

But if you want to use the color rect then try replacing

vec4 color = texture(TEXTURE, UV);

with

vec4 color = texture(SCREEN_TEXTURE, UV);

and add

uniform sampler2D SCREEN_TEXTURE: hint_screen_texture, filter_nearest, repeat_disable;

at the top of the shader code.

Hgo03
Hgo03
4 months ago
Reply to  Big Gum Guy

I was not able to get it to work with the Texture rect, however it works perfectly with the other option, thank you so much!