Sponsored By

Implementing Fake Shadows in Unity for Mobiles

Implementing Fake Shadows in Unity for Mobiles is about my experiences with different available techniques for shadows in unity3d. Here i talk about why i didn't use Hard Shadows or projectors in my game Thumbi.

Vijay Kartick Prem Kumar, Blogger

December 8, 2013

3 Min Read

Shadows are most important for any 3D game. Without shadows the scene looks unrealistic and not so 3D.. The following is my experiences in trying to implement a light weight shadow in unity and this article is in beginner level only.

unnamed

This is the screenshot of my mobile game "Thumbi"  . The Blue and Red Cubes needed shadow to give a good 3d effect.

First I used the builtin hard and soft shadows.  The total number of verts rendered without shadows was around 70,but the number of verts rendered with shadows was in thousands . This is a very simple casual game for mobile and rendering such a heavy shadow is unnecessary. Besides if i made the Floor Quad unlit/Transparent the shadows wont be drawn on it . This is as per design of unity3D. Hence came my next experiment projectors.

Second  Projectors are available in Standard assets of unity. It is not available in  standard assets for mobiles which should have made me think why? Many forums said projectors are cheaper than actual shadows but they are not at all cheap for mobile games. The number of verts when i used projectors was in hundreds and i got around only 15 fps for such a low poly game. For some reason projectors wont draw on transparent textures in unity. Playing this fast paced game at 15 FPS spoiled the gameplay. Hence i needed something else.

Third  Using a simple Black&White Texture with  of 16x16 like the below one

circle_gradient

Just use a quad with Multiply shader from mobile particles. Offset the quad opposite to direction of light to imitate a shadow and shift it below your object. The quad is made a child of the cube and moved along  with the cube. Ok now we got a shadow with only 2 draw calls and few verts. This approach is useful only for flat surface and when the object is at same height from floor and direction of light is fixed.

For a more better shadow you can use Raycast in downward axis to check the distance to the ground and translate the shadow quad downward accordingly. For most mobile games only one light should be used in a scene for good performance. Hence you can find the direction between Cube and Light and move the shadow quad behind the cube in the opposite direction. Eg.,

Vector3  dir= DistanceFactor* (CubeTransform.position- LightTransform.position).normalized;

dir+=CubeTransform.position;

shadowquad.position= new vector3(dir.x,groundDistance,dir.z);

This code will make the shadow opposite to light direction, and fix it and a distance from cube determined by DistanceFactor. Use any texture you like with similar gradient.This shadow will look awkward on inclined floors.

Now you got a good looking Fake shadow for your mobile game. Do play my game "Thumbi". It's FREE.

Read more about:

Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like