Search Unity

Why would using a Stencil stop my scrolling effect from working?

Discussion in 'Shaders' started by LukasO, Sep 22, 2016.

  1. LukasO

    LukasO

    Joined:
    May 23, 2013
    Posts:
    115
    Hi,

    I'm trying to do a pulse effect for some UI. I've had an effect working but then tried to adapt it so it'll work with a mask. However, when adding the stencil operations required, the effect stops. Can somebody explain why? The shader is below. I'm scrolling the UV's on line tex through a script on the other end and the uv's definitely still scroll.

    Code (CSharp):
    1. Shader "Unlit/GraphLinePulse"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData]
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.         _LineTex("LineTex", 2D) = "white" {}
    8.         _Color("Tint", Color) = (1,1,1,1)
    9.         _FullLineColor("FullLineCol", Color) = (1,1,1,1)
    10.  
    11.         _StencilComp("Stencil Comparison", Float) = 8
    12.         _Stencil("Stencil ID", Float) = 0
    13.         _StencilOp("Stencil Operation", Float) = 0
    14.         _StencilWriteMask("Stencil Write Mask", Float) = 255
    15.         _StencilReadMask("Stencil Read Mask", Float) = 255
    16.  
    17.         _ColorMask("Color Mask", Float) = 15
    18.         //_Color("Default", Color) = (0,0,0,1)
    19.     }
    20.     SubShader
    21.     {
    22.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    23.  
    24.         Stencil
    25.         {
    26.             Ref[_Stencil]
    27.             Comp[_StencilComp]
    28.             Pass[_StencilOp]
    29.             ReadMask[_StencilReadMask]
    30.             WriteMask[_StencilWriteMask]
    31.         }
    32.  
    33.             Blend SrcAlpha OneMinusSrcAlpha
    34.             Cull Off
    35.             Lighting Off
    36.             ZWrite Off
    37.             ColorMask[_ColorMask]
    38.         Pass
    39.         {
    40.             CGPROGRAM
    41.             #pragma vertex vert
    42.             #pragma fragment frag
    43.             // make fog work
    44.             #pragma multi_compile_fog
    45.            
    46.             #include "UnityCG.cginc"
    47.  
    48.             struct appdata
    49.             {
    50.                 float4 vertex : POSITION;
    51.                 float4 color    : COLOR;
    52.                 float2 uv : TEXCOORD0;
    53.             };
    54.  
    55.             struct v2f
    56.             {
    57.                 float2 uv : TEXCOORD0;
    58.                 float2 uvTwo : TEXCOORD1;
    59.                 fixed4 color : COLOR;
    60.                 float4 vertex : SV_POSITION;
    61.             };
    62.  
    63.             sampler2D _MainTex;
    64.             sampler2D _LineTex;
    65.             float4 _MainTex_ST;
    66.             float4 _LineTex_ST;
    67.             fixed4 _Color;
    68.             fixed4 _FullLineColor;
    69.            
    70.             v2f vert (appdata v)
    71.             {
    72.                 v2f o;
    73.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    74.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    75.                 o.color = v.color * _Color;
    76.                 o.uvTwo = TRANSFORM_TEX(v.uv, _LineTex);
    77.                 return o;
    78.             }
    79.  
    80.             fixed4 SampleSpriteTexture(float2 uv)
    81.             {
    82.                 fixed4 color = tex2D(_MainTex, uv);
    83.  
    84.                 return color;
    85.             }
    86.            
    87.             fixed4 frag (v2f i) : SV_Target
    88.             {
    89.                 fixed4 col = SampleSpriteTexture(i.uv) * i.color;
    90.  
    91.                 col.rgb *= col.a;
    92.  
    93.                 fixed4 textureCol = (col.a * tex2D(_LineTex, i.uvTwo));
    94.  
    95.                 col.rgb = lerp(_Color, _FullLineColor, textureCol.a) * col.a;
    96.  
    97.                 return col;
    98.             }
    99.             ENDCG
    100.         }
    101.     }
    102. }
    103.  
    Thanks