Make Material about Mesh Density

Hi, I want to make a code to get the density of the mesh and map the density to the color. I want to make the mesh look red yellow green from highest density to lowest density. But I don’t know how to put any color on the vertex. Can you help me?

What do you mean by density? Do you mean vertex density? If so, I think you may need to bake this into a texture beforehand.