On advice from the tutorials out there I've moved to using Simplex noise. It also now creates two layers of noise so that we can have dirt on top of the stone.
Probably the biggest single change is that chunks now generate a mesh, rather than instantiating a cube for each voxel. This is (unsurprisingly) a lot faster and actually allows me to have stacks of blocks.
Creating a mesh in Unity is pretty straightforward. Specify four Vector3s and define as two triangles to form a square face. Use 6 square faces to form a cube.
// Top Face Vector3 vertices = new Vector3; vertices = new Vector3 (x, y, z + 1); vertices = new Vector3 (x + 1, y, z + 1); vertices = new Vector3 (x + 1, y, z ); vertices new Vector3 (x, y, z ); // Triangles int triangles = new int; // Two triangles, 3 points each, 2 verts are shared triangles = 0; triangles = 1; triangles = 2; triangles = 0; triangles = 2; triangles = 3; mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.Optimize(); mesh.RecalculateNormals(); col.sharedMesh = mesh; // Use render mesh for collisions
A simple optimisation then is to check if there is another cube adjacent. Since there is no gap we don't need to include the face on that side.
Lastly, track the block type and assign the appropriate texture.
It's lacking a lot of features that would make it a game. I might look into some basic changes like supporting just because it's straightforward. Really though, this was to serve as a refresher in how to think about 3D and procedural content. In that, it's served its purpose.