How to get the colors of the vertices in a mesh? [or average color of a triangle]

Hi all,

I’m trying to obtain the color of the vertices in a mesh once a directional light makes its action on the mesh. However all the values for all the vertices that I’m obtaining are exactly the same: RGBA(255, 255, 255, 255), and this is not correct since my mesh is grey and it has parts of shadows.

The code is:

Mesh _mesh_runtime = _67P_3Dobject.GetComponent<MeshFilter>().mesh;
Color32[] vertex_colors = _mesh_runtime.colors32;
    
Debug.Log(vertex_colors[index_of_vertex].ToString());

My mesh has a default diffuse shader. Do you know what is happening?
Cheers and thank you!

if you have unity pro… render your camera to a render texture, then you can sample any of the pixel’s rgb color that you want from the render texture.
You’ll just need to convert your screen space co-ordinate, into a texture space co-ordinate.

alternatively… use vectors. compute facing angle or brightness yourself.
vec1 = polygon . normal

vec2 = light.position - poly.position //for a spot or point light

or

vec2 = -light.direction //for a directional

float dp = Vector3.dot(vec1, vec2);

if(dp>0) //threshold check

visible = true;

else

visible = false;

//1=facing (bright) 0 = 90 degrees(dark) -1 = facing opposite direction (dark)

//if you need to go further and use brightness rather than just facing… you can dp to approximate lighting brightness, if you want a threshold.

Easy, I’ve solved the problem placing the main camera on the normal vector of each facet of the mesh, and making the camera point to that facet and rendering the color of that facet (using a very low value of the field of view of the camera to constraint the camera pointing). Consequently, I can obtain a rendered image of each facet in the mesh, thus allowing me to compute the final brightness of each one ;).