- Home /
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:
Only draw visible cubes - but how would I decide which are visible to the third person camera?
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.
Thanks Tom
My reading so far:
http://forum.unity3d.com/threads/63149-After-playing-minecraft...
http://forum.unity3d.com/threads/69573-MinePackage-Minecraft-starter-package/page3
http://answers.unity3d.com/questions/177931/tutorial-minecraft-chunks.html
Answer by Nonakesh · Oct 28, 2012 at 10:53 AM
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: http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html
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: http://en.wikipedia.org/wiki/Marching_cubes Just search for marching cubes and you'll find the code for it. That algorithm will give you a smooth terrain.
Hi, thanks for the reply.
Would you recommend $$anonymous$$arching cubes over Perlin then?
Also, what exactly is a UV? I know what meshes are though :)
$$anonymous$$arching cubes is just a method to display the voxels, not to generate the terrain, so you'd have to use both to get a smooth terrain.
The UV coordinates are used to tell the graphic engine which part of a texture should be used, so if you have a quad with the coordinates
(0 | 0) (0 | 1)
(1 | 0) (1 | 1)
, it will show the whole texture on this quad.
If you use this:
(0 | 0) (0 | 0.5)
(0.5 | 0) (0.5 | 0.5)
It will show the upper left quarter part of the texture.
That is exactly one of the things you could try out by using a 3D modelling program like Blender, but I think it would be quicker to just read a article about it. Like that one: http://en.wikipedia.org/wiki/UV_mapping
Just google it.. you'll definitely find out more about it!
Imagine a texture sheet with textures like Dirt, Stone, Grass and so on, ordered in a grid. You can easily set the UV coordinates to one of the textures, by using the coordinates of the grid.
ok, UV mapping is pretty simple, I didnt realise UV was just a way of saying XY (but without using X and Y :P).
So, basically, I should:
Generate perlin noise to get the height maps.
Iterate through each block location, if the heightmap is equal to the Y coord of the current block, generate grass. If the Y coord is smaller than the perlin height, generate a random block like dirt or stone, if the Y coord is higher than the height from perlin, generate air .
Generate vertices from the block array, and use that to make a mesh?
I am still confused as to how to do step 3.
I think that's the best way to do it. Although I think you shouldn't use the y coordinates to tell if it is grass or not. Just let the first block on the top be grass. For something like stone I would do something like "Every block X blocks under the top block is stone" X should be a bit random...
Step 3 is probably the complex part of the whole thing. I think you should only show blocks that have a transparent block directly touching them. If you do this the polygon count will be drastically reduced.
Any idea how I would deter$$anonymous$$e which faces should be shown? Or should I simply just iterate through the 6 faces and look for transparent blocks?
Answer by WizzDE · May 03, 2014 at 04:46 PM
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: https://www.assetstore.unity3d.com/#/content/17470
Your answer
Follow this Question
Related Questions
Texture mapping procedural mesh with texture atlas? (Minecraft style terrain gen) 1 Answer
Minecraft Biomes: How to Make? 2 Answers
World Generation with Interesting Terrain 0 Answers
Tiled Terrain 0 Answers
Minecraft (Cube) Terrain 1 Answer