Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Bevy 0.11 #28

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Port to bevy 0.11
  • Loading branch information
juliohq committed Oct 4, 2023
commit 65640fdf4c2f666742e85d59b34b6d5c8ba89468
554 changes: 344 additions & 210 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = "0.10.1"
bevy = "0.11.3"
ndshape = "0.3.0"
block-mesh = "0.2.0"
ndcopy = "0.3.0"
thread_local = "1.1.7"
bevy_egui = "0.20"
bevy_egui = "0.21"
float-ord = "0.3.2"
futures-lite = "1.12.0"
once_cell = "1.17.1"
bevy_atmosphere = "0.6"
bevy_atmosphere = "0.7"
bitflags = "2.0.2"
ilattice = { version = "0.3.0", features = ["glam", "morton-encoding"] }
noise = "0.8.2"
Expand Down
24 changes: 13 additions & 11 deletions assets/shaders/terrain_pipeline.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#import bevy_pbr::utils
#import bevy_pbr::clustered_forward
#import bevy_pbr::lighting
#import bevy_pbr::pbr_ambient
#import bevy_pbr::shadows
#import bevy_pbr::fog
#import bevy_pbr::pbr_functions
#import bevy_pbr::pbr_functions PbrInput, pbr_input_new, calculate_view, pbr
#import bevy_pbr::mesh_bindings mesh
#import bevy_pbr::mesh_view_bindings view
#import bevy_core_pipeline::tonemapping tone_mapping

#import "shaders/voxel_data.wgsl"
#import "shaders/terrain_uniforms.wgsl"
#import "shaders/noise.wgsl"
#import "shaders/fog.wgsl"
#import "shaders/voxel_data.wgsl" voxel_data_extract_normal, voxel_data_extract_material_index
#import "shaders/terrain_uniforms.wgsl" VoxelMat, voxel_materials, render_distance, TERRAIN_CHUNK_LENGTH
#import "shaders/noise.wgsl" hash
#import "shaders/fog.wgsl" ffog_apply_fog

