Update mesh_generator.rs

This commit is contained in:
2024-04-27 19:19:41 -04:00
parent a65848fac8
commit a8763ae98a

View File

@@ -24,6 +24,31 @@ const HEX_CORNERS: [Vec3; 6] = [
Vec3::new(-INNER_RADIUS, 0., 0.5 * OUTER_RADIUS),
];
const HEX_NORMALS: [Vec3; 6] = [
Vec3::new(
INNER_RADIUS / 2.,
0.,
(OUTER_RADIUS + 0.5 * OUTER_RADIUS) / 2.,
),
Vec3::Z,
Vec3::new(
INNER_RADIUS / -2.,
0.,
(OUTER_RADIUS + 0.5 * OUTER_RADIUS) / 2.,
),
Vec3::new(
INNER_RADIUS / -2.,
0.,
(OUTER_RADIUS + 0.5 * OUTER_RADIUS) / -2.,
),
Vec3::NEG_Z,
Vec3::new(
INNER_RADIUS / 2.,
0.,
(OUTER_RADIUS + 0.5 * OUTER_RADIUS) / -2.,
),
];
pub fn generate_chunk_mesh(
chunk: &Chunk,
map: &Map,
@@ -35,6 +60,7 @@ pub fn generate_chunk_mesh(
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 {
@@ -57,6 +83,7 @@ pub fn generate_chunk_mesh(
&mut verts,
&mut uvs,
&mut indices,
&mut normals,
// &mut tex,
tile.texture_id,
tile.side_texture_id,
@@ -70,9 +97,8 @@ pub fn generate_chunk_mesh(
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_indices(Indices::U32(indices))
.with_duplicated_vertices()
.with_computed_flat_normals();
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_indices(Indices::U32(indices));
return mesh;
}
@@ -83,6 +109,7 @@ fn create_tile(
verts: &mut Vec<Vec3>,
uvs: &mut Vec<Vec2>,
indices: &mut Vec<u32>,
normals: &mut Vec<Vec3>,
texture_index: u32,
side_texture_index: u32,
) {
@@ -93,6 +120,7 @@ fn create_tile(
let idx = verts.len() as u32;
uvs.push((uv_offset / TEX_MULTI) + tex_off);
verts.push(pos);
normals.push(Vec3::Y);
for i in 0..6 {
let p = pos + HEX_CORNERS[i];
verts.push(p);
@@ -101,6 +129,7 @@ fn create_tile(
indices.push(idx);
indices.push(idx + 1 + i as u32);
indices.push(idx + 1 + ((i as u32 + 1) % 6));
normals.push(Vec3::Y);
}
for i in 0..neighbors.len() {
@@ -108,7 +137,7 @@ fn create_tile(
match cur_n {
Some(n_height) => {
if n_height < pos.y {
create_tile_wall(pos, i, n_height, verts, uvs, indices, side_tex_off);
create_tile_wall(pos, i, n_height, verts, uvs, indices, normals, side_tex_off);
}
}
_ => {}
@@ -123,6 +152,7 @@ fn create_tile_wall(
verts: &mut Vec<Vec3>,
uvs: &mut Vec<Vec2>,
indices: &mut Vec<u32>,
normals: &mut Vec<Vec3>,
tex_off: Vec2,
) {
let p1 = HEX_CORNERS[(dir) % 6] + pos;
@@ -137,6 +167,12 @@ fn create_tile_wall(
verts.push(p3);
verts.push(p4);
let n = HEX_NORMALS[dir].normalize();
normals.push(n);
normals.push(n);
normals.push(n);
normals.push(n);
indices.push(idx);
indices.push(idx + 2);
indices.push(idx + 1);