Skip to content

Commit

Permalink
Upgrade to Bevy v0.12 (#775)
Browse files Browse the repository at this point in the history
Migrated according to https://bevyengine.org/learn/migration-guides/0-11-to-0-12/

Terrain shader was simplified thanks to the possibility to use extended
material (based on Bevy PBR).
  • Loading branch information
Indy2222 authored Feb 23, 2024
1 parent 536eb75 commit 279488b
Show file tree
Hide file tree
Showing 77 changed files with 600 additions and 532 deletions.
461 changes: 258 additions & 203 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ assert_cmd = "2.0.10"
async-compat = "0.2.1"
async-std = "1.11"
async-tar = "0.4.2"
bevy_kira_audio = { version = "0.16.0", features = ["mp3"] }
bevy_kira_audio = { version = "0.18", features = ["mp3"] }
bincode = "2.0.0-rc.3"
chrono = "0.4.24"
clap = { version = "4.0", features = ["derive"] }
Expand All @@ -135,7 +135,7 @@ futures-lite = "1.13.0"
glam = "0.24"
gltf = "1.0"
itertools = "0.11.0"
iyes_progress = "0.9.0"
iyes_progress = "0.10.0"
log = "0.4.17"
nalgebra = { version = "0.32.3", features = ["convert-glam024"] }
nix = "0.26.2"
Expand Down Expand Up @@ -164,7 +164,7 @@ url = { version = "2.3.1", features = ["serde"] }
urlencoding = "2.1.2"

[workspace.dependencies.bevy]
version = "0.11"
version = "0.12"
default-features = false
features = [
"animation",
Expand All @@ -185,7 +185,6 @@ features = [
"ktx2",
"zstd",
"x11",
"filesystem_watcher",
"bevy_gizmos",
"android_shared_stdcxx",
"tonemapping_luts",
Expand Down
12 changes: 9 additions & 3 deletions assets/shaders/bar.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_pbr::mesh_bindings mesh
#import bevy_pbr::mesh_functions mesh_position_local_to_clip
#import bevy_pbr::{
mesh_bindings::mesh,
mesh_functions::{get_model_matrix, mesh_position_local_to_clip},
}

const BACKGROUND_COLOR = vec4<f32>(0., 0., 0., 0.75);
const FOREGROUND_COLOR = vec4<f32>(0.6, 1., 0.6, 0.75);
Expand All @@ -8,6 +10,7 @@ const FOREGROUND_COLOR = vec4<f32>(0.6, 1., 0.6, 0.75);
var<uniform> value: f32;

struct Vertex {
@builtin(instance_index) instance_index: u32,
@location(0) position: vec2<f32>,
@location(1) uv: vec2<f32>,
};
Expand All @@ -25,7 +28,10 @@ struct FragmentInput {
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;

out.clip_position = mesh_position_local_to_clip(mesh.model, vec4<f32>(0., 0., 0., 1.0));
out.clip_position = mesh_position_local_to_clip(
get_model_matrix(vertex.instance_index),
vec4<f32>(0., 0., 0., 1.0),
);

let scale = max(1., out.clip_position.w / 40.);
out.clip_position += vec4<f32>(scale * vertex.position, 0., 0.);
Expand Down
13 changes: 8 additions & 5 deletions assets/shaders/rally_point.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#import bevy_pbr::mesh_view_bindings globals
#import bevy_pbr::mesh_bindings mesh
#import bevy_pbr::mesh_vertex_output MeshVertexOutput
#import bevy_pbr::{
forward_io::VertexOutput,
mesh_bindings::mesh,
mesh_view_bindings::globals,
}

struct CustomMaterial {
color: vec4<f32>,
Expand All @@ -23,9 +25,10 @@ const FADE: f32 = 3.;

@fragment
fn fragment(
in: MeshVertexOutput,
in: VertexOutput,
) -> @location(0) vec4<f32> {
let world_space_length: f32 = length(mesh.model[0].xyz);
let model = mesh[in.instance_index].model;
let world_space_length: f32 = length(vec3(model[0][0], model[1][0], model[2][0]));
let scaled_x: f32 = in.uv.x * world_space_length;
let offset_y: f32 = abs(in.uv.y - 0.5) * POINTINESS;
let scaled_time: f32 = globals.time * SPEED;
Expand Down
120 changes: 40 additions & 80 deletions assets/shaders/terrain.wgsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#import bevy_pbr::mesh_vertex_output MeshVertexOutput
#import bevy_pbr::mesh_bindings mesh
#import bevy_pbr::mesh_view_bindings view
#import bevy_core_pipeline::tonemapping tone_mapping
#import bevy_pbr::pbr_functions PbrInput, pbr_input_new, prepare_world_normal, apply_normal_mapping, calculate_view, pbr
#import bevy_pbr::pbr_types as pbr_types

#import bevy_pbr::{
forward_io::{VertexOutput, FragmentOutput},
pbr_fragment::pbr_input_from_standard_material,
pbr_functions::{
alpha_discard,
apply_pbr_lighting,
main_pass_post_lighting_processing,
},
}

// How large (in meters) is a texture.
const TEXTURE_SIZE = 16.;
const SHAPE_COLOR = vec4<f32>(1., 1., 1., 0.75);
const SHAPE_THICKNESS = 0.15;
// Keep these array lengths in sync with /crates/terrain/src/shader.rs.
Expand All @@ -34,15 +34,12 @@ struct Rectangles {
count: u32,
};

@group(1) @binding(0)
@group(1) @binding(100)
var<uniform> uv_scale: f32;
@group(1) @binding(101)
var<uniform> circles: KdTree;
@group(1) @binding(1)
@group(1) @binding(102)
var<uniform> rectangles: Rectangles;
@group(1) @binding(2)
var terrain_texture: texture_2d<f32>;
@group(1) @binding(3)
var terrain_sampler: sampler;

fn mix_colors(base: vec4<f32>, cover: vec4<f32>) -> vec4<f32> {
let alpha = base.a * cover.a;
let rgb = base.rgb * cover.a + cover.rgb * (1. - cover.a);
Expand All @@ -51,11 +48,11 @@ fn mix_colors(base: vec4<f32>, cover: vec4<f32>) -> vec4<f32> {

fn draw_circle(
base: vec4<f32>,
uv: vec2<f32>,
location: vec2<f32>,
center: vec2<f32>,
radius: f32,
) -> vec4<f32> {
let distance: f32 = distance(uv, center);
let distance: f32 = distance(location, center);
if distance <= (radius + SHAPE_THICKNESS) && radius <= distance {
return mix_colors(base, SHAPE_COLOR);
}
Expand All @@ -73,14 +70,14 @@ struct Next {
potential: f32,
}

fn nearest(uv: vec2<f32>) -> u32 {
fn nearest(location: vec2<f32>) -> u32 {
if circles.count == 0u {
return MAX_KD_TREE_SIZE;
}

var best: KdRecord;
best.index = 0u;
best.distance = distance(circles.nodes[0].location, uv);
best.distance = distance(circles.nodes[0].location, location);

var stack_size: u32 = 1u;
// Make sure that the stack size is large enought to cover balanced three
Expand All @@ -100,14 +97,14 @@ fn nearest(uv: vec2<f32>) -> u32 {

let node = circles.nodes[next.index];

let distance = distance(node.location, uv);
let distance = distance(node.location, location);
if distance < best.distance {
best.index = next.index;
best.distance = distance;
}

let axis = next.depth % 2u;
let diff = uv[axis] - node.location[axis];
let diff = location[axis] - node.location[axis];

var close = 2u * next.index + 2u;
var away = 2u * next.index + 1u;
Expand Down Expand Up @@ -135,25 +132,25 @@ fn nearest(uv: vec2<f32>) -> u32 {
return best.index;
}

fn draw_circles(base: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
fn draw_circles(base: vec4<f32>, location: vec2<f32>) -> vec4<f32> {
var output_color = base;

let index = nearest(uv);
let index = nearest(location);
if index < MAX_KD_TREE_SIZE {
let node = circles.nodes[index];
let center = node.location;
let radius = node.radius;
output_color = draw_circle(output_color, uv, center, radius);
output_color = draw_circle(output_color, location, center, radius);
}

return output_color;
}

fn draw_rectangles(base: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {
fn draw_rectangles(base: vec4<f32>, location: vec2<f32>) -> vec4<f32> {
for (var i = 0u; i < rectangles.count; i++) {
let rectangle = rectangles.items[i];
let local_uv = (rectangle.inverse_transform * vec3(uv, 1.0)).xy;
if all(abs(local_uv) <= rectangle.half_size + SHAPE_THICKNESS) && any(rectangle.half_size <= abs(local_uv)) {
let local_location = (rectangle.inverse_transform * vec3(location, 1.0)).xy;
if all(abs(local_location) <= rectangle.half_size + SHAPE_THICKNESS) && any(rectangle.half_size <= abs(local_location)) {
return mix_colors(base, SHAPE_COLOR);
}
}
Expand All @@ -163,57 +160,20 @@ fn draw_rectangles(base: vec4<f32>, uv: vec2<f32>) -> vec4<f32> {

@fragment
fn fragment(
in: MeshVertexOutput,
in: VertexOutput,
@builtin(front_facing) is_front: bool,
) -> @location(0) vec4<f32> {
var pbr_input: PbrInput = pbr_input_new();
pbr_input.material.perceptual_roughness = 0.8;
pbr_input.material.metallic = 0.23;
pbr_input.material.reflectance = 0.06;

pbr_input.material.base_color = textureSample(
terrain_texture,
terrain_sampler,
in.uv / TEXTURE_SIZE
);

#ifdef VERTEX_COLORS
pbr_input.material.base_color = pbr_input.material.base_color * in.color;
#endif

pbr_input.frag_coord = in.position;
pbr_input.world_position = in.world_position;
pbr_input.world_normal = prepare_world_normal(
in.world_normal,
(pbr_input.material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
is_front,
);

pbr_input.is_orthographic = view.projection[3].w == 1.0;

pbr_input.N = apply_normal_mapping(
pbr_input.material.flags,
pbr_input.world_normal,
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
in.world_tangent,
#endif
#endif
#ifdef VERTEX_UVS
in.uv,
#endif
view.mip_bias,
);
pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic);
pbr_input.flags = mesh.flags;

var output_color = pbr(pbr_input);

#ifdef TONEMAP_IN_SHADER
output_color = tone_mapping(output_color, view.color_grading);
#endif

output_color = draw_circles(output_color, in.uv);
output_color = draw_rectangles(output_color, in.uv);
return output_color;
) -> FragmentOutput {
var pbr_input = pbr_input_from_standard_material(in, is_front);
pbr_input.material.base_color = alpha_discard(pbr_input.material, pbr_input.material.base_color);

var out: FragmentOutput;
out.color = apply_pbr_lighting(pbr_input);

let location = uv_scale * in.uv;
out.color = draw_circles(out.color, location);
out.color = draw_rectangles(out.color, location);

out.color = main_pass_post_lighting_processing(pbr_input, out.color);

return out;
}
8 changes: 5 additions & 3 deletions assets/shaders/trail.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_pbr::mesh_view_bindings globals
#import bevy_pbr::mesh_vertex_output MeshVertexOutput
#import bevy_pbr::{
forward_io::VertexOutput,
mesh_view_bindings::globals,
}

const COLOR = vec4<f32>(1., 0.85, 0.1, 0.7);

Expand All @@ -8,7 +10,7 @@ var<uniform> start_time: f32;

@fragment
fn fragment(
in: MeshVertexOutput,
in: VertexOutput,
) -> @location(0) vec4<f32> {
var color = COLOR;
// Use max(0., ...) because the times are wrapping.
Expand Down
4 changes: 2 additions & 2 deletions crates/audio/src/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ fn setup(mut commands: Commands, server: Res<AssetServer>) {

fn load(server: Res<AssetServer>, tracks: Res<Tracks>) -> Progress {
match server.get_load_state(&tracks.0) {
LoadState::Loaded => true.into(),
LoadState::NotLoaded | LoadState::Loading => false.into(),
Some(LoadState::Loaded) => true.into(),
Some(LoadState::NotLoaded) | Some(LoadState::Loading) => false.into(),
_ => panic!("Unexpected loading state."),
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/audio/src/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ fn load(server: Res<AssetServer>, sounds: Res<Sounds>) -> Progress {
.0
.values()
.map(|handle| match server.get_load_state(handle) {
LoadState::Loaded => 1,
LoadState::NotLoaded | LoadState::Loading => 0,
Some(LoadState::Loaded) => 1,
Some(LoadState::NotLoaded) | Some(LoadState::Loading) => 0,
_ => panic!("Unexpected loading state."),
})
.sum(),
Expand Down Expand Up @@ -146,7 +146,7 @@ fn play(
let camera = camera.single();
let sound_volume = config.audio().sound_volume() as f64;

for PlaySpatialAudioEvent { sound, position } in &mut play_events {
for PlaySpatialAudioEvent { sound, position } in play_events.read() {
let (volume, pan) = calculate_volume_and_pan(camera, &focus, *position);
let handle = audio
.play(sounds.0[*sound].clone())
Expand Down
2 changes: 1 addition & 1 deletion crates/behaviour/src/chase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ChaseTarget {
}

fn handle_chase_events(mut commands: Commands, mut events: EventReader<ChaseTargetEvent>) {
for event in events.iter() {
for event in events.read() {
let mut entity_commands = commands.entity(event.entity());
match event.target() {
Some(target) => entity_commands.insert(ChaseTargetComponent::new(target.clone())),
Expand Down
11 changes: 5 additions & 6 deletions crates/camera/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::f32::consts::FRAC_PI_2;

use bevy::{
ecs::query::Has,
pbr::{CascadeShadowConfig, CascadeShadowConfigBuilder, DirectionalLightShadowMap},
prelude::*,
};
Expand Down Expand Up @@ -391,7 +390,7 @@ fn process_move_focus_events(
terrain: TerrainCollider,
mut out_events: EventWriter<UpdateTranslationEvent>,
) {
let event = match in_events.iter().last() {
let event = match in_events.read().last() {
Some(event) => event,
None => return,
};
Expand Down Expand Up @@ -516,7 +515,7 @@ fn handle_horizontal_events(
mut movement: ResMut<HorizontalMovement>,
mut events: EventReader<MoveCameraHorizontallyEvent>,
) {
if let Some(event) = events.iter().last() {
if let Some(event) = events.read().last() {
movement.set(event.direction());
}
}
Expand All @@ -526,7 +525,7 @@ fn handle_zoom_events(
mut events: EventReader<ZoomCameraEvent>,
mut desired: ResMut<DesiredDistance>,
) {
for event in events.iter() {
for event in events.read() {
desired.zoom_clamped(conf.camera(), event.factor());
}
}
Expand All @@ -535,7 +534,7 @@ fn handle_tilt_events(
mut events: EventReader<TiltCameraEvent>,
mut desired: ResMut<DesiredOffNadir>,
) {
for event in events.iter() {
for event in events.read() {
desired.tilt_clamped(Radian::ONE * event.delta());
}
}
Expand All @@ -544,7 +543,7 @@ fn handle_rotate_events(
mut events: EventReader<RotateCameraEvent>,
mut desired: ResMut<DesiredAzimuth>,
) {
for event in events.iter() {
for event in events.read() {
desired.rotate(Radian::ONE * event.delta());
}
}
Loading

0 comments on commit 279488b

Please sign in to comment.