Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/client/terrain/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ pub fn handle_chunk_mesh_update_events_system(
let chunk_option = chunk_manager.get_chunk(event.position);
match chunk_option {
Some(chunk) => {
for (entity, chunk_mesh) in mesh_query.iter_mut() {
if Chunk::key_eq_pos(chunk_mesh.key, chunk.position) {
commands.entity(entity).despawn();
}
}
add_chunk_objects(
&mut commands,
&mut meshes,
Expand All @@ -105,6 +100,11 @@ pub fn handle_chunk_mesh_update_events_system(
&texture_manager,
&mut meshes,
);
for (entity, chunk_mesh) in mesh_query.iter_mut() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevents flickering, as the meshing takes much more time now.

if Chunk::key_eq_pos(chunk_mesh.key, chunk.position) {
commands.entity(entity).despawn();
}
}
}
None => {
println!("No chunk found");
Expand Down
26 changes: 15 additions & 11 deletions src/client/terrain/util/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod client_block {
use MeshRepresentation::*;

pub struct BlockProperties {
pub transparent: bool,
pub has_collider: bool,
pub mesh_representation: MeshRepresentation,
}
Expand All @@ -40,30 +41,33 @@ pub mod client_block {
use TextureName::*;

let touple = match block_id {
BlockId::Air => (false, None),
BlockId::Air => (true, false, None),
BlockId::Grass => (
false,
true,
Cube([GrassTop, Dirt, GrassSide, GrassSide, GrassSide, GrassSide]),
),
BlockId::Dirt => (true, Cube([Dirt; 6])),
BlockId::Stone => (true, Cube([Stone; 6])),
BlockId::CobbleStone => (true, Cube([CobbleStone; 6])),
BlockId::Bedrock => (true, Cube([Bedrock; 6])),
BlockId::IronOre => (true, Cube([IronOre; 6])),
BlockId::CoalOre => (true, Cube([CoalOre; 6])),
BlockId::OakLeaves => (true, Cube([OakLeaves; 6])),
BlockId::Dirt => (false, true, Cube([Dirt; 6])),
BlockId::Stone => (false, true, Cube([Stone; 6])),
BlockId::CobbleStone => (false, true, Cube([CobbleStone; 6])),
BlockId::Bedrock => (false, true, Cube([Bedrock; 6])),
BlockId::IronOre => (false, true, Cube([IronOre; 6])),
BlockId::CoalOre => (false, true, Cube([CoalOre; 6])),
BlockId::OakLeaves => (false, true, Cube([OakLeaves; 6])),
BlockId::OakLog => (
false,
true,
Cube([
OakLogTop, OakLogTop, OakLogSide, OakLogSide, OakLogSide, OakLogSide,
]),
),
BlockId::Tallgrass => (false, Cross([Tallgrass, Tallgrass])),
BlockId::Tallgrass => (true, false, Cross([Tallgrass, Tallgrass])),
};

BlockProperties {
has_collider: touple.0,
mesh_representation: touple.1,
transparent: touple.0,
has_collider: touple.1,
mesh_representation: touple.2,
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/client/terrain/util/cross_mesher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn create_cross_geometry_for_chunk(
let mut position = vec![];
let mut uv = vec![];
let mut normal = vec![];
let mut color = vec![];
let mut indices = vec![];

let mut index_offset = 0;
Expand Down Expand Up @@ -52,6 +53,7 @@ fn create_cross_geometry_for_chunk(
face_uv[1] + vertex.uv[1] * 0.25,
]);
normal.push(vertex.normal);
color.push([1.0, 1.0, 1.0, 1.0]);
}

let offsets = [0, 1, 3, 1, 2, 3];
Expand All @@ -70,6 +72,7 @@ fn create_cross_geometry_for_chunk(
position,
uv,
normal,
color,
indices,
}
}
Expand Down
60 changes: 58 additions & 2 deletions src/client/terrain/util/cube_mesher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ pub fn create_cube_geometry_data(
faces: u8,
block_id: BlockId,
texture_manager: &TextureManager,
chunk: &Chunk,
) -> GeometryData {
let mut position = Vec::new();
let mut uv = Vec::new();
let mut normal = Vec::new();
let mut color = Vec::new();
let mut indices = Vec::new();
let mut index_offset = 0;

Expand All @@ -38,6 +40,41 @@ pub fn create_cube_geometry_data(
block_uvs[1] + (1.0 - vertex.uv[1]) * 0.25,
]);
normal.push(vertex.normal);

let check = |dx: i32, dy: i32, dz: i32| -> bool {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustc --explain E0434

let dx = dx as f32 + 0.5;
let dy = dy as f32 + 0.5;
let dz = dz as f32 + 0.5;

let cx = (vertex.position[0] * 0.5 + x + dx) as usize;
let cy = (vertex.position[1] * 0.5 + y + dy) as usize;
let cz = (vertex.position[2] * 0.5 + z + dz) as usize;

if !Chunk::valid_padded(cx, cy, cz) {
return false;
}

let neighbor_id = chunk.get_unpadded(cx, cy, cz);
!block_properties(neighbor_id).transparent
};

let checks = [
(check(0, 1, 0)),
(check(1, 0, 0)),
(check(0, 0, 1)),
(check(1, 1, 0) && (check(0, 1, 0) || check(1, 0, 0))),
(check(0, 1, 1) && (check(0, 1, 0) || check(0, 0, 1))),
(check(1, 0, 1) && (check(1, 0, 0) || check(0, 0, 1))),
(check(1, 1, 1) && (check(0, 1, 1) || check(1, 0, 1) || check(1, 1, 0))),
];

let ao_count: f32 = checks.iter().map(|v| *v as u8 as f32).sum();
let max_ao: f32 = 8.0;

let mut ao_value = (max_ao - ao_count) / max_ao;
ao_value += 0.5;

color.push([ao_value, ao_value, ao_value, 1.0]);
}

let offsets = [0, 1, 2, 2, 1, 3];
Expand All @@ -51,6 +88,7 @@ pub fn create_cube_geometry_data(
position,
uv,
normal,
color,
indices,
}
}
Expand All @@ -60,6 +98,7 @@ pub fn create_chunk_mesh(chunk: &Chunk, texture_manager: &TextureManager) -> Opt
position: Vec::new(),
uv: Vec::new(),
normal: Vec::new(),
color: Vec::new(),
indices: Vec::new(),
};

Expand Down Expand Up @@ -105,6 +144,7 @@ pub fn create_chunk_mesh(chunk: &Chunk, texture_manager: &TextureManager) -> Opt
mask,
block_id,
texture_manager,
chunk,
);

