Creating terrain chunks

I had to rewrite my code for the terrain because it consisted out of 1 big mesh. So I decided to split it up into smaller parts (32x32x32). My world itself is 256 x 128 x 256 units big. I’ve never really done this before so I did some calculations…

256 x 128 x 256 devided by 32 would give me 8 x 4 x 8. I made a 3 dimensional array:

[java]TerrainChunk[][][] terrainChunks = new TerrainChunk[chunkX][chunkY][chunkZ];

final int chunkX = 8;

final int chunkY = 4;

final int chunkZ = 8;

for (int z = 0; z < chunkZ; z++) {

for (int x = 0; x < chunkX; x++) {

for (int y = 0; y < chunkY; y++) {

terrainChunks[x][y][z] = new TerrainChunk(m);





In the for loop above I then create a 32x32x32 array for each element in the terrainChunk.

[java]public TerrainChunk(Main m) {

gamePointer = m;

chunk = new byte[32][32][32];


After that I then check what pieces of the array should be filled for each value that the heightmap contains I create an entry (value = 1) in my 32x32x32 array.

[java]for (int z = 0; z < lengthZ; z++) {

for (int x = 0; x < lengthY; x++) {

for (int y = 0; y < (heightmap.getTrueHeightAtPoint(x, z)*0.08f); y++) {

terrainChunks[(x / 32)][(y / 32)][(z / 32)].InitTerrainChunk(x/8, y/4, z/8);




The loading time is absurd long. Even when I don’t have any code behind the InitTerrainChunk() which contains all my code for creating the terrain and making it visible in the scene.

I think somewhere I’m creating too much arrays or it loops too much somewhere I tried the whole night to pinpoint the error but I just cannot find it…

Since you are dividing x,y,z by 32, I can only assume that these are the full “world” coordinates… if so, then you are calling InitTerrainChunks 323232 times more often than necessary.

Loop so that you get rid of the division by 32 and you will be much happier, ie: loop in chunk coordinates.

Actually, I have trouble making sense of it completely because I’m not sure why you’d be passing in x/8, y/4, z/8 to your init terrain chunk method.