Search Unity

TEXCOORD1 not working in CG?

Discussion in 'Shaders' started by CorruptScanline, Jun 30, 2009.

  1. CorruptScanline

    CorruptScanline

    Joined:
    Mar 26, 2009
    Posts:
    217
    So Im trying to make a proper lightmap shader that will modulate the lightmap times 4 to get over exposure. The problem is that I cant seem to access the second UV channel in the CGPROGRAM code.

    I know the model actually has a second UV channel because the built in lightmap shaders work, and i can also sample the uvs with a raycast.

    heres the code:
    Code (csharp):
    1.  
    2. Shader "Lightmapper/DiffuseModulate4" {
    3. Properties {
    4.     _MainTex ("Main Color", 2D) = "white" { }
    5.    _LightMap ("LightMap", 2D) = "black" { }
    6. }
    7. SubShader {
    8.     Pass {
    9.  
    10. CGPROGRAM
    11. #pragma vertex vert
    12. #pragma fragment frag
    13. #pragma fragmentoption ARB_fog_exp2
    14.  
    15. #include "UnityCG.cginc"
    16.  
    17. sampler2D _MainTex;
    18. sampler2D _LightMap;
    19.  
    20. //struct a2v {
    21.     //float2  uv : TEXCOORD0;
    22.     //float2 uv2 : TEXCOORD1;
    23. //};
    24.  
    25. struct v2f {
    26.     V2F_POS_FOG;
    27.     float3 vertex;
    28.     float2  uv : TEXCOORD0;
    29.     float2 uv2 : TEXCOORD1;
    30. };
    31.  
    32. v2f vert (appdata_base v)
    33. {
    34.     v2f o;
    35.     PositionFog( v.vertex, o.pos, o.fog );
    36.     o.uv = TRANSFORM_UV(0);
    37.     o.uv2 = TRANSFORM_UV(1);
    38.     return o;
    39. }
    40.  
    41. half4 frag (v2f i) : COLOR
    42. {
    43.     half4 color = tex2D(_MainTex, i.uv2); //This shows a solid color.
    44.     //half4 color = tex2D(_MainTex, i.uv); //This shows TEXCOORD0 as it should.
    45.     return color;
    46. }
    47. ENDCG
    48.  
    49.     }
    50. }
    51. Fallback "VertexLit"
    52. }
    53.  
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Has the model you use 2 UV sets (and were they exported correctly to FBX)
     
  3. CorruptScanline

    CorruptScanline

    Joined:
    Mar 26, 2009
    Posts:
    217
    Yes, sorry typo in the first post. I know the model has a second UV channel because the unity standard lightmap shaders work correctly and I can use a physics raycast to see the second UV channel.
     
  4. ToreTank

    ToreTank

    Joined:
    Jun 23, 2008
    Posts:
    165
    Not having tested your shader, I am suspecting that you may be suffering from issues mentioned here: http://forum.unity3d.com/viewtopic.php?p=91122#91122


    Out of curiousity, does the following work?

    Code (csharp):
    1. half4 frag (v2f i) : COLOR
    2. {
    3.     half4 color = tex2D(_MainTex, i.uv2); //This shows a solid color.
    4.    color += tex2D(_MainTex, i.uv) * 0.001; //This shows TEXCOORD0 as it should.
    5.     return color;
    6. }
    Also, what is the "vertex" part in your v2f struct for? I cannot see it used anywhere.

    Edit: May be unrelated, but the UnityCG.cginc file suggests that TRANSFORM_TEX is the new "hip" way of doing UV transform.

    Code (csharp):
    1. // Transforms float2 UV by scale/bias property (new method)
    2. #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
    3. // Transforms float4 UV by a texture matrix (old method)
    4. #define TRANSFORM_UV(idx) mul( glstate.matrix.texture[idx], v.texcoord ).xy
     
  5. CorruptScanline

    CorruptScanline

    Joined:
    Mar 26, 2009
    Posts:
    217
    No, trying to actually use both uv sets in the frag shader still doesnt work. This code is kinda hacked together from searches on this forum, and may be totally broken. I just cant seem to find any examples of 2 uv channels used in a CG program.
     
  6. CorruptScanline

    CorruptScanline

    Joined:
    Mar 26, 2009
    Posts:
    217
    I just figured it out. Apparently when you make your own vertex program in unity you need to name the TEXCOORD0 variable "texcoord" or it will give you an error.

    Heres the working code for anyone who may have a similar problem:
    Code (csharp):
    1.  
    2. Shader "Lightmapper/DiffuseModulate4" {
    3.     Properties {
    4.         _MainTex ("Diffuse", 2D) = "white" { }
    5.        _LightMap ("LightMap", 2D) = "black" { }
    6.     }
    7.     SubShader {
    8.         Pass {
    9.         CGPROGRAM
    10.         #pragma vertex vert
    11.         #pragma fragment frag
    12.  
    13.         sampler2D _MainTex;
    14.         sampler2D _LightMap;
    15.  
    16.         struct a2v {
    17.             float4 vertex : POSITION;
    18.             float2  texcoord : TEXCOORD0;
    19.             float2 texcoord1 : TEXCOORD1;
    20.         };
    21.  
    22.         struct v2f {
    23.             float4 vertex : POSITION;
    24.             float2  texcoord : TEXCOORD0;
    25.             float2 texcoord1 : TEXCOORD1;
    26.         };
    27.  
    28.         v2f vert (a2v v)
    29.         {
    30.             v2f o;
    31.             o.vertex = mul( glstate.matrix.mvp, v.vertex);
    32.             o.texcoord = v.texcoord;
    33.             o.texcoord1 = v.texcoord1;
    34.             return o;
    35.         }
    36.  
    37.         half4 frag (v2f i) : COLOR
    38.         {
    39.             half4 color = tex2D(_MainTex, i.texcoord);
    40.             color *= tex2D(_LightMap, i.texcoord1) * 4;
    41.             return color;
    42.         }
    43.         ENDCG
    44.         }
    45.     }
    46.     Fallback "VertexLit"
    47. }
    48.  
    49.  
     
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I know I'm late, but I just stumbled across this tidbit from the page on attenuation and shadows:
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Also, why are you using Cg instead of just adding the QUAD keyword to the built-in lightmap shader?