Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Lost in URP stencil puzzle problem. Occlusion with invisible walls.

Discussion in 'Universal Render Pipeline' started by JuanGuzmanH, Oct 4, 2023.

  1. JuanGuzmanH

    JuanGuzmanH

    Joined:
    Feb 8, 2018
    Posts:
    70
    Hi All! I'm struggling with a stencil setup. I will try to explain as simple as possible my situation.

    I need to recreate the occlusion of invisible walls in Unity2021 URP Android VR.

    I reach a point where Im stuck with the stencils and I believe I'm close to solve but still there is something Im missing.

    As you can see in the image,
    - I achieved what I need, to have a ground occluded by invisible walls, so the walls are masking the ground just revealing the ground as you would have with normal walls.
    - I achieved also to have windows on the walls (holes) and the objects behind are only renderer through the window (see the red cube behind the window) and not through the invisible walls.

    My problem is with the gray cube on the picture. As you can see on the game screen, even if is not behind the wall, the cube is occluded by the wall mask.

    upload_2023-10-4_9-56-51.png

    Setup:

    I have two extra layers and two render features.

    Layers:
    - One for the masks called "Mask"
    - Another one for the objects to be occluded called "SeeThrough"

    This is my shader for the mask objects (walls):

    Code (CSharp):
    1. Shader "Custom/UnlitMask"
    2. {
    3.     Properties{}
    4.  
    5.     SubShader{
    6.  
    7.         Tags {
    8.             "RenderType" = "Opaque"
    9.         }
    10.  
    11.         Pass {
    12.             ZWrite Off
    13.         }
    14.     }
    15. }
    This is the settings of my RP beofre the render features
    upload_2023-10-4_10-19-46.png

    My first Render Feature is turning on the mask pixels like this:

    upload_2023-10-4_10-14-12.png

    And this is my second render feature to bring back the occluded geometry

    upload_2023-10-4_10-15-2.png

    This is my pipeline Settings:
    upload_2023-10-4_10-18-47.png


    On my desesperation, I believe I tried using brute force every single possibility:
    - before after rendering opaques, and may others
    - Compare functions
    - Pass, Fail and Z fail
    - Single Instanced Pass
    - Multiview / Multipass / Single Pass Instanced / Multipass
    - Depth buffer on/off

    Obbiously Im missing something in this stencil puzzle of options.

    I would appreciate a bit of help from somene with a clear undertanding about stencils or any bright mind with another alternative.

    Thanks!
     
  2. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    289
    It's really sad that the render objects feature / renderer in general is so weird to handle at times.

    I recommend the frame debugger and Window > analysis > rendering debugger to figure out better what happens exactly.

    What you can try first though. Set the depth priming to auto. Anything rendered through a render objects feature does not write depth in the scene view for me at all for example. (i.e. in your case that would be all the occluded geometry.. aka also the cube?

    Is the cube part of the second render feature or is is part of the regular render pass?

    Are you sure your custom shader works with the render feature without specifing anything about the stencil buffer? Maybe try an shadergraph built one just to test it out.
     
  3. JuanGuzmanH

    JuanGuzmanH

    Joined:
    Feb 8, 2018
    Posts:
    70
    Thankyou fleity. I will give a try to the frame debugger, good tip.
    The cube inside the room (gray) is in the same layer as the elements outside the room (cube red), otherwise it would be visible through the room invisible walls when you are in another room.

    Im not totally sure of what Im doing. I belive the stencil is set to 1 on my first render feature.

    But Im thinking now in splitting the render of the occluded geometry in two separate render features, one for objects outside (visible through the window) and another for objects inside the room.
     
  4. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    289
    That might be your issue, as I said with depth priming disabled I have a similar problem. At least in the scene view / editor. Try depth priming set to auto and check if it works better.
    But tbh there is something wrong here (on unity's side I believe) with the way the scene view is drawn and it should work with priming disabled too.

    Splitting might get you a bit more control sure, but as long as you need to rely on depth to work correctly you might need to use depth priming.