Pulse effect (Godot 4)
With this shader you can apply a pulse effect to your CanvasItem (for example a Sprite2D)
Parameters:
- shine_color: Shine color to apply to the Texture
- alpha_limit: Only apply the shine if the pixel alpha color is equals or greater than this value. (Usefull if you won’t to apply the shine to shadows for example)
- cycle_speed: The speed of the pulse to apply the shine (Only for mode 1)
- full_pulse_cycle: If true, the pulse is continuosly, otherwise, it makes a pause in each cycle.
- mode: This parameter is used to enable, disable o set to pulse mode.
- 0: Off
- 1: Pulse mode. Apply the shine in a cycle mode
- 2: On. Always apply the shine
Shader code
shader_type canvas_item;
uniform vec4 shine_color : source_color = vec4(1.0); //Shine color
uniform float alpha_limit : hint_range(0.0, 1.0, 0.1) = 0.0; //Alpha color limit to apply the shine, for example, if you won't to apply the shine to semi-transparent pixels
uniform float cycle_speed : hint_range(0.0, 10.0, 0.1) = 1.0; //Pulse cycle speed
uniform bool full_pulse_cycle = false; //[False = Do the effect and make a pause] [True = Do the effect continuosly]
uniform int mode : hint_range(0, 2, 1) = 0; //[0 = Always off] [1 = Pulse mode] [2 = Always on]
void fragment()
{
//Check if the effect is enabled
if (mode > 0)
{
//Check the pixel alpha value
if (COLOR.a >= alpha_limit)
{
//Check the mode
switch (mode)
{
case 1: //Pulse mode
{
float cycle = sin(TIME * cycle_speed);
COLOR.rgb = mix(COLOR.rgb, shine_color.rgb, (((cycle >= 0.0) || (full_pulse_cycle)) ? abs(cycle) : 0.0) * shine_color.a);
break;
}
case 2: //Always on
{
COLOR.rgb = mix(COLOR.rgb, shine_color.rgb, shine_color.a);
break;
}
}
}
}
}
Updated: The hint_range for ‘mode’, must be greater than 0. In this case, it should be 1
Pretty good. Once I saw transparency is allowed, I realized the simple potential of such a common UI element.
Couldn’t this be simplified (so I edit further)
thanks!!!