Loading chunks in radius instead of by travel direction, bugged (not reloading chunks)

This commit is contained in:
Piotrek
2021-05-19 16:07:27 +02:00
parent e9632a20d2
commit a5d109fb0a
5 changed files with 91 additions and 40 deletions
+50 -31
View File
@@ -36,16 +36,11 @@ impl Game {
};
// Preload chunks around player
let timer = Instant::now();
let mut count = 0;
for x in -RENDER_DIST..RENDER_DIST+1 {
for z in -RENDER_DIST..RENDER_DIST+1 {
let pos = Vector3{x, y: 0, z};
instance.world.load_chunk(pos);
count += 1;
for x in -RENDER_DIST..RENDER_DIST {
for z in -RENDER_DIST..RENDER_DIST {
instance.world.load_chunk(Vector3{x, y: 0, z});
}
}
println!("Preloaded {} chunks in {} ms (about {} voxels)", count, timer.elapsed().as_millis(), count * world::block::SIZE_QB);
// Return generated instance
instance
@@ -71,7 +66,7 @@ impl Game {
// Load chunks at the edge
let center = chunk_pos.clone() + RENDER_DIST*dir;
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
for a in -RENDER_DIST..RENDER_DIST+1 {
for a in -RENDER_DIST..RENDER_DIST {
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
self.world.load_chunk(pos);
}
@@ -89,41 +84,65 @@ impl Game {
let cam_pos = camera.get_position();
let cam_pos = Vector3{x: cam_pos.x as i32, y: 0/*cam_pos.y as i32*/, z: cam_pos.z as i32};
let chunk_pos = cam_pos / world::chunk::SIZE as i32;
// why was this there? lol
//if cam_pos.x < 0 { chunk_pos.x -= 1; }
//if cam_pos.y < 0 { chunk_pos.y -= 1; }
//if cam_pos.z < 0 { chunk_pos.z -= 1; }
// Update pointing direction
let yaw = self.player.camera_controller.get_yaw();
let mut yaw_str = "";
if yaw >= 315.0 || yaw <= 45.0 { yaw_str = "+z"; }
else if yaw >= 45.0 && yaw < 135.0 { yaw_str = "+x"; }
else if yaw >= 135.0 && yaw < 225.0 { yaw_str = "-z"; }
else if yaw >= 225.0 && yaw < 315.0 { yaw_str = "-x"; }
renderer.get_ui().set_text("World", 0, format!("Direction: {}", yaw_str));
// let yaw = self.player.camera_controller.get_yaw();
// let mut yaw_str = "";
// if yaw >= 315.0 || yaw <= 45.0 { yaw_str = "+z"; }
// else if yaw >= 45.0 && yaw < 135.0 { yaw_str = "-x"; }
// else if yaw >= 135.0 && yaw < 225.0 { yaw_str = "-z"; }
// else if yaw >= 225.0 && yaw < 315.0 { yaw_str = "+x"; }
// renderer.get_ui().set_text("World", 0, format!("Direction: {}", yaw_str));
// Block position
if self.prev_pos == None || self.prev_pos != Some(cam_pos) {
renderer.get_ui().set_text("World", 1, format!("Position: {}, {}, {}", cam_pos.x, cam_pos.y, cam_pos.z));
self.prev_pos = Some(cam_pos);
}
// if self.prev_pos == None || self.prev_pos != Some(cam_pos) {
// renderer.get_ui().set_text("World", 1, format!("Position: {}, {}, {}", cam_pos.x, cam_pos.y, cam_pos.z));
// self.prev_pos = Some(cam_pos);
// }
// Chunk position
if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
self.load_chunks(renderer, chunk_pos);
}
// if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
// renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
// }
// Receives chunks from workers
self.world.update();
// Send dirty chunks to renderer
if !renderer.is_busy() && chunk_pos == self.prev_chunk_pos.unwrap_or(chunk_pos) {
// Load, unload or write chunks to gpu
if !renderer.is_busy() {
// If chunk position changed
if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
// Shift world
if self.prev_chunk_pos == None { self.prev_chunk_pos = Some(chunk_pos); }
let prev_chunk_pos = self.prev_chunk_pos.unwrap();
let mut dir = chunk_pos - prev_chunk_pos;
if dir.x != 0 { dir = Vector3{x: dir.x.clamp(-1, 1), y:0, z:0 } }
else if dir.y != 0 { dir = Vector3{x:0, y:dir.y.clamp(-1, 1), z:0 } }
else if dir.z != 0 { dir = Vector3{x:0, y:0, z:dir.z.clamp(-1, 1) } }
else { return; }
renderer.shift(dir * world::chunk::SIZE as i32);
// Load chunks in radius
for ox in -RENDER_DIST..RENDER_DIST {
for oz in -RENDER_DIST..RENDER_DIST {
let x = chunk_pos.x+ox;
let z = chunk_pos.z+oz;
self.world.load_chunk(Vector3{x, y: 0, z});
}
}
// Save previous
self.prev_chunk_pos = Some(prev_chunk_pos+dir);
}
// Send dirty to gpu
for (pos, chunk) in self.world.all_chunks() {
if chunk.dirty {
renderer.write_chunk(pos, chunk);
chunk.dirty = false;
chunk.loaded = renderer.write_chunk(pos, chunk);
if !chunk.loaded { println!("Failed to load {:?}", pos); }
}
}
}
+5 -5
View File
@@ -13,8 +13,6 @@ pub const SIZE: usize = 16;
const SIZE_SQ: usize = SIZE*SIZE;
const SIZE_QB: usize = SIZE*SIZE*SIZE;
const NUM_WORKERS: usize = 8;
// 1 chunk times on i7-5930K
// alg | size | compr | decom
// raw 32.89kB 0.06s 1.19s
@@ -25,7 +23,8 @@ const NUM_WORKERS: usize = 8;
pub struct WorldChunk {
nodes: Box<[u32]>,
blocks: Vec<WorldBlock>,
pub dirty: bool // true by default, set to false when uploaded to renderer. should be set to true when unloaded (TODO)
pub dirty: bool, // Request write to GPU
pub loaded: bool // Written to GPU
}
impl WorldChunk {
@@ -33,7 +32,8 @@ impl WorldChunk {
Self {
nodes: vec![0_u32; SIZE_QB].into_boxed_slice(),
blocks: Vec::new(),
dirty: true
dirty: true,
loaded: false
}
}
@@ -43,7 +43,7 @@ impl WorldChunk {
// Header
let (header_bytes, bytes) = bytes.split_at(128);
let file_ver = header_bytes[0];
let mut num_blocks = LittleEndian::read_u32(&header_bytes[1..5]) as usize;
let num_blocks = LittleEndian::read_u32(&header_bytes[1..5]) as usize;
assert_eq!(file_ver, 1);
// Nodes
+15 -1
View File
@@ -51,12 +51,26 @@ impl World {
*/
pub fn load_chunk(&mut self, pos: Vector3<i32>) {
if let Some(chunk) = self.chunks.get_mut(&pos) {
chunk.dirty = true;
if !chunk.loaded {
println!("Chunk {:?} exists but not loaded! Loading now", pos);
chunk.dirty = true;
}
} else {
self.loader.load(pos);
}
}
/*
*
*/
pub fn unload_chunk(&mut self, pos: Vector3<i32>) {
if let Some(chunk) = self.chunks.get_mut(&pos) {
chunk.loaded = false;
chunk.dirty = false;
// TODO: Mark chunk as "to be unloaded" and send it to renderer for freeing resources
}
}
/*
* Loads metadata
*/
+19 -1
View File
@@ -66,7 +66,7 @@ impl Renderer {
// Middleman texture
let texture_descriptor = wgpu::TextureDescriptor {
label: Some("glow_post_process_texture1"),
label: Some("Middle texture"),
size: wgpu::Extent3d { width: swapchain_desc.width,
height: swapchain_desc.height,
depth: 1,
@@ -103,8 +103,26 @@ impl Renderer {
// Update camera
self.camera.set_aspect(size.width as f32 / size.height as f32);
}
// Recreate swapchain
self.swapchain = self.device.create_swap_chain(&self.surface, &self.swapchain_desc);
// Recreate texture
let texture_descriptor = wgpu::TextureDescriptor {
label: Some("Middle texture"),
size: wgpu::Extent3d {
width: self.swapchain_desc.width,
height: self.swapchain_desc.height,
depth: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: self.swapchain_desc.format,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::RENDER_ATTACHMENT,
};
self.texture = self.device.create_texture(&texture_descriptor).create_view(&wgpu::TextureViewDescriptor::default());
self.postprocess_pass = PostprocessPass::new(&self.device, &self.swapchain_desc, &self.texture);
}
/*
+2 -2
View File
@@ -24,7 +24,7 @@ impl RendererView for Renderer {
if pos.y.abs() > game::RENDER_DIST { println!("out of bounds! y: {} off: {:?} pos: {:?}", pos.y, chunk_off, chunk_pos); return false; }
if pos.z.abs() > game::RENDER_DIST { println!("out of bounds! z: {} off: {:?} pos: {:?}", pos.z, chunk_off, chunk_pos); return false; }
// Write chunk to renderer
println!("write chunk {:?}", pos);
//println!("write chunk {:?}", pos);
let pos = pos * chunk::SIZE as i32;
for (block_pos, block) in chunk.all_blocks() {
self.buffers.content.write(&self.queue, &(block_pos + pos), block);
@@ -50,7 +50,7 @@ impl RendererView for Renderer {
*/
fn shift(&mut self, offset: Vector3<i32>) {
self.buffers.content.shift(&-offset);
println!("World shifted by {:?}", offset);
//println!("World shifted by {:?}", offset);
}
fn get_camera_transform(&mut self) -> &mut dyn CameraTransform {