How to Render Light and Shadow in Unity for Augmented Reality

By Huang Hsing aka New Yellow

 

Lights and shadows are very important. They are used to give perspective to a scene. The lack of shadow will make a scene look fake. The picture below shows the difference. You can’t really tell if the man is standing on the plane or floating in the air, if there is no shadow.

 

rendering light and shadow with unity for augmented reality

 

You can see the difference in 3D above, and so it will definitely be the same in Augmented Reality as well. This article will guide you on how to be render light and shadow with Unity and Vuforia SDK (here is a beginners guide to Vuforia, if you are unfamiliar) for Augmented Reality applications. The idea behind this should also work on other SDK or game engines.

Rendering Light and Shadow: The Problem

In Unity, if you want to render lights or shadows, there has to be a mesh. But if we add a mesh on, we can’t see the camera’s view. As you can see in the picture below, with the gray plane I can’t see the desk’s surface.

rendering light and shadow with unity for augmented reality

I also tried with transparent material instead of the gray plane above, but the shadow can’t cast on it. At least not with Unity’s standard shader, maybe some custom shader can do it. As you can see in the picture below, with the transparent material we see the desk’s surface, but there’s no shadow.

rendering light and shadow with unity for augmented reality

The Idea: Rendering Light and Shadow Separately

The first thought I got, is to try something like Addition or Multiplication effect. If I can render the light and shadows separately, and then using some post-effect to mix the shadows onto the camera’s view.

So I separate everything into 3 layers – Background, Light and Shadow, and 3D Object layer. And then mix them together. Background layer, only camera’s image:

 

rendering light and shadow with unity for augmented reality

Light and Shadow layer:

rendering light and shadow with unity for augmented reality

3D Object layer:

rendering light and shadow with unity for augmented reality

The final result with all 3 layers:

rendering light and shadow with unity for augmented reality

 

The image above might not be clear enough since the desk is so dark, you can’t see the shadow properly. But you can check this demo video to see better:

 

 

 

Step-by-Step Tutorial:

This is an overview of the various steps that you have to do for rendering light and shadow. And to learn how to do it, view the video tutorial below it.

  1. There are some custom scripts used for this, which you will see in the video tutorial below. You can find them on my Github.
  2. You need to add 3 cameras to render Background, Light and the Augmented Reality Object separately. And these 3 camera’s views have to be exactly the same with the Vuforia’s Augmented Reality Camera. I use CopyCameraData.cs to do it.
  3. Since you can’t render an object’s shadow onto another layer, you need a copy to do it. Edit the copy’s Mesh Renderer: “Cast Shadows” setting to “Shadow Only”.
  4. To do the Post-Effect thing, I use Colorful FX‘s blend to do it. You can use other Post-Effect scripts as well.

 

 

 

I hope the video above helped you in rendering light and shadow for your augmented reality project. Feel free to ask if you have any problem! You can find my facebook page below.

Recommended Posts
Comments
  • wangbf
    Reply

    You add a Blend Script on the Camera, where did it from, also the CoptTextureToBlend?

Leave a Comment

27 − 17 =

Start typing and press Enter to search