0

I am trying to implement a spotlight (cone-shape) detection FOV for a game. I am currently calculating the lighting using the following function inside my fragment shader.

vec4 calculateLight(Light light)
{
    float aspectRatio = resolution.x / resolution.y; //amt of width / height
    if (aspectRatio > 1.0)
        light.radius.x /= aspectRatio;
    else
        light.radius.x /= aspectRatio;

    vec2 lightDir = fragmentPosition.xy - light.position.xy;
    float lightDistance = length(lightDir);
    if (length(lightDir / light.radius) >= 1.0)
            return vec4(0, 0, 0, 1); //outside of radius make it black

    if (dot(normalize(lightDir), normalize(light.spotDir.xy)) < cos(light.spotAngle/2))
            return vec4(0, 0, 0, 1); //outside of radius make it black
    
    return light.intensity * (1 - length(lightDir / light.radius)) * light.colour;
}

However, I would also like to use my light as something like a detection tool, which represents the enemy's line of sight, is there a way for me to do that? My LightComponent:

    struct LightComponent
    {
        Vec4 colour;
        Vec3 pos;
        
        Vec2 radius;
        float intensity;

        bool lightEnabled;
        //spotlight stuff
        float spotAngle;
        Vec3 spotDir;
        float DirectionAngle;
        bool takeVector = true;

        static Vec4 ambientLight;

        LightComponent();


        void ImGuiDraw();
    };

Range here is the radius.

enter image description here

tomatto
  • 149
  • 10
  • 1
    The detection of whether there is an enemy in the light cone must be done on the CPU and not in the fragment shader. – Rabbid76 Dec 03 '21 at 06:04
  • 1
    use dot between sight central direction (unit vector) and direction to tested point (also unit vector) the result is `cos(angle)` so just `acos(dot(...)) <= FOV/2` where `FOV` is field of view in radians (your angle). on top of this also test distance is <= Range ... also see this [Generate a "pieslice" in C](https://stackoverflow.com/a/58246614/2521214) – Spektre Dec 03 '21 at 07:08

0 Answers0