diff --git a/Cargo.toml b/Cargo.toml index 1fb8446..3cb8f7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bevy = "0.11.3" -bevy_atmosphere = "0.7.0" +bevy = "0.12" bevy_meshem = { git = "https://github.com/Adamkob12/bevy_meshem.git" } # bevy_meshem = { path = "../meshem/workin"} futures-lite = "1.13.0" diff --git a/assets/minecraft-blocks.png b/assets/minecraft-blocks.png new file mode 100644 index 0000000..6bac1af Binary files /dev/null and b/assets/minecraft-blocks.png differ diff --git a/src/block_reg.rs b/src/block_reg.rs index 10e0f6f..0085ccd 100644 --- a/src/block_reg.rs +++ b/src/block_reg.rs @@ -1,5 +1,5 @@ use bevy::prelude::*; -use bevy::render::mesh::{Mesh, MeshVertexAttribute}; +use bevy::render::mesh::MeshVertexAttribute; use bevy_meshem::prelude::*; pub type Block = u16; @@ -17,6 +17,7 @@ pub const GLOWSTONE: Block = 9; pub const WATER: Block = 10; pub const VOXEL_DIMS: [f32; 3] = [1.0, 1.0, 1.0]; +pub const VOXEL_CENTER: [f32; 3] = [0.0, 0.0, 0.0]; const ATLAS_CORDS: [u32; 2] = [24, 24]; const PADDING: f32 = 0.0625; @@ -49,6 +50,7 @@ impl Default for BlockRegistry { (Forward, [11, 0]), (Back, [11, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 0.8, @@ -65,6 +67,7 @@ impl Default for BlockRegistry { (Forward, [1, 0]), (Back, [1, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, @@ -80,6 +83,7 @@ impl Default for BlockRegistry { (Forward, [2, 0]), (Back, [2, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, @@ -95,6 +99,7 @@ impl Default for BlockRegistry { (Forward, [3, 0]), (Back, [3, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, @@ -110,6 +115,7 @@ impl Default for BlockRegistry { (Forward, [4, 0]), (Back, [4, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, @@ -125,6 +131,7 @@ impl Default for BlockRegistry { (Forward, [6, 0]), (Back, [6, 0]), ], + VOXEL_CENTER, PADDING, Some(0.90), 1.0, @@ -140,6 +147,7 @@ impl Default for BlockRegistry { (Forward, [7, 0]), (Back, [7, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, @@ -155,6 +163,7 @@ impl Default for BlockRegistry { (Forward, [8, 0]), (Back, [8, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, @@ -170,6 +179,7 @@ impl Default for BlockRegistry { (Forward, [9, 0]), (Back, [9, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, @@ -185,6 +195,7 @@ impl Default for BlockRegistry { (Forward, [10, 0]), (Back, [10, 0]), ], + VOXEL_CENTER, PADDING, Some(0.75), 1.0, diff --git a/src/chunk/chunk_queue.rs b/src/chunk/chunk_queue.rs index 02aabf9..79bfc23 100644 --- a/src/chunk/chunk_queue.rs +++ b/src/chunk/chunk_queue.rs @@ -135,8 +135,9 @@ impl ChunkQueue { &*breg, MeshingAlgorithm::Culling, Some(PbsParameters { - pbs_value: 0.20, - pbs_smoothing: 0.5, + pbs_value: 0.4, + min: 0.2, + smoothing: PbsSmoothing::Low, }), )?; Some((t, grid, cords)) diff --git a/src/main.rs b/src/main.rs index 98c1244..d949006 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ mod utils; use add_break_blocks::*; use bevy::{prelude::*, window::PrimaryWindow}; -use bevy_atmosphere::prelude::*; use bevy_meshem::prelude::*; use block_reg::*; use chunk::*; @@ -57,7 +56,7 @@ fn main() { mode: bevy::window::WindowMode::BorderlessFullscreen, ..Default::default()}),..Default::default()}), - AtmospherePlugin, + // AtmospherePlugin, PlayerPlugin, ChunkPlugin, InventoryPlugin, @@ -72,21 +71,19 @@ fn main() { .insert_resource(CycleTimer(Timer::new( bevy::utils::Duration::from_millis(50), TimerMode::Repeating,))) - .insert_resource(GlobalSecondsCounter(0)) - .insert_resource(AtmosphereModel::default()); + .insert_resource(GlobalSecondsCounter(0)); + // .insert_resource(AtmosphereModel::default()); // Events app.add_event::(); // Systems app.add_systems(PostStartup, setup) - .add_systems( - PostUpdate, (daylight_cycle).run_if(in_state(InitialChunkLoadState::Complete)),) .add_systems(OnEnter(InitialChunkLoadState::Complete), setup_light) .add_systems(Update, check_if_loaded.run_if(in_state(InitialChunkLoadState::MeshesLoaded)),) .add_systems(Update,(handle_tasks, add_break_detector, /* debug_cage */),) - .add_systems(PostUpdate, (handle_block_break_place, update_seconds)); + .add_systems(PostUpdate, (handle_block_break_place, update_seconds, daylight_cycle)); app.run(); } @@ -228,7 +225,7 @@ fn handle_block_break_place( breg: Res, ) { let breg = breg.into_inner(); - for event in block_change.iter() { + for event in block_change.read() { 'A: for &(chunk, block, onto) in event.blocks.iter() { let ent = chunk_map.get_ent(chunk).expect( "Chunk should be loaded into internal data structure `ChunkMap` but it isn't.", diff --git a/src/player/mod.rs b/src/player/mod.rs index 594ddfa..795e6d5 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -1,6 +1,5 @@ pub mod movement; #[allow(unused_imports)] -use crate::utils::{one_d_cords, position_to_chunk}; use crate::*; use bevy::ecs::event::{Events, ManualEventReader}; use bevy::input::mouse::MouseMotion; diff --git a/src/player/movement.rs b/src/player/movement.rs index f04854f..515fa97 100644 --- a/src/player/movement.rs +++ b/src/player/movement.rs @@ -37,7 +37,6 @@ pub(super) fn setup_player(mut commands: Commands) { }, FlyCam, CurrentChunk([0, 0]), - AtmosphereCamera::default(), VelocityVectors { xV: Vec3::ZERO, yV: Vec3::ZERO, @@ -183,7 +182,7 @@ pub(super) fn player_look( ) { if let Ok(window) = primary_window.get_single() { for mut transform in query.iter_mut() { - for ev in state.reader_motion.iter(&motion) { + for ev in state.reader_motion.read(&motion) { let (mut yaw, mut pitch, _) = transform.rotation.to_euler(EulerRot::YXZ); match window.cursor.grab_mode { CursorGrabMode::None => (), diff --git a/src/sky.rs b/src/sky.rs index d953614..568789c 100644 --- a/src/sky.rs +++ b/src/sky.rs @@ -1,6 +1,5 @@ use crate::*; use bevy::prelude::*; -use bevy_atmosphere::prelude::*; pub fn setup_light(mut commands: Commands, primary_window: Query<&Window, With>) { // commands.insert_resource(AtmosphereModel::new(Nishita { @@ -51,13 +50,8 @@ pub struct Sun; #[derive(Resource)] pub struct CycleTimer(pub Timer); -pub fn daylight_cycle( - mut atmosphere: AtmosphereMut, - mut query: Query<(&mut Transform, &mut DirectionalLight), With>, -) { - atmosphere.sun_position = Vec3::new(0.0, 0.9, 0.7); - - if let Some((mut light_trans, mut directional)) = query.single_mut().into() { +pub fn daylight_cycle(mut query: Query<(&mut Transform, &mut DirectionalLight), With>) { + if let Ok((mut light_trans, mut directional)) = query.get_single_mut() { let t = Transform::from_xyz(0.0, 0.0, 0.0).looking_to(Vec3::new(0.6, -1.0, 0.6), Vec3::Y); light_trans.rotation = t.rotation; directional.illuminance = 9000.0; diff --git a/src/utils.rs b/src/utils.rs index e450079..a80eaf0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,173 +1,3 @@ use crate::Face; use crate::Face::*; use bevy::prelude::Vec3; - -pub fn position_to_chunk(pos: Vec3, chunk_dims: (usize, usize, usize)) -> [i32; 2] { - let chunk_width = chunk_dims.0; - let chunk_length = chunk_dims.1; - let x = pos.x + 0.5; - let z = pos.z + 0.5; - [ - (x / chunk_width as f32 + (x.signum() - 1.0) / 2.0) as i32, - (z / chunk_length as f32 + (z.signum() - 1.0) / 2.0) as i32, - ] -} - -// the bool is for whether or not the pos is within the height bounds -pub fn position_to_chunk_position( - pos: Vec3, - chunk_dims: (usize, usize, usize), -) -> ([i32; 2], [usize; 3], bool) { - let chunk_width = chunk_dims.0; - let chunk_length = chunk_dims.2; - let chunk_height = chunk_dims.1; - let chunk = position_to_chunk(pos, chunk_dims); - - let x = pos.x + 0.5; - let z = pos.z + 0.5; - let y = pos.y + 0.5; - - let chunk_pos = [ - (x - chunk[0] as f32 * chunk_width as f32) as usize, - y as usize, - (z - chunk[1] as f32 * chunk_length as f32) as usize, - ]; - - let flag = y >= 0.0 && y <= chunk_height as f32; - (chunk, chunk_pos, flag) -} - -pub const fn three_d_cords(oned: usize, dims: (usize, usize, usize)) -> (usize, usize, usize) { - let height = dims.2; - let length = dims.1; - let width = dims.0; - - let h = (oned / (length * width)) as usize; - let l = ((oned - h * (length * width)) / width) as usize; - let w = (oned - h * (length * width) - l * width) as usize; - - assert!(w < width, "Out of bounds to convert into 3d coordinate."); - assert!(h < height, "Out of bounds to convert into 3d coordinate."); - assert!(l < length, "Out of bounds to convert into 3d coordinate."); - - (w, h, l) -} - -pub const fn three_d_cords_arr(oned: usize, dims: (usize, usize, usize)) -> [usize; 3] { - let height = dims.2; - let length = dims.1; - let width = dims.0; - - let h = (oned / (length * width)) as usize; - let l = ((oned - h * (length * width)) / width) as usize; - let w = (oned - h * (length * width) - l * width) as usize; - - assert!(w < width, "Out of bounds to convert into 3d coordinate."); - assert!(h < height, "Out of bounds to convert into 3d coordinate."); - assert!(l < length, "Out of bounds to convert into 3d coordinate."); - - [w, h, l] -} - -pub const fn one_d_cords(threed: [usize; 3], dims: (usize, usize, usize)) -> usize { - assert!(threed[0] < dims.0, "3d coordinate out of dimension bounds."); - assert!(threed[1] < dims.1, "3d coordinate out of dimension bounds."); - assert!(threed[2] < dims.2, "3d coordinate out of dimension bounds."); - threed[1] * (dims.0 * dims.2) + threed[2] * dims.0 + threed[0] -} - -pub const fn one_d_cords_safe(threed: [usize; 3], dims: (usize, usize, usize)) -> Option { - if threed[0] >= dims.0 || threed[1] >= dims.1 || threed[2] >= dims.2 { - None - } else { - Some(threed[1] * (dims.0 * dims.2) + threed[2] * dims.0 + threed[0]) - } -} - -// Extract the vertex data for the physics engine. -use bevy::render::mesh::{Mesh, VertexAttributeValues}; -pub fn extract_position_vertex_data(mesh: &Mesh) -> Vec { - let VertexAttributeValues::Float32x3(pos_vertices) = - mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap() else { - panic!("Vertex position data should be in `VertexAttributeValues::Float32x3`") - }; - pos_vertices - .iter() - .map(|[x, y, z]| Vec3::new(*x, *y, *z)) - .collect() -} - -// Extract the indices for the physics engine. -use bevy::render::mesh::Indices; -use bevy_meshem::Dimensions; -pub fn extract_indices_data(mesh: &Mesh) -> Vec<[u32; 3]> { - let Indices::U32(indices) = mesh.indices().unwrap() else { - panic!("Indices data shoud be in `Indices::U32` format") - }; - indices - .chunks(3) - .map(|chunk| [chunk[0], chunk[1], chunk[2]]) - .collect() -} - -pub fn iter_faces_of_chunk(dims: Dimensions, face: Face) -> impl Iterator { - let all = 0..(dims.0 * dims.1 * dims.2); - let m = [dims.1 - 1, 0, dims.0 - 1, 0, dims.2 - 1, 0]; - let i = match face { - Top | Bottom => 1, - Right | Left => 0, - Forward | Back => 2, - }; - let all = all.filter(move |x| three_d_cords_arr(*x, dims)[i] == m[face as usize]); - all -} - -pub fn is_block_on_edge(dims: Dimensions, index: usize) -> Vec { - let mut to_return = vec![]; - let all = 0..(dims.0 * dims.1 * dims.2); - let m = [dims.1 - 1, 0, dims.0 - 1, 0, dims.2 - 1, 0]; - for i in 0..6 { - let face = Face::from(i); - let j = match face { - Top | Bottom => 1, - Right | Left => 0, - Forward | Back => 2, - }; - if three_d_cords_arr(index, dims)[j] == m[face as usize] { - to_return.push(face); - } - } - to_return -} - -pub fn get_neigbhor_across_chunk(dims: Dimensions, index: usize, face: Face) -> usize { - for f in is_block_on_edge(dims, index) { - if f as usize == face as usize { - return match face { - Right => index - dims.0 + 1, - Left => index + dims.0 - 1, - Back => index - dims.0 * (dims.2 - 1), - Forward => index + dims.0 * (dims.2 - 1), - _ => panic!("Shouldn't happen"), - }; - } - } - panic!("`get_neigbhor_across_chunk` was called on a block that wasn't on the edge of a chunk"); -} - -pub fn get_neigbhors_from_across_chunks(dims: Dimensions, index: usize) -> Vec<(Face, usize)> { - let edges = is_block_on_edge(dims, index); - if edges.is_empty() { - return vec![]; - } - edges - .iter() - .filter_map(|f| { - if (*f as usize) < 2 { - None - } else { - Some((*f, get_neigbhor_across_chunk(dims, index, *f))) - } - }) - .collect() -}