So, I’m pretty stuck right now, and I was hopeing somebody could help. I’ve been trying to create a procedural map generator right now, and I’ve come up with the following code:
void BuildMap() {
// create the mesh
MeshFilter meshFilter = mapObject.GetComponent<MeshFilter>();
if(meshFilter==null){
Debug.LogError("MeshFilter not found!");
return;
}
Mesh mapMesh = meshFilter.sharedMesh;
if (mapMesh == null){
meshFilter.mesh = new Mesh();
mapMesh = meshFilter.sharedMesh;
}
mapMesh.Clear ();
List<Vector3> verts = new List<Vector3>();
List<int> triangles = new List<int>();
// create variable to keep track of the current face
int faceCount = 0;
// assign the vertices
for(int x = 0;x<mapSize.x;x++) {
for(int y = 0;y<mapSize.y;y++) {
if(mapData[x, y].wall == false) {
verts.Add(new Vector3(x, 0, y));
verts.Add(new Vector3(x + 2, 0, y));
verts.Add(new Vector3(x + 2, 0, y + 2));
verts.Add(new Vector3(x, 0, y + 2));
faceCount++;
}
}
}
mapMesh.vertices = verts.ToArray();
// assign the UVs
Vector2[] uvs = new Vector2[mapMesh.vertices.Length];
for(int i = 0; i<uvs.Length; i++) {
uvs <em>= new Vector2(mapMesh.vertices_.x, mapMesh.vertices*.z);*_</em>
* }*
* mapMesh.uv = uvs;*
* // assign the triangles*
* triangles.Add (faceCount);*
* triangles.Add (faceCount + 1);*
* triangles.Add (faceCount + 3);*
* triangles.Add (faceCount + 1);*
* triangles.Add (faceCount + 2);*
* triangles.Add (faceCount + 3);*
* mapMesh.triangles = triangles.ToArray ();*
* // recalculate the normals*
* mapMesh.RecalculateNormals();*
* mapMesh.RecalculateBounds();*
* mapMesh.Optimize();*
* }*
However, this code doesn’t do the trick. It’s simply supposed to put a plane with four vertices and two triangles wherever it finds a tile (marked by a ‘true’ boolean in a two dimensional array of Tile objects, created elsewhere).
It’s not working though, and I can’t for the life of me figure out what I’m doing wrong. I’ve looked at all the procedural mesh generation resources I could find, and I’m pretty sure my problem lies in my triangle winding order (is that the correct term?). However, I don’t know how to fix it – and it doesn’t help that the Unify Community Wiki is down. Could somebody explain what I’m doing wrong? Thanks!