# Programmatically generated mesh vertex colors not showing up in Unity?

I'm sorry if this is an obvious thing to solve but I just can't figure it out...did I miss something...I've generated a mesh and during vertices generation I've also set its colors (and colors32 just in case) by reading those values from a ply file, and afterwards I've added a number of vertex color shaders but none of them will show the vertex point colors in play mode, it's either solid black or just like I added no material to the mesh (which I've tried doing manually in play mode too).

Welcome, it's hard to tell with you not showing any code or explaining further what you've done :)

If not then here's a quick example:

Shader "Unlit Vertex Color"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
{
Tags { "RenderType"="Opaque" }

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};

struct v2f
{
float4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
float4 _MainTex_ST;
fixed _Brightness;
fixed _Gamma;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
o.color.w = 1.0;
//UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
col *= i.color;
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}


I just pass COLOR semantic to where necessary and blend the result with the pixel.

You can get started writing your own by templating some of the shaders from the builtin ones:

• For colors I think you should use whichever notation you prefer, e.g. Color is natural since it maps from 0.0 to 1.0, then later, if you notice performance problems by profiling you will want to adjust places in your code to emit Color32 instead. Keep in mind they both have implicit operators to each other so be careful when optimizing. Now for the shaders I learned the hard way: the 'builtin shaders' link I've mentioned. Here's a tip, use Notepad++ folder search on them, it'll be less intimidating and you'll get a better understanding overall on what is used where. – aybe Feb 23 '19 at 22:47
• I do the following by code, get my mesh, add MeshFilter component and set its mesh, add MeshRenderer component and set its material; and obviously if I have a texture I set material.mainTexture. – aybe Feb 23 '19 at 22:52