Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
bevy 0.12, meshem 0.3, updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamkob12 committed Nov 5, 2023
1 parent 17580eb commit 48efe28
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 194 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Binary file added assets/minecraft-blocks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion src/block_reg.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -49,6 +50,7 @@ impl Default for BlockRegistry {
(Forward, [11, 0]),
(Back, [11, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
0.8,
Expand All @@ -65,6 +67,7 @@ impl Default for BlockRegistry {
(Forward, [1, 0]),
(Back, [1, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand All @@ -80,6 +83,7 @@ impl Default for BlockRegistry {
(Forward, [2, 0]),
(Back, [2, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand All @@ -95,6 +99,7 @@ impl Default for BlockRegistry {
(Forward, [3, 0]),
(Back, [3, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand All @@ -110,6 +115,7 @@ impl Default for BlockRegistry {
(Forward, [4, 0]),
(Back, [4, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand All @@ -125,6 +131,7 @@ impl Default for BlockRegistry {
(Forward, [6, 0]),
(Back, [6, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.90),
1.0,
Expand All @@ -140,6 +147,7 @@ impl Default for BlockRegistry {
(Forward, [7, 0]),
(Back, [7, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand All @@ -155,6 +163,7 @@ impl Default for BlockRegistry {
(Forward, [8, 0]),
(Back, [8, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand All @@ -170,6 +179,7 @@ impl Default for BlockRegistry {
(Forward, [9, 0]),
(Back, [9, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand All @@ -185,6 +195,7 @@ impl Default for BlockRegistry {
(Forward, [10, 0]),
(Back, [10, 0]),
],
VOXEL_CENTER,
PADDING,
Some(0.75),
1.0,
Expand Down
5 changes: 3 additions & 2 deletions src/chunk/chunk_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 5 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -57,7 +56,7 @@ fn main() {
mode: bevy::window::WindowMode::BorderlessFullscreen,
..Default::default()}),..Default::default()}),

AtmospherePlugin,
// AtmospherePlugin,
PlayerPlugin,
ChunkPlugin,
InventoryPlugin,
Expand All @@ -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::<BlockChange>();

// 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();
}
Expand Down Expand Up @@ -228,7 +225,7 @@ fn handle_block_break_place(
breg: Res<BlockRegistry>,
) {
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.",
Expand Down
1 change: 0 additions & 1 deletion src/player/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/player/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 => (),
Expand Down
10 changes: 2 additions & 8 deletions src/sky.rs
Original file line number Diff line number Diff line change
@@ -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<PrimaryWindow>>) {
// commands.insert_resource(AtmosphereModel::new(Nishita {
Expand Down Expand Up @@ -51,13 +50,8 @@ pub struct Sun;
#[derive(Resource)]
pub struct CycleTimer(pub Timer);

pub fn daylight_cycle(
mut atmosphere: AtmosphereMut<Nishita>,
mut query: Query<(&mut Transform, &mut DirectionalLight), With<Sun>>,
) {
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<Sun>>) {
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;
Expand Down
170 changes: 0 additions & 170 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<usize> {
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<Vec3> {
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<Item = usize> {
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<Face> {
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()
}

0 comments on commit 48efe28

Please sign in to comment.