How to disable self-shadowing in 2D?

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

How do I prevent a sprite from getting dark inside it when using 2D lights?
Here is a screenshot for example:
enter image description here

My sprite has a square occluder encompassing it.
What I want is to have the shadow “below” the sprite (for any rotation), so it keeps its original color and is not darkened. Or, in math words, have the shadow projected from back edges and not front edges.

Seems like this feature is not done at the moment, maybe you should suggest on Issues · godotengine/godot · GitHub

Bojidar Marinov | 2016-03-21 12:20

:bust_in_silhouette: Reply From: puppetmaster-

Try to change CanvasItem/Visibility/Blend Mode in Sprite2D from Mix to Add.

This makes black parts of the sprite invisible.

Zylann | 2016-03-20 03:03

:bust_in_silhouette: Reply From: Kermer

Use behind parent option or decrease Z value on your LightOccluder.
Or you can increase Z of your Sprite.

This won’t stop shadow being created on the occluder, but the sprite will be drawn after it, so it’ll cover the shadow.

Kermer | 2016-03-20 16:59

Would it work if other objects cast shadows that are “higher” in Z?
For example, shadows projected by walls should darken crates, but crates should not darken walls.

Zylann | 2016-03-21 00:55

Things with lower Z are drawed first, then those with higher.
Your LightOccluder is drawing the shadow.
If needed you can use absolute Z (uncheck “relative”).

Kermer | 2016-03-21 04:33

The problem is that lights draw the shadows (or, to state it better, they don’t draw where shadows should be), and lowering the Z has absolutely no effect in-game, only in the editor.

Bojidar Marinov | 2016-03-21 12:19

My bad then. I didn’t run the game, because I thought it should work same for the editor as in-game. Maybe it’s a bug then?

Kermer | 2016-03-21 15:31

I replied on the original question with a link to issues. If the OP doesn’t open an issue soon, feel free to do so yourself.

Bojidar Marinov | 2016-03-21 16:04

:bust_in_silhouette: Reply From: puppetmaster-

Other possibility are:

a. set a light with texture (same as box but White)

Sprite (Box)

  • LightOccluder2D
  • Light2D (shadow disable, texture same as box in White color)

b. set on sprite a CanvasItem/Material and set shading Mode to unshaded.

:bust_in_silhouette: Reply From: ericdl

Found a temporary solution until this is fixed: simply place a duplicate spite on top of the sprite+occluder and change its blend mode to Add. Brightness can be controlled with Opacity value.
occluder fix scene tree
sprite settings

The result:

Before
occluder before fix

After
occluder after fix

:bust_in_silhouette: Reply From: rgrams

All you have to do is create a new material for your sprite, and set the “Shading Mode” to “Unshaded”. Of course this will stop it from having any shadows cast on it, or from being affected by any CanvasModulate nodes.

enter image description here

Hmm that’s nice to solve my first problem :slight_smile:

But it won’t provide “height affected” shadows, which was my underlying goal actually. For example, if A is taller than B, B should not project shadows on A. Here, walls should project shadows on objects and characters, but characters should not project shadows on walls. And as a result, no self-shadowing, because the top would be “higher” than the sides of the object and won’t receive its own shadow. That would be an interesting feature^^

I’m not working on this anymore at the moment, but if I have to I’ll probably get back some info here :slight_smile:

Zylann | 2016-12-25 00:30

Solution: Use 3D. :stuck_out_tongue:

rgrams | 2016-12-25 02:07

Yeah… that’s a bit excessive when you know the 3D engine is completely different in its way of working, API-wise and performance-wise…

Zylann | 2016-12-25 14:47

:bust_in_silhouette: Reply From: eons

Try with an open light occluder polygon (5 points for a square), by default it makes a closed one, you will have to add the 5th point manually.

But maybe 4 ocluders with simple segments with different cull mode will work better (allowing shadows from other occluders).

All this is accesible from the inspector, editing the polygon.

Couldn’t resist to try it and made a mess of resources with the lights and shadows demo, as long as the cull mode of the closed occluders stay untouched, everything works fine.

Also, the open polygon needs a cull mode set to work (both, polygon and segments seem to do the same).

Here is a modified demo with open occluders (the unlocked are the sprites with modified occluders):
https://cloud.openmailbox.org/index.php/s/afvtQ1K4LmoToyn

eons | 2016-12-26 03:02

:bust_in_silhouette: Reply From: Warlaan

Interesting - 6 working solutions and none of them is the one that I would say is the correct one.
You said “in math words, have the shadow projected from back edges and not front edges.”, and that can be achieved quite literally.
Edit the OccluderPolygon2D resource in the LightOccluder2d node and set the Cull Mode. The default is “disabled” which means that the polygon casts shadows both from the inside and from the outside. Setting the Cull mode to either ClockWise or CounterClockWise chooses whether the polygon should cast shadows only from the outside (so that lights inside the polygon don’t cast shadows) or cast shadows only from the inside, which would mean that in your case only the back edge casts shadows.

I was unable to do it with a closed polygon :confused:

eons | 2016-12-26 17:16

I just tried it again. I don’t know what you are doing wrong, but for me it behaved exactly as advertised.
Keep in mind that LightOccluder2D-Nodes are displayed in the editor, so in there the bitmap will appear darker (but it should still be visible that there is light inside the occluder, just not as clearly as when you are running the scene).


The top left one doesn’t use culling, the top right one uses clockwise culling and the bottom one counter clockwise. Which culling direction is the right one for you depends on the order in which you defined the points of the occluder polygon (I assume, I haven’t tested it).

Warlaan | 2016-12-26 19:52

Maybe I messed up the lights, occluder resources or something, tried on another project and worked, on tilemaps too.

eons | 2016-12-26 23:49