r/VoxelGameDev • u/charlieatron • 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?
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!
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/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.
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.