Search Unity

Texture + Outline + Stencil Buffer, outline color overwrites texture

Discussion in 'Shaders' started by kittik, Jul 3, 2019.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Hi, I am not great with Shaders, but using tutorials and principles I have learned, I have created a shader which gives an outline and allows for a texture. I want to then add a Stencil test, which works successfully - however, I am not sure why the teture isn't visible from the outside of the Stencil buffers view.

    When the Stencil Test is Equal
    upload_2019-7-3_20-20-0.png


    When the Stencil Test is Not Equal
    upload_2019-7-3_20-22-40.png

    The desired effect would be that in both images, it resembles the first image. The Stencil Buffer is used to prevent objects being visible outside of a portal, which works effectively.

    My shader is as follows -
    Code (CSharp):
    1.     Properties
    2.     {
    3.         _MainTex ("Texture", 2D) = "white" {}
    4.         _OutlineColor("Outline color", Color) = (0,0,0,1)
    5.         _OutlineWidth("Outline width", Range(1.03, 2)) = 1.03
    6.  
    7.         [Enum(Equal,3,NotEqual,6)] _StencilTest ("Stencil Test", int) = 6
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "Queue"="Transparent" }
    12.         LOD 100
    13.  
    14.         Stencil
    15.         {
    16.             Ref 1
    17.             Comp [_StencilTest]
    18.         }
    19.  
    20.         Pass
    21.         {
    22.             Cull Off
    23.             ZWrite Off
    24.  
    25.             ZTest Always
    26.  
    27.             CGPROGRAM
    28.             #pragma vertex vert
    29.             #pragma fragment frag
    30.  
    31.             #include "UnityCG.cginc"
    32.  
    33.             struct appdata
    34.             {
    35.                 float4 vertex : POSITION;
    36.             };
    37.  
    38.             struct v2f
    39.             {
    40.                 float4 vertex : SV_POSITION;
    41.             };
    42.  
    43.             float _OutlineWidth;
    44.             float4 _OutlineColor;
    45.  
    46.             v2f vert (appdata v)
    47.             {
    48.                 v.vertex.xzy *= _OutlineWidth;
    49.  
    50.                 v2f o;
    51.                 o.vertex = UnityObjectToClipPos(v.vertex);
    52.  
    53.                 return o;
    54.             }
    55.            
    56.             fixed4 frag (v2f i) : SV_Target
    57.             {
    58.                 return _OutlineColor;
    59.             }
    60.  
    61.             ENDCG
    62.         }
    63.  
    64.         Pass
    65.         {
    66.             CGPROGRAM
    67.             #pragma vertex vert
    68.             #pragma fragment frag
    69.  
    70.             #include "UnityCG.cginc"
    71.  
    72.             struct appdata
    73.             {
    74.                 float4 vertex : POSITION;
    75.                 float2 uv : TEXCOORD0;
    76.             };
    77.  
    78.             struct v2f
    79.             {
    80.                 float2 uv : TEXCOORD0;
    81.                 float4 vertex : SV_POSITION;
    82.             };
    83.  
    84.             sampler2D _MainTex;
    85.             float4 _MainTex_ST;
    86.            
    87.             v2f vert (appdata v)
    88.             {
    89.                 v2f o;
    90.                 o.vertex = UnityObjectToClipPos(v.vertex);
    91.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    92.  
    93.                 return o;
    94.             }
    95.            
    96.             fixed4 frag (v2f i) : SV_Target
    97.             {
    98.                 // sample the texture
    99.                 fixed4 col = tex2D(_MainTex, i.uv);
    100.  
    101.                 return col;
    102.             }
    103.             ENDCG
    104.         }
    105.     }
    106. }
    Thanks for any help.