Adding shadows to 2.5D scenes

Adventure Creator is not a graphical asset, and instead relies on Unity's own rendering capabilities. However, one common requirement for 2.5D games is the ability to display shadows without the floor mesh - so that they can show above the background image:

The 2.5D tutorial includes a Matte Shadow shader, but this only allows shadows for directional lights. In this tutorial, we'll be making changes to a pre-existing scene to allow shadows for lights of all types. Note there are multiple approaches that can be taken, and you may find that another technique is more appropriate for your game's needs.

First, move your Player prefab into the scene, and duplicate their Skinned Mesh Renderer gameobject. Then place it on the BackgroundImage layer, and set the Cast Shadows value to Shadows Only.

You can now either leave the modified Player in the scene, or click Apply at the top to update the prefab and remove him from the scene.

Next, add a Plane to act as the floor, and place it on the BackgroundImage layer as well. Note that this can be your existing floor object if you already have one, but as the Navigation Mesh should be placed on the NavMesh layer, that should not be used.

As well as the floor, you can also create other meshes to act as your scene's walls, etc - so long as they are all placed on the BackgroundImage layer and have the default white standard shader.

Choose GameObject -> Camera from the top toolbar to create a new camera, and name it "Shadow Camera". Remove the Audio Listener, GUI Layer and Flare Layer components from it, so that only the standard Unity Camera component remains. Drop it onto the MainCamera in the Hierarchy so that it becomes a child of it:

Then set it's position and rotation to (0,0,0) so that it shares the same transform as the MainCamera.

In your Project window, create a new Render Texture asset file, and name it "ShadowRT". Viewing its Inspector, set its Size values to match your game's intended resolution:

Going back to your Shadow Camera, set the Culling Mask to BackgroundImage, Clear Flags to Solid Color, the Background colour to white, and Render Texture as the new ShadowRT asset.

The Camera's Field of View value will also need to match your MainCamera's, which may change during gameplay. To do this, create a new C# script named CopyFOV.cs, and replace its contents with the following:

using UnityEngine;
public class CopyFOV : MonoBehaviour
{
	private Camera thisCamera;
	private void Update ()
	{
		if (thisCamera == null) thisCamera = GetComponent ();
		thisCamera.fieldOfView = Camera.main.fieldOfView;
	}
}

Then attach the new Copy FOV component to the Shadow Camera GameObject.

Next, find your scene's main light, and duplicate it. In the Light component, set the Culling Mask to BackgroundImage, and enable shadows:

Especially if the light's Type is Point, you'll need to raise the Intensity so that the floor plane you created earlier is completely white except for the shadows cast by your character:

To overlay the render texture over the MainCamera, we'll need an overlay screen effect. One can be found in this asset.

(Other such assets include Colorful, but the above package can be downloaded for free)

The downloaded package needs a slight tweak to work with our technique. Open up ScreenOverlay.cs, and replace line 22:

public Texture2D texture = null;

with:

public Texture texture = null;

With the correction made, attach the modified Screen Overlay component to your MainCamera object. Set the Blend Mode to Multiple, and the Texture as the ShadowRT render texture asset:

You should now have shadows rendering on top of whatever background image you have, without the need to see the floor mesh as well.

Adventure Creator and this website are copyright Chris Burton, ICEBOX Studios

Go to top
Do NOT follow this link or you will be banned from the site!