Search Unity

struct v2f -> declaration order

Discussion in 'Shaders' started by DerWoDaSo, Oct 7, 2009.

  1. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    Hey hey,

    I just stumbled across a problem I had some time ago, but there I managed to make it work without really understanding why...

    It's about the order of variable declaration in v2f struct.

    Code (csharp):
    1. struct v2f {
    2.     V2F_POS_FOG;
    3.     LIGHTING_COORDS
    4.     float2  uv;
    5.     float3  viewDir;
    6.     float3  lightDir;
    7.     float3  normal;
    8.     float3  tangent;
    9.     float4  vertexColor;
    10. };
    With this order I get the following error:
    When I change the order I don't get an error message:
    Code (csharp):
    1. struct v2f {
    2.     V2F_POS_FOG;
    3.     LIGHTING_COORDS
    4.     float4  vertexColor;
    5.     float2  uv;
    6.     float3  viewDir;
    7.     float3  lightDir;
    8.     float3  normal;
    9.     float3  tangent;
    10. };
    Is there a rule in which order I have to declare my variables?
    Thanks in advance...

    Jan
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I think Cg is just getting confused by too many values to interpolate between vertex fragment programs. Light/shadow stuff (LIGHTING_COORDS) uses 0 to 3 texture interpolators. And you want to use 6 more, which does not fit into 8 texture interpolators.

    Maybe you can pack some things into float4's? Or interpolate something in the color interpolator (e.g. float4 vertexColor : COLOR0)?
     
  3. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    Thanks... I solved the problem by moving the LIGHT_COORDS all the way down. Now I don't get error messages when using TEXCOORD0 or COLOR0 anymore. But packing some things into float4s might be a good solution too... I'll try that next time. ;)

    Code (csharp):
    1. struct v2f {
    2.    V2F_POS_FOG;
    3.    float2  uv : TEXCOORD0;
    4.    float4  vertexColor : COLOR0;
    5.    float3  view;
    6.    float3  light;
    7.    float3  tangent;
    8.    LIGHTING_COORDS
    9. };