Thursday, May 25, 2017

Simple lighting in 2D games + shader (GLSL)


    Using some sort of light system in your game is kind of a double-edged knife. It may improve some scenes visually, but may also oversaturate, overexpose or simply clutter (especially when the engine you use also "creates" shadows) others.

    In the times where games were still written in Assembly and presenting an eighty Kilobyte background image to your lead programmer to cram into the cartridge could cost you your head, Scene lighting in a 2d game was usually "baked" into the tilesets. So for example a dark cave or forest used darker and more muted colors, while a sunny village had a more vibrant palette and strong yellow highlights could also indicate the presence of an sun overhead. Dynamically changing the lighting of a scene during runtime, barring some palette animation tricks, was usually not possible.

A few decades ago, all it took was some dark, dull colors...

...or some bright, vibrant ones.

    These years we have shaders and raster effects and we can pretty much alter the value of our output pixels however we want. It is not uncommon for a 2D game to have diffuse, self-illumination, normal maps and more, in order to achieve a pixel-perfect, accurately lit environment with shadows! But sometimes, all we need is a simple light map that makes some areas brighter and others darker.