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!
This comment has been removed by the author.
ReplyDeleteRender texture to blend pleasea :C
ReplyDeletehere you are: https://gist.github.com/newyellow/abd6883e3e92709f112ba9c1fcc3162c
DeleteQ:how to creative object like this house。
ReplyDeletesorry i am a new user and English not good
Use 3D software such as 3DsMax, Maya, Blender etc. Unity itself is not a tool for modeling.
DeleteAnd 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.
That was sooo, cool and so much fun.
ReplyDeleteDid you ever have a problem with flickering shadows and would this solve it, if not a hint what would?
Thank you haha.
DeleteThere 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)
Hello, thank you for the tutorial.
ReplyDeleteThe 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