Hey,
I’ve generated this plane mesh right here with some grid positions. Each grid Vector2 positions generates one quad.
private Mesh GenerateMeshFromTiles(List<Tile> tiles)
{
Mesh mesh = new Mesh();
Vector3[] vertices;
// Calculate the 4 Vertices of one Tile
List<Vector3> tileVertices = new List<Vector3>();
vertices = new Vector3[(tiles.Count * 3 + 1) * (tiles.Count * 3 + 1)];
for (int i = 0; i < tiles.Count; i++)
{
// -x Going left, +x going right
// -z going bot , +z going top
Tile tile = tiles[i];
// Calculate the 4 Positions from 1 Point
var xPos0 = tile.X - 0.5f;
var yPos0 = 0.02f;
var zPos0 = tile.Y - 0.5f;
var xPos1 = tile.X + 0.5f;
var yPos1 = 0.02f;
var zPos1 = tile.Y - 0.5f;
var xPos2 = tile.X - 0.5f;
var yPos2 = 0.02f;
var zPos2 = tile.Y + 0.5f;
tileVertices.Add(new Vector3(xPos0, yPos0, zPos0));
tileVertices.Add(new Vector3(xPos1, yPos1, zPos1));
tileVertices.Add(new Vector3(xPos2, yPos2, zPos2));
var xPos5 = tile.X + 0.5f;
var yPos5 = 0.02f;
var zPos5 = tile.Y + 0.5f;
tileVertices.Add(new Vector3(xPos5, yPos5, zPos5));
}
vertices = tileVertices.ToArray();
mesh.vertices = vertices;
// Create triangles
int[] triangles = new int[tiles.Count * 2 * 3];
int n = 0;
for (int i = 0; i < triangles.Length / 6; i++)
{
triangles[i * 6 + 1] = 2 + n;
triangles[i * 6 + 0] = 0 + n;
triangles[i * 6 + 2] = 1 + n;
triangles[i * 6 + 3] = 2 + n;
triangles[i * 6 + 4] = 3 + n;
triangles[i * 6 + 5] = 1 + n;
n += 4;
}
mesh.triangles = triangles;
//Calculate UV's
//UV's
Vector2[] uv = new Vector2[vertices.Length];
for (int i = 0; i < mesh.vertices.Length; i++)
{
uv[i] = new Vector2(0, 0);
uv[i + 1] = new Vector2(0, 1);
uv[i + 2] = new Vector2(1, 1);
uv[i + 3] = new Vector2(1, 0);
i += 3;
}
mesh.uv = uv;
//Normals
mesh.RecalculateNormals();
mesh.vertices = vertices;
return mesh;
}
The mesh seems to be correct but my UV’s are not. On the left side is a standard Unity Plane and on the right side is my generated mesh.
Anybody can figure out what I did wrong? Thanks in advance.
