#Modulate sprite color, but ignore some areas of the sprite?

4 messages · Page 1 of 1 (latest)

lean shale
#

I want to modulate my animated sprite (a pixel art slime/blob thingy), but keep the eyes and some white highlighting intact. I've been told I need to write a shader. My experience with writing shaders is very limited.

My initial intuition was to pass a binary mask image as a uniform, where all the pixels I want to modulate are set to 1 (white) and the ones I want to keep unchanged are set to 0. This is what I've come up with so far:

shader_type canvas_item;
uniform sampler2D mask;
uniform vec4 modulate: source_color;

void fragment() {
    vec4 color = texture(TEXTURE,UV);
    vec4 color_mask = texture(mask, UV);
    if (color_mask.rgb != vec3(0.0))
        color *= modulate;
    COLOR = color;
}

but it doesn't do what I want it to. So my question is how do I achieve the coloring that I've described above?

manic hawk
#

Try this:

shader_type canvas_item;
uniform sampler2D mask;
uniform vec4 modulate: source_color;

void fragment() {
    vec4 color = texture(TEXTURE,UV);
    vec4 color_mask = texture(mask, UV);
    if (color_mask.a == 1.0) {
        color *= modulate;
    }
    COLOR = color;
}

You missed out the brackets in the if statement. I also changed it to be based on the mask's alpha since that will be slightly faster to check.

lean shale
#

Checking the alpha doesn't really work because all pixels in the mask are non transparent so it just modulates every pixel, I did check just the red component though and it kind of works but it creates this outline that I don't want:

#

These pixels around the eyes and the highlight in the top right should get modulated too, and idk why they're not. It's not a mistake in the mask, I selected only the 4 pixels for each eye and the 2-3 pixels for the highlight for each frame in the spritesheet, so I don't know what I'm doing wrong