Search Unity

Image with greyscale shader showing out of Mask.

Discussion in 'Editor & General Support' started by Draikz, Apr 19, 2019.

  1. Draikz

    Draikz

    Joined:
    Dec 20, 2016
    Posts:
    12
    Hello guys. I'm having a problem with a shader for UI images.

    I'm using a scrolling view with a mask, and at some point, I want the images to be in a GreyScale (basically to show the player those units are still locked). I have no experience regarding shaders, so I google a GeryScale shader and implemented it. The problem is that, when I apply that GreyScale Shader to the UI images inside the scrolling view, they ignore the mask completely.

    Any one with experience in shaders could give me some hints about what's happening?



    Here the the current Shader I'm using:

    Code (csharp):
    1.  
    2.     // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    3.    
    4.     Shader "Sprites/GrayScale4"
    5.     {
    6.         Properties
    7.         {
    8.             [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    9.             _Color("Tint", Color) = (1,1,1,1)
    10.             [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
    11.             _EffectAmount("Effect Amount", Range(0, 1)) = 1.0
    12.         }
    13.    
    14.             SubShader
    15.             {
    16.                 Tags
    17.                 {
    18.                     "Queue" = "Transparent"
    19.                     "IgnoreProjector" = "True"
    20.                     "RenderType" = "Transparent"
    21.                     "PreviewType" = "Plane"
    22.                     "CanUseSpriteAtlas" = "True"
    23.                 }
    24.                
    25.                 Cull Off
    26.                 Lighting Off
    27.                 ZWrite Off
    28.                 Fog { Mode Off }
    29.                 Blend SrcAlpha OneMinusSrcAlpha
    30.    
    31.                 Pass
    32.                 {
    33.                 CGPROGRAM
    34.                     #pragma vertex vert
    35.                     #pragma fragment frag
    36.                     #pragma multi_compile DUMMY PIXELSNAP_ON
    37.                     #include "UnityCG.cginc"
    38.    
    39.                     struct appdata_t
    40.                     {
    41.                         float4 vertex   : POSITION;
    42.                         float4 color    : COLOR;
    43.                         float2 texcoord : TEXCOORD0;
    44.                     };
    45.    
    46.                     struct v2f
    47.                     {
    48.                         float4 vertex   : SV_POSITION;
    49.                         fixed4 color : COLOR;
    50.                         half2 texcoord  : TEXCOORD0;
    51.                     };
    52.    
    53.                     fixed4 _Color;
    54.    
    55.                     v2f vert(appdata_t IN)
    56.                     {
    57.                         v2f OUT;
    58.                         OUT.vertex = UnityObjectToClipPos(IN.vertex);
    59.                         OUT.texcoord = IN.texcoord;
    60.                         OUT.color = IN.color * _Color;
    61.                         #ifdef PIXELSNAP_ON
    62.                         OUT.vertex = UnityPixelSnap(OUT.vertex);
    63.                         #endif
    64.    
    65.                         return OUT;
    66.                     }
    67.    
    68.                     sampler2D _MainTex;
    69.                     uniform float _EffectAmount;
    70.    
    71.                     fixed4 frag(v2f IN) : COLOR
    72.                     {
    73.                         half4 texcol = tex2D(_MainTex, IN.texcoord);
    74.                         texcol.rgb = lerp(texcol.rgb, dot(texcol.rgb, float3(0.3, 0.59, 0.11)), _EffectAmount);
    75.                         texcol = texcol * IN.color;
    76.                         return texcol;
    77.                     }
    78.                 ENDCG
    79.                 }
    80.             }
    81.                 Fallback "Sprites/Default"
    82.     }
    83.  
     
  2. TheNenet

    TheNenet

    Joined:
    Nov 4, 2017
    Posts:
    5
    Same problem. Any idea?
     
  3. Mirmukhsin

    Mirmukhsin

    Joined:
    Feb 13, 2020
    Posts:
    2
    Same problem. Any idea?