Search Unity

Getting vertex color data in surface shaders

Discussion in 'Shaders' started by Kragh, Dec 13, 2010.

  1. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    I simply can't get vertex colors to work using the "with COLOR semantic" line in my suface shader Input struct.
    This is what it says in the docs:

    float3 viewDir - will contain view direction, for computing Parallax effects, rim lighting etc.
    float4 with COLOR semantic - will contain interpolated per-vertex color.
    float4 screenPos - will contain screen space position for reflection effects. Used by WetStreet shader in Dark Unity for example.
    float3 worldPos - will contain world space position.
    float3 worldRefl - will contain world reflection vector if surface shader does not write to o.Normal. See Reflect-Diffuse shader for example.
    float3 worldRefl; INTERNAL_DATA - will contain world reflection vector if surface shader writes to o.Normal. To get the reflection vector based on per-pixel normal map, use WorldReflectionVector (IN, o.Normal). See Reflect-Bumped shader for example.


    But when I put in the "float4 with COLOR semantic", I just get this error:

     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    The syntax for semantics is:
    Code (csharp):
    1. float4 color : COLOR;
    The idea is that you can call your per-vertex colour variable anything you want, as long as you use the COLOR semantic.
     
    Alekxss and torvald-mgt like this.
  3. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    haha... of course, I get it now.
    Well, that is somewhat a confusing way of documenting it, as in all other lines in that section you write exactly what it says. But in this one case it is an actual sentence :p
    Well, I'm just stupid, I guess... It confused me, that's for sure...

    Thanks a lot, Daniel! :)
     
    john_jay_smith likes this.
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It can be confusing because the Unity documentation assumes you know Cg. I recommend reading the Cg Tutorial cover to cover.
     
  5. Orbital Fan

    Orbital Fan

    Joined:
    Mar 23, 2010
    Posts:
    3
    Hmm i'm trying the same thing but I'm getting the error:

    Code (csharp):
    1. Shader error in 'PlayerShipShader': GLSL vertex shader: ERROR: 0:350: 'color' :  no such field in structure  at line 10
    2.  
    This is the code:


    Code (csharp):
    1.  
    2. Shader "PlayerShipShader" {
    3.  
    4.     Properties {
    5.  
    6.         _Color ("Main Color", Color) = (1,1,1,0.5)
    7.  
    8.         _MainTex ("Base (RGB)", 2D) = "white" {}
    9.  
    10.     }
    11.  
    12.     SubShader {
    13.  
    14.         Tags {"Queue" = "Geometry" }
    15.  
    16.         Lighting Off
    17.  
    18.        
    19.  
    20.         CGPROGRAM
    21.  
    22.         #pragma surface surf Lambert
    23.  
    24.  
    25.  
    26.         float4 _Color;
    27.  
    28.         sampler2D _MainTex;
    29.  
    30.  
    31.  
    32.         struct Input {
    33.  
    34.             float4 screenPos;
    35.  
    36.             float4 vertColor : COLOR;
    37.  
    38.         };
    39.  
    40.  
    41.  
    42.         void surf (Input IN, inout SurfaceOutput o) {
    43.  
    44.             float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
    45.  
    46.             half4 c = tex2D (_MainTex, screenUV);
    47.  
    48.             c = c * _Color * IN.vertColor;
    49.  
    50.             o.Albedo = c.rgb;
    51.  
    52.             o.Alpha = c.a;
    53.  
    54.         }
    55.  
    56.         ENDCG
    57.  
    58.     }
    59.  
    60. }
    61.  
     
    Ladder14 likes this.
  6. ProD

    ProD

    Joined:
    Dec 16, 2010
    Posts:
    6
    Yeah.. I have the same problem... Have somene solved this problem?
     
  7. ProD

    ProD

    Joined:
    Dec 16, 2010
    Posts:
    6
    Ok... if you don't use.. float4 color : COLOR: it's not going to work!
     
  8. Orbital Fan

    Orbital Fan

    Joined:
    Mar 23, 2010
    Posts:
    3
    yes I just encountered the same problem. The docs don't give you required information. You HAVE to have:

    float4 color : COLOR;
     
  9. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    484
    It works for me :D
    Code (csharp):
    1. Shader "Vertex Colored Alpha Surf Shader" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    7.         LOD 200
    8.        
    9.         CGPROGRAM
    10.         #pragma surface surf Lambert alpha
    11.        
    12.         sampler2D _MainTex;
    13.  
    14.         struct Input {
    15.             float2 uv_MainTex;
    16.             float4 color: Color; // Vertex color
    17.         };
    18.  
    19.         void surf (Input IN, inout SurfaceOutput o) {
    20.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    21.             o.Albedo = c.rgb * IN.color.rgb; // vertex RGB
    22.             o.Alpha = c.a * IN.color.a; // vertex Alpha
    23.         }
    24.         ENDCG
    25.     }
    26.     FallBack "Diffuse"
    27. }
    28.  
     
    Last edited: May 26, 2011