- Home /
How to load stacking chunks on the fly?
I'm currently working on an infinite world, mostly inspired by minecraft.
A Chunk consists of 16x16x16 blocks. A block(cube) is 1x1x1.
This runs very smoothly with a ViewRange of 12 Chunks (12x16) on my computer. Fine.
When I change the Chunk height to 256 this becomes - obviously - incredible laggy.
So what I basically want to do is stacking chunks. That means my world could be [∞,16,∞] Chunks large.
The question is now how to generate chunks on the fly?
At the moment I generate not existing chunks circular around my position (near to far). Since I don't stack chunks yet, this is not very complex.
As important side note here: I also want to have biomes, with different min/max height. So in Biome Flatlands the highest layer with blocks would be 8 (8x16) - in Biome Mountains the highest layer with blocks would be 14 (14x16). Just as example.
What I could do would be loading 1 Chunk above and below me for example.
But here the problem would be, that transitions between different bioms could be larger than one chunk on y.
My current chunk loading in action
For the completeness here my current chunk loading "algorithm"
private IEnumerator UpdateChunks(){
for (int i = 1; i < VIEW_RANGE; i += ChunkWidth) {
float vr = i;
for (float x = transform.position.x - vr; x < transform.position.x + vr; x += ChunkWidth) {
for (float z = transform.position.z - vr; z < transform.position.z + vr; z += ChunkWidth) {
_pos.Set(x, 0, z); // no y, yet
_pos.x = Mathf.Floor(_pos.x/ChunkWidth)*ChunkWidth;
_pos.z = Mathf.Floor(_pos.z/ChunkWidth)*ChunkWidth;
Chunk chunk = Chunk.FindChunk(_pos);
// If Chunk is already created, continue
if (chunk != null)
continue;
// Create a new Chunk..
chunk = (Chunk) Instantiate(ChunkFab, _pos, Quaternion.identity);
}
}
// Skip to next frame
yield return 0;
}
}
Update
kay here is a picture of a - what I call it - 'layered chunk'. Taken from Minecraft.
What you see here is actually one chunk - in sense of game mechanics. But in sense of logic / programming these are 16 chunks. 1x16x1 chunks.
So the uppermost layer is a noise-generated field (or whatever, a surface - to keep it simple) All underlying layers are 'filled' (every block is a material, not air).
Since the image is very large, I've decided to just link to the image. IMAGE
Update2:
Well while still thinking about this I came to the following idea, I'll try now:
Load every chunk on x/z viewrange. When the chunk is empty / transpernt (full of air) then I'll go as long as the chunk is empty go one layer down.
I think this should be a way to go. Maybe I'll always load the chunk below the loaded chunk, to avoid falling into a void.
As a design problem, you may want to ask this on Unity Forums ins$$anonymous$$d of here.
Sorry for the off-topic, but your script looks cool! Could I see it, maybe I could help you, if you could use Skype it would be great! $$anonymous$$ine is: ikillnukes Bye! ^^
Answer by Bunny83 · Aug 02, 2014 at 04:14 PM
You don't need any fancy logic how to decide which chunk you should load next, Just create a "sorted job queue". So all you do is adding just the position (of logical representation of your chunks) that are within the current view range into a list. You then sort the list by the distance of the chunk from the player. The nearest ones are generated first.
When moving around you would check in intervals if all chunks in the view range of the player are already loaded. if not, add them to the loading list.
Your loading coroutine just picks a chunk from the list (if there is any), removes that chunk from the loading list and loads the chunk.
You need of course another list to hold the currently loaded chunks. This is also important when you want to remove chunks too far away. Keep in mind to use a larger unloading radius and a smaller loading radius to prevent unnecessary loading / unloading when the player moves in a small area.
Thanks for your answer, I really appreciate that. When you want to discuss, you can even join my Forum post here (as suggested by @robertbu)
Okay even when that makes sense but let me answer with an question:
$$anonymous$$y world is 256 Blocks high. When I'm at y 200. The lowest seen block is at y 150. Why shall I load chunks below this?
I don't have any chunk (un)loading yet - but it is obviously that if the count of loaded chunks rise, the performance sinks. So - why shall I load the 'useless' chunks?
$$anonymous$$aybe I get you wrong!
Well, i'm not sure if i understand your question. Do you want to say that you have a max-view-distance of 50 blocks? That sounds a bit strange to me, however i don't know your world scale, i just compare with $$anonymous$$ecraft which has the exact same world layout (however in mc a chunk actually is from 0 to 255 which isn't possible in Unity since Unity only provides 16bit index buffers).
If your max-view-distance is greater than 50, how do you know that you can stop at 150? It depends on the content of each chunk if the chunk below is visible or not. Since it would be a mess to figure out if the chunk below is visible or completely covered you usually just load them all. At least all within your max-view-dist range.
$$anonymous$$eep in $$anonymous$$d that you only have to create surfaces which are adjacent to air / transparent blocks. If a surface of one voxel / block is covered by a solid block, it doesn't need to be drawn. That's what mc does. So a solid 100x100x100 block of stone (which consists of 1 million cubes and 6 million surfaces) would only need 60000 quads. This optimisation can be done perfectly in a thread as it is based solely on your voxel data.
I said that already a couple of times: A lot people look down at mc and think "such a primitive game" but it's actually one of the most advanced games when it comes to technology. $$anonymous$$c has come a long way and if you want comparable results it won't be much easier for you ;)
Your answer
Follow this Question
Related Questions
Tiled Terrain 0 Answers
Smooth Voxel terrain. How is it done? 2 Answers
Voxel based Terrain generation. Pointers please. 2 Answers
how to create chunks in voxel terrain generation 1 Answer
Anyone got a Voxel generator I can use? 0 Answers