+7 votes

Hey guys,
I'm currently working on a top-down game (orthogonal, like "Harvest Moon: Friends of Mineral Town" for example).

I'm trying to introduce a day/night cycle with moving shadows over the time of day.
In 3d this probably wouldn't even be worth questioning but in 2d it has proven to be a bit more complex.

I tried a few approaches using Light2D and LightOccluder2D or using a shader, added normal maps to the sprite and got the lighting of the sprite itself working.

First i used the Light2D node. Combined with the LightOccluder2D (and a shader so that the sprite wouldn't catch its own shadow) worked pretty well. Until i realized that the shadows were allways casted with infinite length because in 2d you can't really assign a z value that "shortens" the shadows. (i still hope i simply missed something :D)

Now I'm using a fragment() - processor approach in with i tell the shader via parameter in which direction the "sun" is currently shooting its rays. This approach would be fine as for every pixel will receive the same light direction and therefore it creates that "directional light" effect. Downside is I would have to deal with casting the shadows myself.

Is there either a way to "shorten" the shadows a Light2D is casting or is there a way to cast shadow within the light() shader by myself?
Or is there maybe even a way easier approach on how to create a day night - lighting?
Thanks in advance!

in Engine by (21 points)

Looks like the shadow casting would need to know how tall things are then, or if that's not possible, fade it progressively so even though it's infinite it would be cut somehow.

Otherwise I don't have much clues as I never really thought about that in 2D. Usually I just don't bother rotating shadows (some games do that, even in 3D), or I would find a way to get the height data which is missing in 2D.

You wouldn't happen to know how i could fade the shadow progressively, would you?
I even tried a lot of stuff within the light() shader but all i'm really able to access is the LIGHT variable which seems to be the color of the light at the coordinate or the light vector... i could access the the shadow var (tho until now i didn't really understand that one) but even if i could somehow lower the alpha channel of that one i would need a value like... how long is the shadow until that point. but there is no really ... "shadow-vector" or something like that and i don't think it would work if i'm solely relying on the light-vector (like using it's length or something) as for every shadow caster would have a different distance to the light source.. :/

Sorry I don't have practical experience with 2D light shaders in Godot :(

I know this sounds hacky, and it is, but for now maybe just make a normal Light2D node and place it really far from your viewport's center.

1 Answer

+1 vote

This is a very good tutorial for making a 2D day/night cycle

https://www.youtube.com/watch?v=WZ4R2leFHzI&t=622s

by (31 points)

It doesn't answer the question though, as it does not use drop shadows.

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 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 webmaster@godotengine.org with your username.