Modulate xx% of a 2D texture from the left side

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TobiLa

Hey everyone,

I want to implement a star rating in my game. If its a natural number its no problem, I can modulate the corresponding stars with set_modulate() etc.

But if I have a decimal rating like 3.4 I have to modulate the 4th star only for 40%.

e.g. my white star texture should look like this:
enter image description here
I guess this can be done with shaders, but I have no idea how.

Hope anyone can help me.
I am using godot 3.0 stable.

:bust_in_silhouette: Reply From: c-o-d-e

This should work for you:

shader_type canvas_item;

uniform vec4  cover_colour = vec4(1.0, 1.0, 0.0, 1.0);
uniform float percent_covered = 0.0;

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

    if (texture_colour.a == 1.0 && UV.x < percent_covered)
    {
	    texture_colour = cover_colour;
    }

    COLOR = texture_colour;
}