- Home /
How do I get the color of a certain mesh vertex?
I have a custom mesh and I want to get the color of a separate triangle. I tried to use these solutions : https://stackoverflow.com/questions/45854076/set-color-for-each-vertex-in-a-triangle , https://stackoverflow.com/questions/57405631/unity-how-to-use-mesh-triangleindex-to-recolor-a-triangle-when-clicked?rq=1 . I don't have a texture on the object, so there remains only a way with manipulations of triangles and vertices. I was trying to get the color this way :
// Update is called once per frame
void Update()
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hitInfos = Physics.RaycastAll(ray);
// Debug.Log(hitInfos[0].collider.GetComponent<MeshFilter>
().mesh.triangles[hitInfos[0].triangleIndex]);
Debug.Log(hitInfos[0].collider.GetComponent<MeshFilter>().mesh.colors.Length);
}
public static int GetSubMeshIndex(Mesh mesh, int triangleIndex)
{
if (mesh.isReadable == false)
{
Debug.LogError("You need to mark model's mesh as Read/Write Enabled in Import Settings.", mesh);
return 0;
}
int triangleCounter = 0;
for (int subMeshIndex = 0; subMeshIndex < mesh.subMeshCount; subMeshIndex++)
{
var indexCount = mesh.GetSubMesh(subMeshIndex).indexCount;
triangleCounter += indexCount / 3;
if (triangleIndex < triangleCounter)
{
return subMeshIndex;
}
}
Debug.LogError(
$"Failed to find triangle with index {triangleIndex} in mesh '{mesh.name}'. Total triangle count: {triangleCounter}",
mesh);
return 0;
}
}
But this did not give any result because the array is empty
Comment