From my question at Unity Answers:

Hello everyone, I'm facing a bit of a problem here: I need a shader which has the ability to colour individual vertices and be lit pixel by pixel. Until recently, I was planning to use just vertex shading, since I couldn't find any info about this. The thing, it looks like this: Bad shading 1 and Bad shading 2. As you can see, it is horrendously bizarre and even glitchy. Here is the shader I'm using right now (ShaderLab syntax):

Shader "Alpha/VertexLit Colored" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _SpecColor ("Spec Color", Color) = (1,1,1,0)
        _Emission ("Emmisive Color", Color) = (0,0,0,0)
        _Shininess ("Shininess", Range (0.01, 1)) = 0.7
        _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}

    SubShader {
        //ZWrite Off
        Alphatest Greater 0
        Tags {Queue=Transparent}
        Blend SrcAlpha OneMinusSrcAlpha 
        ColorMask RGB
        Pass {
            Material {
                Shininess [_Shininess]
                Specular [_SpecColor]
                Emission [_Emission]    
            ColorMaterial AmbientAndDiffuse
            Lighting On
            SeparateSpecular On
            SetTexture [_MainTex] {
                Combine texture * primary, texture * primary
            SetTexture [_MainTex] {
                constantColor [_Color]
                Combine previous * constant DOUBLE, previous * constant
    Fallback "Alpha/VertexLit", 1

I searched around on the Unity forums, wiki and also on Google, but I found no resources for this. I really need the vertex colours here, it's a crucial element of the graphic style of my game, and is the simplest way to do what I want. Any help is immensely appreciated. Does anyone know if such kind of shader exists?

  • 1
    \$\begingroup\$ What is your question? (we can probably guess what you mean, but it's always best if you state your question explicitly somewhere in the text) \$\endgroup\$ Dec 31 '13 at 20:02
  • \$\begingroup\$ Sorry, I'll edit the question. I'm asking if such a shader exists. \$\endgroup\$
    – Kroltan
    Dec 31 '13 at 20:13

Why not do that with a surface shader? you could have Per Vertex coloring and on a same time keep multi-light shading (Which is "almost" default when using surface shaders)

Surface shaders doc :

According to those docs over here, you should be able to create a basic surface shader with diffuse and lighting. And by tricking the vertex:disp in doing no specific action except getting vertex position and converting it to color you want (using an out struct which could be a bit more evolved).

After doing that and getting back the color you got in vertex:disp inside your albedo infos, you just let surface shaders do their job and take care of the lighting (you can specify it there #pragma surface surfaceFunction lightModel [optionalparams])


I think i'm starting to get what you mean by vertexColor, you mean Weight Shade : giving a vertex a 0-1 value and from this value get the color mix which correspond?

or you just need access to the Color property in your input data for surf :

        struct Input {
            float2 uv_MainTex;
            float4 color : COLOR; //get vertex color (blind shot not sure)

        sampler2D _MainTex;
        sampler2D _NormalMap;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutput o) {
            o.Albedo = IN.color.rgb;
            o.Specular = 0.2;
            o.Gloss = 1.0;
            o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));

As stated in docs :

float4 with COLOR semantic - will contain interpolated per-vertex color

That is definately what you should be looking for

  • \$\begingroup\$ Could you give me an example? I've just started searching about shaders, let alone CGprograms. \$\endgroup\$
    – Kroltan
    Jan 7 '14 at 23:09
  • \$\begingroup\$ Edited with complementary info \$\endgroup\$ Jan 8 '14 at 7:03
  • \$\begingroup\$ Thanks for the detailed answer! And by vertex color I mean using the information from Unity's Mesh class (new Mesh().colors) on the vertices. \$\endgroup\$
    – Kroltan
    Jan 8 '14 at 12:30
  • \$\begingroup\$ ok so i believe you should try adding float4 color : COLOR; to access those infos from Shader as stated in docs (very nice i always wanted to try that) \$\endgroup\$ Jan 8 '14 at 12:34
  • 1
    \$\begingroup\$ Almost forgot this question! If any future reader is interested, here is the resulting shader: pastebin.com/9BgQsrfX \$\endgroup\$
    – Kroltan
    Feb 7 '14 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.