[Unity] Voxel terrain generation - 0 - Basics

in voxel •  6 years ago  (edited)

Here i will show you the idea behind a Voxel world generation in Unity.

There are a few things you have to make sure or keep in mind :

- Unity must be at version 5 or higher (2018 is better).

- You know C# basics.

- Don't copy-paste the code, write it by yourself, you will understand the idea and won't be stuck on errors.

- I am using Visual Studio to make things easier and because Unity 2018 doesn't support MonoDevelop anymore.

I will first explain the whole idea, then a few basics you will need to know. Also, note that this guide won't be optimized. It is a simple base giving fundamentals for more complex projects.

- How will it be generated?

The idea is that the world is divided in "Chunks", each one containing "Blocks" : 

We can't have a massive array containing each block in the world, so we divide it in 10x10x10 (or 16x16x16 to keep a common value) chunks, each containing it's own blocks.

So, we will have a "Block" script containing every informations of one block. Each block have an instance of this script with it's own values (Name, texture, is solid, ...).

We will have a "Chunk" script containing every block in a 10x10x10 zone, next to this chunk there is another one with 10x10x10 blocks. Note that there are also full air chunks.

And a "World" script which will generate columns of 8 chunks on top of each other. It will also contain common values like "world size", "chunk size", terrain textures, etc.

Also, there will be some other scripts like the noises system or the block shape generation, but they will be explained when we will need them.


Now that you know how it will be contained, we can understand how it is drawn :

First, the chunks script will have a 3D array (map) with a given size (10x10x10). This array will contain every block, it is a virtual visualization of the chunk, giving each block at each position inside the chunk. It also helps looking in a neighbor chunk. After generating the array, we assign blocks in it :

We start by what i call the "Chunk virtualization". We loop three times in the 3D array, each loop for each axis (x,y,z), so at each loop we have a position in the chunk map, which is also the block position in the chunk. At each position we look at, we check if we are supposed to have a block here, if we can, we give the array the block we want. Then we continue looping to every next position. (Also, if we are not supposed to have a block somewhere, we put an air block by default. We can't have a null value).

Next, we "Draw" the chunk. We loop once again in the chunk map, but this time, for each time we added a block different to air, we draw the block. When we draw the block, we read what values are stored in it (Texture, shape). Every values we created will be stored in lists inside the chunk. These lists makes the chunk final shape (They will be explained in the next part).  When every block is drawn, we have the final chunk created and we can add collision to it.

The world script will create chunk columns next to each other and then draw every chunk that exists.

We must generate the chunks before drawing all of them. Because during the drawing part, chunks will have to access their neighbors and we have to make sure they already exist.

- The limits

Before jumping into your new project, you must know how it will be done and respect Unity's limits. Here i won't give an optimized explanation, it will be a very basic Voxel terrain, just to get the idea. The complex generation will be explained later. But we still need a minimum, we don't want Unity to crash every time.

Keep in mind that Unity wasn't made for voxel terrain generation. It is optimized for a lot of calculations and lighting systems, so it will still work behind your code and you will easily lose performances. Optimizing the project is hard and requires a lot of work on the code, that's why we will do it later.

There are also some things to consider, like building the project to remove the huge impact from the editor, working without lights, testing without quality when you want to make sure things are working.

(There will be a chapter about optimization for Low specs computers)

- Final note

This guide will be in multiple parts, each one for a new implementation. The final result, for now, will be a world which can me modified by the player, with grass, structures and advanced noise.

(For any suggestion, idea, improvement or error you noticed, don't hesitate to comment).

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!