Search Unity

Problems with Shader utilizing Uv2

Discussion in 'Editor & General Support' started by Kriechgebuesch, Sep 19, 2012.

  1. Kriechgebuesch

    Kriechgebuesch

    Joined:
    Nov 8, 2010
    Posts:
    7
    I'm currently trying to modify the Toony Gooch Shader to use the 2nd Uv layer coordinates (which are normally reserved for Unity Lightmapping) to multiply with the color returned from the Uv1 coordinates.. The line that's supposed to accomplish this is "o.Albedo = c.rgb * c2.rgb * _Color.rgb * IN.color.rgb * IN.color.a;". I also defined all the needed variables and encountered no problem until I actually tried multiplying "c2" in the above code. Now according to my (admittedly shaky) understanding of Shaders so far the code below should work. It appears though that the compiled shader creates unexpected errors namely:

    GLSL vertex shader: ERROR: 0:416: '_MainTexST' : redefinition at line 22 (which appears to be a comment in the compiled Shader?)
    Program 'frag_surf', declaration of "_MainTex_ST" conflicts with previous declaration at line 101
    Program 'vert_surf', declaration of "_MainTex_ST" conflicts with previous declaration at line 101

    Here the excerpt from line 101 of the compiled shader:
    Code (csharp):
    1. #ifndef LIGHTMAP_OFF
    2. struct v2f_surf {
    3.   float4 pos : SV_POSITION;
    4.   float4 pack0 : TEXCOORD0;
    5.   float3 viewDir : TEXCOORD1;
    6.   fixed4 color : COLOR0;
    7.   fixed3 normal : TEXCOORD2;
    8.   float2 lmap : TEXCOORD3;
    9.   LIGHTING_COORDS(4,5)
    10. };
    11. #endif
    And here the uncompiled shader as is:

    Code (csharp):
    1. Shader "Uv2"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.    
    7.         _MainTex ("Base (RGB)", 2D) = "white" {}
    8.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    9.        
    10.         _RimColor ("Rim Color", Color) = (0.8,0.8,0.8,0.6)
    11.         _RimPower ("Rim Power", Float) = 1.4
    12.        
    13.         _SColor ("Shadow Color", Color) = (0.0,0.0,0.0,1)
    14.         _LColor ("Highlight Color", Color) = (0.5,0.5,0.5,1)
    15.     }
    16.  
    17.     SubShader
    18.     {
    19.         Tags { "RenderType"="Opaque" }
    20.         LOD 200
    21.        
    22. CGPROGRAM
    23.         #pragma surface surf ToonRamp nolightmap
    24.        
    25.         sampler2D _MainTex;
    26.         sampler2D _Ramp;
    27.         float4 _LColor;
    28.         float4 _SColor;
    29.         float4 _Color;
    30.         float _RimPower;
    31.         float4 _RimColor;
    32.        
    33.         // custom lighting function that uses a texture ramp based
    34.         // on angle between light direction and normal
    35.         #pragma lighting ToonRamp exclude_path:prepass
    36.         inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
    37.         {
    38.             #ifndef USING_DIRECTIONAL_LIGHT
    39.             lightDir = normalize(lightDir);
    40.             #endif
    41.            
    42.             half d = dot (s.Normal, lightDir)*0.5 + 0.5;
    43.             half3 ramp = tex2D(_Ramp, float2(d,d)).rgb;
    44.             ramp = lerp(_SColor,_LColor,ramp);
    45.            
    46.             half4 c;
    47.             c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
    48.             c.a = 0;
    49.            
    50.             return c;
    51.         }
    52.        
    53.         struct Input
    54.         {
    55.             float2 uv_MainTex : TEXCOORD0;
    56.             float2 uv2_MainTex : TEXCOORD1;
    57.             float3 pos : POSITION0;
    58.             float3 viewDir;
    59.             float4 color      : COLOR;
    60.         };
    61.        
    62.         void surf (Input IN, inout SurfaceOutput o)
    63.         {
    64.             half4 c = tex2D(_MainTex, IN.uv_MainTex);
    65.             half4 c2 = tex2D(_MainTex, IN.uv2_MainTex);
    66.            
    67.             o.Albedo = c.rgb * c2.rgb * _Color.rgb * IN.color.rgb * IN.color.a;
    68.             o.Alpha = c.a;
    69.            
    70.             //Rim Light
    71.             half rim = 1.0f - saturate( dot(normalize(IN.viewDir), o.Normal) );
    72.             o.Emission = (_RimColor.rgb * pow(rim, _RimPower)) * _RimColor.a;
    73.         }
    74. ENDCG
    75.    
    76.     }
    77.  
    78.     Fallback "Toon/Lighted"
    79. }
    80.  
    Did anybody else perhaps already try abusing the second Uv set? Is this a huge undertaking or can this be fixed rather unspectacularly ?

    Currently using Unity 3.5.5f3 on Windows 7.
     
  2. gsantos

    gsantos

    Joined:
    Sep 30, 2012
    Posts:
    7
    Hi There!

    I'm experiencing a similar issue when I tried to declare an UV2 that I want to use with the same sampler
    Code (csharp):
    1.  
    2. struct Input
    3.         {
    4.             float2 uv_MainTex : TEXCOORD0;
    5.             float2 uv2_MainTex : TEXCOORD1;
    6. }
    7.  
    Anyone have an hint how to solve it or a workaround to make it work?
    Thank you.

    I have an macbook pro with OSX Lion.
     
  3. kobunketsu

    kobunketsu

    Joined:
    Dec 11, 2012
    Posts:
    1
    I have the same problem.I think it's a bug.should be fixed
     
  4. SengiG

    SengiG

    Joined:
    Oct 31, 2012
    Posts:
    6
    Still not working with Unity 4.2 :eek:

    Seems shader don't see uv2(to make it clear, uv2 - exist, its a 100% info :wink:) of custom builded mesh on Android (I had tested on three devices Nexus 7, GoClever tab,Samsung Galaxy tab 10.1 ).
     
    Last edited: Oct 25, 2013
  5. darkeyes

    darkeyes

    Joined:
    Oct 21, 2011
    Posts:
    54
    I'm seeing the same problem with script set uv2 in both Unity4.2.2f1 and Unity4.3.1f1 (Testing on an OUYA)

    Anyone got any ideas ... or seen any shaders that work using uv2 on android?
     
  6. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Is this still broken?
     
  7. dnnkeeper

    dnnkeeper

    Joined:
    Jul 7, 2013
    Posts:
    84
    It seems it is still broken. Fix it please or mark it as "known issue" at least.
     
  8. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    I did get some success with this but only by writing my own shader completely from scratch.
     
    Last edited: Apr 14, 2014
  9. RyanFavale

    RyanFavale

    Joined:
    May 30, 2014
    Posts:
    28
    I am able to get uv2 by doing this.


    SubShader
    {
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma target 3.0
    #pragma surface surf Lambert vertex:vert

    sampler2D _MainTex;

    struct Input
    {
    float2 uv_MainTex;
    float2 getUV2;
    };

    void vert (inout appdata_full v, out Input o)
    {
    o.getUV2 = v.texcoord1.xy;
    };



    void surf (Input IN, inout SurfaceOutput o)
    {
    float2 uv2 = IN.getUV2;
    }
    ENDCG
    }