Search Unity

Surface Shader Help

Discussion in 'Shaders' started by m506, Mar 24, 2019.

  1. m506

    m506

    Joined:
    Dec 21, 2015
    Posts:
    93
    Hi,

    I am trying to compile a simple surface shader and I am getting this error:
    "Shader error in 'Custom/TestShader': redefinition of '_MainTex_ST' at line 52 (on d3d11)".

    Code below:

    Code (CSharp):
    1. Shader "Custom/TestShader"
    2. {
    3.     Properties{
    4.       _MainTex("Texture", 2D) = "white" {}
    5.     }
    6.  
    7.         SubShader{
    8.             Tags { "RenderType" = "Opaque" }
    9.  
    10.             CGPROGRAM
    11.  
    12.             #pragma target 3.0
    13.             #pragma surface surf Standard
    14.  
    15.             #include "UnityStandardInput.cginc"
    16.             //sampler2D _MainTex;
    17.  
    18.             struct Input {
    19.                 float2 uv_MainTex;
    20.             };
    21.  
    22.             void surf(Input IN, inout SurfaceOutputStandard o) {
    23.                 o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    24.             }
    25.  
    26.         ENDCG
    27.     }
    28. }
    I know I could declare the variable _MainTex but I really need to use "UnityStandardInput.cginc" (this is a simplified shader of a bigger one I am working atm). Is there a workaround for that or this is how it should behave?

    Thanks for your time
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    What do you need UnityStandardInput.cginc for? The two aren’t really compatible because Surface Shaders automatically add the _ST variables for texture UVs defined in the Input struct, and the UnityStandardInput.cginc also has it, hence the error.

    There are some solutions I can think of though.

    Brute force
    Copy whatever code you need from the cginc into your shader.

    Custom UVs
    Don’t use “uv_MainTex”, instead use a custom named value like “tex_MainTex” or “Fred”, really anything that doesn’t start with “uv_” as that’s what causes the surface shader to add the _ST uniform. Then you have to set that value with a vertex function.
    Code (csharp):
    1. #pragma surface ... vertex:vert
    2.  
    3. struct Input {
    4.   float2 Fred;
    5. };
    6.  
    7. void vert (inout appdata_full v, out Input o) {
    8.   UNITY_INITIALIZE_OUTPUT(Input,o);
    9.   o.Fred = TRANSFORM_TEX(v.texcoord, _MainTex);
    10. }
    #undef
    I don’t know if this one will work, but you might be able to use #undef _MainTex_ST either before or after the #include to make it work. It’s not really how #undef is intended to be used, but it’s worth a try.
     
  3. m506

    m506

    Joined:
    Dec 21, 2015
    Posts:
    93
    Hi, renaming uv_MainTex to something else did the job. Thanks!