Search Unity

Question Merging two simple grayscale shaders

Discussion in 'Shaders' started by FullenMelee, Jan 22, 2021.

  1. FullenMelee

    FullenMelee

    Joined:
    Jan 9, 2021
    Posts:
    1
    Hey everyone, sorry to bother you with what's likely something very dull to most of you, but I've never worked with shaders before and I'm not quite getting it.

    I need to apply a grayscale shader to an Image that's within a Mask. For this, I first got the following shader that worked exactly how I wanted.

    Code (CSharp):
    1. Shader "Sprites-GrayScale"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.         _GrayscaleAmount ("Grayscale Amount", Range (0, 1)) = 1.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             "CanUseSpriteAtlas"="True"
    19.         }
    20.         Cull Off
    21.         Lighting Off
    22.         ZWrite Off
    23.         Fog { Mode Off }
    24.         Blend SrcAlpha OneMinusSrcAlpha
    25.         Pass
    26.         {
    27.         CGPROGRAM
    28.             #pragma vertex vert
    29.             #pragma fragment frag
    30.             #pragma multi_compile DUMMY PIXELSNAP_ON
    31.             #include "UnityCG.cginc"
    32.          
    33.             struct appdata_t
    34.             {
    35.                 float4 vertex   : POSITION;
    36.                 float4 color    : COLOR;
    37.                 float2 texcoord : TEXCOORD0;
    38.             };
    39.             struct v2f
    40.             {
    41.                 float4 vertex   : SV_POSITION;
    42.                 fixed4 color    : COLOR;
    43.                 half2 texcoord  : TEXCOORD0;
    44.             };
    45.          
    46.             fixed4 _Color;
    47.             v2f vert(appdata_t IN)
    48.             {
    49.                 v2f OUT;
    50.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    51.                 OUT.texcoord = IN.texcoord;
    52.                 OUT.color = IN.color * _Color;
    53.                 #ifdef PIXELSNAP_ON
    54.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    55.                 #endif
    56.                 return OUT;
    57.             }
    58.             sampler2D _MainTex;
    59.             uniform float _GrayscaleAmount;
    60.             fixed4 frag(v2f IN) : COLOR
    61.             {
    62.                 half4 texcol = tex2D (_MainTex, IN.texcoord);            
    63.                 texcol.rgb = lerp(texcol.rgb, dot(texcol.rgb, float3(0.3, 0.59, 0.11)), _GrayscaleAmount);
    64.                 texcol = texcol * IN.color;
    65.                 return texcol;
    66.             }
    67.         ENDCG
    68.         }
    69.     }
    70.     Fallback "Sprites/Default"
    71. }
    But the if I apply this shader to my Image that's within a Mask, the Mask stops working and the full image is visible.

    So after some searching, I found this shader that works perfectly with my mask, but I can't change the "_GrayscaleAmount" like I could with the first shader.

    Code (CSharp):
    1.  
    2. Shader "Custom/UISprite" {
    3.     Properties
    4.     {
    5.        _MainTex("Base (RGB), Alpha (A)", 2D) = "white" {}
    6.  
    7.     // Mask cropped portion of the support
    8.     //Start
    9.     _StencilComp("Stencil Comparison", Float) = 8
    10.     _Stencil("Stencil ID", Float) = 0
    11.     _StencilOp("Stencil Operation", Float) = 0
    12.     _StencilWriteMask("Stencil Write Mask", Float) = 255
    13.     _StencilReadMask("Stencil Read Mask", Float) = 255
    14.     _ColorMask("Color Mask", Float) = 15
    15.         //End
    16.     }
    17.  
    18.         SubShader
    19.     {
    20.      LOD 200
    21.      Tags
    22.      {
    23.       "Queue" = "Transparent"
    24.       "IgnoreProjector" = "True"
    25.       "RenderType" = "Transparent"
    26.      }
    27.         // Mask cropped portion of the support
    28.    //Start
    29.     Stencil
    30.     {
    31.       Ref[_Stencil]
    32.       Comp[_StencilComp]
    33.       Pass[_StencilOp]
    34.       ReadMask[_StencilReadMask]
    35.       WriteMask[_StencilWriteMask]
    36.     }
    37.     ColorMask[_ColorMask]
    38.         //End
    39.         Pass
    40.         {
    41.          Cull Off
    42.          Lighting Off
    43.          ZWrite Off
    44.          Fog { Mode Off }
    45.          ColorMask RGB
    46.          AlphaTest Greater .01
    47.          Blend SrcAlpha OneMinusSrcAlpha
    48.          ColorMaterial AmbientAndDiffuse
    49.  
    50.          CGPROGRAM
    51.          #pragma vertex vert
    52.          #pragma fragment frag
    53.          #include "UnityCG.cginc"
    54.          sampler2D _MainTex;
    55.          struct appdata_t
    56.          {
    57.           float4 vertex : POSITION;
    58.           half4 color : COLOR;
    59.           float2 texcoord : TEXCOORD0;
    60.          };
    61.          struct v2f
    62.          {
    63.           float4 vertex : POSITION;
    64.           half4 color : COLOR;
    65.           float2 texcoord : TEXCOORD0;
    66.          };
    67.          v2f vert(appdata_t v)
    68.          {
    69.           v2f o;
    70.           o.vertex = UnityObjectToClipPos(v.vertex);
    71.           o.color = v.color;
    72.           o.texcoord = v.texcoord;
    73.           return o;
    74.          }
    75.          half4 frag(v2f IN) : COLOR
    76.          {
    77.              half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
    78.              float c = 0.299 * col.r + 0.587 * col.g + 0.184 * col.b;
    79.              col.r = col.g = col.b = c;
    80.              return col;
    81.             }
    82.             ENDCG
    83.            }
    84.     }
    85. }
    I tried adding the field myself but that's where I'm failing. Could someone more enlightened guide me to a solution that does both? (Respecting the mask boundaries and being adjustable through a Float)

    I greatly appreciate your time and kindness.