9
\$\begingroup\$

First off, I own Unity Pro.

I've been looking in to occluding lights when they aren't being viewed for a while now to improve performance. The main methods I ran in to were using BecameVisible() and testing the camera frustum.

My main problem was that if the player is viewing an area that would be lit by the light, I still want the light to be on.

Currently I'm using a method which checks to see if the lit area is in the camera frustum, but the problem is sometimes lights are in the frustum without actually being visible by the player (e.g. a wall between them and the player). I've tried raycasting to them but you can never get detailed enough for the actual lit area (the best I could figure was using renderer.bounds.extent and renderer.bounds.center to calculate the maximum lit points from the light).

Does anyone know of an easy way in Unity Pro to occlude lights? Or could you tell me a good way to use that camera fulcrum method I was talking about?

\$\endgroup\$
6
  • 1
    \$\begingroup\$ You can check if the camera frustum and the lights frustum intersect, but I expect unity is already doing this. \$\endgroup\$
    – Archy
    Feb 27, 2013 at 9:15
  • 1
    \$\begingroup\$ In Unity Pro, OcclusionCulling should handle lights too. \$\endgroup\$
    – Archy
    Feb 27, 2013 at 9:24
  • \$\begingroup\$ Alright, my artist had said that Occlusion Culling wouldn't work for lights. So I'd probably just set my various light sources as occludees in the occlusion areas I already have set up? \$\endgroup\$ Feb 27, 2013 at 19:05
  • \$\begingroup\$ And something like checking the camera and light frustum against eachother may work, though I'd need it to work in 360 degrees. \$\endgroup\$ Feb 27, 2013 at 19:06
  • \$\begingroup\$ Occlusion culling seems to not be working with lights, despite what the artist and I try. How exactly would I occlude a light? \$\endgroup\$ Mar 4, 2013 at 23:37

1 Answer 1

1
\$\begingroup\$

You could write your own script to occlude lights. Something like this attached to the main camera:

List<Light> Lights = new List<Light>();
const float OccludeDist = 100;
void Update()
{
    foreach (Light light in Lights)
    {
        if ((transform.position - light.transform.position).sqrMagnitude > OccludeDist * OccludeDist))
        {
            light.enabled = false;
        }
        else{
            light.enabled = true;
        }
    }
}

You could also check if the point is within an angle on the Y axis of the camera (the other axes will just give you pain). Be a bit generous with the angle, maybe 2 times the camera's frustrum, so that lights won't turn off when they're lighting areas within the camera's view. If you choose to add this additional optimization (which probably won't be necessary), make sure you use the distance as the broadphase because it's a lot less costly.

\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .