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. Dismiss Notice

Undeclared Identifier

Discussion in 'Shaders' started by ExplosiveBarrels, Feb 3, 2019.

  1. ExplosiveBarrels

    ExplosiveBarrels

    Joined:
    Oct 18, 2016
    Posts:
    10
    Hello,
    I'm still just a beginner when it comes to shaders, but managed to modify a shaderforge texture to do what i wanted it to do - swap colours so that I could do old-school palette swapping on sprites. And it worked beautifully.
    But recently I wanted to do some modifications to it, and managed to break it... simply by adding more identifiers and trying to reference them.
    I was confused since I had the identifiers declared at the top of the shader, along with the ones that did work. So I tried renaming the existing identifiers that did work and suddenly those didn't work anymore either. I'm stumped.

    So here's the shader below, and the error is on line 98, undeclared identifier _CompareBG

    Code (CSharp):
    1.  
    2. Shader "NES_DualSprite" {
    3.     Properties {
    4.         [PerRendererData]_MainTex ("MainTex", 2D) = "white" {}
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _CompareBG ("Compare Background", Color) = (0.5,0.5,0.5,1)
    7.         _CompareBright ("Compare Bright", Color) = (0.5,0.5,0.5,1)
    8.         _CompareMid ("Compare Mid", Color) = (0.5,0.5,0.5,1)
    9.         _CompareDark ("Compare Dark", Color) = (0.5,0.5,0.5,1)
    10.         _ColorBG ("Color Background", Color) = (1,1,1,1)
    11.         _ColorBright ("Color Bright", Color) = (0.5,0.5,0.5,1)
    12.         _ColorMid ("Color Mid", Color) = (0,0,0,1)
    13.         _ColorDark ("Color Dark", Color) = (0,0,0,0)
    14.                
    15.         [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    16.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    17.         _Stencil ("Stencil ID", Float) = 0
    18.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    19.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    20.         _StencilComp ("Stencil Comparison", Float) = 8
    21.         _StencilOp ("Stencil Operation", Float) = 0
    22.         _StencilOpFail ("Stencil Fail Operation", Float) = 0
    23.         _StencilOpZFail ("Stencil Z-Fail Operation", Float) = 0
    24.     }
    25.     SubShader {
    26.         Tags {
    27.             "IgnoreProjector"="True"
    28.             "Queue"="Transparent"
    29.             "RenderType"="Transparent"
    30.             "CanUseSpriteAtlas"="True"
    31.             "PreviewType"="Plane"
    32.         }
    33.         Pass {
    34.             Name "FORWARD"
    35.             Tags {
    36.                 "LightMode"="ForwardBase"
    37.             }
    38.             Blend SrcAlpha OneMinusSrcAlpha
    39.             Cull Off
    40.             ZWrite Off
    41.            
    42.             Stencil {
    43.                 Ref [_Stencil]
    44.                 ReadMask [_StencilReadMask]
    45.                 WriteMask [_StencilWriteMask]
    46.                 Comp [_StencilComp]
    47.                 Pass [_StencilOp]
    48.                 Fail [_StencilOpFail]
    49.                 ZFail [_StencilOpZFail]
    50.             }
    51.             CGPROGRAM
    52.             #pragma vertex vert
    53.             #pragma fragment frag
    54.             #define UNITY_PASS_FORWARDBASE
    55.             #pragma multi_compile _ PIXELSNAP_ON
    56.             #include "UnityCG.cginc"
    57.             #pragma multi_compile_fwdbase
    58.             #pragma only_renderers d3d9 d3d11 glcore gles
    59.             #pragma target 3.0
    60.             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
    61.             uniform float4 _Color;
    62.             uniform float4 _ColorA;
    63.             uniform float4 _ColorB;
    64.             uniform float4 _ColorC;
    65.             uniform float4 _ColorD;
    66.             uniform float4 _CompareA;
    67.             uniform float4 _CompareB;
    68.             uniform float4 _CompareC;
    69.             uniform float4 _CompareD;
    70.             struct VertexInput {
    71.                 float4 vertex : POSITION;
    72.                 float2 texcoord0 : TEXCOORD0;
    73.                 float4 vertexColor : COLOR;
    74.             };
    75.             struct VertexOutput {
    76.                 float4 pos : SV_POSITION;
    77.                 float2 uv0 : TEXCOORD0;
    78.                 float4 vertexColor : COLOR;
    79.             };
    80.             VertexOutput vert (VertexInput v) {
    81.                 VertexOutput o = (VertexOutput)0;
    82.                 o.uv0 = v.texcoord0;
    83.                 o.vertexColor = v.vertexColor;
    84.                 o.pos = UnityObjectToClipPos( v.vertex );
    85.                 #ifdef PIXELSNAP_ON
    86.                     o.pos = UnityPixelSnap(o.pos);
    87.                 #endif
    88.                 return o;
    89.             }
    90.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    91.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    92.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    93.  
    94.  
    95.                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
    96.                 float3 original_RGB = (_MainTex_var.rgb*_Color.rgb*i.vertexColor.rgb); // RGB
    97.                
    98.                 float RGB_is_BG = ( step(original_RGB,_CompareBG.rgb) == step(_CompareBG.rgb,original_RGB) ? 1 : 0);
    99.                 float RGB_is_Bright = ( step(original_RGB,_CompareBright.rgb) == step(_CompareBright.rgb,original_RGB) ? 1 : 0);
    100.                 float RGB_is_Mid = ( step(original_RGB,_CompareMid.rgb) == step(_CompareMid.rgb,original_RGB) ? 1 : 0);
    101.                 float RGB_is_Dark = ( step(original_RGB,_CompareDark.rgb) == step(_CompareDark.rgb,original_RGB) ? 1 : 0);
    102.            
    103.                 float original_alpha = (_MainTex_var.a*_Color.a*i.vertexColor.a);
    104.                
    105.                 if (RGB_is_BG == 1) { return fixed4(_ColorBG.rgb, original_alpha); }
    106.                 if (RGB_is_Bright == 1) { return fixed4(_ColorBright.rgb, original_alpha); }
    107.                 if (RGB_is_Mid == 1) { return fixed4(_ColorMid.rgb, original_alpha); }
    108.                 if (RGB_is_Dark == 1) { return fixed4(_ColorDark.rgb, original_alpha); }
    109.            
    110.                 if (RGB_is_BG == 0 && RGB_is_Bright == 0 && RGB_is_Mid == 0 && RGB_is_Dark == 0) { return fixed4(_ColorD.rgb, 0); }
    111.                 return fixed4(original_RGB, original_alpha);
    112.             }
    113.             ENDCG
    114.         }
    115.         Pass {
    116.             Name "ShadowCaster"
    117.             Tags {
    118.                 "LightMode"="ShadowCaster"
    119.             }
    120.             Offset 1, 1
    121.             Cull Off
    122.            
    123.             CGPROGRAM
    124.             #pragma vertex vert
    125.             #pragma fragment frag
    126.             #define UNITY_PASS_SHADOWCASTER
    127.             #pragma multi_compile _ PIXELSNAP_ON
    128.             #include "UnityCG.cginc"
    129.             #include "Lighting.cginc"
    130.             #pragma fragmentoption ARB_precision_hint_fastest
    131.             #pragma multi_compile_shadowcaster
    132.             #pragma only_renderers d3d9 d3d11 glcore gles
    133.             #pragma target 3.0
    134.             struct VertexInput {
    135.                 float4 vertex : POSITION;
    136.             };
    137.             struct VertexOutput {
    138.                 V2F_SHADOW_CASTER;
    139.             };
    140.             VertexOutput vert (VertexInput v) {
    141.                 VertexOutput o = (VertexOutput)0;
    142.                 o.pos = UnityObjectToClipPos( v.vertex );
    143.                 #ifdef PIXELSNAP_ON
    144.                     o.pos = UnityPixelSnap(o.pos);
    145.                 #endif
    146.                 TRANSFER_SHADOW_CASTER(o)
    147.                 return o;
    148.             }
    149.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    150.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    151.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    152.                 SHADOW_CASTER_FRAGMENT(i)
    153.             }
    154.             ENDCG
    155.         }
    156.     }
    157.     FallBack "Diffuse"
    158. }
    159.  
     
  2. hjohnsen

    hjohnsen

    Joined:
    Feb 4, 2018
    Posts:
    67
    You need to add a declaration near the other uniforms.
    uniform float4 _CompareBG;
     
    ExplosiveBarrels likes this.
  3. ExplosiveBarrels

    ExplosiveBarrels

    Joined:
    Oct 18, 2016
    Posts:
    10
    I feel dumb now, I kept on scrolling past that area and never looked at it.
    Thank you!