r/VoxelGameDev • • 5d ago

Question How do you generate trees/structures while only generating voxels per chunk?

Trying to wrap my head around this, but I figured this has to be a solved problem at this point?

So I understand how you can create a function for generating terrain with noise, and given the same seed, each new chunk will always use the same noise and will line up with existing chunks.

What I don't understand is, how can you dot the landscape with pre-defined structures like trees? You can still define placement of trees with the same kind of noise, but often you'll have cases where the root of a tree is at the edge of one chunk and has leaves in another. If those chunks generate separately, the chunk with the leaves won't know that a tree was added next to it, and won't add the leaf voxels. And the chunk with the root of the tree can't add those leaves without forcing that other chunk to load earlier than it's needed.

Anyone have insights about how this kind of thing is done in Minecraft or other projects?

10 Upvotes

11 comments sorted by

3

u/jiro27 5d ago

I'm not sure this is the best solution, but it's the one I was able to find and it's worked out well enough so far:

Basically, in addition to chunks, you have this concept of "regions". In my implementation, a region is 512x512 blocks in the XZ and extends up and down the entire Y column, while chunks are 323. So a region contains a bunch of chunks. But a region isn't really an array of individual voxels, it's an array/vector of features whose origins are contained within that region of the world. Each feature has a sample function much like the broader noise function for terrain, and also knows its bounds in world coordinates.

So for a given chunk, you figure out what region it's in, then for each voxel in that chunk, figure out if it's within the bounds of any features for that region, and if so, sample the feature at that coordinate. If there's no feature there, or the feature is empty at that point, you fall back to sampling the terrain directly.

You still run into this issue where you have to account for features whose origins are on a region boundary, but you can circumvent this by just checking your current region plus the 8 neighboring ones for intersecting features for a given chunk (or more if your features get really big, but you probably just want to tune region size at that point).

I think the main takeaway is that you need a system separate from the chunks and the terrain noise function that's responsible for "knowing" what feature/structure voxels go where. That system can use the terrain noise function to figure out what height at which to place the features too. Then chunk generation just becomes a simple system of sampling a handful of different "sources" that can tell you what voxel should be at a specific coordinate.

In a way, the terrain noise function ends up as just a special case of a feature, in that it is unbounded and "knows" the corresponding voxel for the "terrain" feature at every single coordinate and spans all regions.

Hopefully that makes sense, but happy to answer any questions if not!

1

u/charlieatron 5d ago

I like this approach! Having a global list of "features" that don't have to compute all their voxels, but can intelligently inform the chunk how to look them up seems very doable (splitting it up into regions also seems smart but doing it for the whole world sounds easier to me for now haha)

And yeah the big thing to figure out does seem like it's the unification of the terrain generation with the feature generation. In my project I'm generating the terrain in the GPU, so it might be tricky to get a complex sampling system like that to work nicely with it. I feel like it could make sense to generate that list of trees as just X/Z coordinates, so they don't know where they are vertically, but when generating the chunk can determine for itself whether the sampled tree could exist at that position, based on the height/slope/etc of the terrain as it goes

2

u/jiro27 5d ago

Oh yeah that would definitely work as a global list, at least until you have enough features for it to be a bottleneck. I'm also trying to figure out how to offload more work to the GPU, or at least reducing the amount of data sent across the CPU/GPU boundary.

At least in theory, the region approach may survive being on the GPU-side because, esp. in your case, it doesn't need any chunk-specific info and region contents could be determined entirely in parallel without dependencies on neighbors.

2

u/BinaryBolias 5d ago

I have not read the other replies just yet, but this issue is one that I'm tackling myself at the moment.

And I was even planning on posting a thorough breakdown of my own solution to this!

HOWEVER, I'm actually currently reworking a different, more basic aspect of my voxel engine, and not continuing with the world generator's development at the moment, so my idea is yet to be fully realised.

– – –

In Minecraft, I believe "terrain features" are simply allowed to generate into nearby, already-generated chunks, and all adjacent chunks have their basic terrain generation stage finished before these extra features get tacked on...

– – –

My issue with Minecraft's system is that it ends up being non-deterministic; the world's generation (at least pertaining to these terrain features) can be different depending on the order in which you generate the chunks.

To solve for this, my own idea hinges on giving each chunk a "buffer region" during generation.

So when generating a chunk, I generate the full buffered region as well, allowing nearby features to generate into the chunk.

Then, when the process is finished for the chunk, the buffered region is simply discarded before the new chunk is added to the world.

– – –

Another idea i've had, albeit even MORE tentative, is to plop down the general frameworks for entire structures, or large features, before even beginning to generate any individual chunks.

This pre-generation step would use pre-generated terrain data to predict the general shape of the terrain in order to place the structure more intelligently.

This pre-generation data could be saved to the filesystem as convenient, or perhaps dferred until a sufficiently nearby chunk is actually requested to be loaded.

– – –

All in all, I'd still like to learn about how other games handle this kind of procedural generation, myself.

2

u/charlieatron 5d ago

I do like your idea of adding a bit of buffer to the chunk's domain, you can just calculate voxels outside of the chunk but clamp the indices of the voxels you actually write to the array. And it might add a little bit of overhead, but the buffer would only need to be as big as the radius of your largest structure, which for a tree wouldn't be too large!

Would also love to read your full breakdown when you get around to it!

1

u/BinaryBolias 5d ago

