Skip to content

Commit

Permalink
Update to Bevy 0.13 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshiew authored Mar 9, 2024
1 parent 4fcb8d7 commit b62daf1
Show file tree
Hide file tree
Showing 12 changed files with 958 additions and 1,008 deletions.
1,849 changes: 911 additions & 938 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ name = "infinigen"
version = "0.0.0"
edition = "2021"
publish = false
rust-version = "1.70.0"
rust-version = "1.76.0"

[lib]
path = "src/lib/mod.rs"

[dependencies]
bevy = { version = "0.12.1" }
bevy_common_assets = { version = "0.8.0", features = ["ron"] }
bevy_egui = { version = "0.23.0" }
bevy-inspector-egui = { version = "0.21.0" }
bevy = { version = "0.13.0" }
bevy_common_assets = { version = "0.10.0", features = ["ron"] }
bevy_egui = { version = "0.25.0" }
bevy-inspector-egui = { version = "0.23.4" }

# config
config = { version = "0.13.4", default-features = false, features = ["ron"] }
config = { version = "0.14.0", default-features = false, features = ["ron"] }

# voxels
block-mesh = { git = "https://github.com/bonsairobo/block-mesh-rs", rev = "793c53e82c12b3f7e9502006664c6e81f8b1c78a" }

# serialization
bincode = "1.3.3"
serde = { version = "1.0.193", features = ["derive"] }
serde = { version = "1.0.197", features = ["derive"] }

# utils
eyre = "0.6.11"
futures-lite = "2.1.0"
strum = { version = "0.25.0", features = ["derive"] }
lru = "0.12.1"
eyre = "0.6.12"
futures-lite = "2.2.0"
strum = { version = "0.26.1", features = ["derive"] }
lru = "0.12.3"

# instrumentation
tracing = "0.1.40"
Expand All @@ -41,7 +41,7 @@ noise = "0.8.2"
bracket-noise = "0.8.7"
splines = "4.3.1"
ndarray = { version = "0.15.6", features = ["serde"] }
nalgebra = "0.32.3"
nalgebra = "0.32.4"
rustc-hash = "1.1.0"

