Currently, this asset doesn’t work on Unity 5.6 (as of Unity 5.6.2p2). Please use another version of Unity (Unity 5.5 or Unity 2017)
This tutorial shows how to add a Dynamic Shadow Projector object into a scene.
The final result will look like this:
- Start with a new scene
Now, the scene has only a main camera and a directional light. In the project, there are DynamicShadowProjector and Standard Assets (Camera package and Character package).
- Add a plane object and a character object
You will already have a terrain object and a player character object in your scene. In this tutorial, I use a plane object as a terrain, and ThirdPersonController in Standard Assets as a player character.
- Turn off shadowmap
If you can see a shadow at this point, it is a shadowmap. Select the directional light in Hierarchy view, and change the Shadow Type to “No Shadows”. If you want to use GI, it might better to use Quality Settings to disable shadowmap.
- Create a projector object
Create an empty game object under the directional light, so that the transform of the object can be same as the transform of the directional light, and name the object “Shadow Projector”.
- Add “Draw Target Object” component to the projector object
Click “Add Component” button in Inspector view, and select “Scripts” > “DynamicShadowProjector” > “Draw Target Object”.
- Set the character object as a target
The shadow projector object should have Projector component, Shadow Texture Renderer component, and Draw Target Object component now. Drag ThirdPersonController object (character object), and drop it into Target field in Draw Target Object component.
- Create a new material for the shadow projector
Next, you need to create a material for the Projector component. Right click in the Assets folder, and select “Create” > “Material”. Then, name the material “Shadow Projector”. You can use any projector shaders that have “_ShadowTex” property as a shadow texture. Fast Shadow Receiver and Projector For LWRP also provide such shaders. In this tutorial, I would like to use “DynamicShadowProjector/Projector/Dynamic/MipmappedShadow” shader to demonstrate Percentage Closer Filter like soft shadow.
- Assign the material to the Projector component
Select the shadow projector object in Hierarchy view, drag the projector material and drop it into the Projector component.
- Adjust the projector position and size
Now it is ready to draw a shadow. Set the parameters of Projector component, and adjust the position of the projector object in Scene view as in the below image. Please be noted that the right value of the position will change according to the position of the character object. Make sure that the shadow image comes in the center of the texture.
- Change texture settings
To take advantage of mipmapped shadow shader, you need to change texture settings as in the below image. You will see several shadow images in Shadow Texture window in Scene view. The left most image is the image of mip level 0, and the right most image is the image of the most highest level.
* Version 1.0 had a memory leak bug. If mip level was greater than 0, a temporary render texture had not been released every frame. Version 1.0.1 will be released soon. Please don’t forget to update this asset.
- Adjust clip planes
In spite of the blurry mipmapped shadow texture, the character object still has a hard shadow. This is because the image of mip level 0 is always used for the shadow. A mip level picked by mipmapped shadow shader is derived from the distance between the near clip plane of the projector and a point where the shadow is dropped. That means, mip level 0 will be used for drawing a shadow around the near clip plane, and the highest level will be used for drawing a shadow around the far clip plane. So, the clip planes of the projector component need to be adjusted to make the shadow more blurry.
- Add a layer for the character object
If you look at the character from behind, you will notice that the shadow is casted on the feet of the character. To remove the self cast shadow, you need to add a layer for the character object. Click the “Layers” dropdown at the top right side of Unity window, and select “Edit Layers…”. Then you will see “Tags & Layers” settings in Inspector view. Input a layer name to any of User Layers.
- Assign the layer to the character object
Select the character object, and assign the layer in Inspector view.
- Add the layer to ignore layers field in the projector
Go back to the shadow projector object, and add the layer to “Ignore Layers” field in Projector component. Then, the self cast shadow will disappear.
- Add a fallback component
The shadow looks perfect now, but there is one more step left. Mipmapped shadow shader uses tex2Dlod function. It requires shader model 3.0 (DirectX), OpenGL ES 3.0, or OpenGL ES 2.0 + texture lod extension. So, it might not work on some old devices. It is better to prepare a fallback for those devices, especially if you are targeting mobile devices.
Click “Add Fallback Component” button in Inspector view. Then, Mipmapped Shadow Fallback component will be added to the shadow projector object. If you check “Test Fallback”, you can test the fallback in Scene view, and adjust the fallback parameters.
- Add a camera rig and play the scene
It’s time to test the scene. To make it easier to check the shadow, let’s add a camera rig. Drag & drop MultipurposeCameraRig in Standard Assets into the scene, and set the character object as a target of the camera. Don’t forget to delete the old main camera.
Now, you can play the scene. Control the character with arrow keys or A D W S keys (Move), and with space bar (Jump).