Search Unity

Question Finally got working stencil on HDRP Lit with light/shadows (See an objects through other objects)

Discussion in 'High Definition Render Pipeline' started by alexanderkudryavy, Nov 7, 2021.

  1. alexanderkudryavy

    alexanderkudryavy

    Joined:
    Sep 20, 2019
    Posts:
    13
    Hello, finally I got working stencil on HDRP Lit with light and shadows but this solution have one strange bug, maybe you can understand why it happening because I really dont know how fix this.

    upload_2021-11-7_8-28-29.png

    Here is repo with modificated lit shader: https://github.com/mirgorodalexander/Stencil-HDRP

    What I've done:
    - Remove string to got custom settings in shader
    Code (CSharp):
    1. CustomEditor "Rendering.HighDefinition.LitGUI"
    - Add this strings in properties section
    Code (CSharp):
    1. _StencilMask("Stencil Mask", Int) = 0
    2. _StencilWriteMask("Stencil Write Mask", Int) = 0
    3. [Enum(UnityEngine.Rendering.CompareFunction)] _Compare("Compare", Int) = 0
    4. [Enum(UnityEngine.Rendering.StencilOp)] _Pass("Pass", Int) = 0
    - In "GBuffer" pass replace Stencil with this
    Code (CSharp):
    1. Stencil
    2. {
    3.     WriteMask [_StencilWriteMask]
    4.     Ref [_StencilMask]
    5.     Comp [_Compare]
    6.     Pass [_Pass]
    7. }
    - In "MotionVectors" pass replace Stencil with this
    Code (CSharp):
    1. Stencil
    2. {
    3.     WriteMask [_StencilWriteMask]
    4.     Ref [_StencilMask]
    5.     Comp [_Compare]
    6.     Pass [_Pass]
    7. }
    Next I create two materials "Inside" and "Outside"
    Inside properties:
    upload_2021-11-7_8-25-38.png
    Outside propeties:
    upload_2021-11-7_8-25-55.png

    And that's it, it working, but have strange bug, at some positions of "Camera" and "Editor Camera" inside objects disappeard, I don't know why, maybe you can understand how fix this?

    Here is the BUG:
    upload_2021-11-7_8-34-50.gif
     
    pachermann likes this.
  2. Sisay

    Sisay

    Joined:
    Dec 6, 2012
    Posts:
    57
    maybe technical support unity will write something? Epic keeps running away ...
     
  3. sasha2371

    sasha2371

    Joined:
    Jul 20, 2022
    Posts:
    2
    I don't know why they blink, but objects may disappear if bounding box is outside of camera frustum
     
  4. sasha2371

    sasha2371

    Joined:
    Jul 20, 2022
    Posts:
    2
    I downloaded your project. initially shader didn't work so i added back custom setting and set surface type to transperent, then disable that line again and got shader to somewhat work, then i add you stencil code to "TransparentDepthPrepass" and "DepthOnly". Now it seems to work complitly fine, dont see blinking or any other bugs. Using unity 2022.1.8
     
  5. Erfan_SheikhHoseini

    Erfan_SheikhHoseini

    Joined:
    Feb 7, 2019
    Posts:
    32
    I Made a working shader with the help of your post and amplify shader editor.
    at first i also had the flickering issue but i fixed by increasing the render queue of the inside objects.

    i will attach the unity package here. or Github link.
    note that there are two shaders and the only difference is the "inside" shader does have higher render queue in shader because i could not figure out how to expose the render queue into material inspector.
     

    Attached Files:

    Last edited: Nov 29, 2022
  6. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Can you attach a screenshot of the pass setting in ASE?
    because this is what i got from my side
    upload_2023-1-26_20-26-30.png
     
  7. h1ggs

    h1ggs

    Joined:
    Dec 21, 2017
    Posts:
    14
    I'm trying to get this stencil to work on backfaces for a cross-section shader and PassBack Replace isn't working. It seems like no matter what I try, I can't get the stencil buffer to write the ref value to the backfacing pixels.