Skip to content

Instantly share code, notes, and snippets.

@isXander
Created February 14, 2024 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isXander/466db16982f0755f9def11fb64056ac5 to your computer and use it in GitHub Desktop.
Save isXander/466db16982f0755f9def11fb64056ac5 to your computer and use it in GitHub Desktop.
pub fn receive_generated_chunks(&mut self, texture_atlas: &Arc<TextureAtlas>) {
while let Ok((coords, state)) = self.chunkgen_thread_receiver.try_recv() {
let (x, z) = Self::unpack_coordinates(coords);
println!("Generated chunk at {},{}", x, z);
self.chunks.insert(coords, state);
let state = self.chunks.get(&coords).unwrap();
if let ChunkState::Loaded(LoadedChunk::Stored { chunk }) = state {
// once chunk has generated, check its neighbours if they are now able to mesh, with all 4 of their neighbours
// loop thru newly gened's neighbours
for (nx, nz) in [(0, 1), (0, -1), (1, 0), (-1, 0)] { // diagonal neighbours aren't needed
let (nx, nz) = (x + nx, z + nz);
let neighbour = self.get_chunk(nx, nz);
if let Some(ChunkState::Loaded(LoadedChunk::Stored { chunk: neighbour_chunk })) = neighbour {
// we found a stored neighbour, now check if this chunk now has all 4 neighbours
let surrounding_neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)].iter()
.filter_map(|(nnx, nnz)| {
let (nnx, nnz) = (nx + nnx, nz + nnz);
let nn_state = self.get_chunk(nnx, nnz);
if let Some(ChunkState::Loaded(LoadedChunk::Stored { chunk })) = nn_state {
Some(chunk)
} else {
None
}
}).collect::<Vec<_>>();
let neighbour_is_surrounded = surrounding_neighbors.len() == 4;
// this neighbour can now be meshed
if neighbour_is_surrounded {
let nn_up = surrounding_neighbors.get(0).unwrap();
let nn_down = surrounding_neighbors.get(1).unwrap();
let nn_right = surrounding_neighbors.get(2).unwrap();
let nn_left = surrounding_neighbors.get(3).unwrap();
self.dispatch_chunk_mesh(
nx, nz,
neighbour_chunk,
nn_left, nn_right, nn_up, nn_down,
texture_atlas,
)
}
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment