From 7e6746a8cdcb0c43130fa3bdbb1638aadfdf7901 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Tue, 9 May 2023 17:33:46 +0100 Subject: [PATCH] Use new world generator --- src/core/app.rs | 9 ++++ src/voxel/brickmap.rs | 98 ++++++++++++++----------------------- src/voxel/voxel_renderer.rs | 13 ++++- 3 files changed, 58 insertions(+), 62 deletions(-) diff --git a/src/core/app.rs b/src/core/app.rs index ad2c85f..2578eb0 100644 --- a/src/core/app.rs +++ b/src/core/app.rs @@ -68,6 +68,14 @@ impl App { 0.25, ); + let mut world = voxel::world::WorldManager::new(voxel::world::GenerationSettings { + seed: 0, + frequency: 0.04, + octaves: 3, + gain: 0.5, + lacunarity: 2.0, + }); + let mut renderer = voxel::VoxelRenderer::new(&self.render_ctx, &camera_controller); let mut cumulative_dt = 0.0; @@ -95,6 +103,7 @@ impl App { camera_controller.update_buffer(&self.render_ctx); renderer.render(&self.render_ctx); renderer.update(&dt, &self.render_ctx); + renderer.update_brickmap(&self.render_ctx, &mut world); // Simple framerate tracking cumulative_dt += dt.as_secs_f32(); diff --git a/src/voxel/brickmap.rs b/src/voxel/brickmap.rs index 1b7f2a0..3a88719 100644 --- a/src/voxel/brickmap.rs +++ b/src/voxel/brickmap.rs @@ -36,7 +36,6 @@ pub struct BrickmapManager { // - GPU side unpack buffer rather than uploading each changed brickmap part // - Cyclic brickmap cache with unloading // - Brickworld system -// - Move terrain generation to it's own system impl BrickmapManager { pub fn new(context: &render::Context) -> Self { let mut state_uniform = WorldState::default(); @@ -132,7 +131,11 @@ impl BrickmapManager { &self.feedback_result_buffer } - pub fn process_feedback_buffer(&mut self, context: &render::Context) { + pub fn process_feedback_buffer( + &mut self, + context: &render::Context, + world: &mut super::world::WorldManager, + ) { // Get request count let mut slice = self.feedback_result_buffer.slice(0..16); slice.map_async(wgpu::MapMode::Read, |_| {}); @@ -154,47 +157,15 @@ impl BrickmapManager { // Generate a sphere of voxels let world_dims = self.state_uniform.brickmap_cache_dims; - let sphere_center = glam::vec3(3.5, 3.5, 3.5); - let sphere_r2 = u32::pow(4, 2) as f32; for i in 0..request_count { let chunk_x = data[i * 4]; let chunk_y = data[i * 4 + 1]; let chunk_z = data[i * 4 + 2]; - let noise_vals = simdnoise::NoiseBuilder::fbm_3d_offset( - chunk_x as f32 * 8.0, - 8, - chunk_y as f32 * 8.0, - 8, - chunk_z as f32 * 8.0, - 8, - ) - .with_seed(0) - .with_freq(0.005) - .with_octaves(3) - .with_gain(0.5) - .with_lacunarity(2.0) - .generate(); - - // Generate full data - let mut chunk = [(false, 0u32); 512]; - for z in 0..8 { - for y in 0..8 { - for x in 0..8 { - let idx = (x + y * 8 + z * 8 * 8) as usize; - - let val = noise_vals.0[idx]; - if val > 0.0 { - let mut albedo = 0u32; - albedo += ((x + 1) * 32 - 1) << 24; - albedo += ((y + 1) * 32 - 1) << 16; - albedo += ((z + 1) * 32 - 1) << 8; - albedo += 255; - chunk[idx] = (true, albedo); - } - } - } - } + let chunk_pos = glam::ivec3(0, 0, 0); + let block_pos = glam::uvec3(chunk_x, chunk_y, chunk_z); + let block = world.get_block(chunk_pos, block_pos); + assert_eq!(block.len(), 512); // Cull interior voxels let mut bitmask_data = [0xFFFFFFFF as u32; 16]; @@ -206,30 +177,37 @@ impl BrickmapManager { for x in 0..8 { // Ignore non-solids let idx = x + y * 8 + z * 8 * 8; - if !chunk[idx].0 { - continue; - } + let empty_voxel = super::world::Voxel::Empty; - // A voxel is on the surface if at least one of it's - // cardinal neighbours is non-solid. Also for simplicity if - // it's on the edge of the chunk - let surface_voxel: bool; - if x == 0 || x == 7 || y == 0 || y == 7 || z == 0 || z == 7 { - surface_voxel = true; - } else { - surface_voxel = !(chunk[idx + 1].0 - && chunk[idx - 1].0 - && chunk[idx + 8].0 - && chunk[idx - 8].0 - && chunk[idx + 64].0 - && chunk[idx - 64].0); - } + match block[idx] { + super::world::Voxel::Empty => continue, + super::world::Voxel::Color(r, g, b) => { + // A voxel is on the surface if at least one of it's + // cardinal neighbours is non-solid. Also for simplicity + // if it's on the edge of the chunk + let surface_voxel: bool; + if x == 0 || x == 7 || y == 0 || y == 7 || z == 0 || z == 7 { + surface_voxel = true; + } else { + surface_voxel = !(block[idx + 1] == empty_voxel + && block[idx - 1] == empty_voxel + && block[idx + 8] == empty_voxel + && block[idx - 8] == empty_voxel + && block[idx + 64] == empty_voxel + && block[idx - 64] == empty_voxel); + } - // Set the appropriate bit in the z entry and add the shading - // data - if surface_voxel { - entry += 1 << (x + y * 8); - albedo_data.push(chunk[idx].1); + // Set the appropriate bit in the z entry and add the + // shading data + if surface_voxel { + entry += 1 << (x + y * 8); + let albedo = ((r as u32) << 24) + + ((g as u32) << 16) + + ((b as u32) << 8) + + 255u32; + albedo_data.push(albedo); + } + } } } } diff --git a/src/voxel/voxel_renderer.rs b/src/voxel/voxel_renderer.rs index a1c3908..f8ea15f 100644 --- a/src/voxel/voxel_renderer.rs +++ b/src/voxel/voxel_renderer.rs @@ -215,7 +215,16 @@ impl render::Renderer for VoxelRenderer { frame.present(); } - fn update(&mut self, dt: &Duration, context: &render::Context) { - self.brickmap_manager.process_feedback_buffer(context); + fn update(&mut self, _dt: &Duration, _context: &render::Context) {} +} + +impl VoxelRenderer { + pub fn update_brickmap( + &mut self, + context: &render::Context, + world: &mut super::world::WorldManager, + ) { + self.brickmap_manager + .process_feedback_buffer(context, world); } }