Search Unity

Finding and using a Vertex Colors Shader

Discussion in 'Shaders' started by VesuvianPrime, Mar 25, 2014.

  1. VesuvianPrime

    VesuvianPrime

    Joined:
    Feb 26, 2013
    Posts:
    135
    Hey guys

    I'm trying to make use of the ability to set vertex colors on a Mesh. I have all of the logic figured out, but unfortunately it seems the only way to actually see the fruits of my labor is to use a vertex shader.

    Are there any stock Unity shaders that should work for me here?

    I've tried to save the following shaders in my project but for some reason I get a syntax error on line 3 in both:
    http://wiki.unity3d.com/index.php?title=VertexColor
    http://wiki.unity3d.com/index.php/AlphaVertexColor

    I'm currently using the stock Transparent/Diffuse shader, so I don't want to lose my alpha map functionality.

    I'm very new to playing with shaders in Unity, so I'm sure I've overlooked something simple.

    Thanks,
    Ves
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    The built-in particle shaders use vertex colours, because that is where per-particle colour is stored.
     
  3. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    This is a shader I am using to do some boxy procedural terrain.
    I dont use a color texture map, just vert color and a detail texture on top.
    The detail texture has an alpha channel I use to vary the Gloss.

    The only way I figured out how to do this was from some code another guy posted here.
    So good luck.



    Code (csharp):
    1.  
    2. Shader "DAB/Vertex Detail Specular"
    3. {
    4.   Properties
    5.   {
    6.       _Color ("Main Color", Color) = (1,1,1,1)
    7.       _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    8.       _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    9.       //_MainTex ("Base (RGB)", 2D) = "white" {}
    10.       _Detail ("Detail (RGB) Gloss (A)", 2D) = "gray" {}
    11.   }
    12.  
    13.   SubShader
    14.   {
    15.       Tags { "RenderType"="Opaque" }
    16.       LOD 300
    17.    
    18.     CGPROGRAM
    19.     #pragma surface surf BlinnPhong vertex:vert
    20.     //#pragma surface surf BlinnPhong
    21.     //#pragma surface surf Lambert
    22.  
    23.     //sampler2D _MainTex;
    24.     sampler2D _Detail;
    25.     float4 _Color;
    26.     float _Shininess;
    27.  
    28.     struct Input
    29.     {
    30.         //float2 uv2_MainTex;
    31.         float2 uv_Detail;
    32.         float3 vertColors;
    33.     };
    34.  
    35.     void vert(inout appdata_full v, out Input o)
    36.     {
    37.       o.vertColors= v.color.rgb;    // grab vertex colors from appdata
    38.       o.uv_Detail = v.texcoord;   // maybe need this
    39.  
    40.     }
    41.  
    42.  
    43.     void surf (Input IN, inout SurfaceOutput o)
    44.     {
    45.         //half4 c = tex2D(_MainTex, IN.uv2_MainTex) * _Color;
    46.         half3 c = IN.vertColors.rgb * _Color.rgb;               //half3 check to see that alpha works
    47.         c.rgb *= tex2D(_Detail,IN.uv_Detail).rgb*2;
    48.         o.Albedo = c.rgb;
    49.         o.Gloss = tex2D(_Detail,IN.uv_Detail).a;
    50.         //o.Alpha = c.a * _Color.a;
    51.         o.Specular = _Shininess;
    52.         //o.Normal = UnpackNormal(tex2D(_Detail, IN.uv_Detail));
    53.     }
    54.     ENDCG
    55.   }
    56.  
    57.   Fallback "Specular"
    58. }
    59.  
    This image got shrunk, so its hard to see the speckled detail texture, but those edge lines are on the detail too.
    $MainImage.jpg
     
    okarad, ametkly, EpiKnightz and 2 others like this.