Search Unity

Resolved Occlusion of gameobjects based on Texture2D

Discussion in 'AR' started by avinnus, Jun 8, 2021.

  1. avinnus

    avinnus

    Joined:
    Apr 10, 2021
    Posts:
    3
    Hi, I am trying to implement far-distance gameobject occlusion in an AR app. For this, I have managed to incorporate a real-time semantic segmentation cnn using Barracuda and convert the output tensor to a RGBA32 Texture2D (or RenderTexture).
    Based on this information I now want to occlude certain parts of the gameobject using a shader.
    I have tried OnRenderImage(), however this occludes not only the gameobjects but also the camera input which is supposed to stay unchanged.
    Do you have any ideas on how to only occlude the gameobjects?
     
  2. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    You could potentially use a Stencil for your occlusion-texture (write to a stencil-buffer), then read those values in the shader of your objects to clip any pixels where values are in the stencil?
     
  3. avinnus

    avinnus

    Joined:
    Apr 10, 2021
    Posts:
    3
    Thanks a lot, I think this might be the solution for my issue! However, I am having difficulties in writing the occlusion-texture into the stencil-buffer. I looked into stencils and managed to write a single value into the buffer like this:

    Code (CSharp):
    1. Properties{
    2.     [IntRange] _StencilMask("Stencil Mask", Range(0, 255)) = 0
    3. }
    4. SubShader{
    5.     Stencil {
    6.         Ref[_StencilMask]
    7.         Comp always
    8.         Pass replace
    9.     }
    10. }
    I also imported my Texture2D into the shader. But now I want to write two different values in the stencil-buffer based on something like an if-condition on the values in my Texture2D. Could you please specify how to accomplish this?
     
  4. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    You cannot write two different values into 1 buffer.
    However, the stencil-map stores an 8-bit integer.
    e.g.:
    integer 1 = 00000001
    integer 2 = 00000010
    integer 3 = 00000011
    So 'writing' a 3 into the stencil would allow you to 'read' both a 1 and a 2
    You can thus use bit-masks to check for different values (using readMask/writeMask).