Skip to content

Commit

Permalink
test part: chunks_meshing mark_remesh dirty to neighbor chunks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamtowards committed Jan 15, 2024
1 parent bd0b900 commit fb635eb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,11 @@ fn tick_world(
worldinfo.time_inhabited += dt_sec;

// DayTime
worldinfo.daytime += dt_sec / worldinfo.daytime_length;
worldinfo.daytime -= worldinfo.daytime.trunc(); // trunc to [0-1]
if worldinfo.daytime_length != 0. {
worldinfo.daytime += dt_sec / worldinfo.daytime_length;
worldinfo.daytime -= worldinfo.daytime.trunc(); // trunc to [0-1]
}



// Atmosphere SunPos
Expand Down
41 changes: 27 additions & 14 deletions src/voxel/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use std::sync::{RwLock, Weak, Arc};

use bevy::prelude::*;
use bevy::{prelude::*, math::ivec3};

use super::chunk_system::ChunkPtr;

Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct Chunk {

// cached neighbor chunks (if they are not empty even if they are loaded)
// for Quick Access neighbor voxel, without global find neighbor chunk by chunkpos
pub neighbor_chunks: [Option<Weak<RwLock<Chunk>>>; 6],
pub neighbor_chunks: [Option<Weak<RwLock<Chunk>>>; Self::NEIGHBOR_DIR.len()],



Expand All @@ -83,7 +83,7 @@ impl Chunk {
Self {
cells: [Cell::default(); 16*16*16],
chunkpos,
neighbor_chunks: [None, None, None, None, None, None],
neighbor_chunks: Default::default(),
entity: Entity::PLACEHOLDER,
}
}
Expand All @@ -101,7 +101,7 @@ impl Chunk {
if let Some(neib_weak) = &self.neighbor_chunks[neib_idx] {
if let Some(neib_chunkptr) = neib_weak.upgrade() {
let neib_chunk = neib_chunkptr.read().unwrap();
assert!(neib_chunk.chunkpos == self.chunkpos + Self::NEIGHBOR_DIR[neib_idx] * Chunk::SIZE, "self.chunkpos = {}, neib {} pos {}", self.chunkpos, neib_idx, neib_chunk.chunkpos);
// assert!(neib_chunk.chunkpos == self.chunkpos + Self::NEIGHBOR_DIR[neib_idx] * Chunk::SIZE, "self.chunkpos = {}, neib {} pos {}", self.chunkpos, neib_idx, neib_chunk.chunkpos);

return Some(*neib_chunk.get_cell(Chunk::as_localpos(relpos)));
}
Expand Down Expand Up @@ -159,13 +159,27 @@ impl Chunk {
(localpos.x << 8 | localpos.y << 4 | localpos.z) as usize
}

pub const NEIGHBOR_DIR: [IVec3; 6] = [
IVec3::new(-1, 0, 0),
IVec3::new( 1, 0, 0),
IVec3::new( 0,-1, 0),
IVec3::new( 0, 1, 0),
IVec3::new( 0, 0,-1),
IVec3::new( 0, 0, 1),
pub const NEIGHBOR_DIR: [IVec3; 6+12] = [
// 6 Faces
ivec3(-1, 0, 0),
ivec3( 1, 0, 0),
ivec3( 0,-1, 0),
ivec3( 0, 1, 0),
ivec3( 0, 0,-1),
ivec3( 0, 0, 1),
// 12 Edges
ivec3(0, -1, -1), // X
ivec3(0, 1, 1),
ivec3(0, 1, -1),
ivec3(0, -1, 1),
ivec3(-1, 0, -1), // Y
ivec3(1, 0, 1),
ivec3(1, 0, -1),
ivec3(-1, 0, 1),
ivec3(-1, -1, 0), // Z
ivec3( 1, 1, 0),
ivec3(-1, 1, 0),
ivec3( 1, -1, 0),
];

fn neighbor_idx(relpos: IVec3) -> Option<usize> {
Expand All @@ -177,10 +191,9 @@ impl Chunk {
None
}

// assert!(Self::NEIGHBOR_DIR[idx] + Self::NEIGHBOR_DIR[opposite_idx] == IVec3::ZERO, "idx = {}, opposite = {}", idx, opposite_idx);
pub fn neighbor_idx_opposite(idx: usize) -> usize {
let i = idx / 2 * 2 + (idx + 1) % 2;
assert!(Self::NEIGHBOR_DIR[idx] + Self::NEIGHBOR_DIR[i] == IVec3::ZERO, "idx = {}, opposite = {}", idx, i);
i
idx / 2 * 2 + (idx + 1) % 2
}

}
Expand Down
13 changes: 10 additions & 3 deletions src/voxel/chunk_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use bevy::{
prelude::*,
utils::HashMap
utils::{HashMap}
};

use super::{chunk::*, TerrainMaterial};
Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct ChunkSystem {
// chunks_svo: SVO<Arc<RwLock<Chunk>>>,

// pub chunks_loading: HashSet<IVec3>,
// pub chunks_meshing: HashMap<IVec3, ChunkMeshingState>,
pub chunks_meshing: HashMap<IVec3, u32>,

pub view_distance: IVec2,

Expand All @@ -58,6 +58,7 @@ impl Default for ChunkSystem {
entity: Entity::PLACEHOLDER,
vox_mtl: Handle::default(),
dbg_remesh_all_chunks: false,
chunks_meshing: HashMap::default(),
}
}
}
Expand Down Expand Up @@ -120,11 +121,13 @@ impl ChunkSystem {
let neib_dir = Chunk::NEIGHBOR_DIR[neib_idx];
let neib_chunkpos = chunkpos + neib_dir * Chunk::SIZE;

self.mark_chunk_remesh(neib_chunkpos);

// set neighbor_chunks cache
chunk.neighbor_chunks[neib_idx] =
if let Some(neib_chunkptr) = self.get_chunk(neib_chunkpos) {

// update neighbor chunks' neighbor_chunk
// update neighbor's `neighbor_chunk`
neib_chunkptr.write().unwrap().neighbor_chunks[Chunk::neighbor_idx_opposite(neib_idx)] = Some(Arc::downgrade(&chunkptr));

Some(Arc::downgrade(neib_chunkptr))
Expand Down Expand Up @@ -158,6 +161,10 @@ impl ChunkSystem {
}
}

pub fn mark_chunk_remesh(&mut self, chunkpos: IVec3) {
self.chunks_meshing.insert(chunkpos, 0);
}

// pub fn set_chunk_meshing(&mut self, chunkpos: IVec3, stat: ChunkMeshingState) {
// self.chunks_meshing.insert(chunkpos, stat);
// }
Expand Down
3 changes: 1 addition & 2 deletions src/voxel/meshgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ impl MeshGen {
let p = lp + Self::ADJACENT[axis_i][winded_vi];
//let c = chunk.get_cell(p);

let fp = //Self::sn_featurepoint(p, chunk);
vec3(0.5, 0.5, 0.5);
let fp = Self::sn_featurepoint(p, chunk);
let norm = -Self::sn_grad(p, chunk);

vbuf.push_vertex(
Expand Down

0 comments on commit fb635eb

Please sign in to comment.