Voxel based Terrain generation. Pointers please.

Ok, I’m new to Unity, and somewhat new to C#. I have programmed extensively in vb.NET (so I have a reasonable knowledge in .NET’s core data types etc…/) and also in PHP (so, other than type defining, the syntax is fairly similar when combined with vb).

Now, before I start, YES I have Googled around, but I haven’t found what I’m looking for. And, NO, I’m not trying to make a minecraft clone ^_^.

What I am trying to make is a 3rd person RTS style game, but with voxel based cubic terrain. I do, however need to be able to have characters mine these blocks, so I’ll need some way of knowing what type of block they are.

Now, I understand how Perlin works, and I found a good script to work from for that, so I can generate an array of smoothed noise for the terrain. However, I’m not sure how to store/draw it.

I was thinking of a 3 dimensional array (X,Y,Z) of a structure containing the block ID. The program could then look in an array of a structure containing the actual block data (texture, type, name etc…). However, my main question is how would i draw it out?

I tried simply instantiating each block, but basically it crashed Unity due to the sheer number of prefabs (and my machine is pretty beefy with 16gb RAM). So, I read that I could:

  1. Only draw visible cubes - but how would I decide which are visible to the third person camera?
  2. Draw meshes? I have no idea how you would actually implement this, as I’m not familiar with meshes and UVs (I think that was their name).

Any help, pointers, good reading etc would be appreciated.


My reading so far:



I think Minecraft uses a algorithm that only shows voxels if they have at least one side that is touching a transparent block, like glass, air, water and so on.

I think they are textured by placing the UVs of each block on the right place on a texture sheet.

Whatever you do I don’t think you’ll be able to do it without generating a mesh. Look at this class: Unity - Scripting API: Mesh

If you can’t understand what is going on there, maybe you should look into a 3D modelling program. Just look at some beginner tutorials to understand the basic techniques.

Maybe this helps you as well: Marching cubes - Wikipedia
Just search for marching cubes and you’ll find the code for it. That algorithm will give you a smooth terrain.

You might want to check out my voxel package I have just released in the unity assetstore. It has some interesting features like pathfinding whitch can be really usefull if you want to create an rpg type game. There is also a model editor for creating characters, weapons, items, etc. I have also provided an example scene with random terrain generation. Check it out: Unity Asset Store - The Best Assets for Game Making