Unity 5 Lighting Cookbook

Quick notes on how to deal with Unity 5 lighting.

The source project for the screenshots in this article is hosted at GitHub.

Standard realtime lighting

Standard realtime lighting includes ambient light, environment reflection and realtime lights and shadows. It doesn’t require any GI or precomputed stuff whatsoever. Both options Precomputed Realtime GI and Baked GI are disabled in Lighting > Scene.

  Unity-5-Lighting-Cookbook-Standard-Lighting

1) Ambient Light

Equally affects all objects.

Unity-5-Lighting-Cookbook-Ambient-Light

Ambient Source (Skybox, Gradient, Color) and Intensity

2) Environment Reflection

Reflected path that changes with the angle of the camera. Applied independently of Ambient. Affects the Fresnel effect as well.

If Reflection Probes are used they become the Reflection Source and Intensity for the objects under their influence, so the global reflection settings don’t have effect on these objects. Reflection Bounces define how much times the reflection can bounce among several probes (i.e. a probe capturing the objects under the influence of another probe).

Unity-5-Lighting-Cookbook-Environment-Reflection  Unity-5-Lighting-Cookbook-Reflection-Probes

Left: Reflection Source (Skybox, Cubemap), Resolution and Reflection Intensity.
Right: A Reflection Probe affects this object, so the global Environment Reflection Source doesn’t affect this mesh.

3) Standard Lights

All standard realtime lights in the scene apply direct lighting and shadows on the objects.

The first Directional Light in the scene is considered as Sun by default. The default Skybox gets its intensity and color adjusted depending on the Sun’s horizontal inclination. Alternatively, a specific directional light can be selected as Sun for affecting the default Skybox:

  Unity-5-Lighting-Cookbook-Skybox-Sun

Precomputed realtime lighting

Precomputed realtime lighting throws in light bounces, color bleeding, and emissive materials in real time. Everything can change dynamically (light color and intensity, material color, material emission, etc) an the lighting gets updated in the scene accordingly in real time.

Lighting > Scene > Precomputed Realtime GI enabled, Baked GI disabled.

Unity-5-Lighting-Cookbook-Precomputed-Realtime-GI

4) Light Bounces

Each light is configured to bounce off of objects and affect others.

Unity-5-Lighting-Cookbook-Light-Bounces

5) Emissive Materials

Objects flagged as Static containing an emissive material emit light according to their geometry.

Unity-5-Lighting-Cookbook-Emissive-Object  Unity-5-Lighting-Cookbook-Emissive-Material

Precomputed realtime lighting is calculated on the scene objects flagged as Static. A precompute phase is triggered whenever the Transform of any object marked as Static is modified, or when the Static flag itself changes.

The parameter Lighting > Scene > Precomputed Realtime GI > Realtime Resolution directly affects the time it will take to precompute the data. It’s ok to use values as low as 0.01 – 0.5 for allow quickly prototyping while working in the Editor.

Unity-5-Lighting-Cookbook-Precomputed-Resolution

CPU Usage affects the reactions of the dynamic light changes in runtime. It doesn’t affect the CPU used for baking the precomputed data in the editor.

Light bouncing and material emission affect static objects only. Light probes must be used for the precomputed effects to be applied on dynamic objects as well.

Ambient Light in precomputed realtime lighting

Ambient Light affects the parts of the static geometry that can be reached “from the exterior” by the light. Backfaces also count here. For instance, at interior scenes where the static geometry defines a closed hull, the ambient light won’t have effect at all in the static meshes. Any “gaps” or parts that are reachable from the exterior of the static geometry will be affected by the Ambient Light accordingly.

Baked lightmaps

Ambient Light, Standard Lights (baked or mixed), Light Bounces and Emissive Materias are baked into lightmap textures. Only Environment Reflection can change in runtime.

Lighting > Scene > Baked GI enabled, Precomputed Realtime GI disabled.

Unity-5-Lighting-Cookbook-Baked-GI

Standard Lights should be configured as Baked or Mixed for allowing to seamlessly switching among Bake only and Realtime only GI modes.

