Depth occlusion with custom stencil for post processing causes flickering

I am trying to create occlusion on objects with a custom stencil for use with post processing.
With this I am trying to get a post processing effects (for instance a different colour tint) on an object, but not have it rendered on parts that are behind another object.

After searching for answers I found some methods of achieving this through “SceneDepth” and “CustomDepth”.
However with each method I keep getting a lot of flickering on the objects with the “Render CustomDepth Pass” enabled.

StencilSetup01

This is the first method I found, and it causes a lot of flickering.
If I change the “Ceil” node with a “Floor” node I get a bit less flickering.
Hard to display the flickering in a static image, but it is definitly there.

So I figured maybe its because the depth value of “CustomDepth” and “SceneDepth” are nearly (or exactly) the same on the affected object.
Going with that I tried an “If” node with a slightly larger threshold.
Still flickering objects.

I came across some more setups to make it happen, including this last one.
It still flickers, but weird enough the person who posted this had no issue with flickering.
In fact none of the solutions I came across have flickering in their posted results.

So I guess maybe there is some other setting that makes it flicker so much for me, instead of the nodes in my Post Process Material?

Do these solutions cause flickering for you?
Would you happen to know a possible solution?

(Using UE 5.3.2-29314046 with Engine Scalability Settings on Epic; “Blendable Location” set to “After Tonemapping”)
(I have the latest driver downloaded from the NVIDIA website for my “NVIDIA GeForce RTX 3080 Laptop GPU”)

This is what I have discovered so far.
Connecting “SceneDepth” or “CustomDepth” directly gives me a completely white screen.
Then I figured its because the depth values are all so large they just display a “1” (or more) meaning white pixel.

So I divide it by some number (I picked 4000) to make the gradient more visible.
Then I clamped it, so it will not have a value larger than 1.
This is because I think the distance to the skybox is really large and will return a value far larger then 1, causing an “illuminating” effect on the whole scene.

So now the “SceneDepth” displays a nice gradient scene.
Using the same nodes, but now with the “CustomDepth” I also get a gradient scene, but this one has different shades, and its less foggy.

I tried the “Buffer Visualization” in the editor view, and it also gives a different gradient.
Does this mean the nodes “SceneDepth” and “CustomDepth” are using different values for the same distance?
I dont understand why that would be.

Then I tried the subtraction on these more visible values and it gave me a weird view with some light gradient across the scene.
But no flickering, so that got me thinking.
If this version does not cause flickering, could it be that the undivided raw depth view has numbers so large that it causes the simple subtraction to have rounding errors?
It would explain the flickering at least.

If so however, why am I the only one with this issue?
Could someone try the setup from the initial post (which works according to tutorials) and see if they have a different result?

(I did try it on a different computer with UE 5.1, and it had the same flickering.)

1 Like

Tried it again with the new 5.4.2 update.
Still flickering.
The flickering in the GIF below is a lot slower than the actual flickering in the editor.

Showgif

Still using a very simple post processing material:

Also still no clue where or what to look for, in order to fix this.

We use custom depth with stencil for selected object highlights.

Looking at settings they are similar to yours except we have “Custom Depth With Temporal AA Jitter” == true. Does that have any effect?

Thank you for the reply.

Yes, thank you for taking the time to help.
Setting it to true fixed it.

No more flickering.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.