geometry_data.indices.extend(
Expand All @@ -116,6 +156,7 @@ pub fn create_chunk_mesh(chunk: &Chunk, texture_manager: &TextureManager) -> Opt
geometry_data.position.extend(cube_data.position);
geometry_data.uv.extend(cube_data.uv);
geometry_data.normal.extend(cube_data.normal);
geometry_data.color.extend(cube_data.color);
}
}
}
Expand Down Expand Up @@ -200,6 +241,12 @@ mod tests {
],
uv: vec![[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]],
normal: vec![[0.0, 0.0, 1.0]; 4],
color: vec![
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
],
indices: vec![0, 1, 2, 2, 3, 0],
};

Expand All @@ -210,12 +257,21 @@ mod tests {
#[test]
fn test_create_cube_geometry_data() {
let texture_manager = TextureManager::new();
let geometry_data =
create_cube_geometry_data(0.0, 0.0, 0.0, 0b111111, BlockId::Stone, &texture_manager);
let chunk = Chunk::new(Vec3::ZERO);
let geometry_data = create_cube_geometry_data(
0.0,
0.0,
0.0,
0b111111,
BlockId::Stone,
&texture_manager,
&chunk,
);

assert_eq!(geometry_data.position.len(), 6 * 4);
assert_eq!(geometry_data.uv.len(), 6 * 4);
assert_eq!(geometry_data.normal.len(), 6 * 4);
assert_eq!(geometry_data.color.len(), 6 * 4);
assert_eq!(geometry_data.indices.len(), 6 * 6);
}
}
11 changes: 9 additions & 2 deletions src/client/terrain/util/mesher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ pub fn create_cube_mesh_from_data(geometry_data: GeometryData) -> Option<Mesh> {
position,
uv,
normal,
color,
indices,
} = geometry_data;

if (position.is_empty() || uv.is_empty() || normal.is_empty() || indices.is_empty())
|| (position.len() != uv.len() || uv.len() != normal.len())
if (position.is_empty()
|| uv.is_empty()
|| normal.is_empty()
|| indices.is_empty()
|| color.is_empty())
|| (position.len() != uv.len() || uv.len() != normal.len() || normal.len() != color.len())
{
return None;
}
Expand All @@ -22,6 +27,7 @@ pub fn create_cube_mesh_from_data(geometry_data: GeometryData) -> Option<Mesh> {
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, position)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uv)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normal)
.with_inserted_attribute(Mesh::ATTRIBUTE_COLOR, color)
.with_inserted_indices(Indices::U32(indices)),
)
}
Expand All @@ -37,4 +43,5 @@ pub struct GeometryData {
pub uv: Vec<[f32; 2]>,
pub normal: Vec<[f32; 3]>,
pub indices: Vec<u32>,
pub color: Vec<[f32; 4]>,
}