fixed hex coords, added tile walls

This commit is contained in:
2024-03-31 01:37:25 -04:00
parent b7853beb88
commit 45d4e8cfa0
7 changed files with 74 additions and 89 deletions

View File

@@ -21,30 +21,21 @@ const HEX_CORNERS: [Vec3; 6] = [
];
pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh {
let vertex_count: usize = Chunk::SIZE * Chunk::SIZE;
let vertex_count: usize = Chunk::SIZE * Chunk::SIZE * 6;
let mut verts = Vec::with_capacity(vertex_count);
let mut uvs = Vec::with_capacity(vertex_count);
let mut normals = Vec::with_capacity(vertex_count);
let mut indices = Vec::with_capacity(vertex_count);
for z in 0..Chunk::SIZE {
for x in 0..Chunk::SIZE {
let coord = HexCoord::from_grid_pos(x, z);
let height = chunk.points[coord.to_index(Chunk::SIZE) as usize];
let height = chunk.points[x + z * Chunk::SIZE];
let off_pos = Vec3::new(x as f32, height, z as f32);
let grid_pos = offset3d_to_world(off_pos);
let n = map.get_neighbors(&HexCoord::from_offset(
coord.to_offset() + chunk.chunk_offset,
));
create_tile(
grid_pos,
&n,
&mut verts,
&mut uvs,
&mut normals,
&mut indices,
0,
let tile_pos = offset3d_to_world(off_pos);
let coord = HexCoord::from_offset(
IVec2::new(x as i32, z as i32) + (chunk.chunk_offset * Chunk::SIZE as i32),
);
let n = map.get_neighbors(&coord);
create_tile(tile_pos, &n, &mut verts, &mut uvs, &mut indices, 0);
}
}
@@ -54,7 +45,6 @@ pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh {
)
.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))
.with_duplicated_vertices()
.with_computed_flat_normals();
@@ -67,20 +57,16 @@ fn create_tile(
neighbors: &[Option<f32>; 6],
verts: &mut Vec<Vec3>,
uvs: &mut Vec<Vec2>,
normals: &mut Vec<Vec3>,
indices: &mut Vec<u32>,
texture_index: u32,
) {
let idx = verts.len() as u32;
let center = Vec3::new(pos.x, 0., pos.z);
normals.push(Vec3::Y);
uvs.push(pos.xz());
verts.push(pos);
for i in 0..6 {
let p = pos + HEX_CORNERS[i];
verts.push(p);
uvs.push(p.xz());
normals.push((p - center).normalize());
indices.push(idx);
indices.push(idx + 1 + i as u32);
indices.push(idx + 1 + ((i as u32 + 1) % 6));
@@ -90,17 +76,8 @@ fn create_tile(
let cur_n = neighbors[i];
match cur_n {
Some(n_height) => {
if true {
create_tile_wall(
pos,
i,
pos.y + 1.,
verts,
uvs,
normals,
indices,
texture_index,
);
if n_height < pos.y {
create_tile_wall(pos, i, n_height, verts, uvs, indices, texture_index);
}
}
_ => {}
@@ -114,18 +91,14 @@ fn create_tile_wall(
height: f32,
verts: &mut Vec<Vec3>,
uvs: &mut Vec<Vec2>,
normals: &mut Vec<Vec3>,
indices: &mut Vec<u32>,
_texture_index: u32,
) {
println!("{dir}");
let p1 = HEX_CORNERS[(dir + 1) % 6] + pos;
let p2 = HEX_CORNERS[(dir + 2) % 6] + pos;
let p1 = HEX_CORNERS[(dir) % 6] + pos;
let p2 = HEX_CORNERS[(dir + 1) % 6] + pos;
let p3 = Vec3::new(p1.x, height, p1.z);
let p4 = Vec3::new(p2.x, height, p2.z);
let normal = Vec3::Y;
let idx = verts.len() as u32;
verts.push(p1);
@@ -133,11 +106,6 @@ fn create_tile_wall(
verts.push(p3);
verts.push(p4);
normals.push(normal);
normals.push(normal);
normals.push(normal);
normals.push(normal);
indices.push(idx);
indices.push(idx + 2);
indices.push(idx + 1);