public void LoadChunk(Vector3i chunkPos) { Chunk chunk = chunkPool.Get(); chunk.Setup(chunkPos, this); // Skip generating if outside terrain generator bounds if (chunk.CheckTerrainBounds()) { chunk.GenerateVoxelData(); } // Notify adjacent chunks this chunk can be used for meshing chunk.FillAdjChunks(); // Added the chunk to the dictionary chunkMap.Add(chunkPos, chunk); // Mark the chunk position as complete and remove from the queue chunkQueue.Remove(chunkPos); }
public IEnumerator LoadChunkThreaded(Vector3i chunkPos) { Chunk chunk = chunkPool.Get(); chunk.Setup(chunkPos, this); // Skip generating if outside terrain generator bounds if (chunk.CheckTerrainBounds()) { // Start a new thread to generate the voxel data threadCount++; bool done = false; Thread thread = new Thread(() => { chunk.GenerateVoxelData(); done = true; }) { Priority = System.Threading.ThreadPriority.BelowNormal }; thread.Start(); // Corountine waits for the thread to finish before continuing on the main thread while (!done) { yield return(null); } threadCount--; } // Notify adjacent chunks this chunk can be used for meshing chunk.FillAdjChunks(); // Added the chunk to the dictionary chunkMap.Add(chunkPos, chunk); // Mark the chunk position as complete and remove from the queue // This is needed for threaded loading as there is a delay between dequeuing and it being added to the chunkMap chunkQueue.Remove(chunkPos); }