struct Vertex {
@location(0) position: vec3<f32>,
Expand All @@ -30,10 +32,10 @@ struct VertexOutput {

@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
let world_position = mesh_position_local_to_world(mesh.model, vec4<f32>(vertex.position, 1.0));
let world_position = bevy_pbr::mesh_functions::mesh_position_local_to_world(mesh.model, vec4<f32>(vertex.position, 1.0));

var out: VertexOutput;
out.clip_position = mesh_position_world_to_clip(world_position);
out.clip_position = bevy_pbr::mesh_functions::mesh_position_world_to_clip(world_position);
out.voxel_normal = voxel_data_extract_normal(vertex.voxel_data);
out.voxel_data = vertex.voxel_data;
out.world_position = world_position.xyz;
Expand Down Expand Up @@ -65,10 +67,10 @@ fn prepare_pbr_input_from_voxel_mat(voxel_mat: VoxelMat, frag: Fragment) -> PbrI

pbr_input.frag_coord = frag.frag_coord;
pbr_input.world_position = vec4<f32>(frag.world_position, 1.0);
pbr_input.world_normal = (f32(frag.front_facing) * 2.0 - 1.0) * mesh_normal_local_to_world(frag.voxel_normal);
pbr_input.world_normal = (f32(frag.front_facing) * 2.0 - 1.0) * bevy_pbr::mesh_functions::mesh_normal_local_to_world(frag.voxel_normal);

pbr_input.is_orthographic = view.projection[3].w == 1.0;
pbr_input.N = normalize(mesh_normal_local_to_world(frag.voxel_normal));
pbr_input.N = normalize(bevy_pbr::mesh_functions::mesh_normal_local_to_world(frag.voxel_normal));
pbr_input.V = calculate_view(vec4<f32>(frag.world_position, 1.0), pbr_input.is_orthographic);
pbr_input.flags = mesh.flags;
return pbr_input;
Expand All @@ -80,7 +82,7 @@ fn fragment(frag: Fragment) -> @location(0) vec4<f32> {

/// PBR lighting input data preparation
var pbr_input = prepare_pbr_input_from_voxel_mat(material, frag);
let pbr_colour = tone_mapping(pbr(pbr_input));
let pbr_colour = tone_mapping(pbr(pbr_input), view.color_grading);

// @todo: switch to bevy_pbr::fog

Expand Down
35 changes: 20 additions & 15 deletions src/debug/debug_ui.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use bevy::{
diagnostic::{Diagnostics, EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin},
diagnostic::{DiagnosticsStore, EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin},
input::{keyboard::KeyboardInput, ButtonState},
prelude::{
Color, CoreSet, EventReader, IntoSystemConfig, IntoSystemConfigs, IntoSystemSetConfigs,
KeyCode, Plugin, Res, ResMut, Resource, SystemSet,
Color, EventReader, IntoSystemConfigs, IntoSystemSetConfigs, KeyCode, Plugin, Res, ResMut,
Resource, SystemSet, Update,
},
};

use bevy_egui::{
egui::{self, Rgba, Slider},
EguiContexts, EguiPlugin, EguiSet,
Expand All @@ -16,7 +17,7 @@ use crate::voxel::{
CurrentLocalPlayerChunk, DirtyChunks,
};

fn display_debug_stats(mut egui: EguiContexts, diagnostics: Res<Diagnostics>) {
fn display_debug_stats(mut egui: EguiContexts, diagnostics: Res<DiagnosticsStore>) {
egui::Window::new("performance stuff").show(egui.ctx_mut(), |ui| {
ui.label(format!(
"Avg. FPS: {:.02}",
Expand Down Expand Up @@ -128,7 +129,7 @@ fn display_material_editor(
// base_color
ui.label("Base color");

let mut selected_mat = materials.get_mut_by_id(ui_state.selected_mat).unwrap();
let selected_mat = materials.get_mut_by_id(ui_state.selected_mat).unwrap();

let mut editable_color = Rgba::from_rgba_unmultiplied(
selected_mat.base_color.r(),
Expand Down Expand Up @@ -179,24 +180,28 @@ pub struct DebugUIPlugins;

impl Plugin for DebugUIPlugins {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_plugin(EguiPlugin)
.add_plugin(FrameTimeDiagnosticsPlugin)
.add_plugin(EntityCountDiagnosticsPlugin)
.add_systems((
toggle_debug_ui_displays.in_set(DebugUISet::Toggle),
display_material_editor
.in_set(DebugUISet::Display)
.run_if(display_mat_debug_ui_criteria),
))
app.add_plugins(EguiPlugin)
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_plugins(EntityCountDiagnosticsPlugin)
.add_systems(
Update,
(
toggle_debug_ui_displays.in_set(DebugUISet::Toggle),
display_material_editor
.in_set(DebugUISet::Display)
.run_if(display_mat_debug_ui_criteria),
),
)
.add_systems(
Update,
(display_debug_stats, display_chunk_stats)
.in_set(DebugUISet::Display)
.distributive_run_if(display_debug_ui_criteria),
)
.configure_sets(
Update,
(DebugUISet::Toggle, DebugUISet::Display)
.chain()
.in_base_set(CoreSet::Update)
.after(EguiSet::ProcessInput),
)
.init_resource::<DebugUIState>();
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ mod voxel;
fn main() {
let mut app = App::default();
app.add_plugins(DefaultPlugins)
.add_plugin(voxel::VoxelWorldPlugin)
.add_plugin(debug::DebugUIPlugins)
.add_startup_system(setup)
.add_plugins(voxel::VoxelWorldPlugin)
.add_plugins(debug::DebugUIPlugins)
.add_systems(Startup, setup)
.run();
}

Expand Down
12 changes: 6 additions & 6 deletions src/voxel/render/chunk_material.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::voxel::material::VoxelMaterialRegistry;
use bevy::{
prelude::*,
reflect::TypeUuid,
reflect::{TypePath, TypeUuid},
render::{
extract_component::ExtractComponent,
mesh::MeshVertexAttribute,
Expand All @@ -28,7 +28,7 @@ pub struct GpuVoxelMaterial {
reflectance: f32,
}

#[derive(AsBindGroup, ShaderType, Clone, TypeUuid)]
#[derive(AsBindGroup, ShaderType, Clone, TypePath, TypeUuid)]
#[uuid = "1e31e29e-73d8-419c-8293-876ae81d2636"]
pub struct GpuTerrainUniforms {
#[uniform(0)]
Expand Down Expand Up @@ -127,13 +127,13 @@ pub struct ChunkMaterialPlugin;
impl Plugin for ChunkMaterialPlugin {
fn build(&self, app: &mut App) {
// @todo: figure out race conditions w/ other systems
app.add_plugin(MaterialPlugin::<GpuTerrainUniforms>::default())
app.add_plugins(MaterialPlugin::<GpuTerrainUniforms>::default())
.init_resource::<ChunkMaterialSingleton>()
.add_system(
.add_systems(
Update,
update_chunk_material_singleton
.run_if(resource_changed::<VoxelMaterialRegistry>())
.in_set(ChunkMaterialSet)
.in_base_set(CoreSet::Update),
.in_set(ChunkMaterialSet),
);
}
}
3 changes: 1 addition & 2 deletions src/voxel/terraingen/biomes/plains.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::voxel::{
material::VoxelMaterial,
materials::{Dirt, Grass, Leaves, Wood, Rock},
materials::{Dirt, Grass, Leaves, Rock, Wood},
storage::VoxelBuffer,
terraingen::{
common::{make_rock, make_tree},
Expand Down Expand Up @@ -47,7 +47,6 @@ impl LayeredBiomeTerrainGenerator for BasicPlainsBiomeTerrainGenerator {
}
}


// Let's put some rock boulders in the plains to populate a lil bit
let rock_spawn_chance = noise::rand2to1(
(pos.xz().as_vec2() + key.xz().as_vec2()) * 0.1,
Expand Down
11 changes: 6 additions & 5 deletions src/voxel/world/chunks.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy::{
math::IVec3,
prelude::{
Changed, Commands, CoreSet, Entity, GlobalTransform, IntoSystemConfig, IntoSystemConfigs,
IntoSystemSetConfig, Plugin, Query, Res, ResMut, Resource, SystemSet, With,
Changed, Commands, Entity, GlobalTransform, IntoSystemConfigs, Last, Plugin, PostUpdate,
Query, Res, ResMut, Resource, SystemSet, Update, With,
},
utils::{HashMap, HashSet},
};
Expand Down Expand Up @@ -211,13 +211,14 @@ impl Plugin for VoxelWorldChunkingPlugin {
})
.init_resource::<ChunkCommandQueue>()
.init_resource::<DirtyChunks>()
.configure_set(ChunkLoadingSet.in_base_set(CoreSet::Update))
.configure_set(Update, ChunkLoadingSet)
.add_systems(
Update,
(update_player_pos, update_view_chunks, create_chunks)
.chain()
.in_set(ChunkLoadingSet),
)
.add_system(destroy_chunks.in_base_set(CoreSet::PostUpdate))
.add_system(clear_dirty_chunks.in_base_set(CoreSet::Last));
.add_systems(PostUpdate, destroy_chunks)
.add_systems(Last, clear_dirty_chunks);
}
}
10 changes: 5 additions & 5 deletions src/voxel/world/chunks_anim.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{
prelude::{
Commands, Component, CoreSet, Entity, IntoSystemConfigs, IntoSystemSetConfig, Plugin,
Query, RemovedComponents, Res, SystemSet, Transform, Visibility,
Commands, Component, Entity, IntoSystemConfigs, IntoSystemSetConfig, Plugin, PostUpdate,
Query, RemovedComponents, Res, SystemSet, Transform, Update, Visibility,
},
time::Time,
};
Expand Down Expand Up @@ -67,11 +67,11 @@ pub struct ChunkAppearanceAnimatorSet;
impl Plugin for ChunkAppearanceAnimatorPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_set(
ChunkAppearanceAnimatorSet
.after(ChunkMeshingSet)
.before(CoreSet::UpdateFlush),
PostUpdate,
ChunkAppearanceAnimatorSet.after(ChunkMeshingSet),
)
.add_systems(
Update,
(step_chunk_animation, attach_chunk_animation).in_set(ChunkAppearanceAnimatorSet),
);
}
Expand Down
7 changes: 3 additions & 4 deletions src/voxel/world/meshing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ pub struct VoxelWorldMeshingPlugin;
impl Plugin for VoxelWorldMeshingPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_set(
ChunkMeshingSet
.in_base_set(CoreSet::Update)
.after(TerrainGenSet)
.after(ChunkLoadingSet),
Update,
ChunkMeshingSet.after(TerrainGenSet).after(ChunkLoadingSet),
)
.add_systems(
Update,
(prepare_chunks, queue_mesh_tasks, process_mesh_tasks)
.chain()
.in_set(ChunkMeshingSet),
Expand Down
22 changes: 11 additions & 11 deletions src/voxel/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ pub struct VoxelWorldPlugin;
impl Plugin for VoxelWorldPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.insert_resource(ChunkMap::<Voxel, ChunkShape>::new(ChunkShape {}))
.add_plugin(chunks::VoxelWorldChunkingPlugin)
.add_plugin(meshing::VoxelWorldMeshingPlugin)
.add_plugins(chunks::VoxelWorldChunkingPlugin)
.add_plugins(meshing::VoxelWorldMeshingPlugin)
// ordering of plugin insertion matters here.
.add_plugin(terraingen::TerrainGeneratorPlugin)
.add_plugin(terrain::VoxelWorldTerrainGenPlugin)
.add_plugin(super::material::VoxelMaterialPlugin)
.add_plugin(super::render::ChunkMaterialPlugin)
.add_plugin(materials::VoxelWorldBaseMaterialsPlugin)
.add_plugin(chunks_anim::ChunkAppearanceAnimatorPlugin)
.add_plugin(bevy_atmosphere::plugin::AtmospherePlugin)
.add_plugin(player::VoxelWorldPlayerControllerPlugin)
.add_plugin(sky::InteractiveSkyboxPlugin);
.add_plugins(terraingen::TerrainGeneratorPlugin)
.add_plugins(terrain::VoxelWorldTerrainGenPlugin)
.add_plugins(super::material::VoxelMaterialPlugin)
.add_plugins(super::render::ChunkMaterialPlugin)
.add_plugins(materials::VoxelWorldBaseMaterialsPlugin)
.add_plugins(chunks_anim::ChunkAppearanceAnimatorPlugin)
.add_plugins(bevy_atmosphere::plugin::AtmospherePlugin)
.add_plugins(player::VoxelWorldPlayerControllerPlugin)
.add_plugins(sky::InteractiveSkyboxPlugin);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/voxel/world/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ pub fn handle_player_input(
direction.y += 1.0;
}

if keys.pressed(KeyCode::LShift) {
if keys.pressed(KeyCode::ShiftLeft) {
direction.y -= 1.0;
}

if keys.pressed(KeyCode::LControl) {
if keys.pressed(KeyCode::ControlLeft) {
acceleration *= 8.0;
}

Expand All @@ -125,9 +125,9 @@ pub struct VoxelWorldPlayerControllerPlugin;
impl Plugin for VoxelWorldPlayerControllerPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
(handle_player_input, handle_player_mouse_move)
.chain()
.in_base_set(CoreSet::Update)
.after(DebugUISet::Display),
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/voxel/world/sky.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::{
Color, Commands, Deref, DirectionalLight, DirectionalLightBundle, Entity, ParamSet, Plugin,
Query, Res, Resource, Transform, Vec3, With,
Query, Res, Resource, Startup, Transform, Update, Vec3, With,
};

use super::player::PlayerController;
Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct InteractiveSkyboxPlugin;

impl Plugin for InteractiveSkyboxPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_startup_system(setup_sky_lighting)
.add_system(update_light_position);
app.add_systems(Startup, setup_sky_lighting)
.add_systems(Update, update_light_position);
}
}
21 changes: 9 additions & 12 deletions src/voxel/world/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::voxel::{
};
use bevy::{
prelude::{
Added, Commands, Component, CoreSet, Entity, IntoSystemConfigs, IntoSystemSetConfig,
Plugin, Query, ResMut, SystemSet,
Added, Commands, Component, Entity, IntoSystemConfigs, IntoSystemSetConfig, Plugin, Query,
ResMut, SystemSet, Update,
},
tasks::{AsyncComputeTaskPool, Task},
};
Expand Down Expand Up @@ -67,16 +67,13 @@ pub struct TerrainGenSet;

impl Plugin for VoxelWorldTerrainGenPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_set(
TerrainGenSet
.in_base_set(CoreSet::Update)
.after(ChunkLoadingSet),
)
.add_systems(
(queue_terrain_gen, process_terrain_gen)
.chain()
.in_set(TerrainGenSet),
);
app.configure_set(Update, TerrainGenSet.after(ChunkLoadingSet))
.add_systems(
Update,
(queue_terrain_gen, process_terrain_gen)
.chain()
.in_set(TerrainGenSet),
);
}
}

Expand Down