Search Unity

Mask Shader

Discussion in 'Shaders' started by Dark_Seth, Mar 19, 2019.

  1. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    Hi Guys. Can somebody help me turn this into a Sprite Mask.

    My Shader creates the black 8bit Dithering effect.


    Code (CSharp):
    1. //
    2.  
    3. Shader "Deon/8bitImage"
    4. {
    5.     Properties
    6.     {
    7.         _MainTex("", 2D) = "" {}
    8.         _DitherTex("", 2D) = "" {}
    9.         _Color0("Dark", Color) = (0, 0, 0)
    10.         _Color1("Light", Color) = (1, 1, 1)
    11.         [Gamma] _Opacity("", Range(0, 1)) = 1
    12.         _Scale ("Scale", Range(0, 10)) = 4
    13.        _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    14.     }
    15.  
    16.     CGINCLUDE
    17.  
    18.     #include "UnityCG.cginc"
    19.  
    20.     sampler2D _MainTex;
    21.     float2 _MainTex_TexelSize;
    22.  
    23.     sampler2D _DitherTex;
    24.     float2 _DitherTex_TexelSize;
    25.     fixed _Cutoff;
    26.     half _Scale;
    27.     half3 _Color0;
    28.     half3 _Color1;
    29.     half _Opacity;
    30.  
    31.     half4 frag(v2f_img i) : SV_Target
    32.     {
    33.         half4 source = tex2D(_MainTex, i.uv);
    34.  
    35.         // Dither pattern sample
    36.         float2 dither_uv = i.uv * _DitherTex_TexelSize;
    37.         dither_uv /= _MainTex_TexelSize * _Scale;
    38.         half dither = tex2D(_DitherTex, dither_uv).a + 0.5 / 256;
    39.        
    40.  
    41.         // Relative luminance in linear RGB space
    42.     #ifdef UNITY_COLORSPACE_GAMMA
    43.         half rlum = LinearRgbToLuminance(GammaToLinearSpace(saturate(source.rgb)));
    44.     #else
    45.         half rlum = LinearRgbToLuminance(source.rgb);
    46.     #endif
    47.  
    48.         // Blending
    49.         half3 rgb = rlum < dither ? _Color0 : _Color1;
    50.         return half4(lerp(source.rgb, rgb, _Opacity), source.a);
    51.         clip(source.a - _Cutoff);
    52.     }
    53.  
    54.     ENDCG
    55.  
    56.     SubShader
    57.     {
    58.         Pass
    59.         {
    60.             ZTest Always Cull Off ZWrite Off
    61.             CGPROGRAM
    62.             #pragma multi_compile _ UNITY_COLORSPACE_GAMMA
    63.             #pragma vertex vert_img
    64.             #pragma fragment frag
    65.             ENDCG
    66.         }
    67.     }
    68. }
    69.