I have a C# Struct with a static method, "Get Shape" which populates a List with the vertices of a polyhedron.
Method Signature:
public static void GetShape(Block b, int x, int y, int z,
List<Vector3> vertices, List<int> triangles,
List<Vector2> uvs, List<Vector2> uv2s)
Adding directly to the vertices list (via vertices.Add(vector3) ), the code works as expected, and the new polyhedron appears when I trigger the method. However, I want to do some processing on the vertices I'm adding (a rotation), and the most sensible way I can think to do that is by creating a separate list of Vector3s, and then combining the lists when I'm done.
However,
vertices.AddRange(newVerts) does not add the shape to the mesh, nor does a foreach loop with verts.Add(vertices[i]). And this is before I've added in any of the processing!
I have a feeling this might stem from passing the list of vertices in as a parameter, rather than returning a list and then adding to the vertices in the calling object, but since I'm filling 4 lists, I was trying to avoid having to create a data struct to return all four at once.
Any ideas? The working version of the method is reprinted below, in full:
public static void GetShape(Block b, int x, int y, int z,
List<Vector3> vertices, List<int> triangles,
List<Vector2> uvs, List<Vector2> uv2s)
{
//List<Vector3> vertices = new List<Vector3>();
int l_blockShape = b.blockShape;
int l_blockType = b.blockType;
//CheckFace checks if the block is empty
//if this block is empty, don't draw anything.
int vertexIndex;
//only y faces need to be hidden.
//if((l_blockShape & BlockShape.NegZFace) == BlockShape.NegZFace)
{
vertexIndex = vertices.Count;
//top left, top right, bottom right, bottom left
vertices.Add(new Vector3(x+.2f, y + 1, z+.2f));
vertices.Add(new Vector3(x+.8f, y + 1, z+.2f));
vertices.Add(new Vector3(x+.8f, y , z+.2f));
vertices.Add(new Vector3(x+.2f, y , z+.2f));
// first triangle for the face
triangles.Add(vertexIndex);
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+3);
// second triangle for the face
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+2);
triangles.Add(vertexIndex+3);
//UVs for the face
uvs.Add( new Vector2(0,1));
uvs.Add( new Vector2(1,1));
uvs.Add( new Vector2(1,0));
uvs.Add( new Vector2(0,0));
//UV2s (lightmapping?)
uv2s.Add( new Vector2(0,1));
uv2s.Add( new Vector2(1,1));
uv2s.Add( new Vector2(1,0));
uv2s.Add( new Vector2(0,0));
}
//XY Z+1 face
//if((l_blockShape & BlockShape.PosZFace) == BlockShape.PosZFace)
{
vertexIndex = vertices.Count;
//top left, top right, bottom right, bottom left
vertices.Add(new Vector3(x+.8f, y + 1, z+.8f));
vertices.Add(new Vector3(x+.2f, y + 1, z+.8f));
vertices.Add(new Vector3(x+.2f, y , z+.8f));
vertices.Add(new Vector3(x+.8f, y , z+.8f));
// first triangle for the face
triangles.Add(vertexIndex);
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+3);
// second triangle for the face
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+2);
triangles.Add(vertexIndex+3);
//UVs for the face
uvs.Add( new Vector2(0,1));
uvs.Add( new Vector2(1,1));
uvs.Add( new Vector2(1,0));
uvs.Add( new Vector2(0,0));
//UV2s (lightmapping?)
uv2s.Add( new Vector2(0,1));
uv2s.Add( new Vector2(1,1));
uv2s.Add( new Vector2(1,0));
uv2s.Add( new Vector2(0,0));
}
//ZY face
//if((l_blockShape & BlockShape.NegXFace) == BlockShape.NegXFace)
{
vertexIndex = vertices.Count;
//top left, top right, bottom right, bottom left
vertices.Add(new Vector3(x+.2f, y + 1, z+.8f));
vertices.Add(new Vector3(x+.2f, y + 1, z+.2f));
vertices.Add(new Vector3(x+.2f, y , z+.2f));
vertices.Add(new Vector3(x+.2f, y , z+.8f));
// first triangle for the face
triangles.Add(vertexIndex);
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+3);
// second triangle for the face
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+2);
triangles.Add(vertexIndex+3);
//UVs for the face
uvs.Add( new Vector2(0,1));
uvs.Add( new Vector2(1,1));
uvs.Add( new Vector2(1,0));
uvs.Add( new Vector2(0,0));
//UV2s (lightmapping?)
uv2s.Add( new Vector2(0,1));
uv2s.Add( new Vector2(1,1));
uv2s.Add( new Vector2(1,0));
uv2s.Add( new Vector2(0,0));
}
//ZY X+1 face
// if((l_blockShape & BlockShape.PosXFace) == BlockShape.PosXFace)
{
vertexIndex = vertices.Count;
//top left, top right, bottom right, bottom left
vertices.Add(new Vector3(x+.8f, y + 1, z+.2f));
vertices.Add(new Vector3(x+.8f, y + 1, z+.8f));
vertices.Add(new Vector3(x+.8f, y , z+.8f));
vertices.Add(new Vector3(x+.8f, y , z+.2f));
// first triangle for the face
triangles.Add(vertexIndex);
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+3);
// second triangle for the face
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+2);
triangles.Add(vertexIndex+3);
//UVs for the face
uvs.Add( new Vector2(0,1));
uvs.Add( new Vector2(1,1));
uvs.Add( new Vector2(1,0));
uvs.Add( new Vector2(0,0));
//UV2s (lightmapping?)
uv2s.Add( new Vector2(0,1));
uv2s.Add( new Vector2(1,1));
uv2s.Add( new Vector2(1,0));
uv2s.Add( new Vector2(0,0));
}
//ZX face
if((l_blockShape & BlockShape.NegYFace) == BlockShape.NegYFace)
{
vertexIndex = vertices.Count;
//top left, top right, bottom right, bottom left
vertices.Add(new Vector3(x+.8f, y , z+.8f));
vertices.Add(new Vector3(x+.8f, y , z+.2f));
vertices.Add(new Vector3(x+.2f, y , z+.2f));
vertices.Add(new Vector3(x+.2f, y , z+.8f));
// first triangle for the face
triangles.Add(vertexIndex+3);
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex);
// second triangle for the face
triangles.Add(vertexIndex+3);
triangles.Add(vertexIndex+2);
triangles.Add(vertexIndex+1);
//UVs for the face
uvs.Add( new Vector2(0,1));
uvs.Add( new Vector2(1,1));
uvs.Add( new Vector2(1,0));
uvs.Add( new Vector2(0,0));
//UV2s (lightmapping?)
uv2s.Add( new Vector2(0,1));
uv2s.Add( new Vector2(1,1));
uv2s.Add( new Vector2(1,0));
uv2s.Add( new Vector2(0,0));
}
//ZX + 1 face
if((l_blockShape & BlockShape.PosYFace) == BlockShape.PosYFace)
{
vertexIndex = vertices.Count;
//top left, top right, bottom right, bottom left
vertices.Add(new Vector3(x+.8f, y+1 , z+.2f));
vertices.Add(new Vector3(x+.8f, y+1 , z+.8f));
vertices.Add(new Vector3(x+.2f, y+1 , z+.8f));
vertices.Add(new Vector3(x+.2f, y+1 , z+.2f));
// first triangle for the face
triangles.Add(vertexIndex+3);
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex);
// second triangle for the face
triangles.Add(vertexIndex+3);
triangles.Add(vertexIndex+2);
triangles.Add(vertexIndex+1);
//UVs for the face
uvs.Add( new Vector2(0,1));
uvs.Add( new Vector2(1,1));
uvs.Add( new Vector2(1,0));
uvs.Add( new Vector2(0,0));
//UV2s (lightmapping?)
uv2s.Add( new Vector2(0,1));
uv2s.Add( new Vector2(1,1));
uv2s.Add( new Vector2(1,0));
uv2s.Add( new Vector2(0,0));
}
}