shader_type canvas_item;

uniform float cone_angle : hint_range(0.0, 3.14) = 0.8; // Total width of beam
uniform float direction_degrees : hint_range(0.0, 360.0) = 0.0;
uniform float feather : hint_range(0.0, 1.0) = 0.05;

void fragment() {
    // 1. Get the UVs centered at (0.5, 0.5)
    vec2 uv = UV - vec2(0.5);
    
    // 2. Calculate the current pixel angle
    // atan2 returns values from -PI to PI
    float pixel_angle = atan(uv.y, uv.x);
    
    // 3. Convert uniform direction to radians
    float target_rad = radians(direction_degrees);
    
    // 4. Calculate difference between angles (wrapped)
    float angle_diff = abs(atan(sin(pixel_angle - target_rad), cos(pixel_angle - target_rad)));
    
    // 5. Create the cone mask
    // We compare the difference to half the cone angle
    float half_cone = cone_angle * 0.5;
    float mask = 1.0 - smoothstep(half_cone - feather, half_cone, angle_diff);
    
    // 6. Apply mask to the texture
    vec4 tex_color = texture(TEXTURE, UV);
    COLOR = vec4(tex_color.rgb, tex_color.a * mask);
}