Idea for fake 2D lightning effect

This is an idea I had for faking lightning in 2D games, I have no idea about shader coding so I will leave the idea here in case someone wants to give it a try. Since I dont know about shader coding It maybe wrong :lol:

I will use this little map as an example
1642200--101951--$map.png

First we modify the gamma of the screen (or maybe a dark overlay?)

1642200--101952--$mapgamma.png

Then we create a map with the shape of the light

1642200--101954--$texture.png

we apply the texture to the scene and make the shader not change the gamma where the texture is not transparent.

result should look like this (if I’m not mistaken :lol:)

1642200--101955--$maplightning.png

If this works it could be called with a trigger collider and have the lightning progressively darken an area as you enter it, giving it a more natural aspect.

Part of the problem with this is that when you first darken the background images then try to lighten it again, information is lost from the color resolution. e.g. making things half brightness reduces the resolution from 8-bit to 7-bit… When you then go to double brightness again the color gradients begin to look blocky.

An alternative approach here is to FIRST draw your lighting shapes to a black background, where 1.0 is full light and 0 is no light… and then draw the tiles on top of it in MULTIPLY blend mode, so that they are darkened based on the background intensity. This of course does not support alphablending. It is essentially impossible to do proper alphablending at the same time as taking the background’s intensity (or an dest alpha channel) into account, due to the limitations in complexity of the blending hardware. Some multi-pass method can work if you have render textures (draw background to the render texture, feed it back it, combine it with lighting, then draw alpha-blended parts on top combined with lighting).