Search Unity

Question This stencil shader won't compile

Discussion in 'Shaders' started by Meaningless-Trinket, Jan 14, 2022.

  1. Meaningless-Trinket

    Meaningless-Trinket

    Joined:
    Apr 25, 2020
    Posts:
    83
    When I build the shader will not compile and I don't know why.

    When I run the build the stencil write shader has missing game objects behind it.

    My build works correctly when I remove the shader.

    From ronja-tutorials.
    Code (CSharp):
    1. Shader "Tutorial/022_stencil_buffer/write"{
    2.     //show values to edit in inspector
    3.     Properties{
    4.         [IntRange] _StencilRef ("Stencil Reference Value", Range(0,255)) = 0
    5.     }
    6.  
    7.     SubShader{
    8.         //the material is completely non-transparent and is rendered at the same time as the other opaque geometry
    9.         Tags{ "RenderType"="Opaque" "Queue"="Geometry-1"}
    10.  
    11.         //stencil operation
    12.         Stencil{
    13.             Ref [_StencilRef]
    14.             Comp Always
    15.             Pass Replace
    16.         }
    17.  
    18.         Pass{
    19.             //don't draw color or depth
    20.             Blend Zero One
    21.             ZWrite Off
    22.  
    23.             CGPROGRAM
    24.             #include "UnityCG.cginc"
    25.  
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.  
    29.             struct appdata{
    30.                 float4 vertex : POSITION;
    31.             };
    32.  
    33.             struct v2f{
    34.                 float4 position : SV_POSITION;
    35.             };
    36.  
    37.             v2f vert(appdata v){
    38.                 v2f o;
    39.                 //calculate the position in clip space to render the object
    40.                 o.position = UnityObjectToClipPos(v.vertex);
    41.                 return o;
    42.             }
    43.  
    44.             fixed4 frag(v2f i) : SV_TARGET{
    45.                 return 0;
    46.             }
    47.  
    48.             ENDCG
    49.         }
    50.     }
    51. }
     
    Last edited: Jan 14, 2022
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    Please check the shader inspector, it should say, what the issue is and where.
     
  3. Meaningless-Trinket

    Meaningless-Trinket

    Joined:
    Apr 25, 2020
    Posts:
    83
    It compiles now, but build still has problems.

    When I test the shader with a FBX imported game map it does not work.

    The shader only works with quads.

    The FBX import has read/write enabled.

    There is a problem with this shader and the FBX file.

    Maybe this is a bug?
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    That's unlikely.

    This specific shader only writes to the stencil buffer, and explicitly disables writing to color and depth. I'm still not sure, what exactly you expect from it and what do you mean by "it works only with quads".
     
  5. Meaningless-Trinket

    Meaningless-Trinket

    Joined:
    Apr 25, 2020
    Posts:
    83
    The shader works with quads made in Unity.

    The FBX imported file is not working for some reason.