Color Replacer
This shader will search each pixel for a color and replace it with a different color. I made it so I can allow my players to change the color of their clothing, skin, and hair and decided to share it here. It’s pretty simple and can be extended to allow for more than 4 colors if you know a little about shading, I know very little myself 🙂
To use, just attach a Shader Material to a TextureRect and paste the shader code in there, the properties is where you can choose what color gets replaced by what color.
Shader code
shader_type canvas_item;
//Hat
uniform vec4 hat_color : source_color;
uniform vec4 hat_replace_color : source_color;
uniform float hat_color_threshold : hint_range(0.0, 1.0, 0.001);
//Hair
uniform vec4 hair_color : source_color;
uniform vec4 hair_replace_color : source_color;
uniform float hair_color_threshold : hint_range(0.0, 1.0, 0.001);
//Skin
uniform vec4 skin_color : source_color;
uniform vec4 skin_replace_color : source_color;
uniform float skin_color_threshold : hint_range(0.0, 1.0, 0.001);
//Clothes
uniform vec4 clothes_color : source_color;
uniform vec4 clothes_replace_color : source_color;
uniform float clothes_color_threshold : hint_range(0.0, 1.0, 0.001);
void fragment() {
// Called for every pixel the material is visible on.
vec4 tex_color = texture(TEXTURE, UV);
float hat_distance = length(tex_color.rgb - hat_color.rgb);
float hair_distance = length(tex_color.rgb - hair_color.rgb);
float skin_distance = length(tex_color.rgb - skin_color.rgb);
float clothes_distance = length(tex_color.rgb - clothes_color.rgb);
if(hat_distance < hat_color_threshold){
tex_color = hat_replace_color;
}else if(hair_distance < hair_color_threshold){
tex_color = hair_replace_color;
}else if(skin_distance < skin_color_threshold){
tex_color = skin_replace_color;
}else if(clothes_distance < clothes_color_threshold){
tex_color = clothes_replace_color;
}
COLOR = tex_color;
}