Skip to content

Instantly share code, notes, and snippets.

@mbolt35
Created November 1, 2012 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbolt35/3991618 to your computer and use it in GitHub Desktop.
Save mbolt35/3991618 to your computer and use it in GitHub Desktop.
public class ChunkMesh {
private List<Vector3> _vertices = new List<Vector3>();
private List<Vector2> _uvs = new List<Vector2>();
private List<Vector3> _normals = new List<Vector3>();
private List<int> _triangles = new List<int>();
private int _triangleCount = 0;
public ChunkMesh() {
}
public void AddFaceFor(Block block, Vector3 normal) {
Vector3 worldPosition = (Vector3) block.World;
// Retrieve a generic quad definition given the normal
Quad quad = Quads.ForNormal(normal);
// Move the generic quad vertices to their world position
foreach (Vector3 v in quad.Vertices) {
_vertices.Add(worldPosition + v);
}
// Add all the UVs
_uvs.AddRange(quad.UVs);
// Add the normal for each vertex
int normals = 4;
while (normals-- > 0) {
_normals.Add(quad.Normal);
}
// Add the vertex indices that make up the quad
_triangles.Add(_triangleCount);
_triangles.Add(_triangleCount + 1);
_triangles.Add(_triangleCount + 2);
_triangles.Add(_triangleCount + 2);
_triangles.Add(_triangleCount + 1);
_triangles.Add(_triangleCount + 3);
_triangleCount += 4;
}
public Mesh ToMesh() {
Mesh m = new Mesh();
m.vertices = _vertices.ToArray();
m.uv = _uvs.ToArray();
m.normals = _normals.ToArray();
m.triangles = _triangles.ToArray();
return m;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment