0 votes

Hi everyone,

I have a greyscale sprite-png with a range of shades from black to white. Modulating the visibility to different color-hues affects all shades including complete white and black, the same with the different material/canvas item blend-modes.

What I would like to achieve is that 100% white and black stay untouched while colorising the grey-shades. Is that possible?

in Engine by (492 points)

1 Answer

+1 vote
Best answer

Hi! you have to write a shader for that.

modulate just multiplies the texture color with the modulate color, so if you want the same effect, but avoiding touching black and white colors, you could add this shader to the sprite:

shader_type canvas_item;
uniform vec4 modulate : hint_color = vec4(1.0);

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

    if (color.a == 1.0){
    if (color.rgb != vec3(1.0) && color.rgb != vec3(0.0)){
            color *= modulate;
        }
    }
    COLOR = color;
}

Instead of changing the modulate property of the sprite, change the modulate param in the shader params from the editor.

by (3,491 points)
selected by

Brilliant, thanks a million!

I'm sure there is a way to "smoothen the gradients"... ignoring pure black and white leads to sharp edges here, looking "1-Bit-y", so I wonder how to "feather" those edges. Is it possible to expand the range from pure white to, say, including the "10% brightest greys" (and the darkest according to black), to achieve something like the "add" blend mode in an canvas material?

Hi! Sorry, but i dont quite understand what you are asking. Would you share an image or something of what you want to achieve. The add blend mode sums the texture color with the background color. Is that what you need?
Also, maybe opening a second question for this is a good idea.. so others can see it and help too.

Yes, sorry I obviously got carried away a bit with my thoughts and you're right, I probably should put this into an extra question. Thanks again for your code, it does what I asked for quite wonderfully!

EDIT:
For completion, this is what I meant:
(Oh, it seems I can't put a picture in here... so here's a link:)
https://we.tl/t-j7LOCsmkEt
The white areas in the shader-version are kind of "hard". For the "want"-version I layered a colored layer on top of the original in add-mode...

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 [email protected] with your username.