The parameter Lighting > Scene > Baked GI > Baked Resolution directly affects the time it will take the lightmap to be generated. It’s ok to use values as low as 1-4 for quickly prototyping while working in the Editor.

Unity-5-Lighting-Cookbook-Baked-Resolution

Modifying any parameter involved in lighting (Ambient Light, Standard Lights, Light Bounces, Emissive Materias) or modifying any Static object will trigger a new lightmap calculation phase.

General GI settings

Unity-5-Lighting-Cookbook-General-GI

The Indirect Light is the lighting effect of Ambient Light, Light Bounces and Emissive Materials (effect on nearby objects, not the emissive objects themselves), but not Environment Reflection. Scene Window > Irradiance shows the Indirect Light in the scene:

Unity-5-Lighting-Cookbook-Irradiance-Indirect-Light

These settings affect the Indirect Light in both realtime and baked modes:

  • Indirect intensity: Scales the indirect lighting (ambient + bounces + emissive)
  • Bounce Boost: How strong the light bounces from one surface to the next. The bounced light is multiplied by the albedo of the surface intensified by this setting.

These settings affect the Baked GI mode only:

  • Directional mode: whether to store directional and specular information in the lightmaps in Baked GI mode.
  • Atlas size: size of the lightmaps in Baked GI mode.

Combining different lighting methods

The tutorial Unity 5 – Lighting and Rendering is a must-read for understanding the insights of the Global Illumination (GI) in Unity 5. The quotes here are extracted from this tutorial.

Precomputed realtime lighting + Baked lightmaps

It’s not a good idea, even given that both are enabled by default:

Although it is possible to simultaneously use Baked GI lighting and Precomputed Realtime GI, be wary that the performance cost associated with rendering both systems simultaneously is the sum of the cost of each of them. Not only do we have to store both sets of lightmaps in video memory, but we also pay the processing cost of decoding both in shaders.

[…] The decision on which approach to take will have to be evaluated based on the nature of your particular project and desired target platform. Remember that when targeting a range of different hardware, that often it is the least performant which will determine which approach is needed.

Standard realtime lighthting + Baked lightmaps

This is a convenient method for static and dynamic objects under the same constant lighting conditions:

Selecting the ‘Mixed’ baking mode [in the Light component], GameObjects marked as static will still include this light in their Baked GI lightmaps. However, unlike lights marked as ‘Baked’, Mixed lights will still contribute realtime, direct light to non-static GameObjects within your scene. This can be useful in cases where you are using lightmaps in your static environment, but you still want a character to use these same lights to cast realtime shadows onto lightmapped geometry.

Generating lightmap files

By default lightmap files are generated and stored internally each time a lightmap calculation phase is triggered (i.e. by moving static elements).

Lightmaps can be calculated manually by disabling the Auto checkbox in the Lighting window, then clicking the Build button. The generated files are stored in a folder with the same name as the scene they belong to. Baked data includes lightmaps and reflection probes.

The parameters Directional Mode and Atlas Size at the General GI section affect the type and number of the lightmap files.

Unity-5-Lighting-Cookbook-Generating-Lightmap-Files

 

16 Comments

