Search Unity

Disabling the light component or changing its light/intensity value?

Discussion in 'Global Illumination' started by Max_Bol, Aug 7, 2018.

?

Turning the <Light> component off or changing the <Light> value?

  1. Turn <Light>.enable to false is better.

    4 vote(s)
    80.0%
  2. Changing the <Light> value (intensity or color for example) is better.

    1 vote(s)
    20.0%
  1. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    Hey guys!

    I got this question about lights' states in Unity 2018.2.

    To put in it in a simple way, I was wondering which was the best way of handling lights states while in-game.
    Which is the best practice when you change the light state of a Light component?

    There are many examples as possibles situations and I would like some clarification on some of them.
    Since the introduction of light probe groups in Unity, this allowed us to do some really good adaptation of the lights sources without going too deep into the lighting system mecanism.

    If you got a room that has a light that can be turned off with a switch, would it be better to simply set <Light>.enabled = false or would it be better to keep the component active and just change the light values like its color or intensity?

    What about a scene with a good number of lights? Should I just keep all those lights enabled and change their intensity to 0 when they are turned off or would it be better to turn off the light components when needed? Note that I'm writing about something like switching on and off 80-120 lights at once if not more in a scene. It's kinda like if you were to switch from Night lights to Day lights instantly where Night Light has a lot of street lights poles illuminating the streets. (I give this example as it's quite similar to some of the stuff in my project.)

    I know that both "could work", but I'm wondering about the benefits and inconvenient in each methods?
    I'm not that familiar with how Unity 2018 manages the lights, memory wise, in both cases.

    Note: I'm not asking about how to manage the Occlusion Culling nor about how it's bad having too many lights in a scene. All that is not important as I have already created my own system that manage what's active or not since it's for a 2.5D Metroidvania sci-fi project and there's a "visibility" system where unknown room (not visited yet) are pitch black (not light related), visited but unseen room are dark but slightly visible and visible rooms are lighted. I'm just wondering about changing the lighting in some room based on some specific events in the game such as turning a bunch of rooms into their "Alert" mode where red light pop-out while the "normal" lights turn off. There's also the fact that some lights can be destroyed by the player (they turn back on when revisited later). All the "behavior" management is already done and I'm wondering just about the impact on performance between turning the light component on and off (can be quite often) and changing their intensity.
     
    Last edited: Aug 7, 2018
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,456
    I have no informed viewpoint on this, but I've recently seen in one of our Unite Talks, that AudioSources with a volume of 0 are still impacting performance when calculating the voice mix. I'd hope the situation is not the same with lights but I'd err on disabling the lights.

    However, I'd just go and profile it (on a build on your target platform, not in the editor since there might be editor overhead). Build some test case scenarios and check what the CPU performance and memory say for either solution.
     
  3. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    Thanks for the reply.

    In the case of only a couple of lights, I also consider disabling the light component as the best way, but the thing that makes me unease with a large number of light is what happen when you suddently turn on a lots of them.

    For exemple, there's a difference in the batching process and the visual result depending on ehat you do.

    If you bake the lightmaps with the light activated (with mixed lighting setting), their light will be obviously added to the baked lightmaps which mean, in the example of red lights poping out mixed with the normal light, it's oversaturated and you'll be stuck with a red light even without having the red light active. In my test, the batching shows 93 batches for a small part of my map.

    If you bake the lightmaps with the light disabled and activate the light at a later point in time, the batching of the scene will take an huge hit and you'll end up with 2-3x more batches. The 93 batches raise to 234 batches as soon as I switch the lights.

    If you bake the lightmaps with the lights enabled, but with an intensity of 0.01, the batching of the scene does get affected as much. It's higher than when the light are disabled, but lower than when enabling the light afterwards. The batches are shown at 124 and doesn't saire when changed.

    All that is done in a 100% static scene without any animation nor particles.
    The high batches is due to the custom shader I'm using.

    From the results of my tests, changing the intensity seems to be more efficient when the number of lights changing is high as the changes in the impact on the performances is lower. I'm still not at the point where I can successfully test the impact ranges yet as I'm still modeling assets fif the first scene.

    EDIT :

    After a few more days to test it out, I noticed that setting the intensity to 0 is actually not that good, but it's not because I was wrong, but more because Unity, sometimes, skip the batching of the light if it's considered as useless. So, the trick is to set the intensity as low as possible, but above 0. Another possibility if you don't aim for a pitch black shadow environment is to keep the intensity to its default value (usually 1 unless you requires a more intense light) and instead change the color to something really dark. Not black as it seems that any light that has its color drop to (0,0,0) ends up out of the batching process. Something like setting up the intensity to 0.025 or the light color to (0.05f,0.05f,0.05f,1f) does the trick.
     
    Last edited: Aug 12, 2018
    Marou1 and MartinTilo like this.