[Bug] Custom Depth/Stencil doesn't work consistently when enabled from Blueprint

Summary: static meshes that should render to the CustomDepth and CustomStencil buffers fail to do so when they are occluded by other meshes. This ONLY occurs when said functionality is enabled from script rather than on the Static Mesh Component itself.

Apologies if this was already submitted but I couldn’t find anything. I ran into this when upgrading my game from 4.15 → 4.17.

Here’s a fresh sample project [(link)][2] with a 100% repro (20/20 attempts).

Steps:

  • Place a cube static mesh and make it
    a Blueprint with the following nodes:

  • Add a post process material that
    simply visualizes the CustomStencil
    buffer.
  • Run the game from the default camera
    position. The cube SHOULD appear as a
    white box onscreen because of the
    post process material, visible
    through the occluding wall mesh, but it
    doesn’t. If you strafe the camera around the wall
    so that the cube isn’t occluded
    anymore, it will suddenly be
    visualized by the postprocess.
  • Now go to the cube’s static mesh
    component and set “Render CustomDepth
    Pass”=True and set “CustomDepth
    Stencil Value”=1. Result: the cube
    will always appear in the postprocess, regardless of whether
    the mesh itself is occluded, as desired.

My game has a lot of meshes that are visualized through walls using the CustomStencil feature after certain scripted conditions are met, but unfortunately this bug totally breaks that functionality. It seems like I can work around it by turning on custom depth on all my meshes, turning it off in script, and then turning it back on again when I need it enabled, but that’s a pretty awkward workaround for the dozens of blueprints I need to do it for. Help would be appreciated, thanks!

Hello ,

Thank you for submitting a bug report. I have reproduced this issue and logged a report for it here

You can track the report’s status as the issue is reviewed by our development staff. Please be aware that this issue may not be prioritized or fixed soon.

I found workaround for this bug:

In c++ create function and call MarkRenderStateDirty() on your mesh component (after SetRenderCustomDepth()).

Example:

lMeshComponent->SetRenderCustomDepth(InEnable);
lMeshComponent->SetCustomDepthStencilValue(InStencilValue);

lMeshComponent->MarkRenderStateDirty();

I found out that in 4.15 MarkRenderStateDirty was called everytime in SetRenderCustomDepth(), but why epic changed that?

It sounds like a good solution! I tried… but it didn’t work.
In my case, I have several ProceduralMeshes and RenderCurstomDepth activated by bluprint works only with some of them.
I’m almost sure that is related to the fact that the ones that don’t work have translucid materiales. Is this true? Anyone experienced the same issue?

Yeah that’s true. Be sure the material has “Allow Custom Depth Writes” checked in the translucency advanced options.

I’ve recently ran into this issue as well. Strangely enough, any skeletal meshes or static meshes attached to any characters (autonomous or simulated) display their custom depth just fine.