// Find chunks within render distance which are not yet being loaded // and tell the chunk loader thread to load them private void loadNewChunks() { Vector3Int p = getPlayerChunkPos(); playerChunkPosition = p; if (!p.Equals(prevPlayerChunkPos)) { Queue <ChunkLoaderThread.ChunkRequest> toQueue = new Queue <ChunkLoaderThread.ChunkRequest>(); for (int y_ = 0; y_ < Constants.RENDER_DISTANCE; y_++) { // Alternate back and forth //So y= 0,1,-1,2,-2,3,-3, etc. int y = -((y_ % 2) * 2 - 1) * ((y_ + 1) / 2); // Load each layer of chunks in rings from player position outwards for (int i = 0; i < Constants.RENDER_DISTANCE; i++) { for (int x = -i; x <= i; x++) { for (int z = -i; z <= i; z++) { if (x == -i || x == i || z == -i || z == i) { if (chunkCoordsToLength(x, y_, z) <= Constants.RENDER_DISTANCE) { ulong key = Chunk.getHashKey1(p.x + x, p.y + y, p.z + z); if (!allChunks.ContainsKey(key)) { // Chunk has not been loaded before, create new chunk object Chunk c = new Chunk(p.x + x, p.y + y, p.z + z); allChunks.Add(c.getHashKey(), c); toQueue.Enqueue(new ChunkLoaderThread.ChunkRequest(p.x + x, p.y + y, p.z + z)); } else { // Chunk has been loaded before Chunk c = (Chunk)allChunks[key]; if (c.destroyed) { // Chunk was loaded but went out of render distance and was freed c.destroyed = false; toQueue.Enqueue(new ChunkLoaderThread.ChunkRequest(p.x + x, p.y + y, p.z + z)); } } } } } } } } // Get players direction in each axis (-1/0/1) and send chunk requests Vector3Int playerDirection = new Vector3Int(); Vector3 rotation = cameraControl.GetRotation(); rotation.y %= 360.0f; if (rotation.y < 0) { rotation.y += 360; } playerDirection.y = rotation.x > 15.0f ? -1 : (rotation.x > -15.0f ? 0 : 1); if ((rotation.y > 360 - 15 || rotation.y < 15) || (rotation.y > 180 - 15 && rotation.y < 180 + 15)) { playerDirection.x = 0; } else { playerDirection.x = rotation.y < 180 ? 1 : -1; } if ((rotation.y > 90 - 15 && rotation.y < 90 + 15) || (rotation.y > 270 - 15 && rotation.y < 270 + 15)) { playerDirection.z = 0; } else { playerDirection.z = (rotation.y > 90 && rotation.y < 270) ? -1 : 1; } chunkLoaderThread.queueChunks(toQueue, p, playerDirection); } prevPlayerChunkPos = p; }