0 votes

I am trying to fade out a Sprite2D by using tween:

@onready var tween

func _ready():
texture = load("res://Player/Sprites/Player.png")

tween = create_tween()
tween.tween_property(self, "modulate.a", 0, 0.25)
tween.set_trans(Tween.TRANS_QUART)

This in itself works perfectly fine and delivers the desired effect.

However, I want to change the color of the texture at the same time using a shader.

shader_type canvas_item;

uniform bool whiten = true;
uniform float mix_weight = 1.0;

void fragment() {
    vec4 texture_color = texture(TEXTURE, UV);
    if (whiten) {
        vec3 white = vec3(1,1,1);
        vec3 whitened_texture_rgb = mix(texture_color.rgb, white, mix_weight);
        COLOR.rgb = whitened_texture_rgb;
        COLOR.a   = texture_color.a;
    } else {
        COLOR = texture_color;
    }
}

When this shader is loaded into the sprite, it does apply the white coloring effect to it but the fade from the tween does not appear to work anymore. I assume the alpha value, modulate:a, during the tweening does not actually get picked up by the texture_color.a in the shader script. And I just cannot figure out why. I'd highly appreciate some help!

Disclaimer: I got the code from a youtube video about character dash ghosts. And it seems simple enough for me to think I understand it all. But I am also very new to shaders and might be missing something trivial here.

Godot version 4.0
in Engine by (12 points)

Please log in or register to answer this question.

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.