how to get custom depth stencil value?

I wanted Object No. 3 to be obscured by Object No. 2 and Object No. 3 to penetrate object No. 1 and be displayed. I managed to achieve this in viewport by using custom depth stencil value and custom depth stencil write mask.


but it was not stable. It would cause the mask result to change according to the distance.

Moreover, this way of obtaining the depth of the custom depth stencil value is still based on the screen, which doesn’t feel like the method I want.

Is there any way to obtain a complete custom mask selection in Unreal Engine?

Hi,
I hope I correctly interpret your request here.
You correctly chose the “Custom Depth Stencil Write Mask” to be bit masked, but the value you entered seems to indicate that you don’t know how this bit mask mode actually works.

How it works

The default stencil mode operates completely different to the modes with “ignore depth”.

You can choose 8 different bit masks.

  1. 00000001 “First bit (1), ignore depth”
  2. 00000010 “Second bit (2), ignore depth”
  3. 00000100 “Third bit (4), ignore depth”
  4. 00001000 “Fourth bit (4), ignore depth”
  5. 00010000 “Fifth bit (4), ignore depth”
  6. 00100000 “Sixth bit (4), ignore depth”
  7. 01000000 “Seventh bit (4), ignore depth”
  8. 10000000 “Eigth bit (4), ignore depth”

They only let you write at the place where the mask has a 1. All other places in the 8 bit integer will have no influence on the pixel value of the stencil buffer (scene texture).
Think of it like having 8 bool values, that can be set 1, but are 0 on default.

It is suggested to use the number in brackets as a sencil value.
So for example on “Third bit (4), ignore depth” you should use 4 as a stencil value.
4 in 8bit binray is
00000100, which is the same as your mask
00000100.
3 in 8bit binary is
00000011, but your mask is
00000100. So it would write 0.
That is a problematic situation, since you don’t control the order in which the objects write over the stencil buffer value. The order may vary from frame to frame, which would lead to flickering.

In the Unreal Editor viewport you should be able to see how the values add up as they overlap.

How to use it in the postprocess material

You can use the node setup, that converts the half precision float into an integer and so on from the article here

I would recommend using a custom node to copy-paste HLSL code into it.

  1. add custom node
  2. copy-paste this code into the node
    return (int)Stencil & (int)Bit;
  3. in the details panel of the custom node
    a) add a second input
    b) name the first input “Stencil”
    c) name the second input “Value”
  4. connect the output of a SceneTexture node (in Custom Stencil mode) into the “Stencil” input of the custom node
  5. connect a “constant” (scalar/float value) node to the “Value” input of the custom node

you can use the code provided in the article
return (float)((1 << ((int)Bit - 1)) == (int)Mask);
my code returns the same value, but works with a bit wise operation on integers (that’s the single & sign).