Hide sprites when outside of a light source

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

I have a light and a sprite, and I only want the parts of the sprite that itersect my light source to be visible. How can I do that? When I try to use a masking blend mode, the sprite is colorless.

Edit: Sorry for the bad question, I thought this was simpler than it now seems to be. @eons solution works great for basic sprites/textures, but I want my sprites to use their normal map. When I set “Shading Mode” to “Light Only” only the normal map is visible, the rest of the sprite is black.

enter image description here

It still looks that way if the Light2D is Mix or Mask. However, if the material of the regular sprite (on right in the picture) is set to “Light Only” it is hidden just fine as described in the example project.

Normal mapping is achieved by pasting this code into the “Fragment” section of the CanvasItemShader and linking the proper normal map as a shader param. Since the masking stuff is not working for normal mapped sprites, is there something wrong with my shader? I don’t know much about shaders unfortunately.

uniform texture normal;
//normal maps expect Y-up, but 2D is Y-down, so must mirror this.
NORMAL = tex(normal,UV).rgb * vec3(2.0,-2.0,1.0) - vec3(1.0,-1.0,0.0);

Check the “using lights as masks” demo, it adds a CanvasItemMaterial to the node that needs to be hidden, set to be affected by lights only to make the mask effect.

EDIT: turned this into a comment to leave it unanswered

eons | 2017-05-01 03:42

Hey @eons, thanks for responding. I updated the question with more relevant info.

jarlowrey | 2017-05-01 12:20

I guess this may need some light shader trick but I don’t know how that works, maybe the best could be to talk about this on IRC, there you may find more people used to Godot shader language.

eons | 2017-05-01 13:38

Maybe I’m misunderstanding something, but the shader seems to work fine (both the texture and normal mapping effect are visible when lit, otherwise dark) when the shading mode is set to ‘Light Only’ when I try it on the ‘2D Normal Mapping’ demo. What does your normal map look like?

mollusca | 2017-05-01 15:27

Yeah I put my sprite in the demo project and it works. I think my light and sprite are exactly the same across the Godot projects, and the normal map definitely is. Not sure what the heck is going on. Here’s the images enter image description here

enter image description here

jarlowrey | 2017-05-02 00:01

Strange. The normal map looks alright. You could of course try rebuilding your scene from scratch and checking at which point it goes wrong, if you haven’t already.

mollusca | 2017-05-02 07:45