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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question HDRP Stencil Buffer

Discussion in 'High Definition Render Pipeline' started by Joao_Valentini, Sep 6, 2021.

  1. Joao_Valentini

    Joao_Valentini

    Joined:
    Nov 22, 2018
    Posts:
    5
    Hey! Im trying to create holes in walls using the stencil buffer but im not experienced with shaders. I found some shaders online to write and read from the stencil buffer in the hdrp

    The write stencil code:
    Code (CSharp):
    1. Shader "Custom/StencilWriteShader"
    2. {
    3.     Properties
    4.     {
    5.         _StencilMask("Stencil Mask", Int) = 0
    6.     }
    7.  
    8.     HLSLINCLUDE
    9.  
    10.     #pragma target 4.5
    11.     #pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
    12.     #pragma multi_compile_instancing
    13.  
    14.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    15.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
    16.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl"
    17.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl"
    18.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitProperties.hlsl"
    19.  
    20.     ENDHLSL
    21.  
    22.     SubShader
    23.     {
    24.         Tags{ "RenderPipeline" = "HDRenderPipeline"
    25.         "RenderType" = "HDUnlitShader"
    26.         "Queue"="Geometry+1"
    27.          }
    28.  
    29.         Pass
    30.         {
    31.             Name "Forward Unlit"
    32.             Tags { "LightMode" = "ForwardOnly" }
    33.  
    34.             ZWrite Off
    35.             ColorMask 0
    36.  
    37.             Stencil
    38.             {
    39.                 WriteMask [_StencilMask]
    40.                 Ref [_StencilMask]
    41.                 Comp Always
    42.                 Pass Replace
    43.             }
    44.  
    45.             HLSLPROGRAM
    46.  
    47.             #pragma multi_compile _ DEBUG_DISPLAY
    48.  
    49.             #ifdef DEBUG_DISPLAY
    50.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl"
    51.             #endif
    52.  
    53.             #define SHADERPASS SHADERPASS_FORWARD_UNLIT
    54.  
    55.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
    56.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.hlsl"
    57.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/ShaderPass/UnlitSharePass.hlsl"
    58.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitData.hlsl"
    59.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassForwardUnlit.hlsl"
    60.  
    61.             #pragma vertex Vert
    62.             #pragma fragment Frag
    63.  
    64.             ENDHLSL
    65.         }
    66.     }
    67. }

    The read code is the same the only changes are the render queue, the stencil part and the Color Mask
    Code (CSharp):
    1. "Queue"="Geometry+2"
    2.      ZWrite On
    3.      ColorMask RGB
    4.      Stencil
    5.             {
    6.                 Ref [_StencilMask]
    7.                 Comp [_Compare]
    8.             }
    It works fine:
    upload_2021-9-6_15-18-13.png

    But I need the wall material to be a normal HDRP Lit with textures and etc... I copied an empty shader graph code and inserted the stencil part from the read code but it didnt work, its a massive 9k line code and I dont know where to edit it to make it work.

    If anyone knows how to change the stencil buffer on the default Lit shader, I need some help

    Thanks!
     
  2. alexanderkudryavy

    alexanderkudryavy

    Joined:
    Sep 20, 2019
    Posts:
    13