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;
}
Tags
change, change-color, Color, color-changer
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

color splash (show only one color)

Convert Color

Fading color override effect

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments