prime31 / SpriteLightKit

Blended lighting system for Unity that works with standard Sprites
239 stars 43 forks source link

SpriteLightKit

SpriteLightKit brings back the old two buffered blend trick to get pseudo lighting with just sprites. It handles the setup process of getting that second buffer blended with your normal scene.

how it works

The same scene with two different ambient light setups:

low ambient light brighter ambient light

Setup

You can set the ambient lighting by changing the background color of the camera on the SpriteLightKit GameObject. Each of your lights can use the normal sprite tint color to change how it affects the underlying scene. The SpriteLightKitImageEffect has a toggle for 1x or 2x multiplicative blending (bool use2xMultiplicationBlending). 2x is useful for a scene where the sprite lights can lighten or darken a scene. Use colors darker than the ambient color to darken the scene and lighter colors to lighten it.

Lights look best when they are white and falloff to 0 alpha. That lets you use the tint color to color the lights and the tint color.alpha to set the intensity of the lights. Get creative with your light shapes and experiment! If you need to occlude lights (if you have walls where light shouldn't pass for example) you can just use any black sprite and place it so that it blocks the light however you want it to.

Advanced Emissive Features

SpriteLightKit has some extra, more advanced features baked in as well along with some shaders/materials to help you utilize them. The following emissive features are for use on your normal GameObjects that are not on the Light Layer. The emissive materials will write to the stencil buffer which will then be read by the SpriteLightKitImageEffect and lights will not be rendered for those pixels. This basically makes the masked pixels always display as if lights do not affect them. Materials are provided that let you use a Sprite or a Mesh as the emissive mask.

Since the emissive materials main purpose is to write to the stencil buffer you can stick your geometry that uses them behind all other geometry. This gives you some flexibility when using the SpriteLightEmissiveSpriteMaterial. If it is behind your other Sprites/geometry then it will work like a mask. If it is in front of your other Sprites/geometry it will work like a mask and it will also be displayed.

Advanced Shadow Features

SpriteLightKit can also cast simple offset shadows. The current implementation uses a brute force approach to find affected lights but a b-tree or other spatial search will be added to speed things up. The shadow shader is ready to handle approximated planar shadows (via skew and scale in addition to the offset) but it hasn't yet been implemented due to some decisions that need to be made about how to best handle things.

Implementing shadows requires the following steps:

Credit

The sweet little town sketch is from the amazing work of @pixelatedcrown. Follow on Twitter and Tumblr to see more awesome art!

License

Attribution-NonCommercial-ShareAlike 3.0 Unported with simple explanation with the attribution clause waived. You are free to use SpriteLightKit in any and all games that you make. You cannot sell SpriteLightKit directly or as part of a larger game asset.