+2 votes

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!

in Engine by (14 points)

2 Answers

+1 vote

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:
https://www.youtube.com/watch?v=haU-lUkitas

Here's a game that uses this technique: (check for 3:00 and 9:00 timestamp to see what I mean)
https://www.youtube.com/watch?v=fhQMt6YLWoI

Here in this discussion there is some information as well,
https://www.reddit.com/r/gamedev/comments/7cti1q/2d_daynight_cycles/

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.

by (373 points)

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.)
https://www.youtube.com/watch?v=hVEc31JrO6Y

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.

+1 vote

DirectionalLight2D has been implemented in master branch. Wait for Godot 4.0...
https://godotengine.org/article/godots-2d-engine-gets-several-improvements-upcoming-40

by (309 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.