CustomStencil usage in PP material?

Hi there. I’m trying to create a post process material that creates an outline around certain objects. What i need is to have different colors depending on what a mesh is. I’ve read this in the forums:

There is a new feature in 4.9 that
allows Custom Depth with Custom
Stencil. It allows another buffer to
be accessed called Custom Stencil that
allows each static mesh to have a
single Byte from 0-255 to be sent to
that buffer. This can then be used in
the shader to mask props as different
colours when being highlighted for
example.

Original post can be found here

I wold like to know how this masking is done in the material editor, as i’ve not been able to find documentation about this. Thanks!

On a static mesh, you can set “Render Custom Depth” as well as a byte value for the “Custom Depth Stencil Value”.

In the post process material, drop a scene texture sample and select “CustomStencil”.

You can get a mask for each desired value using the material function “BitMask” by plugging the SceneTexture:CustomStencil to the BitMask input and a constant for each bit you wish you mask for.

You could perform a sobel type post process for each main color. The easiest example to copy is from the “Stylized rendering” example project.

You also need to go into the editor project settings under rendering ->post processing and set the “custom depth stencil pass” setting to Enabled with Stencil. You probably have to restart the editor for it to take effect.

Awesome! Thanks a lot!

Can you show a screenshot of the connections in the material using that BitMask to select different colors? I can’t seem to figure it out how to use the SceneTexture: CustomStencil to select them.

Hi Celito this is how you use the bitmask. Sorry for the delayed response, I wasn’t able to test this until today. Hope it helps:

Replace the 255 constant with the number of stencil you want to mask. Cheers!

1 Like

Thank you very much Bariudol. That helped me a lot.

RyanB, is there a way to allow the custom stenciled element to be occluded normally, in the case that we want post processing for a given mesh yet don’t want it to render on top of everything else?

You can use an if node to check if custom depth is greater or less than scene depth. If it is greater, plug a 0, if it is less than or equal plug in a 1 and then use that as your mask.

I couldn’t find any result image using Custom Stencil on internet, so I tried it.
And I don’t think you have to use BitMask node!

63019-customstencil04.jpg

1 Like

Brilliant, thanks for that, I was struggling to find out how to do exactly this!

I found Its doesnt work in UE4.2,doesnet it?