Palette Swap: Post-Process, Image-Parametrized

This shader takes two pallete images (with colored squares, like the ones downloaded from lospec.com, or in example) and swaps colors of origPalette to palette colors.

Useful for implementing swaping palette for entire pixel-art game, like in World of Horror or so.

To use it create new CanvasLayer with a ColorRect covering the whole screen, and apply shader to this rectangle.

Uniform paletteSize needs to contain number of colors in the palette image, horizontally and vertically.

Palettes need to have the same number of colors, horizontally and vertically.

Shader code
shader_type canvas_item;
uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, repeat_disable, filter_nearest;

// palette that will be displayed on the screen
uniform sampler2D palette: repeat_disable, filter_nearest;

// original palette
uniform sampler2D origPalette: repeat_disable, filter_nearest;

// number of colors in palettes, horizontally and vertically.
uniform ivec2 paletteSize;

void fragment() {
	vec4 screenColor = COLOR = texture(SCREEN_TEXTURE, SCREEN_UV);
	vec2 uvOffset = 1.0/vec2(paletteSize);
	
	for (int x = 0; x < paletteSize.x; x++)
	for (int y = 0; y < paletteSize.y; y++) {
		vec2 paleteUV = vec2(float(x) + 0.5, float(y) + 0.5) * uvOffset;
		
		vec4 oldColor = texture(origPalette, paleteUV);
		vec4 newColor = texture(palette, paleteUV);

		float areSameColor = step(distance(screenColor, oldColor), 0.0);
		
		COLOR = mix(COLOR, newColor, areSameColor);
	}
}
Tags
palette, palette swap, pixel-art, retro
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

Pixelate Postprocess

Palette Swap (no recolor / recolor)

2 Colour Palette Swap

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments