directional light shadow areas

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By noobGuy
:warning: Old Version Published before Godot 3 was released.

Hi guys,
Here’s my problem, I use a directional light to simulate the sun in my game.
It works well a simple pi rotation along x axis allows me to simulate the sun path from sunrise to sunset, and it creates shadows accordingly to the sun position (= rotation as i use a directional light as i said previously).
Now how can i detect if a tile of the ground is in shadow or enlightened ?
My first idea would be to ask the directional light (but i don’t know how to do it…)
My 2nd idea would be to cast a ray (which sounds useless to me as the directional light must have already performed a ray cast to produce light (and therefore shadows)). Whatever, if the way to go is to (re)cast such a ray, what would be the “from point” ?
I can’t just take a random distance from the scene, as obviously the light and shadows depends on the distance from the directional light to the scene, meanwhile the doc describes a directional light as a light far from the scene… but far ain’t a distance i can use as a “from point” far a raycast :wink:
Help me please, i’m so lost here XD
Ty for your time guys

:bust_in_silhouette: Reply From: eons

I don’t think there is a simple way for the program to know details on these kind of effects, a shader may know if there is light but the rest of the game wont.


For the static part you know which gets sunlight at each light angle (you may need to do some external prototype with a more complete 3D tool or a real mockup to get started), you can create Areas or groups for them, then use rays for the moving ones only.

The rays from the moving tiles need to be as high as the lower obstacle at a low light angle, I guess.

ps: don’t try to make it perfect, approximate to get a good feeling, you may find better techniques on the way.

:bust_in_silhouette: Reply From: Zylann

A raycast seem to be a correct solution to me. Directional lights don’t perform raycasts, they perform projections on the graphic card, which is different and not easy to get from gameplay code.

You can pick any position and throw a ray in the sky where the sun is, at a decently big distance (like camera’s far clip distance). If it hits anything, then the point you started from is in shadow. You may want to correctly setup collision shapes, no need for exact match though.
You might also want to exclude transparent stuff or invisible walls, by putting them in a separate collision layer.

Also, you might want to refine what you mean by “tile in shadow”. With directional lights, a tile can be fully lit, partially lit, or completely in shadow. If you use a raycast, it all depends from which position on the tile you cast it, as it will give you this information in one single point in space.
But you could find better solutions by thinking about your game rules :wink:

:bust_in_silhouette: Reply From: noobGuy

Ok guys thanks for these details and for your time. I will try with a far away empty and a ray cast and try to fix the correct distance according to visual shadows area.
Or maybe i will try with some other kind of light as i want to be able to know which amount of light a tile receives (for fields and cultures…)