Search Unity

Are dynamic stencil passes possible yet?

Discussion in 'Universal Render Pipeline' started by Orangy-Tang, Oct 27, 2020.

  1. Orangy-Tang

    Orangy-Tang

    Joined:
    Jul 24, 2013
    Posts:
    39
    I have a portal renderer that uses the stencil buffer to mask out each portal when drawing the sector behind it. Since portals are used often to divide areas, it's typical for multiple portals to be visible at once (up to 8 at the moment) and often nested within each other. With the Standard pipeline this is easy to set up, everything has a custom (hand written) shader, which exposes the stencil test/read/write values. Every frame the visibility code finds the visible portals and sets up all the materials and stencil ids and queue values, which is straightforward in the Standard/built-in pipeline.

    However this seems rather difficult to port to the URP way of working.

    As far as I know:
    1. I can't set per-material stencil values, as hand written shaders aren't supported (or I'll have to constantly re-write them as the API changes).
    2. I can't use Shader Graph, as it doesn't support stencil.
    3. I can only set stencil values for URP 'passes' but these are fixed at design-time, and I can't change them at runtime as needed.
    4. I guess I could set up 8 fixed URP passes, but that means I'm rendering 8 passes all the time which sounds hilariously inefficient. (I suspect that it would need even more because each stencil pass would also need Stencil-write/Opaque/Alpha-tested/Transparent passes so might be looking at 32 passes, which I have no idea of the performance characteristics of)

    Documentation on all this seems really thin on the ground, with lots of outdated info from 2019 and 2018, so I'm *hoping* that something in the above list has changed.

    Thanks.
     
  2. Orangy-Tang

    Orangy-Tang

    Joined:
    Jul 24, 2013
    Posts:
    39
    Wow, 58 views and no reply? Guess this forum is pretty dead.

    After some more research my assumptions above are all correct and there's no progress towards fixing any of them. I guess that means I'll be sticking with the Standard pipeline for the future. :(
     
  3. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I am also making a research on this.
    My conclusion so far is that you can do everything like in the standard builtin pipeline, except that it is better not to use shadergraphs (if stencils are needed).

    You can get any standard urp shader source and add the stencil code to it.

    Using shadergraphs would incur extra draw calls to render the geometry once again to write or read stencils.
    You can do it by adding stencil overrides using RenderObjects, or material overrides adding stencil (colormask0, "ghost") materials, that only write or read from stencils.
    Then you can access these material properties or stencil override settings at runtime.
     
    Last edited: Apr 19, 2021
  4. AzurySimon

    AzurySimon

    Joined:
    Jul 16, 2018
    Posts:
    38
    Where did you get the idea that hand written shaders are not supported? I am using custom shaders with URP, some of them 2+ years old, never had problems with API changes.

    That is true unfortunately, documentation could be a lot better. Regarding shader code for URP I can fully recommend this.
     
    tomekkie2 likes this.