Skip to content

Commit

Permalink
Merge pull request #29 from rparrett/bevy-0.12
Browse files Browse the repository at this point in the history
Upgrade to Bevy 0.12
  • Loading branch information
Game4all authored Nov 15, 2023
2 parents 75d314d + 20e0ecb commit 4799fd0
Show file tree
Hide file tree
Showing 14 changed files with 909 additions and 711 deletions.
1,510 changes: 848 additions & 662 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.11.3"
bevy = "0.12"
ndshape = "0.3.0"
block-mesh = "0.2.0"
ndcopy = "0.3.0"
thread_local = "1.1.7"
bevy_egui = "0.21"
bevy_egui = "0.23"
float-ord = "0.3.2"
futures-lite = "1.12.0"
once_cell = "1.17.1"
bevy_atmosphere = "0.7"
bevy_atmosphere = "0.8"
bitflags = "2.0.2"
ilattice = { version = "0.3.0", features = ["glam", "morton-encoding"] }
noise = "0.8.2"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ This was ported to bevy 0.10.0 thanks to the efforts of [@naomijub](https://gith

Ported to bevy 0.11.3 thanks to [@juliohq](https://github.com/juliohq).

Ported to bevy 0.12.0 thanks to [@rparrett](https://github.com/rparrett).

This uses the awesome [block-mesh](https://github.com/bonsairobo/block-mesh-rs) crate which handles greedy meshing.
4 changes: 2 additions & 2 deletions assets/shaders/fog.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const FOG_COLOR: vec4<f32> = vec4<f32>(0.4, 0.4, 0.4, 1.0);
fn ffog_calc_factor(clamped_d: f32, fog_distance: f32, chunk_size: f32) -> f32 {
let fog_max: f32 = fog_distance;
let fog_min: f32 = fog_max - 4.5 * chunk_size;
let clamped_d = clamp(fog_min, clamped_d, fog_max);
return 1.0 - (fog_max - clamped_d) / (fog_max - fog_min);
let new_clamped_d = clamp(fog_min, clamped_d, fog_max);
return 1.0 - (fog_max - new_clamped_d) / (fog_max - fog_min);
}

fn ffog_apply_fog(d: f32, fog_min: f32, chunk_size: f32, color: vec4<f32>) -> vec4<f32> {
Expand Down
6 changes: 3 additions & 3 deletions assets/shaders/noise.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See https://github.com/veloren/veloren

fn hash(p: vec4<f32>) -> f32 {
var p: vec4<f32> = fract(p * 0.3183099 + 0.1) - fract(p + 23.22121);
p = p * 17.0;
return (fract(p.x * p.y * (1.0 - p.z) * p.w * (p.x + p.y + p.z + p.w)) - 0.5) * 2.0;
var n: vec4<f32> = fract(p * 0.3183099 + 0.1) - fract(p + 23.22121);
n = n * 17.0;
return (fract(n.x * n.y * (1.0 - n.z) * n.w * (n.x + n.y + n.z + n.w)) - 0.5) * 2.0;
}
64 changes: 37 additions & 27 deletions assets/shaders/terrain_pipeline.wgsl
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::pbr_bindings
#import bevy_pbr::mesh_bindings
#import bevy_pbr::mesh_functions

#import bevy_pbr::utils
#import bevy_pbr::clustered_forward
#import bevy_pbr::lighting
#import bevy_pbr::shadows
#import bevy_pbr::fog
#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" 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
#import bevy_pbr::{
clustered_forward,
fog,
lighting,
mesh_bindings::mesh,
mesh_view_bindings::view,
mesh_functions,
pbr_bindings,
pbr_functions::{calculate_view, apply_pbr_lighting},
pbr_types::{PbrInput, pbr_input_new},
shadows,
utils,
view_transformations
}
#import bevy_core_pipeline::tonemapping::tone_mapping

#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 {
@builtin(instance_index) instance_index: u32,
@location(0) position: vec3<f32>,
@location(1) voxel_data: u32,
};
Expand All @@ -28,17 +30,21 @@ struct VertexOutput {
@location(0) voxel_normal: vec3<f32>,
@location(1) voxel_data: u32,
@location(2) world_position: vec3<f32>,
@location(3) instance_index: u32,
};

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

var out: VertexOutput;
out.clip_position = bevy_pbr::mesh_functions::mesh_position_world_to_clip(world_position);
out.voxel_normal = voxel_data_extract_normal(vertex.voxel_data);
let voxel_normal = voxel_data_extract_normal(vertex.voxel_data);
out.clip_position = view_transformations::position_world_to_clip(world_position.xyz);
out.voxel_normal = voxel_normal;
out.voxel_data = vertex.voxel_data;
out.world_position = world_position.xyz;
out.instance_index = vertex.instance_index;

return out;
}
Expand All @@ -52,12 +58,15 @@ struct Fragment {
@location(1) voxel_data: u32,
/// The world position of the voxel vertex.
@location(2) world_position: vec3<f32>,
@location(3) instance_index: u32,
};

fn prepare_pbr_input_from_voxel_mat(voxel_mat: VoxelMat, frag: Fragment) -> PbrInput {
var base_color: vec4<f32> = voxel_mat.base_color;
base_color = base_color + hash(vec4<f32>(floor(frag.world_position - frag.voxel_normal * 0.5), 1.0)) * 0.0226;

let voxel_world_normal = bevy_pbr::mesh_functions::mesh_normal_local_to_world(frag.voxel_normal, frag.instance_index);

var pbr_input: PbrInput = pbr_input_new();
pbr_input.material.metallic = voxel_mat.metallic;
pbr_input.material.perceptual_roughness = voxel_mat.perceptual_roughness;
Expand All @@ -67,12 +76,13 @@ 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) * bevy_pbr::mesh_functions::mesh_normal_local_to_world(frag.voxel_normal);
pbr_input.world_normal = (f32(frag.front_facing) * 2.0 - 1.0) * voxel_world_normal;

pbr_input.is_orthographic = view.projection[3].w == 1.0;
pbr_input.N = normalize(bevy_pbr::mesh_functions::mesh_normal_local_to_world(frag.voxel_normal));
pbr_input.N = normalize(voxel_world_normal);
pbr_input.V = calculate_view(vec4<f32>(frag.world_position, 1.0), pbr_input.is_orthographic);
pbr_input.flags = mesh.flags;
pbr_input.flags = mesh[frag.instance_index].flags;

return pbr_input;
}

Expand All @@ -82,7 +92,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), view.color_grading);
let pbr_colour = tone_mapping(apply_pbr_lighting(pbr_input), view.color_grading);

// @todo: switch to bevy_pbr::fog

Expand Down
2 changes: 1 addition & 1 deletion src/debug/debug_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn toggle_debug_ui_displays(
mut inputs: EventReader<KeyboardInput>,
mut ui_state: ResMut<DebugUIState>,
) {
for input in inputs.iter() {
for input in inputs.read() {
match input.key_code {
Some(key_code) if key_code == KeyCode::F3 && input.state == ButtonState::Pressed => {
ui_state.display_debug_info = !ui_state.display_debug_info;
Expand Down
3 changes: 2 additions & 1 deletion src/voxel/material.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::{
prelude::{info, Color, Plugin, Resource},
log::info,
prelude::{Color, Plugin, Resource},
utils::HashMap,
};
use bitflags::bitflags;
Expand Down
7 changes: 3 additions & 4 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::{TypePath, TypeUuid},
reflect::TypePath,
render::{
extract_component::ExtractComponent,
mesh::MeshVertexAttribute,
Expand All @@ -18,7 +18,7 @@ impl VoxelTerrainMesh {
MeshVertexAttribute::new("Vertex_Data", 0x696969, VertexFormat::Uint32);
}

#[derive(ShaderType, Clone, Copy, Default)]
#[derive(ShaderType, Clone, Copy, Debug, Default)]
pub struct GpuVoxelMaterial {
base_color: Color,
flags: u32,
Expand All @@ -28,8 +28,7 @@ pub struct GpuVoxelMaterial {
reflectance: f32,
}

#[derive(AsBindGroup, ShaderType, Clone, TypePath, TypeUuid)]
#[uuid = "1e31e29e-73d8-419c-8293-876ae81d2636"]
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct GpuTerrainUniforms {
#[uniform(0)]
pub render_distance: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/voxel/world/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl Plugin for VoxelWorldChunkingPlugin {
})
.init_resource::<ChunkCommandQueue>()
.init_resource::<DirtyChunks>()
.configure_set(Update, ChunkLoadingSet)
.configure_sets(Update, ChunkLoadingSet)
.add_systems(
Update,
(update_player_pos, update_view_chunks, create_chunks)
Expand Down
6 changes: 3 additions & 3 deletions src/voxel/world/chunks_anim.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{
prelude::{
Commands, Component, Entity, IntoSystemConfigs, IntoSystemSetConfig, Plugin, PostUpdate,
Commands, Component, Entity, IntoSystemConfigs, IntoSystemSetConfigs, Plugin, PostUpdate,
Query, RemovedComponents, Res, SystemSet, Transform, Update, Visibility,
},
time::Time,
Expand All @@ -25,7 +25,7 @@ fn attach_chunk_animation(
time: Res<Time>,
mut commands: Commands,
) {
removed_chunk_meshes.iter().for_each(|entity| {
removed_chunk_meshes.read().for_each(|entity| {
if ready_chunks.contains(entity) {
commands.entity(entity).insert(ChunkSpawnAnimation {
start_time: time.elapsed_seconds(),
Expand Down Expand Up @@ -66,7 +66,7 @@ pub struct ChunkAppearanceAnimatorSet;

impl Plugin for ChunkAppearanceAnimatorPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_set(
app.configure_sets(
PostUpdate,
ChunkAppearanceAnimatorSet.after(ChunkMeshingSet),
)
Expand Down
2 changes: 1 addition & 1 deletion src/voxel/world/meshing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub struct VoxelWorldMeshingPlugin;

impl Plugin for VoxelWorldMeshingPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_set(
app.configure_sets(
Update,
ChunkMeshingSet.after(TerrainGenSet).after(ChunkLoadingSet),
)
Expand Down
2 changes: 1 addition & 1 deletion src/voxel/world/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn handle_player_mouse_move(
let mut delta = Vec2::ZERO;

if controller.cursor_locked {
for mouse_move in mouse_motion_event_reader.iter() {
for mouse_move in mouse_motion_event_reader.read() {
delta += mouse_move.delta;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/voxel/world/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::voxel::{
};
use bevy::{
prelude::{
Added, Commands, Component, Entity, IntoSystemConfigs, IntoSystemSetConfig, Plugin, Query,
Added, Commands, Component, Entity, IntoSystemConfigs, IntoSystemSetConfigs, Plugin, Query,
ResMut, SystemSet, Update,
},
tasks::{AsyncComputeTaskPool, Task},
Expand Down Expand Up @@ -67,7 +67,7 @@ pub struct TerrainGenSet;

impl Plugin for VoxelWorldTerrainGenPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_set(Update, TerrainGenSet.after(ChunkLoadingSet))
app.configure_sets(Update, TerrainGenSet.after(ChunkLoadingSet))
.add_systems(
Update,
(queue_terrain_gen, process_terrain_gen)
Expand Down

0 comments on commit 4799fd0

Please sign in to comment.