AR Lighting and Shadows with Unity

In this tutorial, you will learn how to illuminate the real world with virtual lights, and cast shadows on it from virtual objects.

We will use the project created in the previous tutorial, Build your first MR App.

Adding Lights #

  • In the Hierarchy, click on Camera_Left.

    Hierarchy camera

  • Right-click the ZED_Rig prefab object in the Hierarchy panel, and select Light > Spotlight. You should see the spotlight pointing in the same direction as the camera in the Scene window. (If not, adjust the position and rotation so that it is).

  • With the spotlight selected, click on the white box beside Color in the Inspector window. Select a bright and distinct color, like red.

  • Make sure the sphere you added in the last tutorial still has its position set to X = 0, Y = 0 and Z = 2. If you click on the Spotlight again, its “cone” of light should be touching the sphere.

  • In the Inspector panel, select your Rendering Path (Deferred or Forward). We recommend using Forward Rendering for better performance. See below for details on how each will affect your scene.

If you choose Deferred Rendering, your scene is ready to go. If you choose Forward, you must follow one more step.

Forward Rendering #

Forward rendering is the default rendering path in Unity. This rendering mode is very fast and allows the use of anti-aliasing (MSAA) which greatly improves the integration of virtual objects in real images. However, you are limited to 8 lights at a time that will affect the real world, and must attach a script to each of them.

To enable illumination of the real world with virtual lights, attach a script called ZEDLight.cs located in Helpers/Scripts/Lighting to each light you want to use. A light without a script will only illuminate virtual objects.

Lights

Deferred Rendering #

In deferred rendering, you do not need to add any scripts to your lights. The ZED plugin has its own custom deferred lighting pass that handles mixed-reality lighting. However, this path is less performant and will not allow you to use the Garbage Matte feature of the plugin’s green screen component.

Casting Shadows #

To allow virtual objects to cast shadows on the real world, you need to add a Directional Light to the scene. The light will not illuminate the real world, it will only hit virtual objects and project shadows.

  • To enable shadows, add a Directional Light to your scene from the GameObject->Light menu.

    Lights

  • Set Shadow Type to Soft Shadows and adjust the Strength value. A value of 0.2 or lower usually works well for most scenes.

  • Adjust the directional light angle to 90 degrees in the Transform panel. This will project shadows directly under the virtual objects.

    Lights

Play the scene #

You can now run your scene. You’ll see colored light on both the sphere and the real world behind it, and the sphere will cast shadows on the floor if you added a Directional Light to the scene. You can play with the Range and Intensity values on the spotlight to make it stronger.