Render 2d lights by subtracting them form base layer (similar to vision cones)

Hey everybody,

In my game a want to archive a similar light (visibility) effect to the game door kickers
(See example image:

As you can see, when there is no light (or in the case of door kickers the vision cone) the game overlays the map with a gray alpha map and subtracts the light cones form it.

The light code I set up so far does all visibility calculations and returns the light area as an array of points (the vertices of the shape) which are sorted clockwise.
My question now is:
What would be the best way of creating this effect? I thought about using a shader, but couldn’t find anything promising so far.
Another idea was using one base mesh and subtracting meshes that describe the lights from it, but this seemed very complicated for such a simple effect to me.

Important information:

  • The light shapes can be concave The
    lights can update during gameplay, so
  • the calculation must be fast enough
    to be executed every frame.
  • The new Freeform Light2D in the Universal Render Pipeline is not an option, as the code for the 2D light isn’t set up for these kind of realtime effects

Any ideas or hints in the right direction would be greatly appreciated.

(Note: When I speak of lights, I mean my own code, I don’t use any 2d Lights or other lights in my 2D game)

For anyone having similar problems in the future:

I solved this problems by creating two shaders that use the stencil buffer. The light writes into the stencil buffer at the area it fills and a mask that overlays the complete level only renders the areas, where the stencil buffer doesn’t contain the value of the light mask.
To get to this solution I used this excellent Tutorial by Sebastian Lague:
Field of view visualisation (E03: stencil shader) - YouTube (However I modified the shader a bit, because I don’t want objects to disappear completely)
Also helpful:
The Unity manual also helps understand lots of commands that the stencil buffer uses: Unity - Manual: ShaderLab command: Stencil