Palette Limiter Shader

Overview:

Screen reading shader which limits the colors in the viewport to a palette you provide. The shader works by picking the color within the palette that is the most similar to each pixel. Can just grab a palette off of something like Lospec and instantly get a cohesive visual style.

How to use:

  • Place the shader material onto a SubViewPort Container
  • Download or create a palette, then load it into the Palette Texture parameter
  • Make sure to set the Num Colors parameter to however many colors are in your palette!

 

(Palette used in example is https://lospec.com/palette-list/slso8)

Shader code
shader_type canvas_item;
uniform int num_colors: hint_range(2,16) = 8;
uniform sampler2D PALETTE_TEXTURE: hint_default_black;

vec3 palette_limiter (in vec3 albedo){
	float estimation_cutoff = 0.001;
	vec3 closest_color;
	float min_dist = 2.0;
	float n = float(num_colors);
	
	for (int i=0; i<num_colors; i++ ){
		float index = 1.000/(2.000*n)+float(i)/n;
		vec3 index_color = texture(PALETTE_TEXTURE, vec2(index,0.5)).rgb;
		float dist = length(index_color - albedo);
		if (dist < min_dist) {
			min_dist = dist;
			closest_color = index_color;
			if (min_dist < estimation_cutoff){
				return closest_color;
			}
		}
	}
	return closest_color;
}

void fragment() {
	COLOR.rgb = palette_limiter(texture(TEXTURE,UV).rgb);
}
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.

More from DrManatee

Animated TV-Static Border Shader

Related shaders

Extensible Color Palette (Gradient Maps) Now with Palette Blending!

Palette Shader

Palette shader (lospec compatible)

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Andrew
Andrew
8 months ago

I cant seem to get it to work, at first it was just making everything the brightest color on the pallate and now its making everything the darkest, does anyone have any information?

Colin
Colin
2 months ago
Reply to  Andrew

you need add this uniform at the top:

uniform sampler2D screen_texture : hint_screen_texture;

and replace line 27 with:

COLOR.rgb = palette_limiter(texture(screen_texture,SCREEN_UV).rgb);