Only render part of object inside a circle

I would like to only render the part of objects in circle colliders. Below is what I mean.

Context: I want to have a full scene where only the background is rendered. Then I would like to spawn circle colliders in certain areas to and show what is there. (I’m open to other suggestions beside circle colliders).

Unity seems to finally support stencil operations in custom shaders. This simplifies the implementation of such fancy things.

One approach would be:

  • render your background
  • render your mask geometry to the stencil buffer and increase the buffer value
  • use a custom depthmask shader which only renders where the stencil buffer is 0. Render a fullscreen quad to mask everything out except the area that the mask geometry occupies.
  • render your second camera which should render everything that should be masked.

The advantage of this approach is, that you don’t need to use special shaders for everything else. You probably want to use 3 cameras. One for the background which actually clears the buffers, one for the masking geometry and one for everything else. You would need at least 3 layers. The fullscreen quad can be rendered manually with the GL class.

You can simply disable all cameras except the first camera which renders the background. On that camera use a script that has a OnPostRender method. In that method you would simply render the masking geometry camera manually followed by the fullscreen rect, followed by the main camera.

This is just an idea how it could be done.