Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Undeclared identifier Error in desaturate shader

Discussion in 'Shaders' started by unity_WMNne1P3fyWTnQ, Jul 31, 2020.

  1. unity_WMNne1P3fyWTnQ

    unity_WMNne1P3fyWTnQ

    Joined:
    Jul 15, 2019
    Posts:
    2
    Hello community!
    I am very new to shader and google did not help me this time.
    Is anyone here can point me into the problem please?
    so here is the shader
    I get an Undeclared identifier "_MainTex" at line 103 (on metal)
    Code (CSharp):
    1. Shader "Sprites/Desaturate"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         _Saturate ("Saturate", Range (0, 1)) = 1
    8.         _Bright ("Brightness", Float) = 0
    9.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    10.        
    11.         // required for UI.Mask
    12.         _StencilComp ("Stencil Comparison", Float) = 8
    13.         _Stencil ("Stencil ID", Float) = 0
    14.         _StencilOp ("Stencil Operation", Float) = 0
    15.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    16.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    17.         _ColorMask ("Color Mask", Float) = 15
    18.  
    19.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    20.     }
    21.  
    22.     SubShader
    23.     {
    24.         Tags
    25.         {
    26.             "Queue"="Transparent"
    27.             "IgnoreProjector"="True"
    28.             "RenderType"="Transparent"
    29.             "PreviewType"="Plane"
    30.             "CanUseSpriteAtlas"="True"
    31.         }
    32.                 Stencil
    33.         {
    34.             Ref [_Stencil]
    35.             Comp [_StencilComp]
    36.             Pass [_StencilOp]
    37.             ReadMask [_StencilReadMask]
    38.             WriteMask [_StencilWriteMask]
    39.          }
    40.  
    41.         Cull Off
    42.         Lighting Off
    43.         ZWrite Off
    44.         Fog { Mode Off }
    45.         Blend One OneMinusSrcAlpha
    46.  
    47.         Pass
    48.         {
    49. //            Stencil {
    50. //                Ref 2
    51. //                Comp Less
    52. //                Pass keep
    53. //                Fail decrWrap
    54. //                ZFail keep
    55. //            }
    56.         CGPROGRAM
    57.             #pragma vertex vert
    58.             #pragma fragment frag
    59.             #pragma multi_compile DUMMY PIXELSNAP_ON
    60.             #include "UnityCG.cginc"
    61.            
    62.             struct appdata_t
    63.             {
    64.                 float4 vertex   : POSITION;
    65.                 float4 color    : COLOR;
    66. //                float saturation: FLOAT;
    67.                 float2 texcoord : TEXCOORD0;
    68.             };
    69.  
    70.             struct v2f
    71.             {
    72.                 float4 vertex   : SV_POSITION;
    73.                 fixed4 color    : COLOR;
    74.                 float saturation: FLOAT;
    75.                 fixed brightness: FIXED;
    76.                 half2 texcoord  : TEXCOORD0;
    77.             };
    78.            
    79.             fixed4 _Color;
    80.             fixed _Saturate;
    81.             fixed _Bright;
    82.            
    83.             v2f vert(appdata_t IN)
    84.             {
    85.                 v2f OUT;
    86.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    87.                 OUT.texcoord = IN.texcoord;
    88.                 OUT.color = IN.color * _Color;
    89.                 OUT.saturation = _Saturate;
    90.                 OUT.brightness = _Bright;
    91.                 #ifdef PIXELSNAP_ON
    92.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    93.                 #endif
    94.  
    95.                 return OUT;
    96.             }
    97.  
    98.  
    99.             fixed4 frag(v2f IN) : SV_Target
    100.             {
    101.                 float luminance = (tex2D(_MainTex, IN.texcoord).r + tex2D(_MainTex, IN.texcoord).g + tex2D(_MainTex, IN.texcoord).b)/3.0;
    102.                 fixed4 colorSature = fixed4(lerp(luminance, tex2D(_MainTex, IN.texcoord).r, IN.saturation) + IN.brightness,    lerp(luminance, tex2D(_MainTex, IN.texcoord).g, IN.saturation) + IN.brightness, lerp(luminance, tex2D(_MainTex, IN.texcoord).b, IN.saturation) + IN.brightness, tex2D(_MainTex, IN.texcoord).a);
    103.                 fixed4 c = colorSature * IN.color;
    104.                 c.rgb *= c.a;
    105.                 return c;
    106.             }
    107.         ENDCG
    108.         }
    109.     }
    110. }
    111.  
     
  2. unity_WMNne1P3fyWTnQ

    unity_WMNne1P3fyWTnQ

    Joined:
    Jul 15, 2019
    Posts:
    2
    Hi guys I figured it out

    it misses a line name of the variable in the pass of shader
    sampler2D _MainTex;
     
    Joness likes this.
  3. Joness

    Joness

    Joined:
    Jun 25, 2020
    Posts:
    2
    Last edited: May 3, 2021