So I am trying to generate a procedural mesh, but I am encountering problems with the normals.
![Where’s the cream filling!?][1]
I find this very strange, since the mesh itself seems to be of the right shape. Here is my attempt to explain the code, if anyone ends up wanting to help me:
- Based on a variable called “mapsize”, I populate an array, blobVerts, with the position of each vertex. If mapsize is set to 10, then the result is a 10x10 units mesh. (Note that mapverts is just mapsize + 1, because there’s one more vertex per row than there are squares)
- On alternating rows, the x-value is shifted over by 0.5 units. This gets the zigzag pattern
- The triangles are generated in batches of two, with six indices handled in each batch. The “CurrentRow” switch statement is used to alternate the pattern of the batch. Within each batch, the six indices are calculated based on a pivotal vertex. this central vertex is held by the “g” variable (Which is actually being reused from the earlier Vertex section… ugh).
That’s about all I can think of to explain it for now. If anyone has questions, such as “Jesus dude, where did you learn to program? The Internet!?”, please ask. So here’s the code:
blobVerts = new BlobVertex[mapverts * mapverts];
blobVertsArray = new Vector3[mapverts * mapverts];
blobUV = new Vector2[mapverts * mapverts];
blobTriangles = new int[(mapsize * mapsize * 6)];
//Populate the blobVerts class array
for (int i = 0; i < blobVerts.Length; i++)
{
blobVerts *= new BlobVertex();*
}
//VERTICES
int g = 0;
int row = 3;
//Set the vertices to fill the map grid
for (int i = 0; i < mapverts; i++)
{
for (int h = 0; h < mapverts; h++)
{
blobVerts[g].position = new Vector3(h - ((row % 2) * .5f), 0, i);
g = g + 1;
}
row++;
}
//Transfers positions from blobVerts[].position to blobVertsArray
UpdateBlobVertsArray();
//END VERTICES
//UV
Debug.Log("blobUV.Length: " + blobUV.Length);
// Set the UV verts
for (int i = 0; i < blobUV.Length; i++)
{
//Debug.Log("i = " i);
blobUV*.x = 0;*
blobUV*.y = 0;*
}
//END UV
//TRIANGLES
// Set the triangles
// Interger “h” corresponds with the batch of triangles (form a square)
// Interger “g” is reused as a holder for the real starting vertex in each batch
g = 0;
row = 2;
for (int h = 0; h < (mapsize * mapsize); h++)
{
//this is my attempt at skipping the last vertex in each row, since it does not have a square
if ((g + 1) >= mapverts && (g + 1) % mapverts == 0)
g++;
//Cycle through the six vertices for a pair of triangles (parallelogram)
for (int i = 0; i < 6; i++)
{
int triIndex = (h * 6) + i;
switch((CurrentRow(g) + 2) % 2)
{
case 0:
switch (i)
{
case 5:
blobTriangles[triIndex] = g + mapsize + 2;
break;
case 4:
blobTriangles[triIndex] = g + mapsize + 1;
break;
case 3:
blobTriangles[triIndex] = g + 1;
break;
case 2:
blobTriangles[triIndex] = g + mapsize + 1;
break;
case 1:
blobTriangles[triIndex] = g + 1;
break;
case 0:
blobTriangles[triIndex] = g;
break;
}
break;
case 1:
switch (i)
{
case 5:
blobTriangles[triIndex] = g + mapsize + 2;
break;
case 4:
blobTriangles[triIndex] = g + 1;
break;
case 3:
blobTriangles[triIndex] = g;
break;
case 2:
blobTriangles[triIndex] = g + mapsize + 2;
break;
case 1:
blobTriangles[triIndex] = g + mapsize + 1;
break;
case 0:
blobTriangles[triIndex] = g;
break;
}
break;
}
}
g++;
row++;
}
//END TRIANGLES
----------
EDIT: Here’s a few more images to demonstrate what is happening.
With a texture (viewed from the bottom)
![A a-a-a a-al co hol, baby][2]
[1]: http://s7.postimage.org/9wckg52h7/Meshed_Up.png
[2]: http://s7.postimage.org/7eamkjp3f/Meshed_Up2.png