[dev-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions src/lib/camera/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub struct KeyBindings {
impl Default for KeyBindings {
fn default() -> Self {
Self {
move_forward: KeyCode::W,
move_backward: KeyCode::S,
move_left: KeyCode::A,
move_right: KeyCode::D,
move_forward: KeyCode::KeyW,
move_backward: KeyCode::KeyS,
move_left: KeyCode::KeyA,
move_right: KeyCode::KeyD,
move_ascend: KeyCode::Space,
move_descend: KeyCode::ShiftLeft,
}
Expand All @@ -34,7 +34,7 @@ pub struct InputState {
}

pub fn keyboard(
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
primary_window: Query<&Window, With<PrimaryWindow>>,
key_bindings: Res<KeyBindings>,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn setup(mut primary_window: Query<&mut Window, With<PrimaryWindow>>) {
}

pub fn handle_input(
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
mut primary_window: Query<&mut Window, With<PrimaryWindow>>,
) {
let mut window = primary_window.get_single_mut().unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/lib/debug/chunk_borders.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::render::render_asset::RenderAssetUsages;
/// Adapted from lines example of Bevy
use bevy::{
pbr::{MaterialPipeline, MaterialPipelineKey},
Expand Down Expand Up @@ -30,7 +31,7 @@ pub struct LineList {

impl From<LineList> for Mesh {
fn from(line: LineList) -> Self {
let mut mesh = Mesh::new(PrimitiveTopology::LineList);
let mut mesh = Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::default());

let vertices: Vec<_> = line.lines.into_iter().flat_map(|(a, b)| [a, b]).collect();
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, vertices);
Expand Down Expand Up @@ -62,7 +63,7 @@ impl Material for LineMaterial {
}

pub fn toggle(
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<LineMaterial>>,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/debug/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ pub fn display_debug_info(
ui.label(format!(
"FPS: {:.02}",
diagnostics
.get(FrameTimeDiagnosticsPlugin::FPS)
.get(&FrameTimeDiagnosticsPlugin::FPS)
.unwrap()
.average()
.unwrap_or_default()
));
ui.label(format!(
"Entities: {}",
diagnostics
.get(EntityCountDiagnosticsPlugin::ENTITY_COUNT)
.get(&EntityCountDiagnosticsPlugin::ENTITY_COUNT)
.unwrap()
.average()
.unwrap_or_default()
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Default for UiState {
}
}

pub fn toggle_debug_info(keys: Res<Input<KeyCode>>, mut ui_state: ResMut<UiState>) {
pub fn toggle_debug_info(keys: Res<ButtonInput<KeyCode>>, mut ui_state: ResMut<UiState>) {
for key in keys.get_just_pressed() {
if key == &KeyCode::F7 {
ui_state.show_debug_info = !ui_state.show_debug_info;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/debug/wireframe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{pbr::wireframe::WireframeConfig, prelude::*};

pub fn toggle(keys: Res<Input<KeyCode>>, mut wireframe_cfg: ResMut<WireframeConfig>) {
pub fn toggle(keys: Res<ButtonInput<KeyCode>>, mut wireframe_cfg: ResMut<WireframeConfig>) {
for key in keys.get_just_pressed() {
if key == &KeyCode::F3 {
wireframe_cfg.global = !wireframe_cfg.global;
Expand Down
23 changes: 3 additions & 20 deletions src/lib/mesh/textures.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
use bevy::prelude::TextureAtlasLayout;
use rustc_hash::FxHashMap;

use bevy::sprite::TextureAtlas;
use serde::{Deserialize, Serialize};
use strum::EnumIter;

use crate::common::world::ChunkBlockId;

const TEXTURE_SIZE: usize = 64;

// translate UVs for a texture atlas
pub fn to_tex_coords(
uvs: [[f32; 2]; 4],
texture_idx: usize,
texture_atlas: &TextureAtlas,
) -> [[f32; 2]; 4] {
let texture_start = [
texture_atlas.textures[texture_idx].min[0] as usize,
texture_atlas.textures[texture_idx].min[1] as usize,
];
let texture_atlas_size = [
texture_atlas.size[0] as usize,
texture_atlas.size[1] as usize,
];
to_tex_coords_raw(uvs, texture_start, texture_atlas_size)
}

// translate UVs for a texture atlas given raw coordinates
pub fn to_tex_coords_raw(
mut uvs: [[f32; 2]; 4],
Expand Down Expand Up @@ -118,8 +101,8 @@ impl TextureMap {
}
}

impl From<&TextureAtlas> for TextureMap {
fn from(tatlas: &TextureAtlas) -> Self {
impl From<&TextureAtlasLayout> for TextureMap {
fn from(tatlas: &TextureAtlasLayout) -> Self {
let size = [tatlas.size[0] as usize, tatlas.size[1] as usize];
Self {
size,
Expand Down
8 changes: 6 additions & 2 deletions src/lib/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use bevy::prelude::*;
use bevy::render::mesh::{Indices, VertexAttributeValues};
use bevy::render::render_asset::RenderAssetUsages;
use bevy::render::render_resource::PrimitiveTopology;

use crate::common::chunks::UnpackedChunk;
Expand All @@ -21,7 +22,10 @@ pub fn to_bevy_mesh(
indices,
}: MeshInfo,
) -> Mesh {
let mut render_mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut render_mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
);
render_mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
VertexAttributeValues::Float32x3(positions),
Expand All @@ -35,7 +39,7 @@ pub fn to_bevy_mesh(
VertexAttributeValues::Float32x4(colors),
);
render_mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, VertexAttributeValues::Float32x2(uvs));
render_mesh.set_indices(Some(Indices::U32(indices)));
render_mesh.insert_indices(Indices::U32(indices));
render_mesh
}

Expand Down
34 changes: 11 additions & 23 deletions src/lib/scene/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,8 @@ pub enum MaterialType {
}

#[derive(
Debug,
Clone,
PartialEq,
Eq,
Hash,
Serialize,
Deserialize,
Ord,
PartialOrd,
TypePath,
bevy::reflect::TypeUuid,
Asset,
Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd, TypePath, Asset,
)]
#[uuid = "125a8e86-14d2-4c46-9c45-06b0c80cae11"]
pub struct BlockDefinition {
pub id: BlockId,
#[serde(default)]
Expand Down Expand Up @@ -179,18 +167,18 @@ pub fn setup(
let name = name.trim_end_matches(".png");

block_texture_handles_by_name.insert(name.to_owned(), handle.clone_weak());
block_tatlas_builder.add_texture(handle.id(), texture);
block_tatlas_builder.add_texture(Some(handle.id()), texture);
} else {
tracing::warn!("{:?} did not resolve to an `Image` asset.", path,);
panic!();
};
}
let block_tatlas = block_tatlas_builder.finish(&mut textures).unwrap();
tracing::info!(?block_tatlas.size, ?block_tatlas.textures, "Stitched texture atlas");
let block_tatlas_texture = block_tatlas.texture.clone();
let (atlas_layout, texture_atlas) = block_tatlas_builder.finish().unwrap();
tracing::info!(?atlas_layout.size, ?atlas_layout.textures, "Stitched texture atlas");
let texture_atlas = textures.add(texture_atlas);

let mut block_textures = TextureMap::default();
block_textures.size = [block_tatlas.size[0] as usize, block_tatlas.size[1] as usize];
block_textures.size = [atlas_layout.size[0] as usize, atlas_layout.size[1] as usize];

// map block definitions in alphabetical order by ID
// so for the same set of block definitions, we should get the same mapping
Expand All @@ -217,11 +205,11 @@ pub fn setup(
.get(texture_file_names.get(&face).unwrap())
.unwrap();
tracing::info!(?face, ?block_definition.id, "Found specific texture");
let tidx = block_tatlas.get_texture_index(texture_handle).unwrap();
let tidx = atlas_layout.get_texture_index(texture_handle).unwrap();
let tidx = FaceAppearance::Texture {
coords: [
block_tatlas.textures[tidx].min[0] as usize,
block_tatlas.textures[tidx].min[1] as usize,
atlas_layout.textures[tidx].min[0] as usize,
atlas_layout.textures[tidx].min[1] as usize,
],
};
faces[face as usize] = tidx;
Expand All @@ -242,7 +230,7 @@ pub fn setup(
base_color: Color::WHITE,
perceptual_roughness: 0.75,
reflectance: 0.25,
base_color_texture: Some(block_tatlas_texture),
base_color_texture: Some(texture_atlas),
..default()
});
registry.materials[MaterialType::Translucent as usize] = materials.add(StandardMaterial {
Expand Down Expand Up @@ -282,7 +270,7 @@ impl Plugin for RegistryPlugin {
fn build(&self, app: &mut App) {
tracing::info!("Initializing registry plugin");
app.init_resource::<Registry>()
.add_state::<AppState>()
.init_state::<AppState>()
.add_systems(OnEnter(AppState::LoadAssets), load_assets)
.add_systems(Update, check_assets.run_if(in_state(AppState::LoadAssets)))
.add_systems(OnEnter(AppState::RegisterAssets), setup);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl bevy::prelude::Plugin for Plugin {
.insert_resource(ClearColor(SKY_COLOR))
.add_systems(Startup, (lights::setup, init_config))
.init_resource::<Registry>()
.add_state::<AppState>()
.init_state::<AppState>()
.add_event::<UpdateSettingsEvent>()
.add_event::<ManageChunksEvent>()
.add_systems(OnEnter(AppState::LoadAssets), load_assets)
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn main() -> ExitCode {
.set(LogPlugin {
filter: LOG_FILTER.into(),
level: bevy::log::Level::DEBUG,
update_subscriber: None,
})
.set(ImagePlugin::default_nearest())
.set(WindowPlugin {
Expand Down

0 comments on commit b62daf1

Please sign in to comment.