Sprite palette swap from palette image
Asked Answered
G

2

0

Hey! I'm trying to make a shader like this:

Here's my pathetic attempt at getting somewhere near that:

shader_type canvas_item;

uniform sampler2D palette_texture;
uniform float palette_number;
uniform float total_palettes : hint_range(1, 12);

void fragment(){
	vec4 referenceColors[12];
	for (int ind = 0; ind < 12; ind++) {
		/* referenceColors[ind] = vec4(float(ind), 0.2, 0.2, 1); */
		/* referenceColors[ind] = texture(palette_texture, vec2(float(ind%4), 0.2)); */
		referenceColors[ind] = vec4(float(1/(ind%4)), 0.2, 0.2, 1);
	}
	
	vec4 textureColor = texture(TEXTURE, UV);
	COLOR.rgb = referenceColors[int(floor(textureColor.r * float(total_palettes)))].rgb;
	COLOR.a = textureColor.a;
}

If you have suggestions for better ways to do something similar, please let me know also!

Gabor answered 11/9, 2021 at 15:25 Comment(0)
G
0

Addendum: The palettes will always have 4 colors, my vision for now is many recolor/dye-able objects a bit like starbound's clothes dyeing with two or more parts that can be dyed separate colors.

Gabor answered 11/9, 2021 at 15:27 Comment(0)
O
0

You don't need any for loops in the shader. Just read the current pixel form the input texture and use its value as an uv offset into the palette texture.

Oblique answered 11/9, 2021 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.