2d Lighting with shaders

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
thesandrobrito
Prole
Posts: 4
Joined: Sat Nov 23, 2024 3:14 pm

2d Lighting with shaders

Post by thesandrobrito »

Hello everyone.

I am making a game that's set in the 5 boroughs of New York and it's set at night.

Although my game's art is already having a darker colour palette, I would love to have a lighting effect that made so that the street lamps and cards headlights lightened the scene (or their absence darkens it).
Screenshot 2024-11-30 at 00.38.02.png
Screenshot 2024-11-30 at 00.38.02.png (104.85 KiB) Viewed 952 times
Something to the lines of this.

How would I go about doing something like this?
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: 2d Lighting with shaders

Post by RNavega »

That's a fun challenge.
While I can think of two ways of doing that, one is better than the other.
The first way is: you draw the entire scene at night (like using nighttime editions of all background and foreground graphics, or using a dark tint color set by love.graphics.setColor so it multiplies with their original colors), then after that you re-paint parts of the scene using the "lit up" version of those graphics (like drawing the same graphics but using a lighter tint color with setColor so they appear brighter, or literally drawing lit up editions of the graphics like from different images). The bright parts of the scene will appear as the parts lit by the lamps.

However, that won't work well if you want multiple lights overlapping each other and the effect accumulating between them.

For that, there's another method which is a relative color change -- you are painting shapes or images onto the screen with the "add" blending mode, pushing the colors on screen in some direction like lighting them up.
You can mask the image or shape used for additive blending based on the fact that a black color results in no change (the color being overpainted will stay the same).

temp.png
temp.png (42.82 KiB) Viewed 874 times

PS sometimes the interpolation between two colors results in a muddy intermediary color that doesn't look right, and that's because the blending is happening while both colors are in gamma space (sRGB). It's a nuance anyway and people don't notice while playing, but if you want to fix that, you can either turn on the gamma correction mode in conf.lua or doing the color conversions yourself in a pixel shader. You can search the forums about this, or also read more here if interested:
- https://blog.johnnovak.net/2016/09/21/w ... r-blending
- https://love2d.org/wiki/love.graphics.isGammaCorrect
- http://www.gamedev.net/forums/topic/709 ... d/5438887/
thesandrobrito
Prole
Posts: 4
Joined: Sat Nov 23, 2024 3:14 pm

Re: 2d Lighting with shaders

Post by thesandrobrito »

Thanks for your reply. I was hoping to do it with shaders. I'll give that a try tho
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest