How to create a day/night-cycle in Godot 2D (Lighting) - Shadow Length in 2D

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

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!

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.

Zylann | 2018-08-17 17:24

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… :confused:

mcsora | 2018-08-18 23:38

Sorry I don’t have practical experience with 2D light shaders in Godot :frowning:

Zylann | 2018-08-18 23:39

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.

SIsilicon | 2018-09-27 03:08

:bust_in_silhouette: Reply From: Jelmar

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

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

njamster | 2020-03-01 12:26