- Home /
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).
$$anonymous$$aybe you could use a shader that supports masking, give it a circle mask, then make the camera RenderToTexture using this shader as a replacement shader (and make the camera render no background), and display the resulting RenderTexture in your main camera view. It won't be the circle collider, but at least a circle :)
Did you ever get this problem solved @wolfdog ? this is the only post in the internet ive been able to fine that even comes close to the issue i need a soloution for? i thought/tried Camera.layercull.spherical but just wouldnt work
Answer by Bunny83 · Feb 19, 2016 at 04:08 PM
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.