Implements several post-processing effects in Unity for 2D pixel perfect rendering with lighting and dynamic shadows.
Table of Contents
Combines the Unity 2D Pixel Perfect Camera with this 2D shadows technique and other post processing effects.
- 2D Pixel Perfect Rendering
- Sprite lighting
- Dynamic shadows
- Post-processing effects such as bloom
- Unity3D 2018.4.14f1
- C#
- Visual Studio Code
Clone the repo and use with Unity 2018.4.14f1. Repo contains a demo scene to showcase the rendering.
The main camera has a 2D Pixel Perfect Camera component. Adjust pixels per unit and reference resolution to match those of your pixel art.
Note: this method of drawing shadows is no longer necessary for Unity 2019 and beyond. See Unity documentation for more details.
I have used this method which implements this script to blend cameras together.
Since we can’t render shadows in 2D, we need to create a copy of our scene with anything we want to cast shadows having 3D geometry. These objects are then given the 'Shadowcaster' layer. Using Unity 3D primitives such as cubes is the easiest way to approximate simple shadows, and these objects can be attached as children of their respective sprites. Use greater Z scales for geometry or adjust the Z position of lights to adjust the ‘height’ of lights/objects.
Example Scene without Shadows:
Note: the fox and eagle are not currently drawing shadows.
This is achieved with the use of two cameras. Our main camera renders all sprites and UI as usual, however our second camera, the 'ShadowCamera', only renders shadows.
Then these cameras are blended together to give the effect of shadows:
Belding is done by outputting the shadow camera to a texture, and then blending this texture with the main camera. This also allows sprites to be partially shadowed.
The shadow camera is not pixel perfect. We cannot both upscale render textures and output the camera to a texture because this is not supported by the package. Instead, we use soft shadows, which is what most other pixel art games do. The difference between pixel perfect shadows and soft shadows is very subtle and only really visible during motion. A blend of pixel art and high resolution effects can look pleasing regardless. Additional post processing effects can be applied with the
Further details can be found here.
Email: tim.jameswork9800@gmail.com
Project Link: https://github.com/Tim-W-James/Pixel-Perfect-Lighting