+1 vote

I was just wondering, if it was possible to replace one specific color in a Sprite. I'd like to have n NPCs walking around, everyone having a diferent shirt color. I don't know in advance how many instances of this scene I will have, so I cannot prepare n sprites with different colors beforehand.
I would like to replace every pixel of one color, lets say #ffffff, with a different color, lets say #ababab, in the whole texture of the sprite.
I think modulate does not do what I want, because as far as I understand, it changes every color, and not just one.

in Engine by (47 points)

1 Answer

0 votes

Add this shader to your Sprite:

shader_type canvas_item;

uniform vec4 origin:hint_color;
uniform vec4 new:hint_color;

void fragment() {
    vec4 current_pixel = texture(TEXTURE, UV);

    if (current_pixel == origin)
        COLOR = new;
        COLOR = current_pixel;

You change change the colors inside Shader Param in the Inspector.

EDIT: There's a shader here that can change multiple shadings of color to another: https://godotengine.org/qa/20926/changing-specific-color-in-a-sprite-using-shaders-in-godot-3

by (4,169 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.