Skip to main content

Rendering Light and Shadow with Unity in AR

    Lights and shadows are very important. The lack of shadow will make a scene looks not real. The picture below shows the difference, you can't really tell is the man standing on the plane or floating in the air, if there is no shadow.
With and without shadow makes a big difference

    You can see the difference in 3D, so it definitely affect a lot in AR. This article will guide you how to render lights and shadows with Unity and Vuforia SDK. And I think the idea should also work on other SDK or game engines.


Problem


    In Unity, if you want to render lights or shadows, there has to be a mesh. But we add a mesh on, we can't see the camera's view.

With the plane, I can't see the desk's surface

    I also tried with transparent material, but the shadow can't cast on it. (at least not with Unity's standard shader, maybe some custom shader can do it)

With transparent material, we see the desk's surface, but there's no shadow


Thought - Render Light and Shadow Separately


    The first though I got, is to try something like Additive or Multiply 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

Light and Shadow layer

3D Object layer

the final result with all 3 layers

    The result image above might not be clear enough, since the desk is so dark. You can check this demo video.




Video Tutorial


    Here is some hint for you, and below is a step-by-step video tutorial.

  • There are some custom script used in video, you can find them on my github: https://github.com/newyellow/Unity-Vuforia-AR-Tools.
  • You need to add 3 cameras to render Background, Light, AR Object separately, and these 3 cameras' view have to be exactly the same with Vuforia ARCamera. I use CopyCameraData.cs to do it.
  • Since you can't render an object's shadow onto another layer, so you need a copy to do it. Edit the copy's Mesh Renderer cast shadows setting to "Shadow Only".
  • To do the Post-Effect thing, I use Colorful FX's blend to do it. You can use other Post-Effect script as well.
  • If you purchased ColorfulFX, here is the RenderTextureToBlend.cs


    Feel free to ask if you have any problem!

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Render texture to blend pleasea :C

    ReplyDelete
    Replies
    1. here you are: https://gist.github.com/newyellow/abd6883e3e92709f112ba9c1fcc3162c

      Delete
  3. Q:how to creative object like this house。


    sorry i am a new user and English not good

    ReplyDelete
    Replies
    1. Use 3D software such as 3DsMax, Maya, Blender etc. Unity itself is not a tool for modeling.

      And of course, if the structure is simple, you could use those basic shapes to simulate. But shapes like the door, need 3d model editing though.

      Delete
  4. That was sooo, cool and so much fun.

    Did you ever have a problem with flickering shadows and would this solve it, if not a hint what would?

    ReplyDelete
    Replies
    1. Thank you haha.

      There are some settings in Quality Setting affects shadow. You may try to play around those a little bit, and find the best setting for your scene. (I couldn't tell what is the best setting, because it depends on your scene)

      Delete
  5. Hello, thank you for the tutorial.
    The same process would be for an object target instead of an image target?
    now that AR camera has a camera integrated, we still need to create 3 cameras?

    Sorry for the questions, i am new to this.
    Thank you

    ReplyDelete

Post a Comment

Popular posts from this blog

Rendering Lights and Shadows in AR - The Projector Method

I talked about the problems of rendering light and shadows in AR with Unity3D and Vuforia in my previous post - Rendering Light and Shadow with Unity in AR . In the post, I also developed a method using Post-Effect to do the light and shadow rendering. However, the Post-Effect method is doable. But since Unity can't separate an object and its shadow into a different layer. So you have to make a copy to an object if you want to render its shadow in AR. That is very problematic. The performance is bad (though I didn't test it, but it should be), and the most annoying is it mess up the Hierarchy. So I tried to develop another method. And after several tests, I found a solution using the Projector in Unity's Standard Assets Package. And this projector method is the solution I implemented in all my A Week A Project AR series. (the Post-Effect method only use in the Paper House Demo ) This Projector method is simple. You need to build the 3D model of the environment, and cr