Terrablox Source

Posted on 29/10/2014

So as a break from ignoring Terrablox I've decided to write it again from the ground up. This is my usual way of implementing real changes. Every version I've ever written has had vast improvements, but it quickly becomes dead weight again as I learn more. But this time, I'm coming at it with a different intention; I'm going to make just a core engine and release the source (which, at least for learning purposes and contributions, will be free). Later on, I will get back to the GAME side of it, using this new engine. So that's the goal.

As this is an engine, foremost, it'll have features that people have wanted and generally expect in this kind of system. So, procedural generation is a key feature. Onto this, a loose chunk system; chunks are simply stand alone objects, so they can be placed anywhere and in any configuration and will keep the terrain seamless if you fill in the gaps later. Memory is something I've already brought down using buffers with 8 bits per value, rather than GM's ds_grids which are 64 bits per value.

I plan to take memory usage further down by using RLE (Run-Length Encoding), as I've so far had no need to access every voxel thanks to my new mesh building system. When building a mesh, previous Terrablox done it in layers, I always knew stacks/columns would be faster and now I'm doing it that way. The thing with columns is you can quickly find where the first visible block is going to be (I track the population of each stack) and start the scan from there, continuing to the point where the last visible block will be. So, at least on a featureless terrain thus far, it's super fast to build the mesh as it reads the minimal number of voxels. And with RLE, even with complex features, it will be able to skip over the gaps, and when comparing to adjacent columns knowing exactly where everything actually visible starts and ends.

So the previous Terrablox, at my last reckoning could hold around 20,000,000 voxels in memory I think, before it hit the GM limit of about 1.7GB, it could render more but it'd just be a wrap of the world. This engine so far, without RLE, the most "physical space" I''ve had is 170,000,000 voxels (and there was plenty of room left). It took a while at the start to generate but once it had it built the meshes 1 chunk at a time at 100+fps, while drawing them all, I think since then I've optimized it further.

But enough of that nonsense, here's a picture:

tbsource1
This is only just over 8,000,000 voxels I think, the world is 256 x 256 x 128. But the reason I'm showing this one is to do with the procedural generation. This terrain is made using simplex noise, I've made a few area types, like swapland and mountains. This is more of a river/valleys place, as it produces very nice river-like meanders just from the in-between bit of the positive and negative areas of noise (you make them all positive and you're left with these twisting seams just at position 0, good for rivers!). Later there will be separate passes for features like lakes and canyons. So producing the height is one thing, later on I'll be trying to blend all these different types of terrain together, biome style. Another thing I'd like to mention about this, and it's just something I'm playing with, is the density of dirt in the lower areas. My thinking here was that dirt and sediment would naturally fall down and end up lower, and it's pretty trivial to determine this if you know the max height of your region. And as you can see from the cross-section I think it makes visual sense and looks quite natural.

Anyway, that's that. This post was a lot longer than I expected it would be, I guess that's not a bad thing, but there you go. :)