update water waves

This commit is contained in:
2024-10-22 20:36:56 -04:00
parent b3c44c5351
commit f2ff8f7bc4
4 changed files with 146 additions and 63 deletions

View File

@@ -42,9 +42,47 @@ impl Map {
sealevel: self.sealevel,
heights: chunk.heights.clone(),
textures: chunk.textures.clone(),
distance_to_land: self.get_distance_from_land(chunk.chunk_offset, 4),
};
}
fn get_distance_from_land(&self, chunk_offset: IVec2, range: usize) -> [f32; Chunk::AREA] {
#[cfg(feature = "tracing")]
let _spawn_span = info_span!("Chunk Land Dist Data").entered();
let mut dists = [0.0; Chunk::AREA];
let cx = chunk_offset.x as usize * Chunk::SIZE;
let cz = chunk_offset.y as usize * Chunk::SIZE;
for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE {
let coord = HexCoord::from_grid_pos(x + cx, z + cz);
let index = coord.to_chunk_local_index();
if !self.is_in_bounds(&coord) {
warn!("Coord is not in bounds!?");
}
//Current tile is land tile
if self.sample_height(&coord) > self.sealevel {
dists[index] = 0.0;
continue;
}
//Find closest land tile
if let Some(d) = self.hex_select_first(&coord, range, false, |_t, h, r| {
if h > self.sealevel {
return Some(r as f32);
}
return None;
}) {
dists[index] = d;
} else {
dists[index] = range as f32;
}
}
}
return dists;
}
pub fn get_neighbors(&self, pos: &HexCoord) -> [Option<f32>; 6] {
let mut results: [Option<f32>; 6] = [None; 6];
let w = self.width * Chunk::SIZE;
@@ -96,47 +134,6 @@ impl Map {
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.biome_id[pos.to_chunk_local_index()];
}
/*
pub fn get_biome_noise(&self, pos: &HexCoord) -> &BiomeData {
assert!(
self.is_in_bounds(pos),
"The provided coordinate is not within the map bounds"
);
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return &chunk.biome_data[pos.to_chunk_local_index()];
}
pub fn get_moisture(&self, pos: &HexCoord) -> f32 {
assert!(
self.is_in_bounds(pos),
"The provided coordinate is not within the map bounds"
);
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.biome_data[pos.to_chunk_local_index()].moisture;
}
pub fn get_tempurature(&self, pos: &HexCoord) -> f32 {
assert!(
self.is_in_bounds(pos),
"The provided coordinate is not within the map bounds"
);
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.biome_data[pos.to_chunk_local_index()].temperature;
}
pub fn get_continentality(&self, pos: &HexCoord) -> f32 {
assert!(
self.is_in_bounds(pos),
"The provided coordinate is not within the map bounds"
);
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.biome_data[pos.to_chunk_local_index()].continentality;
}
*/
pub fn get_center(&self) -> Vec3 {
let w = self.get_world_width();
@@ -206,6 +203,80 @@ impl Map {
return result;
}
pub fn hex_select_first<OP, Ret>(
&self,
center: &HexCoord,
radius: usize,
include_center: bool,
op: OP,
) -> Option<Ret>
where
OP: (Fn(&HexCoord, f32, usize) -> Option<Ret>) + Sync + Send,
{
assert!(radius != 0, "Radius cannot be zero");
if include_center {
let h = self.sample_height(&center);
let r = (op)(center, h, 0);
if r.is_some() {
return r;
}
}
for k in 0..(radius + 1) {
let mut p = center.scale(4, k);
for i in 0..6 {
for _j in 0..k {
p = p.get_neighbor(i);
if self.is_in_bounds(&p) {
let h = self.sample_height(&p);
let r = (op)(&p, h, k);
if r.is_some() {
return r;
}
}
}
}
}
return None;
}
pub fn ring_select_first<OP, Ret>(
&self,
center: &HexCoord,
start_radius: usize,
end_radius: usize,
op: OP,
) -> Option<Ret>
where
OP: (Fn(&HexCoord, f32, usize) -> Option<Ret>) + Sync + Send,
{
assert!(start_radius != 0, "Start radius cannot be zero");
assert!(
start_radius > end_radius,
"Start radius cannot be lower than end radius"
);
for k in start_radius..(end_radius + 1) {
let mut p = center.scale(4, k);
for i in 0..6 {
for _j in 0..k {
p = p.get_neighbor(i);
if self.is_in_bounds(&p) {
let h = self.sample_height(&p);
let r = (op)(&p, h, k);
if r.is_some() {
return r;
}
}
}
}
}
return None;
}
pub fn hex_select_mut<OP, Ret>(
&mut self,
center: &HexCoord,

View File

@@ -7,6 +7,7 @@ pub struct MeshChunkData {
pub textures: [[u32; 2]; Chunk::AREA],
pub min_height: f32,
pub sealevel: f32,
pub distance_to_land: [f32; Chunk::AREA],
}
impl MeshChunkData {
@@ -24,17 +25,18 @@ impl MeshChunkData {
return data;
}
pub fn get_neighbors_with_water_info(&self, coord: &HexCoord) -> ([f32; 6], bool) {
pub fn get_neighbors_with_water_info(&self, coord: &HexCoord) -> ([(f32, Option<f32>); 6], bool) {
let mut has_land = false;
let mut data = [self.min_height; 6];
let mut data = [(self.min_height, None); 6];
let n_tiles = coord.get_neighbors();
for i in 0..6 {
let n = n_tiles[i];
if !n.is_in_bounds(Chunk::SIZE, Chunk::SIZE) {
continue;
}
data[i] = self.heights[n.to_index(Chunk::SIZE)];
if data[i] > self.sealevel {
let idx = n.to_index(Chunk::SIZE);
data[i] = (self.heights[idx], Some(self.distance_to_land[idx]));
if data[i].0 > self.sealevel {
has_land = true;
}
}