UPDATE: This document applies to Unity 4. For Unity 5 lighting take a look at the Unity 5 Lighting Cookbook. |
How can I make dynamic objects to cast realtime shadows on lightmapped (static) objects?
- Forward Rendering (Single Lightmap or Directional Lightmap only):
Dynamic objects cast realtime shadows on lightmapped objects for a single directional light configured as Lightmapping = Auto or Realtime-only.
- Deferred Lighting:
- Single Lightmap or Directional Lightmap:
Dynamic objects cast realtime shadows on lightmapped objects for the lights configured as Lightmapping = Realtime-only. - Dual Lightmaps:
Full realtime lighting and shadows are applied on all objects below Shadow Distance (Quality settings).
- Single Lightmap or Directional Lightmap:
How can I make lightmapped objects to also cast realtime shadows?
- Forward Rendering:
It’s not possible to make lightmapped objects to also cast realtime shadows in Forward Rendering.
The intermediate solution is to use Light Probes to adjust the lighting of the dynamic objects in the lightmapped scene. - Deferred Lighting:
- Single Lightmap or Directional Lightmap:
– Lightmapped (static) objects cast realtime shadows on dynamic objects for the lights marked as Lightmapping = Auto.
– All objects cast and receive full realtime shadows for lights configured as Lightmapping = Realtime-only. - Dual Lightmaps:
Full realtime lighting and shadows are applied on all objects below Shadow Distance (Quality settings).
- Single Lightmap or Directional Lightmap:
How can I make lightmapped objects to cast AND receive realtime shadows from themselves (self-shadow) and from other static and dynamic objects?
- Forward Rendering:
It’s not possible to make lightmapped objects to also cast realtime shadows in Forward Rendering. - Deferred Lighting:
- Single Lightmap or Directional Lightmap:
All objects cast and receive full realtime shadows for lights configured as Lightmapping = Realtime-only. - Dual Lightmaps:
Full realtime lighting and shadows are applied on all objects below Shadow Distance (Quality settings).
- Single Lightmap or Directional Lightmap:
Can’t get the realtime shadows I expect
Ensure you’re using the correct Rendering Path (Forward Rendering or Deferred Lighting).
If you have changed lightmap-related settings (lightmapping mode, lightmap static, light mode) then re-bake the lightmaps for the scene.
Shadow intensity is different in the lightmap than in runtime
The Shadow Strength setting in the light objects is a realtime-only setting. Shadows in the lightmap are calculated using the actual illumination: ambient light, sky, indirect lighting (bounces)…
You must manually adjust Shadow Strength for matching the lightmapped shadows when the scene is casting realtime shadows (i.e. in Dual Lightmaps mode).
Lightmapping in detail
The Lightmapping effects can be previewed in the Unity 3D editor when the Lightmapping window is selected. The Lightmap Display options are revealed then in the Scene view. The option Lightmapping > Bake > Mode configures the type of lightmaps that will be generated with the Bake button. Also, the Bake button captures the actual Static flag for each object and the Lightmapping mode for the lights (changing these later requires a new Bake for the new settings to have effect).
Lightmapping results in Unity strongly depend on the actual Rendering Path. This is configured either globally (at the Player Settings) or per-camera (Rendering Path option). The Rendering Path can be changed anytime and it does not require a new Bake.
Lightmapping UVs
- Provided by the 3D model: Lightmapping will use the second UV channel (uv2) if available. If not, lightmapping will use the main UV channel. If no valid UV channels are available for an object, its lightmap will be completely dark.
- Generated by Unity 3D: Enabling Generate Lightmap UVs at the Import Settings. All objects will receive a proper UV map for lightmapping upon import.
Lightmapping UVs must conform the specific requirements of lightmaps in order to avoid visual distortions and artifacts. The Generate Lightmap UVs option of Unity 3D already follows all these requirements.
Best practice: Separately import the objects that will make use of lightmaps, then use the Generate Lightmap UVs import setting on them. This will save unnecessary UV data on objects that won’t make use of lightmapping.
In this picture the UVs at the left side have been provided by Blender’s Lightmap Pack UV unwrap option with default settings. The UVs at the right side have been calculated by Unity 3D, option Generate Lightmap UVs with default settings:
Dual lightmaps
- Work with Deferred Lighting only
- Two sets of lightmaps: far, near
- Far: full illumination
- Near: indirect lighting form Auto sources, full lighting from Bake-only sources, emissive materials and sky lights
- The Shadow Distance quality setting defines the transition point between the Near and Far regions
Near | Far | |
---|---|---|
Static objects | Real-time lighting and shadows, Specular / Bump effects | Auto lights: Lightmap only – Major bump details are captured in lightmap as allowed by the lightmap resolution (1) Realtime-only lights: Real-time lighting combined with the lightmap, specular / bump effects, no shadows (2) |
Dynamic objects | Real-time lighting and shadows, Specular / Bump effects | Real-time lighting, specular / bump effects, no shadows (2) |
(1) Many draw calls are saved as no lighting, bump/spec effects, nor real-time shadows are being applied
(2) Some draw calls are saved as no shadows are being calculated
Baked-only lights are baked in the lightmap only, casting the shadows of the Static objects. They have no effect in runtime.
Single lightmap
- Works with any rendering path (Forward Rendering, Deferred Lighting, Vertex Lit)
- Single set of lightmaps (half memory used)
- Only one directional light can cast realtime shadows in Forward Rendering
Realtime shadows strongly vary depending on the rendering path used for the lights marked as Auto:
- Forward Rendering: Dynamic objects cast shadows on Static objects, but do NOT receive shadows from them.
- Deferred Lighting: Dynamic objects receive shadows from Static objects, but do NOT cast shadows on them.
Forward Rendering
Lighting source | Realtime shadows (single directional light) | Specular / Bump | |
---|---|---|---|
Static objects | Lightmap + Realtime-only lights | Static objects receive shadows from Dynamic objects | Auto lights: No – Only major bump details are captured in lightmap as allowed by the lightmap resolution. Realtime-only lights: Full Specular / Bump effects combined with lightmap details. |
Dynamic objects | Realtime from all lights except Bake-only lights | Dynamic objects cast shadows over Static and Dynamic objects, but do NOT receive shadows from Static objects. | Full Specular / Bump effects. |
Deferred Lighting with “Auto” lights
Lighting source | Realtime shadows | Specular / Bump | |
---|---|---|---|
Static objects | Lightmap only | Static objects cast shadows over Dynamic objects. | No – Only major bump details are captured in lightmap as allowed by the lightmap resolution. |
Dynamic objects | Realtime from all Auto lights | Dynamic objects cast shadows over other Dynamic objects, but NOT over Static objects. | Full Specular / Bump effects. |
Deferred Lighting with “Realtime-only” lights
Lighting source | Realtime shadows | Specular / Bump | |
---|---|---|---|
Static objects | Lightmap + Realtime-only lights | Full realtime shadows combined with lightmap shadows | Full Specular / Bump effects combined with lightmap details |
Dynamic objects | Realtime from all Realtime-only lights | Full realtime shadows over both Static and Dynamic objects | Full Specular / Bump effects |
Directional Lightmaps
- Work exactly like Single Lightmap with one addition: Static objects exhibit full lighting and specular / bump effects from *all* the light sources (Auto / Realtime-only / Bake-only). Note that this includes Ambient light as well!
- Two sets of lightmaps: Color + Scale (directional information). Uses the same memory as Dual Lightmaps.
- Works with Forward Rendering and Deferred Lighting (not with Vertex Lit).
The scene below contains Static objects and “Auto” lights only:
In terms of performance, the bump / specular effects in Directional Lightmap are provided for free! (no additional draw calls):
Thanks! Awesome tutorial! Even Unity docs dont give this details!
As I seen the Unity built-in UV maps generator give some artefacts and different sizes in some areas. Looks like there is no way to improve that? And anyway this doesnt affect too much the result, right?
Thank you! In fact, the lack of details in the Unity docs was the motivation for this document.
The main feature of the UV maps generated in Unity is that each UV side is proportional to the actual surface in the scene, and they’re unwrapped properly for avoiding artifacts. Then the Lightmap Resolution value (at Lightmapping > Bake) is uniformly applied to all the mesh.
If you need more (or less) resolution applied to specific objects you can select them in Hierarchy and modify their Scale In Lightmap value (at Lightmapping > Object). This will assign more or less amount of lightmap texture to that object.
Thank you Edy,
I really appreciate you tutorial!
Cheers!
Dobri
The “Deferred” render path is a no go as long as Unity can’t do this with Antialiasing!!
Many thanks for your time and help…
Hello there, how can i reach you for some advice ? We have a really compicated outside scene for mobile, and before baking, we want to create a plan.
Jeff, you can find my email at the About section.
I’m starting to love this place
hey In unity 5.3 lightmaps are not loading with asset bundles. How can I achieve that?