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.

Bug Stencil only visible in left eye and offset in VR, URP

Discussion in 'Shaders' started by unity_AE5Cgcm4bzSRTg, Dec 12, 2022.

  1. unity_AE5Cgcm4bzSRTg


    Aug 13, 2019

    I've been having issues regarding the use of stencil in VR using the URP.

    I've setup a simple stencil using code in custom shaders and the whole thing works fine in normal game view without VR. However when I put on the headset, the stencil seems to shift and get squashed like the entire mask tried to fit into the left eye, leaving the right eye with no mask at all.

    Here is an image of it in the Scene View :


    Here is the same model in VR view of the Left Eye :


    For visualization purposes, here's another image approximatively outlining the different visuals (Green being the current masked plane, Red the current stencil mask and Yellow the place where the mask should be) :


    The mask does shift around when I look at it from different angles.

    Now I did try the usual approach of just not using shader code to handle the Stencil, instead using the Renderer Settings the URP provides. It does work perfectly except that I need the full customizable range of the coded stencil for the case where I can get multiple of these "cups" close to each other and I don't want the plane of lets say Cup1 to show inside Cup2 because they are both next to each other.

    To make that work I need the Read/Write specific attributes where I can set up more values for each cup masks and planes, not the Layer based solution URP proposes since I can't get enough layers for this to work.

    Is there a way to make regular coded stencil work in VR using the URP?

    I've been using Unity 2022.1.20f1 and the 13.1.8 version of the URP.

    Here's the code I've been using for the mask :

    Code (CSharp):
    1. Shader "Custom/Stencil/Mask"
    2. {
    3.     Properties
    4.     {
    5.         [Header(Stencil)]
    6.         _StencilRef ("Stencil Ref", Int) = 0
    7.         _StencilRead ("Stencil Read", Int) = 0
    8.         _StencilWrite ("Stencil Write", Int) = 0
    9.         [Enum(UnityEngine.Rendering.CompareFunction)]_StencilComp ("Stencil Comp", Float) = 0
    10.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilPass ("Stencil Pass", Float) = 0
    11.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilFail ("Stencil Fail", Float) = 0
    12.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilZFail ("Stencil ZFail", Float) = 0
    13.     }
    14.     SubShader{          
    15.         Tags { "RenderType" = "Opaque"}
    16.         ZWrite Off
    18.         Pass
    19.         {
    20.             Name "Stencil Mask"
    22.             Stencil
    23.             {
    24.                 Ref [_StencilRef]
    25.                 ReadMask  [_StencilRead]
    26.                 WriteMask [_StencilWrite]
    27.                 Comp [_StencilComp]
    28.                 Pass [_StencilPass]
    29.                 Fail [_StencilFail]
    30.                 ZFail [_StencilZFail]
    31.             }
    32.         }
    33.     }
    34. }
    Sorry about the long post. Also thank you for reading!
    Last edited: Dec 12, 2022
  2. unity_AE5Cgcm4bzSRTg


    Aug 13, 2019
    Well, I cleaned up my shaders since I didn't need that many properties and tags as well as reassign the render pipeline since my Unity kept un-assigning it and the bug went away. I even tried reverting my version control and it still wasn't present. I'm not sure how it even happened in the first place but it somehow fixed itself. I'll leave it as a bug since it still happened but I'm not sure how to even help figure it out since I can't repro it.