Masking a Light/Illumination

Godot Version

4.3

Question

How can I apply a mask to a light/illumination?
I have a lamp object in my game and I want the sprite and the light to be visible on the screen only if they’re insesecting the player’s field of view. The lamp must remain collidable regardless of its visibility.
This effect is inspired by the game Darkwood.

Here’s a drawing showing expected result:

I’ve tried using a subviewport but it hasn’t worked well. Light2D doesn’t show up if you put it in a subviewport.
I’ve tried using a LightOcculider2D attached to player that hides light outside of vision cone but light2D behaves weirdly when completely inside of LightOcculder2D.
I’ve also seen this tutorial: https://www.youtube.com/watch?v=iKRJqx9KCJU but this would require me to duplicate all hideable objects into another layer and that would be very messy.
I’ve tried this tutorial as well: GitHub - jhlothamer/godot4_visibility_masking_demo: Demo of visibility masking in Godot 4 but this doesn’t hide its light.

Any help is much appreciated.


Maybe something like this can help.

  • Create a CanvasLayer for foreground objects, and another for backrgound.
  • Add a CanvasModulate node with color black in both CanvasLayers.
  • Add a PointLight2D to your CharaterBody2D, with a cone image (white is light). And change in the Range Section Layer Min to -1 and Layer Max to 1 (you can create several layers, each in a separate CanvasLayer if you need, and decide if it is afected by light or not).
  • Added some sprites, using Godot icon, to show an example. And a “floor” in the backround too with godot icon.

Hope that helps you

I think I missunderstud the problem. The objects in the scene needs to be lights that needs to be hidden when it is out of the field of view.
Then maybe using a Shader you can implement that.

Maybe this one: Polygon Mask - Godot Shaders

Found a solution:

Just do this:

Create a light with blend mode: Mix.


For this sample, I created 2 lights for the character, one is specific to the layer where the objects to be hidden are. (Layer Min and Max set to 2, and CavasLayer2 has layer=2)

And a transparent cone:

light_transparent

For the Sprite to be hidden off light, set a new CanvasMaterial with Light Mode: Lights Only

Hope this helps now!

Sorry, I must be missing something. How does masking sprite helps with masking light?

P.S. Clarification: I’m looking for a way to hide illumination/cast light outside of vision cone. I’m not looking for a way to hide a sprite outside of vision cone.

Ohh, sorry. It is not working with lights!