This commit is contained in:
griffi-gh 2023-01-27 03:24:21 +01:00
parent 6b8deab094
commit 783b6a1f84
3 changed files with 47 additions and 27 deletions

View file

@ -131,7 +131,7 @@ fn start_required_tasks(
let chunk = world.chunks.get_mut(&position).unwrap();
chunk.current_state = CurrentChunkState::Loading;
// ===========
log::info!("Started loading chunk {position}");
//log::trace!("Started loading chunk {position}");
},
DesiredChunkState::Rendered if chunk.current_state == CurrentChunkState::Loaded => {
//get needed data
@ -147,7 +147,7 @@ fn start_required_tasks(
let chunk = world.chunks.get_mut(&position).unwrap();
chunk.current_state = CurrentChunkState::CalculatingMesh;
// ===========
log::info!("Started generating mesh for chunk {position}");
//log::trace!("Started generating mesh for chunk {position}");
}
_ => ()
}

View file

@ -1,3 +1,4 @@
use glam::{vec3a, Vec3A, Vec4Swizzles, Vec3};
use shipyard::{NonSendSync, UniqueView, UniqueViewMut, View, IntoIter};
use glium::{
implement_vertex, uniform,
@ -68,25 +69,35 @@ pub fn draw_world(
});
let view = camera.view_matrix.to_cols_array_2d();
let perspective = camera.perspective_matrix.to_cols_array_2d();
let camera_mat = camera.perspective_matrix * camera.view_matrix;
for (&position, chunk) in &chunks.chunks {
if let Some(key) = chunk.mesh_index {
let mesh = meshes.get(key).expect("Mesh index pointing to nothing");
let world_position = (position.as_vec3() * CHUNK_SIZE as f32).to_array();
if mesh.index_buffer.len() > 0 { //maybe this is a bit hacky?
target.0.draw(
&mesh.vertex_buffer,
&mesh.index_buffer,
&program.0,
&uniform! {
position_offset: world_position,
view: view,
perspective: perspective,
tex: texture_sampler
},
&draw_parameters
).unwrap();
let world_position = position.as_vec3a() * CHUNK_SIZE as f32;
if mesh.index_buffer.len() == 0 {
continue
}
//basic culling
// let chunk_center = world_position + Vec3A::splat(CHUNK_SIZE as f32) * 0.5;
// let cull_point = camera_mat * chunk_center.extend(1.);
// if (cull_point.xyz() / cull_point.w).abs().cmpgt(Vec3::splat(1.)).any() {
// continue
// }
target.0.draw(
&mesh.vertex_buffer,
&mesh.index_buffer,
&program.0,
&uniform! {
position_offset: world_position.to_array(),
view: view,
perspective: perspective,
tex: texture_sampler
},
&draw_parameters
).unwrap();
}
}
}

View file

@ -13,17 +13,26 @@ pub fn generate_world(chunk_position: IVec3, seed: u64) -> BlockData {
let mut blocks = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]);
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
let position = ivec3(x as i32, y as i32, z as i32) + offset;
let noise = noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32);
if (0.7..0.8).contains(&noise) {
blocks[x][y][z] = Block::Stone;
}
}
}
}
blocks[0][0][0] = Block::Stone;
blocks[0][CHUNK_SIZE - 1][0] = Block::Stone;
blocks[CHUNK_SIZE - 1][0][0] = Block::Stone;
blocks[CHUNK_SIZE - 1][CHUNK_SIZE - 1][0] = Block::Stone;
blocks[0][0][CHUNK_SIZE - 1] = Block::Stone;
blocks[0][CHUNK_SIZE - 1][CHUNK_SIZE - 1] = Block::Stone;
blocks[CHUNK_SIZE - 1][0][CHUNK_SIZE - 1] = Block::Stone;
blocks[CHUNK_SIZE - 1][CHUNK_SIZE - 1][CHUNK_SIZE - 1] = Block::Stone;
// for x in 0..CHUNK_SIZE {
// for y in 0..CHUNK_SIZE {
// for z in 0..CHUNK_SIZE {
// let position = ivec3(x as i32, y as i32, z as i32) + offset;
// let noise = noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32);
// if (0.7..0.8).contains(&noise) {
// blocks[x][y][z] = Block::Stone;
// }
// }
// }
// }
blocks
}