Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Help Wanted Setup stencil pass using RenderObjects

Discussion in 'Universal Render Pipeline' started by tomekkie2, Apr 14, 2021.

  1. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    687
    I would like to add some stencil overrides using RenderObjects in Universal Render Pipeline.

    Is there a way to setup PassFront or PassBack - instead of just Pass?
     
  2. CaptainKirby

    CaptainKirby

    Joined:
    Dec 12, 2011
    Posts:
    34
    Yes it is possible - but it takes a bit of work :)
    You can update RenderStateData to have the operations you wish to add(or make your own, as it is used by other things), then in RenderObjectPass.SetStencilState, you can add the operations you want:
    For instance to do the pass front, add stencilState.passOperationFront etc.

    They will however not show up unless you add them to the UI, by updating StencilStateDataDrawer.

    Additionally, keep in mind that updating Unity/URP could override your changes, so making a copy of the RenderObject feature and putting it into your project might be a solution as well.
    Hope it helped. If there is anything unclear, feel free to ask :)
     
    tomekkie2 likes this.
  3. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    687
    Thank you for the precious hint.
    I am just wondering if it is possible to have a MonoBehaviour script with variables for filter and stencil settings in the scene and create RenderObjects and add it to the ScriptableRendererData in OnEnable.
    This way it would not get destroyed in the course of updating urp.

    But how to setup StencilStateData?
    It has only the passOperation, neither passOperationFront nor passOperationBack fields.
    How to limit the passOperation to front or back faces?
    I would like to get an effect of PassFront or PassBack in the shader.
    Or analogically - stencilCompareFunction - neither stencilCompareFront nor stencilCompareBack field is available.
    How to limit the stencilCompareFunction to front or back faces and how to get an effect of CompFront or CompBack in the shader?
     
    Last edited: Apr 15, 2021
  4. Simon_E_Sorensen

    Simon_E_Sorensen

    Unity Technologies

    Joined:
    Feb 4, 2020
    Posts:
    5
    StencilStateData is just a settings class, basically. The actual operation happens in RenderObjectPass.SetStencilState.
    For example stencilState.passOperationFront(Your IDE should show you the available fields).
     
  5. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    687
    Thank you.
    But how to access RenderObjectsPass?
    Anyway is there any advantage of editing StencilState over adding a stencil Colormask 0 material instead?
    I have discovered that adding a stencil override incurs extra passes even if no material override is added, so possibly it makes no difference, and the best is to use a scripted shader with stencils encoded in case you need stencils.
    Am I right or wrong?
     
    Last edited: Apr 16, 2021
unityunity