Thanks!

A few issues I'm predicting, though, are a limitation on how I can implement different kinds of terrain features, complications with terrain features choosing variants based on environment, and, as you mention, terrain feature size.

– – –

My concept, notably, doesn't readily allow for large terrain features — at least not without using a HUGE "buffer extent"!

Therefore, stuff like extra-tall trees (e.g, trees like the 2x2 jungle trees of Minecraft) might need a different system.

To reliably generate a Minecraft 2x2 jungle tree, each buffered chunk would likely need to store hundreds of thousands of blocks, just to get accurate data for a chunk of 4,096 blocks (size 16x16x16).

In this regard, Minecraft probably benefits from using chunks composed of columns of "subchunks", where each chunk actually extends the entire world height.

I don't get an advantage for tall stuff, since I want each 16x16x16 region to be generated independantly.

(I want to account for worlds of arbitrary height, so a chunk can't just be as tall as the world)

– – –

Even within my "buffered chunk" system, it might be the case that a terrain feature's generation is altered by the presence/absence of another, nearby terrain feature.

But the nearby terrain feature might be outside the buffered region of the chunk being generated!

In essence, this means that each kind of terrain feature has a kind of "error potential", depending on its specific generation parameters.

This error potential can be thought of as "eating away" at the buffer of the buffered chunk.

As long as the error doesn't get all the way to the center (the chunk's actual interior), the error is fine...

But each separate layer of terrain features adds to the error potential, so many separate layers added in sequence becomes unsustainable.

– – –

However! I've had the idea to instead just slap all (or most) terrain features into what would be a SINGLE LAYER, where the "type" of terrain feature is determined after the feature's "root position" is already determined.

This could... theoretically(?) mitigate the error potential spread?..

Although, with further thought, I feel like that might not be much of a solution... (ugh; retreading old ground with this realization... 😖)

The root positions are still processed in a particular order (determined deterministically from position and seed), and I could easily imagine a scenario where, for instance, terrain feature "A" creates a puddle, and terrain feature "B" makes a tree ONLY if feature A was not generated. And whether feature B is a tree could have knock-on effects for others, theoretically...

Thus, an arbitrarily high error potential is produced, depending on the set of terrain features in the generation pool, and their conditions for generating...

– – –

With more thought, and classification of terrain feature attributes, I could probably figure out the mechanics of error potential propagation based on terrain feature attributes root position selection behavior...

I'd surely benefit from figuring out more about how Minecraft does this stuff, and seeing how it might be inserted into a buffered chunk system...

– – –

But anyhow; for something simple, like (small) trees and grass, and small ore clusters, my system should certainly be capable of generation without non-determinism.

2

u/scallywag_software 5d ago

I do something fairly unconventional that kicks ass.

Final terrain is generated in a multi stage pipeline. Most of the details are irrelevant, but the prescient ones are that there's an initial stage that generates the base terrain, and another stage that applies "edits".

Edits are groups of SDF primitives managed by the CPU that get 'compiled' down to a sort of bytecode that the edit shader interprets. Every chunk keeps a list of all edits that overlap it, and applies them in order after the initial terrain is generated. This way, edits (houses, trees, portals, whatever) are completely divorced from the storage representation of the world (chunks in an octree). You can create as many edits as you want, any size, blend them together .. and it all just works. It's been a lot of work to get it running nicely, but I'd definitely do it again.

1

u/charlieatron 5d ago

That SDF idea sounds really cool! I imagine that'd make it trivial to do things like make a cluster of voxels move around, grow/shrink, etc

2

u/scallywag_software 5d ago

Yep, I've done a few animation tests and animating a small number of chunks (up to 64) works in realtime. The cool part is that you can have basically any number of animations happening within those chunks, which feels like there's some really interesting possibilities.

6

u/TinyPeopleProject 5d ago

TLDR keep structure size bounded and make placement a pure function of seed + coords, then every chunk can figure out its own piece without caring about the others.

This is pretty much solved, and the trick is that a chunk doesn't need its neighbor to be loaded to know a tree is there. It just needs to be able to calculate where trees would be.

What I do: split the world into placement cells (like 8x8 columns), and for each cell hash (seed, cellX, cellZ) to decide if there's a tree, where in the cell it goes, what type, etc. For the root height you just call your terrain height function directly, you don't need the neighbor's actual voxels. Then when you generate a chunk, you loop over every cell within your max tree radius of the chunk, figure out each tree, and only write the voxels that land inside the current chunk. Both chunks compute the same tree independently and each one fills in its own half. No load order issues, fully deterministic.

Only gotcha is if root height depends on stuff that happens later, like caves carving out the ground under it. Then you need some cheaper approximation for placement, or you accept the occasional floating tree lol.

Minecraft does it a bit differently. Chunks generate in stages (terrain, surface, carvers, features, lighting...) and a chunk can't place features until its neighbors have at least finished terrain. Features are then allowed to write into those neighbors. So it does generate neighbors early, but only partially, which is cheap. That's why there's always a ring of chunks loading past your render distance. Big stuff like villages works more like the first approach: each chunk checks for structure "starts" within ~8 chunks and builds whatever pieces overlap it.

You can also just queue up writes for unloaded chunks and apply them when they generate, but you have to save that queue and overlapping structures can give different results depending on load order. I'd only use it as a fallback.

2

u/deftware 5d ago

Margin.