Directional "sun" light in a 2d game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By XEA

Hi everyone,

I’m making a 2d platformer. Some sections are underground while others are above ground. In order to differentiate the two, I would like to implement a “sun” that casts shadows over the environment.

My current solution has me using a Light2D node with a texture large enough to cover my whole stage (about 6000x6000px, 72p resolution). I then drastically offset the light’s point of origin so it sits high above the stage. This works visually – my tile map has lightoccluders built in, so the “sun” casts shadows on my environment.

However, this significantly drops my framerate, making the game unplayable on weaker machines.

My question: Is there a more elegant solution to overhead lighting that won’t make my game lag?

Thanks for your time!

:bust_in_silhouette: Reply From: The_Black_Chess_King

WELL, realtime shadows are more common in 3D, and even then they are kind of baked into the game, not calculated in realtime. But mostly in 2D you generally do not work with big dynamic lights, instead you will normally use fake lighting, using tint or LUTs to the landscape, to make it night, use a bluewish dark tone, and sunset, orange-yellow. The light itself could be a ambient tint colored rectagle on top of your world, multiplying your colors or tinting them.

The shadows could be 2d copies of the sprites, squished down and inverted on the y axis, distorted in a angle, to make it look like the sun is rotating around the objects. I don’t think using a big light to create dynamic shadows will ever be optmized and keep a nice FPs, unless it’s 2.5D, but that’s way overkill in my opinion, unless you know what you are doing.

This idea I mentioned is easily understood here, but keep in mind it’s a different engine:

Here’s a game that uses this technique: (check for 3:00 and 9:00 timestamp to see what I mean)

Here in this discussion there is some information as well,

I don’t know the graphics you are going for, a screeshot would be nice. Shadow is a complex thing in 2d games, search on google and notice how other games did it as a reference.

Thanks so much! This is SUPER helpful!

I don’t have screenshots atm but here’s a teaser trailer for the project. (00:34 - 00:40 has two different outdoor scenes where you can see the light casting shadows.)

I played around with it more, and currently I’m reducing lag by using a Light2d w/ a custom texture that sits wherever the light should be. Since I don’t have to enable shadows w/ this texture, it seems to have reduced the lag. The only issue is that now certain bodies don’t produce a shadow (ex: the creature at the 00:38 timestamp).

I’ll try using the techniques you’ve mentioned for bodies like that… or I’ll just swallow my pride an skip out on giving them shadows until I make the jump to 2.5d.

XEA | 2020-04-01 18:26

:bust_in_silhouette: Reply From: MintSoda

DirectionalLight2D has been implemented in master branch. Wait for Godot 4.0…

:bust_in_silhouette: Reply From: Sokz

a bit late, but if you need dynamic lights, you could have one follow the camera