diff --git a/engine/world_generation/src/mesh_generator.rs b/engine/world_generation/src/mesh_generator.rs index 6e331c8..798d77b 100644 --- a/engine/world_generation/src/mesh_generator.rs +++ b/engine/world_generation/src/mesh_generator.rs @@ -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, uvs: &mut Vec, indices: &mut Vec, + normals: &mut Vec, 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, uvs: &mut Vec, indices: &mut Vec, + normals: &mut Vec, 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);