Using Modulate and Shader at the same time

Godot Version

v4.6.stable.mono.official [89cea1439]

Question

I have an issue where if Sprite2D/AnimatedSprite2D has shader as material, Modulate method is completely ignored. I have found bug reports dating back to 2022, and I don’t know if it has been fixed or not.

Example shader (hit flash, triangledevv on godotshaders.com)

shader_type canvas_item;

uniform bool enable = false;

void fragment() {
    vec4 previous_color = texture(TEXTURE, UV);
    vec4 white_color = vec4(1.0, 1.0, 1.0, previous_color.a);
    vec4 new_color = previous_color;
    if (enable == true)
    {
        new_color = white_color;
    };
    COLOR = new_color;
}

Works perfectly fine, but as I said, Modulate doesn’t do anything if this shader is applied.

Apparently to fix it you need to multiply COLOR by COLOR, and while it does fix the issue (Modulate actually does things now), it also makes the Sprite2D much darker than intended (even when enable is false, sprite is darker at all times).

COLOR = new_color * COLOR;

The only solution I can think about right now is to dynamically remove and reapply shader before and after Modulate is used, but this sounds pretty suspicious, so I would want to avoid it.

You don’t need to multiply by COLOR, just use COLOR instead of texture(TEXTURE, UV).

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.