Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity, please stop hurting my brain... "redefinition / undeclared"

Discussion in 'Shaders' started by Ravel, Apr 2, 2018.

  1. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    598
    Okay, I came to a really weird bug while creating a custom shader.
    So what I wanted to achieve was to get a seprate uv set for the alpha channel, but it came to an end with a ping/pong'ing error

    Code (CSharp):
    1. Shader "Standard (Decal Blend)" {
    2.     Properties{
    3.         _Color("Color", Color) = (1,1,1,1)
    4.         _SpecColor("Specular Color", Color) = (.2,.2,.2,1)
    5.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    6.         [Enum(UV0,0,UV1,1,UV2,2,UV3,3)] _UVPrim("UV Set for Blend", Float) = 0
    7.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    8.         _SpecGlossMap("Specular", 2D) = "white" {}
    9.         _Metallic("Metallic", Range(0,1)) = 0.0
    10.     }
    11.     SubShader{
    12.         Tags{
    13.         "RenderType" = "Opaque"
    14.         "IgnoreProjector" = "True"
    15.         "Queue" = "Geometry+1"
    16.         "ForceNoShadowCasting" = "True" }
    17.         LOD 200
    18.         Offset -4, -4
    19.         CGPROGRAM
    20.         // Physically based Standard lighting model, and enable shadows on all light types
    21.         #pragma surface surf Standard fullforwardshadows decal:blend vertex:vert
    22.  
    23.         // Use shader model 3.0 target, to get nicer looking lighting
    24.         #pragma target 3.0
    25.         #pragma SHADER_API_XBOX360
    26.  
    27.         float4 _MainTex_ST; // error!
    28.  
    29.         sampler2D _MainTex;
    30.         sampler2D _SpecGlossMap;
    31.  
    32.         struct Input {
    33.             float2 uv_MainTex;
    34.             float2 uv_BlendTex;
    35.             fixed4 vertexColor;
    36.         };
    37.  
    38.         half _UVPrim;
    39.         half _Glossiness;
    40.         half _Metallic;
    41.         fixed4 _Color;
    42.  
    43.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    44.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    45.         // #pragma instancing_options assumeuniformscaling
    46.         UNITY_INSTANCING_CBUFFER_START(Props)
    47.         // put more per-instance properties here
    48.         UNITY_INSTANCING_CBUFFER_END
    49.  
    50.         void vert(inout appdata_full v, out Input o)
    51.         {
    52.             UNITY_INITIALIZE_OUTPUT(Input, o);
    53.             half2 buv = (_UVPrim == 0) ? v.texcoord : (_UVPrim == 1) ? v.texcoord1 : (_UVPrim == 2) ? v.texcoord2 : v.texcoord3;
    54.             o.uv_BlendTex = TRANSFORM_TEX(buv, _MainTex);
    55.             o.vertexColor = v.color;
    56.         }
    57.  
    58.         void surf(Input IN, inout SurfaceOutputStandard o) {
    59.  
    60.             // Albedo comes from a texture tinted by color
    61.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    62.             o.Albedo = c.rgb;
    63.  
    64.             // Metallic and smoothness come from slider variables
    65.             fixed4 g = tex2D(_SpecGlossMap, IN.uv_MainTex) * _SpecColor;
    66.             o.Metallic = g.rgb * _Metallic;
    67.             o.Smoothness = g.a * _Glossiness;
    68.  
    69.             // Alpha blend
    70.             fixed b = tex2D(_MainTex, IN.uv_BlendTex).a * _Color.a;
    71.             o.Alpha = b * IN.vertexColor.a;
    72.         }
    73.         ENDCG
    74.     }
    75.     FallBack "Diffuse"
    76. }
    77.  
    So I need to add a _MainTex_ST for it to work, but when I do, I get this error:
    Shader error in 'Standard (Decal Blend)': redefinition of '_MainTex_ST' at line 84 (on d3d11)
    and if I dont, I get this error:
    Shader error in 'Standard (Decal Blend)': undeclared identifier '_MainTex_ST' at line 54 (on d3d11)

    So whats the case here? Where is it redefined? I dont even have a 84'th line in the code lol...
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,080
    If you need to use TRANSFORM_TEX, then don't define your uv using the magic "uv_" macro. It should just be "float2 _MainTex;" in your Input struct.

    Also, you should make your UV selection switching be based on shader_feature switches instead of evaluating that constantly since it's a rigid change.

    You're also going to run out of interpolators on target 3.0 when using appdata_full and defining extra samplers while using a surface shader. So you'll either have to bump up to target 3.5 (which I'm not sure the 360 supports, but Unity doesn't support 360 either so...), or you'll have to manually define the input/output structs yourself and use vert/frag to limit what is being defined and reduce your interpolator count, but that will mean a lot more manual work. You could also just add "nodirlightmap" to your #pragma to get back a couple interpolators (would wouldn't want to try and run dynamic light mapping on the 360 anyway). Then your shader should work.
     
unityunity