Shader: Outlined Diffuse with Vertex Color?

hello all,

Before anything, I must admit I have absolutely no knowledge in shader coding.

I currently use the 'Outlined Diffuse 3" shader:
http://wiki.unity3d.com/index.php?title=Outlined_Diffuse_3

But I would like to use what seems to be called ‘Vertex Color’ with this shader:
I build a mesh at runtime, and would like to ‘paint’ each triangles from this mesh with a different color depending on its Y position.
I know my code to do so works as I tested it with other basic shaders. But it does not with the ‘Oultined Diffuse 3’ shader.

Could anyone explain how I could modify this shader to make that work?
Or point out another ‘Outline Diffuse Shader’ that has this ‘Vertex Color’ possibility?

If you need any more information to help me, I’ll gladly answer.
Thank you all

If you go through that shader’s code, you’ll notice that it never uses the vertex color, as you correctly guessed. A ‘vertex color’ is a color associated with each vertex. They are the colors you set on the mesh using the Mesh.colors property. But to be visible, the shader has to actually make use of them, typically by passing them onto the pixel shader in the vertex shader.

The shader you linked to has a vertex function here:

v2f vert(appdata v) {
	// just make a copy of incoming vertex data but scaled according to normal direction
	v2f o;
	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
	float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
	float2 offset = TransformViewToProjection(norm.xy);
 
	o.pos.xy += offset * o.pos.z * _Outline;
	o.color = _OutlineColor;
	return o;
}

This vertex function appears to be the one that renders the (typically black) outline, so it passes the _OutlineColor on to the pixel shader instead of the vertex color. This, I assume, is fine. So let’s not mess with that. Instead, let’s have a look at the two surface functions. There is one per subshader, and you should modify both. They’re both very simple and have totally identical code bodies, which is these three lines of code

fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;

Notice here, that the color it sets is a multiplication of the _Color property (the main color you set on the Material) and a color read from the material’s primary texture. If you want vertex colors here, you have to tell those surface shaders to take the vertex colors into account. First, you must change the surface shaders’ Input structs so that they include the interpolated vertex colors, like in the following. This:

struct Input {
	float2 uv_MainTex;
};

Must become this:

struct Input {
	float2 uv_MainTex;
    float4 color : COLOR;
};

Do this for both subshaders. Next, change the surface shaders to use it. How is up to you. For pure vertex colors, something like this should work:

o.Albedo = IN.color.rgb;
o.Alpha = IN.color.a;

But if you want it to mix the vertex colors into what it’s already doing, you could just add it to the multiplication, like this:

fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color * IN.color;
o.Albedo = c.rgb;
o.Alpha = c.a;

Doing these things should work, but keep in mind I haven’t tested it myself. :slight_smile: Feel free to report back here with whatever compiler errors your modified shader throws, if any.