A shadow requires a Directional Light object and a model with a material that supports shadow casting.
Shadows are only supported by the following materials:
The workflow demonstrated in this guide shows you how to cast a shadow of a target cube mesh and project the shadow onto a plane.
Download the following file to get started:
Enable Shadow Casting for a Light Source
To enable shadow casting for your main light source, select your light object in the Hierarchy panel. In the Inspector panel, locate the light’s properties and select the Cast Shadow checkbox.
For this example workflow, select the Directional Light object in the Hierarchy panel, and then select the Cast Shadow checkbox in the Inspector panel.
Shadow Properties of a Light Source
Once you’ve enabled shadows on your light source, you will have access to the following basic and advanced properties to adjust the shadows to your liking.
Resolution: The x and y resolution of your shadow map.
Changing the resolution may impact the performance of your effect.
Strength: Set the darkness of your shadow.
- Bias: Shift pixels closer to the light source in your scene, helping combat shadow acne. The bias property should be used with caution, as too much bias will cause your shadows to “float away” – a phenomenon called Peter Panning.
- Color: The color of your shadow. Black is the default color.
- Self Gradient: Aid transition to a smooth surface by adding a value that helps counter jagged or hard edges.
- Soft Shadow: Control whether or not your shadow is blurred at the edges.
Enabling soft shadow may impact the performance of your effect.
Softness: This affects the blurriness of the edges of your shadows.
Auto Frustrum (for Directional Light objects only): Allow the engine to automatically determine the size of your light’s frustrum, or the portion that is visible on your screen. For most cases, enabling this property is best. If disabled, the following properties must be set:
- Shadow Area: The size of the shadow map inside the scene. If the shadow map area is larger, it will include more objects within the scene. However, each object’s shadow will take up a smaller part of the shadow map, thus making them appear at a lower resolution.
- Frustum Near: The near plane of the light’s frustrum used in calculating the shadow map. Use this to capture objects closer to the light, ensuring that they aren’t cut off.
- Frustum Far: The far plane of the light’s frustum used in calculating the shadow map. Set this to capture more objects farther away from the light. If the frustum is too far, further objects will disappear.
Add a Mesh Object to Cast a Shadow Onto It
For this workflow, a cube’s shadow will be casted onto a plane. Add a Cube and Plane object by clicking the Add button [+] > 3D in the Hierarchy panel.
Select the Plane object and modify the Transform properties in the Inspector panel to make it horizontal along the plane. Move and adjust the plane in the scene so you can cast a shadow onto it.
Create Materials to Support Shadow Casting
To cast a shadow on your mesh object, it must have a supported material (Standard PBR, MatCap, Simple Lighting, or Matte Shadow) applied to it.
Apply a Material to a Cube
For this workflow, first add a Simple Lighting material by clicking the Add button [+] > Material > Simple Lighting in the Assets panel. Apply this material to the cube by clicking the Cube object in the Hierarchy panel and locating its components and properties in the Inspector panel.
Under the Mesh Renderer component, click the Material field to select Simple Lighting. Then select the Cast Shadow checkbox as the cube’s shadow will be casted.
Apply a Material to a Plane
Next add a Standard PBR material by clicking the Add button [+] > Material > Standard PBR in the Assets panel. Apply this material to the plane by clicking the Plane object in the Hierarchy panel and locating its components and properties in the Inspector panel.
Under the Mesh Renderer component, click the Material field to select PBR Material. You don’t need select the Cast Shadow checkbox as the plane is used to receive the cube’s shadow.
Enable Shadow Casting for a Material
In the Assets panel, select the Standard PBR material and locate its properties in the Inspector panel. Under Shadow, select the Shadow On checkbox.
You can now see the cube’s shadow casted onto the plane!