defold / sample-lights-and-shadows

Example of how to achieve pixel perfect shadows in a 2D view
MIT License
31 stars 5 forks source link

Lights and Shadows

Example of how to achieve pixel perfect shadows in a 2D view. The sample is based on the following sources:

Installation

You can use this solution in your own project by adding this this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

https://github.com/defold/sample-lights-and-shadows/archive/master.zip

Usage

The main components of this light and shadow casting example are:

Step 1 - Render script

Open game.project and scroll down to Bootstrap and change Render file to lights/render/lights.render. This render script works like the default render script with the addition of also drawing lights and shadows.

Step 2 - Add light quad

Add the lights/render/light_quad.go to a collection where lights and shadows should be calculated.

Step 3 - Add lightsources

Attach the lights/lightsource.script to any game object that should act as a lightsource.

Configure the lightsource properties to your liking.

Step 4 - Add light occluders

Change the material for any component that should cast shadows when lit by a lightsource. Select a material from lights/materials/ matching the component type casting shadows.

Example

There is an example included in this repository. It sets up a few lightsources and occluders and lets you move around the scene.