How to hide sprite when out of 2D light sight?

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

In Godot 3.0.6 (Ubuntu & Win7), I try to recreate the light effect from Darkwood, but with CanvasItemMaterial → Light Mode → Light Only, the shown part becomes transparent.

I tried to use a custom shader, but it doesn’t make any diffrence…

shader_type canvas_item;
void fragment() {
if(AT_LIGHT_PASS) {
// For all fragments in the light
COLOR = vec4(texture(TEXTURE, UV).rgb, 1.0);
}
else {
// For all other fragments
COLOR = vec4(texture(TEXTURE, UV).rgb, 0.0);
}
}

Is there a problem with the 2d lighting system or am i missing something?

Thanks in advance for any help.

:bust_in_silhouette: Reply From: User1011

I’ve figured it out!

You have to change the Light2D mode to mix (not add !) and by using a shader, like the one above, process every pixel’s color as you want, based on if the light passes through it or not. And shadows work just fine! And for a sprite to be visible only under light, just set it’s material mode to light only.

Also if you want a light to affect only specific sprites, you have to set the Range → Item Cull Mask level of the Light2D to the corresponding level of the sprites.

More information on this very helpful article:
https://www.braindead.bzh/entry/godot-quick-tip-3-black-white-visual-field