Add a Comment
  1. Very interesting, just bookmarked the page.
    Thanks for the info!

  2. Great overview!! Now i wonder, we can have both options (Precomputed GI and Baked GI)activated at the same time?. Does it have a main impact on performance/quality over final result?…or it is a waste of resoruses to have both activated?. thanks in advance for taking the time to help!

  3. I had done some tests but results were inconclusive, maybe because of the early state of the GI integration in Unity. When both Precomputed and Baked GI are enabled, lights should behave depending on their specific settings (real-time, baked, mixed). I’ll do further tests in a more recent Unity version.

  4. Thanks for your aswer. I’ll keeping an eye on the page while reading your other great UNITY stuff.

  5. This was very useful to me, thank you!

    You mentioned that

    Mixed lights will still contribute realtime, direct light to non-static GameObjects within your scene.

    I’m trying to get realtime light to also affect static GameObjects. Is this possible? I’m thinking of a flashlight on static scenery kind of thing.

  6. but how can i get generated lightmap file?

  7. @Othniel that light should be marked as Mixed.

    @nooklp I’ve just added a section for covering the generated lightmap files.

  8. Othniel Cundangan

    Thanks for the response Edy, I found out that my problem had to do with the fact that my quality settings didn’t allow any pixel lights so I couldn’t get the flashlight cookie to show on any of my meshes.

    Setting pixel lights to at least 1 fixed my problem.

  9. Hi Edy, congrats for the useful blog.
    Suppose i have a scene with just a directional baked light, some static and moving objects, and Baked GI enabled.

    Will the moving objects be lit by the directional light in realtime even if the light is marked as Baked?
    Will moving objects cast a shadow on static geometry as well?

    And i suppose when a moving object walk on a baked shadow it will not receive the shadow, right?
    How can i address this issue, maybe light probes can help? (i don’t know how to use them yet)

    Thanks!

  10. The directional light must be marked as Mixed for that to work (moving objects receiving directional light and casting shadows on static geometry).

    Baked shadows don’t affect moving object. Yes, light probes help mitigating the effect. You should place them around the boundaries of the shadow volume: some probes right inside the shadow volume, and others right outside it. Thus, the moving object would appear fully shadowed shortly after having entered the shadow volume. Otherwise, the effect might appear “smooth”, “progressive”.

  11. But it looks like Mixed lights do not work properly and devs are working to fix it: http://forum.unity3d.com/threads/unity-5-how-to-light-large-scenes.307611/

    But the bug is still there from March…i fear we’re not going to have mixed lights before 2016.
    So i’m thinking about an alternative

    Baked light
    +
    Use light probes to approximate the baked light and shadows on moving objects
    +
    Use shadow projectors to approximate the shadow cast by moving objects. Maybe one projector per each mesh part would approximate nicely the shape of the object, example a projector for each arm, hand, leg, head and body of the character.

    What do you think about this approach? Would it kill the performance?

  12. As far as I’ve tested, I haven’t found any proper bug at the mixed lights. However I’ve encountered some kind of restrictions depending on the Rendering Path used:

    – Forward: dynamic objects cast shadows on static geometry, but not the opposite.
    – Deferred: static geometry cast shadows on dynamic objects, but not the opposite.
    – Legacy Vertex Lit: only the baked shadows at the static geometry are visible.
    Legacy Deferred (light prepass): dynamic objects cast shadows on static geometry, AND receives shadows from static geometry.

    The last rendering path seems to work, but it comes with an important drawback: the shadow strength (configured at the light) affects the shadowed parts of the static geometry. So when the strength is 1, it completely voids the effect of the baked lightmap.

    What I’d do is using the method that best fits the actual project, then wait until Unity releases a more convenient solution. Otherwise, you might spend a lot of time trying to figure out a workaround to an issue that will be resolved by Unity at some point.

  13. the new Elighthen bake times is killing me.
    Do you have a couple of suggestion on the values for baked resolution, baked padding and indirect resolution for a non photo-realistic scene, targeted for a mid-level android device?

  14. @Miguel
    First of all, are you using LODs? I suspect this because a mid-level smartphone has limited performance, so usually you need LODs to get good graphics and performance.

    You should be aware of the fact that Unity’s light baking is currently buggy or gives bad results when applied to LODS, read here: http://forum.unity3d.com/threads/lod-group-baked-lighting-how-to-use.349103/

    After you have solved the lightbaking+LOD problem, then you can start worrying about how much time your computer will take to generate lightmaps 🙂

    I would try disabling indirect lighting and i would use half or 1/4 of the default baked resolution, that would speed up generation time , and then you see the result.
    I think those settings would give good results for small monitors like smartphones.

    What computer are you using with Unity?

  15. @Pixel,
    Hi, I’m not using LODs, had to wrap all my static geometry in a GameObject, scalled down to 1/3, teduced backed resolution to 4 and improved quite alot.
    My PC is a core i5

  16. @Miguel
    Good to hear

    Has the game’s framerate improved now compared to before when you used realtime light?

    And without losing much graphics quality?

Leave a Reply

Your email address will not be published. Required fields are marked *

Confirm that you are not a bot - select a man with raised hand: