- Home /
The question is answered, right answer was accepted
Question about "Chunks"
Hey I'm having a little trouble with "Chunks" I understand how they work logically but I can't for the life of me figure out how to put it into practice programmatically. I've checked out a few examples but I'm still quite stuck so I thought I'd see if anyone on here could maybe write up some pseudo code and explain it a bit.
Thanks in advance everyone, Carl
Explain 'Chunks'. I'm not sure what you're trying to do here! Are we talking $$anonymous$$inecraft here? Or does it mean something else? I think you would do that kind of thing with LoadLevelAdditively, but you might not (depending on how it works).
Sorry I should have been clearer, apologies. Yes similar to $$anonymous$$ecraft, where the level is split up into "Chunks" sections, it's similar to Octrees (Which I also can't wrap my head around programmatically) http://en.wikipedia.org/wiki/Octree
So if I had my terrain generated already how would I split it up into chunks or would I have to do as part of the generation process?
Thanks, Carl
Octrees are fantastic, although yeah, hard to program if you can't get your head around them.
You should split your terrain up into chunks when you generate it, and then manually load the ones you want when they become relevant. There are a lot of ways of doing this, and as I said before, it really depends on exactly what your application is.
Well I'm going to try for something similar to $$anonymous$$ecraft as it seems the easiest thing to practice and learn with.
Lets say I was to generate 64x64x128 set of primitives to then divide up into 4 16x16x128 chunks, and to keep it simple just iterated through 3 loops instantiating a primitive to get a cube of cubes (not worrying about vertices, triangles etc)
for (int x = 0; x < 64; x++) { for (int y = 0; y < 128; y++) { for (int z = 0; z < 64; z++) { // intantiate primitive at x,y,z } } }
How would I go about slicing it into chunks? Or would you recommend doing it a different way? I'm not trying to make a full game I'm just trying to broaden my knowledge and learn efficient methods
Thanks, Carl
(It wouldn't let me format the code in a comment)
Ok so after so more researching I've managed to work out octrees to a degree but I'm still stuck with "Chunks" and indexing them
Answer by DavidDebnar · Oct 17, 2011 at 09:10 PM
Minecraft is really advanced stuff but the basics are quite simple. Notch generates few perlin noises and uses them as density map and height map and some other ones. Then he iterates trough the pixels of the noise and splits it into "chunks". If you want good optimization and you are working on a minecraft like game than a 32x32x128 chunk has really good performance. So you iterate trough the noise and at every 32 pixel just split it into another chunk. Also one chunk is one mesh. So you have to add sides, yes sides not cubes, to it, and check if the side can be seen, so you don't waste ram. Chunks are good for not having the whole world in ram at once.
Short: World is made of chunks. Chunks are made of faces that make cubes. Chunks are parts of noise. Don't use instantiate it will kill your PC/MAC, instead use the Mesh class and generate meshes manually.
Also, take a look at MinePackage. It is a really nice terrain generation algorithm made in unity, and you can download the source code.
Edit: I read your coment about "indexing the chunks". If you want to save it, just save the noise and the changed cubes instead of whole meshes and generate the world again and again on each run from the noise + changed cubes.
David
Thank you for the reply dzebna, I knew most of that already but I appreciate you taking the time to reply and im sure itll help others
I was looking at $$anonymous$$inePackage but it's quite complicated to break down, I'm getting there I just wondered if anyone else could explain how to go about splitting into chunks, programmatically anyway
So just make another int and in the z loop just increment it by one if its 32 create a new chunk and reset the counter, something like that?
Thank you
Carl
EDIT: Actually you would have 2 ints one for the x axis and one for the z axis otherwise it will only create "chunks" on the x or z axis?
Yes, you have to iterate trough your blocks and then add them to your chunks. One chunk is one gameobject so when you add a new block to it, just add the sides that are visible by code, do not instantiate if you want better (or even some) performance.
Follow this Question
Related Questions
Sending Level Data over Network 1 Answer
cube based enging 1 Answer
I need to instantiate a bunch of objects without performance dips 1 Answer
Random generated debris in empty space 1 Answer
How Do I Fix My Grass? 2 Answers