experimenting with water surface mesh

This commit is contained in:
2024-10-19 01:33:03 -04:00
parent b61dacf5e2
commit cc3e43da16
17 changed files with 263 additions and 124 deletions

View File

@@ -2,6 +2,7 @@ use crate::hex_utils::HexCoord;
use crate::{hex_utils::offset3d_to_world, prelude::*};
#[cfg(feature = "tracing")]
use bevy::log::*;
use bevy::math::VectorSpace;
use bevy::{
prelude::*,
render::{
@@ -94,6 +95,74 @@ fn create_tile(
}
}
pub fn generate_chunk_water_mesh(chunk: &MeshChunkData, sealevel: f32, map_width: usize, map_height: usize) -> Mesh {
#[cfg(feature = "tracing")]
let _gen_mesh = info_span!("Generate Water Surface Mesh").entered();
let vertex_count: usize = Chunk::SIZE * Chunk::SIZE * 7;
let mut verts = Vec::with_capacity(vertex_count);
let mut uvs = Vec::with_capacity(vertex_count);
let mut indices = Vec::with_capacity(vertex_count);
let mut normals = Vec::with_capacity(vertex_count);
for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE {
let idx = x + z * Chunk::SIZE;
let height = chunk.heights[idx];
if height > sealevel {
continue;
}
let off_pos = Vec3::new(x as f32, sealevel, z as f32);
let tile_pos = offset3d_to_world(off_pos);
let coord = HexCoord::from_grid_pos(x, z);
let n = chunk.get_neighbors(&coord);
create_tile_water_surface(tile_pos, &n, &mut verts, &mut uvs, &mut indices, &mut normals);
}
}
let mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_indices(Indices::U32(indices));
return mesh;
}
fn create_tile_water_surface(
pos: Vec3,
neighbors: &[f32; 6],
verts: &mut Vec<Vec3>,
uvs: &mut Vec<Vec2>,
indices: &mut Vec<u32>,
normals: &mut Vec<Vec3>,
) {
let idx = verts.len() as u32;
//todo: only use triangle fan when on shoreline
verts.push(pos);
uvs.push(Vec2::ZERO);
normals.push(Vec3::Y);
for i in 0..6 {
let p = pos + HEX_CORNERS[i];
verts.push(p);
let mut uv = Vec2::ZERO;
let n = neighbors[i];
let nn = neighbors[(i + 5) % 6];
if nn > pos.y || n > pos.y {
uv = Vec2::ONE;
}
indices.push(idx);
indices.push(idx + 1 + i as u32);
indices.push(idx + 1 + ((i as u32 + 1) % 6));
uvs.push(uv);
normals.push(Vec3::Y);
}
}
fn create_tile_wall(
pos: Vec3,
dir: usize,

View File

@@ -39,7 +39,7 @@ pub fn generate_heightmap(cfg: &GenerationConfig, seed: u32, painter: &BiomePain
chunks,
height: cfg.size.y as usize,
width: cfg.size.x as usize,
sea_level: cfg.sea_level as f32,
sealevel: cfg.sea_level as f32,
min_level: min,
max_level: max,
biome_count: painter.biomes.len(),

View File

@@ -13,7 +13,7 @@ pub struct Map {
pub chunks: Vec<Chunk>,
pub height: usize,
pub width: usize,
pub sea_level: f32,
pub sealevel: f32,
pub min_level: f32,
pub max_level: f32,
pub biome_count: usize,
@@ -39,6 +39,7 @@ impl Map {
return MeshChunkData {
min_height: self.min_level,
sealevel: self.sealevel,
heights: chunk.heights.clone(),
textures: chunk.textures.clone(),
};
@@ -140,7 +141,7 @@ impl Map {
pub fn get_center(&self) -> Vec3 {
let w = self.get_world_width();
let h = self.get_world_height();
return Vec3::new(w / 2., self.sea_level, h / 2.);
return Vec3::new(w / 2., self.sealevel, h / 2.);
}
pub fn get_world_width(&self) -> f32 {

View File

@@ -68,7 +68,7 @@ pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer<image::Rgba<u8
let height = map.sample_height(&coord);
let mut color = Hsla::hsl(138.0, 1.0, 0.4);
if height < map.sea_level {
if height < map.sealevel {
color.hue = 217.0;
}

View File

@@ -6,6 +6,7 @@ pub struct MeshChunkData {
pub heights: [f32; Chunk::AREA],
pub textures: [[u32; 2]; Chunk::AREA],
pub min_height: f32,
pub sealevel: f32,
}
impl MeshChunkData {
@@ -22,4 +23,18 @@ impl MeshChunkData {
return data;
}
pub fn get_neighbors_map_bounded(&self, coord: &HexCoord, width: usize, height: usize) -> [f32; 6] {
let mut data = [self.min_height; 6];
let n_tiles = coord.get_neighbors();
for i in 0..6 {
let n = n_tiles[i];
if !n.is_in_bounds(Chunk::SIZE * width, Chunk::SIZE * height) {
continue;
}
data[i] = self.heights[n.to_index(Chunk::SIZE)];
}
return data;
}
}