Hi,
so i am trying to get the color of a specific part of my randomly generated “terrain” mesh so that i can determine if it is grass or sand ect.
i generated my mesh at runtime and it has textures applied at differant heights. one material with a few differant textures.
I am trying to eventually place prefabs randomly on the grass only. my current aproach is to create a grid in the sky above the ground then from each grid point i raycast down and check to see what color it hits and compare it with the grass texture.
after hours of reading i came up with this but it just crashes unity. for reference my grid is 100x100
i have stripped it back and it seems to crash at the last stage where i check to see if the mesh vertices == triangle and i get no debug log
public void CheckColor()
{
mesh = GameObject.Find("/Map Generator/Terrain Chunk").GetComponent<MeshFilter>().mesh;
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int o = 0; o < grid.GetLength(1); o++)
{
RaycastHit hit;
if (Physics.Raycast(grid[i, o].GetComponent<Transform>().position, Vector3.up * -1, out hit))
{
int v0Index = hit.triangleIndex * 3 + 0;
int v1Index = hit.triangleIndex * 3 + 1;
int v2Index = hit.triangleIndex * 3 + 2;
Vector3 triangle = new Vector3(v0Index, v1Index, v2Index);
Color colour;
for (int p = 0; p < mesh.vertices.Length; p += 3)
{
if (mesh.vertices[p] == triangle)
{
colour = mesh.colors[p];
Debug.Log(colour);
}
}
/*Renderer rend = hit.transform.GetComponent<MeshRenderer>();
Texture2D tex = rend.material.mainTexture as Texture2D;
if(tex == Grass)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.point;
}*/
}
}
}
}