Sprite3D Indexed Color Palette Swapping Shader

texture_albedo is the image texture you wish to change the palette of.

num_colors is the length of your palette. (it should be 1px in height)

precision is the amount of how accurate it should be, preferable 0.01.

palette_in is the image of the colormap palette.

palette_out is the image of the palette you wish to change the colors to.

 

Made this because the one’s here didnt work properly for sprite3D.

Have fun ^^! Credit to DETOX and lenscas on discord for helping me figure this out.

Shader code
shader_type spatial;
render_mode blend_mix,depth_draw_alpha_prepass,cull_disabled,unshaded,specular_disabled;
uniform sampler2D texture_albedo : hint_albedo;

uniform int num_colors : hint_range(1, 100, 1) = 1;
uniform float precision : hint_range(0.0, 1.0, 0.01) = 0.1;

uniform sampler2D palette_in : hint_albedo;
uniform sampler2D palette_out : hint_albedo;

vec4 swap_color(vec4 color) {
    float inc = 1.0 / float(num_colors); 
    for (float i = 0.0; i < 1.0; i += inc) {
        vec2 uv = vec2(i, 1.0);
        vec4 color_in = texture(palette_in, uv);
        if (distance(color, color_in) <= precision) {
            return texture(palette_out, uv);
        }
    }
    //return color;
}

void fragment() {
	vec4 albedo_tex = texture(texture_albedo,UV);
	ALBEDO = albedo_tex.rgb;
	ALBEDO = swap_color(albedo_tex).rgb;
	ALPHA = albedo_tex.a;
}
Tags
godot3
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 SRC Corp

Shapes Shader

Related shaders

Billboard Sprite3D Sway (Godot 4.0)

Gradient Color Fog

Grass shader

guest

0 Comments
Inline Feedbacks
View all comments