Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Did stencil buffers change in 5.6?

Discussion in 'Shaders' started by smetzzz, Apr 11, 2017.

  1. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
    I'm using a stencil buffer on a sprite. After I upgraded my sprite show very transparent and blocky. I am pretty much a noob at graphics programming and curious if some API was changed. Thanks for any insight.

    Code (csharp):
    1.  
    2. Shader "PortalShader/PortalSPRITE"
    3. {
    4.     Properties
    5.     {
    6.         _StencilMask("PORTAL REFERENCE", Int) = 0
    7.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    8.         _Color ("Tint", Color) = (1,1,1,1)
    9.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags
    15.         {
    16.             "Queue"="Transparent"
    17.             "IgnoreProjector"="True"
    18.             "RenderType"="Transparent"
    19.             "PreviewType"="Plane"
    20.             "CanUseSpriteAtlas"="True"
    21.         }
    22.  
    23.         Cull Off
    24.         Lighting Off
    25.         ZWrite Off
    26.         Blend One OneMinusSrcAlpha
    27. Stencil {
    28.             Ref[_StencilMask]
    29.             Comp Equal
    30.             //Pass keep
    31.          
    32.         }
    33.         Pass
    34.         {
    35.         CGPROGRAM
    36.             #pragma vertex vert
    37.             #pragma fragment frag
    38.             #pragma target 2.0
    39.             #pragma multi_compile _ PIXELSNAP_ON
    40.             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    41.             #include "UnityCG.cginc"
    42.          
    43.             struct appdata_t
    44.             {
    45.                 float4 vertex   : POSITION;
    46.                 float4 color    : COLOR;
    47.                 float2 texcoord : TEXCOORD0;
    48.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    49.             };
    50.  
    51.             struct v2f
    52.             {
    53.                 float4 vertex   : SV_POSITION;
    54.                 fixed4 color    : COLOR;
    55.                 float2 texcoord  : TEXCOORD0;
    56.                 UNITY_VERTEX_OUTPUT_STEREO
    57.             };
    58.          
    59.             fixed4 _Color;
    60.  
    61.             v2f vert(appdata_t IN)
    62.             {
    63.                 v2f OUT;
    64.                 UNITY_SETUP_INSTANCE_ID(IN);
    65.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    66.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    67.                 OUT.texcoord = IN.texcoord;
    68.                 OUT.color = IN.color * _Color;
    69.                 #ifdef PIXELSNAP_ON
    70.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    71.                 #endif
    72.  
    73.                 return OUT;
    74.             }
    75.  
    76.             sampler2D _MainTex;
    77.             sampler2D _AlphaTex;
    78.  
    79.             fixed4 SampleSpriteTexture (float2 uv)
    80.             {
    81.                 fixed4 color = tex2D (_MainTex, uv);
    82.  
    83. #if ETC1_EXTERNAL_ALPHA
    84.                 // get the color from an external texture (usecase: Alpha support for ETC1 on android)
    85.                 color.a = tex2D (_AlphaTex, uv).r;
    86. #endif //ETC1_EXTERNAL_ALPHA
    87.  
    88.                 return color;
    89.             }
    90.  
    91.             fixed4 frag(v2f IN) : SV_Target
    92.             {
    93.                 fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
    94.                 c.rgb *= c.a;
    95.                 return c;
    96.             }
    97.         ENDCG
    98.         }
    99.     }
    100. }
    101.  
    102.  
     
  2. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
unityunity