This commit is contained in:
griffi-gh 2023-01-27 02:14:10 +01:00
parent 90c5b2a1e2
commit 6b8deab094
2 changed files with 5 additions and 2 deletions

View file

@ -1,6 +1,7 @@
use flume::{Sender, Receiver}; use flume::{Sender, Receiver};
use glam::IVec3; use glam::IVec3;
use shipyard::Unique; use shipyard::Unique;
use rayon::{ThreadPool, ThreadPoolBuilder};
use super::{ use super::{
chunk::BlockData, chunk::BlockData,
render::ChunkVertex, render::ChunkVertex,
@ -33,16 +34,18 @@ pub enum ChunkTaskResponse {
#[derive(Unique)] #[derive(Unique)]
pub struct ChunkTaskManager { pub struct ChunkTaskManager {
channel: (Sender<ChunkTaskResponse>, Receiver<ChunkTaskResponse>), channel: (Sender<ChunkTaskResponse>, Receiver<ChunkTaskResponse>),
pool: ThreadPool,
} }
impl ChunkTaskManager { impl ChunkTaskManager {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
channel: flume::unbounded::<ChunkTaskResponse>(), //maybe put a bound or even bound(0)? channel: flume::unbounded::<ChunkTaskResponse>(), //maybe put a bound or even bound(0)?
pool: ThreadPoolBuilder::new().num_threads(4).build().unwrap()
} }
} }
pub fn spawn_task(&self, task: ChunkTask) { pub fn spawn_task(&self, task: ChunkTask) {
let sender = self.channel.0.clone(); let sender = self.channel.0.clone();
rayon::spawn(move || { self.pool.spawn(move || {
let _ = sender.send(match task { let _ = sender.send(match task {
ChunkTask::GenerateMesh { position, data } => { ChunkTask::GenerateMesh { position, data } => {
let (vertices, indexes) = generate_mesh(data); let (vertices, indexes) = generate_mesh(data);

View file

@ -18,7 +18,7 @@ pub fn generate_world(chunk_position: IVec3, seed: u64) -> BlockData {
for z in 0..CHUNK_SIZE { for z in 0..CHUNK_SIZE {
let position = ivec3(x as i32, y as i32, z as i32) + offset; let position = ivec3(x as i32, y as i32, z as i32) + offset;
let noise = noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32); let noise = noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32);
if noise > 0.8 { if (0.7..0.8).contains(&noise) {
blocks[x][y][z] = Block::Stone; blocks[x][y][z] = Block::Stone;
} }
} }