fixed node texture shifting bugs

This commit is contained in:
Piotrek
2021-05-16 19:08:47 +02:00
parent 67bb4fddd0
commit eecedbf4f4
3 changed files with 38 additions and 43 deletions
+17 -17
View File
@@ -80,28 +80,28 @@ impl Game {
}
// 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));
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));
// let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1
// if dir.x != 0 || dir.y != 0 || dir.z != 0 {
let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1
if dir.x != 0 || dir.y != 0 || dir.z != 0 {
// // Shift world
// renderer.shift(dir * world::chunk::SIZE as i32);
// Shift world
renderer.shift(dir * world::chunk::SIZE as i32);
// // 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()};
// println!("Load at {:?}", center);
// 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()};
println!("Load at {:?}", center);
// for a in -RENDER_DIST..RENDER_DIST+1 {
// let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
// self.world.load_chunk(pos);
// }
// }
// self.prev_chunk_pos = Some(chunk_pos);
// }
for a in -RENDER_DIST..RENDER_DIST+1 {
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
self.world.load_chunk(pos);
}
}
self.prev_chunk_pos = Some(chunk_pos);
}
// Send dirty chunks to renderer
// TODO: Unload chunks above certain radius
+16 -26
View File
@@ -104,11 +104,18 @@ impl Content {
pub fn write(&mut self, queue: &wgpu::Queue, block_pos: &Vector3<i32>, block: &WorldBlock) {
// 0,0 at node buffer center
let half = NODE_TEX_SIZE / 2;
let block_pos = block_pos + &Vector3{x: half as i32, y: half as i32, z: half as i32};
let half = NODE_TEX_SIZE as i32 / 2;
let pos = block_pos + &Vector3{x: half, y: half, z: half};
// Check bounds
let size = NODE_TEX_SIZE as i32;
if pos.x < 0 || pos.x >= size || pos.y < 0 || pos.y >= size || pos.z < 0 || pos.z >= size {
println!("{:?} outside bounds", pos);
return;
}
// Get node
let index = (block_pos.x + NODE_TEX_SIZE as i32 * (block_pos.y + NODE_TEX_SIZE as i32 * block_pos.z)) as usize;
let index = (pos.x + NODE_TEX_SIZE as i32 * (pos.y + NODE_TEX_SIZE as i32 * pos.z)) as usize;
let mut value = self.nodes.get_node(index) as usize;
// Allocate new block
@@ -120,6 +127,12 @@ impl Content {
value = self.block_freeidx + 1;
}
// Not enough blocks left
if value >= BLOCK_TEX_SIZE_QB {
println!("Blocks buffer filled, no more blocks can be allocated!");
return;
}
// Assign
self.block_freeidx = value;
self.nodes.set_node(index, value as u32);
@@ -145,29 +158,6 @@ impl Content {
pub fn shift(&mut self, offset: &Vector3<i32>) -> bool {
self.nodes.shift(offset)
// let timer = Instant::now();
// let mut result = vec![0_u32; self.node_buffer.len()].into_boxed_slice();
// let (ox, oy, oz) = (offset.x, offset.y, offset.z);
// let size = NODE_TEX_SIZE as i32;
// let a = max(0, -oz);
// let b = min(size-oz, size);
// println!("from {} to {}", a, b);
// for x in max(0, -ox)..min(size-ox, size) {
// for y in max(0, -oy)..min(size-oy, size) {
// for z in max(0, -oz)..min(size-oz, size) {
// let idx0 = x + size * (y + size * z);
// let idx1 = (x+ox) + size * ((y+oy) + size * (z+oz));
// result[idx1 as usize] = self.node_buffer[idx0 as usize];
// }
// }
// }
// self.node_buffer = result;
// self.node_dirty = true;
// println!("Shifted node buffer by {:?} in {} ms", offset, timer.elapsed().as_millis());
}
pub fn update(&mut self, queue: &wgpu::Queue) {
+5
View File
@@ -28,6 +28,7 @@ impl Nodes {
pub fn set_node(&mut self, index: usize, value: u32) {
self.node_buffer[self.active_buffer][index] = value;
self.modified = true;
}
pub fn shift(&mut self, offset: &Vector3<i32>) -> bool {
@@ -38,6 +39,10 @@ impl Nodes {
let (ox, oy, oz) = (offset.x, offset.y, offset.z);
let size = NODE_TEX_SIZE as i32;
for i in self.node_buffer[b].iter_mut() {
*i = 0;
}
for x in max(0, -ox)..min(size-ox, size) {
for y in max(0, -oy)..min(size-oy, size) {
for z in max(0, -oz)..min(size-oz, size) {