<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/990a5646-e002-4109-a69f-20c178203389/List-Iconv3.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/990a5646-e002-4109-a69f-20c178203389/List-Iconv3.png" width="40px" /> This was made using Unity’s Universal Rendering Pipeline, Built-in and High Definition RP need a different implementation.
</aside>
This effect is one of the simplest to do yet it looks really interesting. It consist basically in telling the camera what to render after rendering specific objects. Let gasp the theory first. The camera follows an order to render objects in every frame. And as many things in this awesome engine it could be customizable.
As we see in this effect the rendering order is not necessary related with the distance between the object-to-render and the camera. However, by default it works this way with the opaque objects (Transparent objects are render first).
In the Blue cube example we can see the rendering order manipulation in action. The plane moving is rendering before the blue cube. In world space the are at different distances from the camera.
Stencils are a shader feature which lets us set up rules to control whether to render certain objects or part of objects. When rendering objects, we can tell Unity to remember which parts of the screen contained that object, even if we decide to make the object completely invisible.
To understand how it works is worth to mention that this is one of the buffers that Unity uses before rendering a frame. The word “buffer” refers to a memory space in which data is stored temporarily. Textures that store values in RGB and are used to measure different steps before the rasterization of a frame. The Z-Buffer corresponds to those depth values between the objects in the scene and the camera, which are assigned to each pixel. The Color Buffer handles the information of each pixel of the frame to be rendered and its influence. And the Stencil Buffer defined ****by Unity ****official documentation:
“The Stencil Buffer stores an integer value of eight bits (0 to 255) for each pixel in the Frame Buffer. Before running the Fragment ShaderStage for a given pixel, the GPU can compare the current value in the Stencil Buffer with a determined reference value. This process is called Stencil Test. If the Stencil Test passes, the GPU performs the DepthTest. If the Stencil Test fails, the GPU skips the rest of the processing for that pixel. This means that you can use the Stencil Buffer as a mask to tell the GPU which pixels to draw and which pixels to discard.”
Basically, what the Stencil Buffer does, is to activate the Stencil Test, which discards fragments (pixels) so that they are not processed in the Fragment Shader Stage, thus generating a mask effect in the shader.
Buffers explanation - Resource site.