Hello, I am currently trying to implement an algorithm to place 3D tiles on a grid, and to do that I would like to calculate some adjacency constraints. I try to get each vertex which is on the face of my 2 by 2 by 2 large tiles by checking if the x, y or z value is equal to 1 or -1, and then store them in a list for later use. However, when I try to get these vertecies, they are often moved slighty.
// get the mesh data
var meshData = tiles[i].GetComponent<MeshFilter>().sharedMesh;
for (int j = 0; j < meshData.vertices.Length; j++)
{
Vector3 vertex = meshData.vertices[j];
Vector3 normal = meshData.normals[j];
Debug.Log(vertex.x);
if (vertex.x == 1) { posX.Add(new Vector2(vertex.z, vertex.y)); posXnormals.Add(new Vector2(normal.z, normal.y)); }
if (vertex.x == -1) { negX.Add(new Vector2(vertex.z, vertex.y)); negXnormals.Add(new Vector2(normal.z, normal.y)); }
if (vertex.y == 1) { posY.Add(new Vector2(vertex.x, vertex.z)); posYnormals.Add(new Vector2(normal.x, normal.z)); }
if (vertex.y == -1) { negY.Add(new Vector2(vertex.x, vertex.z)); negYnormals.Add(new Vector2(normal.x, normal.z)); }
if (vertex.z == 1) { posZ.Add(new Vector2(vertex.x, vertex.y)); posZnormals.Add(new Vector2(normal.x, normal.y)); }
if (vertex.z == -1) { negZ.Add(new Vector2(vertex.x, vertex.y)); negZnormals.Add(new Vector2(normal.x, normal.y)); }
}
When debugging, I can see that the mesh I put in contains only the vertecies that it had when I exported the .fbx file from blender:
However, once the loop is through, the values are not asigned correctly. For example, vertecies [6] and [7] both have an x value of 1, so the condition if (vertex.x == 1)
should have triggered, but both according to the debug menue and my results, that doesn’t happen:
The posX array, which is supposed to contain the vertecies where x == 1, is empty. When I try to debug using Debug.Log(vertex.x);
I get weird results, in which the vertecies are often moved slightly:
This also happens with other values, not just the x value. In other words, vertecies are being moved at the 7th or 8th place after the comma and I don’t know why. I am importing fbx files, with these settings:
These are the default settings in unity, with the exception of enabaling read/ write.
Any pointers as to what I am doing wrong would be greatly appreciated.