# Using Stencil buffer

**URL:** <https://forums.unrealengine.com/t/using-stencil-buffer/149974>\
**Category:** Rendering\
**Tags:** unreal-engine\
**Created:** [August 30, 2020, 3:09am UTC](https://forums.unrealengine.com/t/using-stencil-buffer/149974 "2020-08-30T03:09:42Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Arkiras](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arkiras/32/1008648_2.png) [@Arkiras](https://forums.unrealengine.com/u/Arkiras)\
**Post date:** [August 30, 2020, 11:38am UTC](https://forums.unrealengine.com/t/using-stencil-buffer/149974/4 "2020-08-30T11:38:51Z")

</div>

It shows you how to enable and use the stencil buffer. I assumed that was your problem.

> [@](#):
>
> Now I have a mask but it works until target object in front of stencil object, and I need it to work then the target object behind the stencil object

I don’t understand what this means.

For the portal you need a plane mesh and a material for it. The material for the portal itself is just an empty translucent material that is set to allow writing to the depth buffer (details panel of the material). The portal mesh should be set to not render in main pass and to render into the stencil buffer at a given index (these are in the details panel of the static mesh actor.)

You then create a translucent material for your object that reads from the stencil buffer to determine what areas are opaque vs. what is translucent. All of this was in that article, I’m just repeating it basically, so I don’t know if this is relevant to what you’re trying to accomplish. If your issue is with something else then you’re going to need to be a lot more specific…

Result:

 ![UE4_StencilPortal.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/e/a/ea0dd6dec2850098620e4d41eb6b1bbf71f62ff9.jpeg)  
 ![UE4_StencilPortalMaterial.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/e/0/e0b1731ffbb28aec72aaa169c52299e692d70baa.jpeg)  
 ![UE4_StencilPortalSettings.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/d/2/d2116e560ec4e7796c49398567f468fb0fc333a6.jpeg)  
 ![UE4_StencilPortalSphereMat.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/d/d/ddc203d033e10b17af26dc0e59ca98510767ff22.jpeg)

I don’t know how it works in Unity but in UE4 you can’t read from the stencil buffer in a masked material, which means you have to use a translucent material. This makes it an expensive effect, and you’ll end up with the usual problems with translucent materials, namely you may get inconsistent lighting or sorting issues.

---

_[View the full topic](https://forums.unrealengine.com/t/using-stencil-buffer/149974)._
