mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
ced216f59a
# Objective - Update winit dependency to 0.29 ## Changelog ### KeyCode changes - Removed `ScanCode`, as it was [replaced by KeyCode](https://github.com/rust-windowing/winit/blob/master/CHANGELOG.md#0292). - `ReceivedCharacter.char` is now a `SmolStr`, [relevant doc](https://docs.rs/winit/latest/winit/event/struct.KeyEvent.html#structfield.text). - Changed most `KeyCode` values, and added more. KeyCode has changed meaning. With this PR, it refers to physical position on keyboard rather than the printed letter on keyboard keys. In practice this means: - On QWERTY keyboard layouts, nothing changes - On any other keyboard layout, `KeyCode` no longer reflects the label on key. - This is "good". In bevy 0.12, when you used WASD for movement, users with non-QWERTY keyboards couldn't play your game! This was especially bad for non-latin keyboards. Now, WASD represents the physical keys. A French player will press the ZQSD keys, which are near each other, Kyrgyz players will use "Цфыв". - This is "bad" as well. You can't know in advance what the label of the key for input is. Your UI says "press WASD to move", even if in reality, they should be pressing "ZQSD" or "Цфыв". You also no longer can use `KeyCode` for text inputs. In any case, it was a pretty bad API for text input. You should use `ReceivedCharacter` now instead. ### Other changes - Use `web-time` rather than `instant` crate. (https://github.com/rust-windowing/winit/pull/2836) - winit did split `run_return` in `run_onDemand` and `pump_events`, I did the same change in bevy_winit and used `pump_events`. - Removed `return_from_run` from `WinitSettings` as `winit::run` now returns on supported platforms. - I left the example "return_after_run" as I think it's still useful. - This winit change is done partly to allow to create a new window after quitting all windows: https://github.com/emilk/egui/issues/1918 ; this PR doesn't address. - added `width` and `height` properties in the `canvas` from wasm example (https://github.com/bevyengine/bevy/pull/10702#discussion_r1420567168) ## Known regressions (important follow ups?) - Provide an API for reacting when a specific key from current layout was released. - possible solutions: use winit::Key from winit::KeyEvent ; mapping between KeyCode and Key ; or . - We don't receive characters through alt+numpad (e.g. alt + 151 = "ù") anymore ; reproduced on winit example "ime". maybe related to https://github.com/rust-windowing/winit/issues/2945 - (windows) Window content doesn't refresh at all when resizing. By reading https://github.com/rust-windowing/winit/issues/2900 ; I suspect we should just fire a `window.request_redraw();` from `AboutToWait`, and handle actual redrawing within `RedrawRequested`. I'm not sure how to move all that code so I'd appreciate it to be a follow up. - (windows) unreleased winit fix for using set_control_flow in AboutToWait https://github.com/rust-windowing/winit/issues/3215 ; ⚠️ I'm not sure what the implications are, but that feels bad 🤔 ## Follow up I'd like to avoid bloating this PR, here are a few follow up tasks worthy of a separate PR, or new issue to track them once this PR is closed, as they would either complicate reviews, or at risk of being controversial: - remove CanvasParentResizePlugin (https://github.com/bevyengine/bevy/pull/10702#discussion_r1417068856) - avoid mentionning explicitly winit in docs from bevy_window ? - NamedKey integration on bevy_input: https://github.com/rust-windowing/winit/pull/3143 introduced a new NamedKey variant. I implemented it only on the converters but we'd benefit making the same changes to bevy_input. - Add more info in KeyboardInput https://github.com/bevyengine/bevy/pull/10702#pullrequestreview-1748336313 - https://github.com/bevyengine/bevy/pull/9905 added a workaround on a bug allegedly fixed by winit 0.29. We should check if it's still necessary. - update to raw_window_handle 0.6 - blocked by wgpu - Rename `KeyCode` to `PhysicalKeyCode` https://github.com/bevyengine/bevy/pull/10702#discussion_r1404595015 - remove `instant` dependency, [replaced by](https://github.com/rust-windowing/winit/pull/2836) `web_time`), we'd need to update to : - fastrand >= 2.0 - [`async-executor`](https://github.com/smol-rs/async-executor) >= 1.7 - [`futures-lite`](https://github.com/smol-rs/futures-lite) >= 2.0 - Verify license, see [discussion](https://github.com/bevyengine/bevy/pull/8745#discussion_r1402439800) - we might be missing a short notice or description of changes made - Consider using https://github.com/rust-windowing/cursor-icon directly rather than vendoring it in bevy. - investigate [this unwrap](https://github.com/bevyengine/bevy/pull/8745#discussion_r1387044986) (`winit_window.canvas().unwrap();`) - Use more good things about winit's update - https://github.com/bevyengine/bevy/pull/10689#issuecomment-1823560428 ## Migration Guide This PR should have one.
426 lines
13 KiB
Rust
426 lines
13 KiB
Rust
//! This example compares Forward, Forward + Prepass, and Deferred rendering.
|
|
|
|
use std::f32::consts::*;
|
|
|
|
use bevy::{
|
|
core_pipeline::{
|
|
fxaa::Fxaa,
|
|
prepass::{DeferredPrepass, DepthPrepass, MotionVectorPrepass, NormalPrepass},
|
|
},
|
|
pbr::NotShadowReceiver,
|
|
pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap},
|
|
pbr::{DefaultOpaqueRendererMethod, NotShadowCaster, OpaqueRendererMethod},
|
|
prelude::*,
|
|
render::render_resource::TextureFormat,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(Msaa::Off)
|
|
.insert_resource(DefaultOpaqueRendererMethod::deferred())
|
|
.insert_resource(AmbientLight {
|
|
color: Color::WHITE,
|
|
brightness: 1.0 / 5.0f32,
|
|
})
|
|
.insert_resource(DirectionalLightShadowMap { size: 4096 })
|
|
.add_plugins(DefaultPlugins)
|
|
.insert_resource(Normal(None))
|
|
.insert_resource(Pause(true))
|
|
.add_systems(Startup, (setup, setup_parallax))
|
|
.add_systems(
|
|
Update,
|
|
(animate_light_direction, switch_mode, spin, update_normal),
|
|
)
|
|
.run();
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
asset_server: Res<AssetServer>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
) {
|
|
commands.spawn((
|
|
Camera3dBundle {
|
|
camera: Camera {
|
|
// Deferred both supports both hdr: true and hdr: false
|
|
hdr: false,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(0.7, 0.7, 1.0)
|
|
.looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
|
|
..default()
|
|
},
|
|
FogSettings {
|
|
color: Color::rgba_u8(43, 44, 47, 255),
|
|
falloff: FogFalloff::Linear {
|
|
start: 1.0,
|
|
end: 8.0,
|
|
},
|
|
..default()
|
|
},
|
|
EnvironmentMapLight {
|
|
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
|
|
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
|
|
},
|
|
DepthPrepass,
|
|
MotionVectorPrepass,
|
|
DeferredPrepass,
|
|
Fxaa::default(),
|
|
));
|
|
|
|
commands.spawn(DirectionalLightBundle {
|
|
directional_light: DirectionalLight {
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
cascade_shadow_config: CascadeShadowConfigBuilder {
|
|
num_cascades: 3,
|
|
maximum_distance: 10.0,
|
|
..default()
|
|
}
|
|
.into(),
|
|
transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 0.0, -FRAC_PI_4)),
|
|
..default()
|
|
});
|
|
|
|
// FlightHelmet
|
|
let helmet_scene = asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0");
|
|
|
|
commands.spawn(SceneBundle {
|
|
scene: helmet_scene.clone(),
|
|
..default()
|
|
});
|
|
commands.spawn(SceneBundle {
|
|
scene: helmet_scene,
|
|
transform: Transform::from_xyz(-4.0, 0.0, -3.0),
|
|
..default()
|
|
});
|
|
|
|
let mut forward_mat: StandardMaterial = Color::rgb(0.1, 0.2, 0.1).into();
|
|
forward_mat.opaque_render_method = OpaqueRendererMethod::Forward;
|
|
let forward_mat_h = materials.add(forward_mat);
|
|
|
|
// Plane
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(shape::Plane::from_size(50.0).into()),
|
|
material: forward_mat_h.clone(),
|
|
..default()
|
|
});
|
|
|
|
let cube_h = meshes.add(Mesh::from(shape::Cube { size: 0.1 }));
|
|
let sphere_h = meshes.add(Mesh::from(shape::UVSphere {
|
|
radius: 0.125,
|
|
..default()
|
|
}));
|
|
|
|
// Cubes
|
|
commands.spawn(PbrBundle {
|
|
mesh: cube_h.clone(),
|
|
material: forward_mat_h.clone(),
|
|
transform: Transform::from_xyz(-0.3, 0.5, -0.2),
|
|
..default()
|
|
});
|
|
commands.spawn(PbrBundle {
|
|
mesh: cube_h,
|
|
material: forward_mat_h,
|
|
transform: Transform::from_xyz(0.2, 0.5, 0.2),
|
|
..default()
|
|
});
|
|
|
|
let sphere_color = Color::rgb(10.0, 4.0, 1.0);
|
|
let sphere_pos = Transform::from_xyz(0.4, 0.5, -0.8);
|
|
// Emissive sphere
|
|
let mut unlit_mat: StandardMaterial = sphere_color.into();
|
|
unlit_mat.unlit = true;
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: sphere_h.clone(),
|
|
material: materials.add(unlit_mat),
|
|
transform: sphere_pos,
|
|
..default()
|
|
},
|
|
NotShadowCaster,
|
|
));
|
|
// Light
|
|
commands.spawn(PointLightBundle {
|
|
point_light: PointLight {
|
|
intensity: 1.0,
|
|
radius: 0.125,
|
|
shadows_enabled: true,
|
|
color: sphere_color,
|
|
..default()
|
|
},
|
|
transform: sphere_pos,
|
|
..default()
|
|
});
|
|
|
|
// Spheres
|
|
for i in 0..6 {
|
|
let j = i % 3;
|
|
let s_val = if i < 3 { 0.0 } else { 0.2 };
|
|
let material = if j == 0 {
|
|
materials.add(StandardMaterial {
|
|
base_color: Color::rgb(s_val, s_val, 1.0),
|
|
perceptual_roughness: 0.089,
|
|
metallic: 0.0,
|
|
..default()
|
|
})
|
|
} else if j == 1 {
|
|
materials.add(StandardMaterial {
|
|
base_color: Color::rgb(s_val, 1.0, s_val),
|
|
perceptual_roughness: 0.089,
|
|
metallic: 0.0,
|
|
..default()
|
|
})
|
|
} else {
|
|
materials.add(StandardMaterial {
|
|
base_color: Color::rgb(1.0, s_val, s_val),
|
|
perceptual_roughness: 0.089,
|
|
metallic: 0.0,
|
|
..default()
|
|
})
|
|
};
|
|
commands.spawn(PbrBundle {
|
|
mesh: sphere_h.clone(),
|
|
material,
|
|
transform: Transform::from_xyz(
|
|
j as f32 * 0.25 + if i < 3 { -0.15 } else { 0.15 } - 0.4,
|
|
0.125,
|
|
-j as f32 * 0.25 + if i < 3 { -0.15 } else { 0.15 } + 0.4,
|
|
),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
// sky
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Box::default())),
|
|
material: materials.add(StandardMaterial {
|
|
base_color: Color::hex("888888").unwrap(),
|
|
unlit: true,
|
|
cull_mode: None,
|
|
..default()
|
|
}),
|
|
transform: Transform::from_scale(Vec3::splat(1_000_000.0)),
|
|
..default()
|
|
},
|
|
NotShadowCaster,
|
|
NotShadowReceiver,
|
|
));
|
|
|
|
// Example instructions
|
|
commands.spawn(
|
|
TextBundle::from_section(
|
|
"",
|
|
TextStyle {
|
|
font_size: 18.0,
|
|
..default()
|
|
},
|
|
)
|
|
.with_style(Style {
|
|
position_type: PositionType::Absolute,
|
|
top: Val::Px(10.0),
|
|
left: Val::Px(10.0),
|
|
..default()
|
|
}),
|
|
);
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
struct Pause(bool);
|
|
|
|
fn animate_light_direction(
|
|
time: Res<Time>,
|
|
mut query: Query<&mut Transform, With<DirectionalLight>>,
|
|
pause: Res<Pause>,
|
|
) {
|
|
if pause.0 {
|
|
return;
|
|
}
|
|
for mut transform in &mut query {
|
|
transform.rotate_y(time.delta_seconds() * PI / 5.0);
|
|
}
|
|
}
|
|
|
|
fn setup_parallax(
|
|
mut commands: Commands,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut normal: ResMut<Normal>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
// The normal map. Note that to generate it in the GIMP image editor, you should
|
|
// open the depth map, and do Filters → Generic → Normal Map
|
|
// You should enable the "flip X" checkbox.
|
|
let normal_handle = asset_server.load("textures/parallax_example/cube_normal.png");
|
|
normal.0 = Some(normal_handle);
|
|
|
|
let mut cube: Mesh = shape::Cube { size: 0.15 }.into();
|
|
|
|
// NOTE: for normal maps and depth maps to work, the mesh
|
|
// needs tangents generated.
|
|
cube.generate_tangents().unwrap();
|
|
|
|
let parallax_material = materials.add(StandardMaterial {
|
|
perceptual_roughness: 0.4,
|
|
base_color_texture: Some(asset_server.load("textures/parallax_example/cube_color.png")),
|
|
normal_map_texture: normal.0.clone(),
|
|
// The depth map is a greyscale texture where black is the highest level and
|
|
// white the lowest.
|
|
depth_map: Some(asset_server.load("textures/parallax_example/cube_depth.png")),
|
|
parallax_depth_scale: 0.09,
|
|
parallax_mapping_method: ParallaxMappingMethod::Relief { max_steps: 4 },
|
|
max_parallax_layer_count: 5.0f32.exp2(),
|
|
..default()
|
|
});
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: meshes.add(cube),
|
|
material: parallax_material,
|
|
transform: Transform::from_xyz(0.4, 0.2, -0.8),
|
|
..default()
|
|
},
|
|
Spin { speed: 0.3 },
|
|
));
|
|
}
|
|
|
|
/// Store handle of the normal to later modify its format in [`update_normal`].
|
|
#[derive(Resource)]
|
|
struct Normal(Option<Handle<Image>>);
|
|
|
|
// See `examples/3d/parallax_mapping.rs` example for reasoning
|
|
fn update_normal(
|
|
mut already_ran: Local<bool>,
|
|
mut images: ResMut<Assets<Image>>,
|
|
normal: Res<Normal>,
|
|
) {
|
|
if *already_ran {
|
|
return;
|
|
}
|
|
if let Some(normal) = normal.0.as_ref() {
|
|
if let Some(image) = images.get_mut(normal) {
|
|
image.texture_descriptor.format = TextureFormat::Rgba8Unorm;
|
|
*already_ran = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct Spin {
|
|
speed: f32,
|
|
}
|
|
|
|
fn spin(time: Res<Time>, mut query: Query<(&mut Transform, &Spin)>, pause: Res<Pause>) {
|
|
if pause.0 {
|
|
return;
|
|
}
|
|
for (mut transform, spin) in query.iter_mut() {
|
|
transform.rotate_local_y(spin.speed * time.delta_seconds());
|
|
transform.rotate_local_x(spin.speed * time.delta_seconds());
|
|
transform.rotate_local_z(-spin.speed * time.delta_seconds());
|
|
}
|
|
}
|
|
|
|
#[derive(Resource, Default)]
|
|
enum DefaultRenderMode {
|
|
#[default]
|
|
Deferred,
|
|
Forward,
|
|
ForwardPrepass,
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn switch_mode(
|
|
mut text: Query<&mut Text>,
|
|
mut commands: Commands,
|
|
keys: Res<ButtonInput<KeyCode>>,
|
|
mut default_opaque_renderer_method: ResMut<DefaultOpaqueRendererMethod>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
cameras: Query<Entity, With<Camera>>,
|
|
mut pause: ResMut<Pause>,
|
|
mut hide_ui: Local<bool>,
|
|
mut mode: Local<DefaultRenderMode>,
|
|
) {
|
|
let mut text = text.single_mut();
|
|
let text = &mut text.sections[0].value;
|
|
|
|
text.clear();
|
|
|
|
if keys.just_pressed(KeyCode::Space) {
|
|
pause.0 = !pause.0;
|
|
}
|
|
|
|
if keys.just_pressed(KeyCode::Digit1) {
|
|
*mode = DefaultRenderMode::Deferred;
|
|
default_opaque_renderer_method.set_to_deferred();
|
|
println!("DefaultOpaqueRendererMethod: Deferred");
|
|
for _ in materials.iter_mut() {}
|
|
for camera in &cameras {
|
|
commands.entity(camera).remove::<NormalPrepass>();
|
|
commands.entity(camera).insert(DepthPrepass);
|
|
commands.entity(camera).insert(MotionVectorPrepass);
|
|
commands.entity(camera).insert(DeferredPrepass);
|
|
}
|
|
}
|
|
if keys.just_pressed(KeyCode::Digit2) {
|
|
*mode = DefaultRenderMode::Forward;
|
|
default_opaque_renderer_method.set_to_forward();
|
|
println!("DefaultOpaqueRendererMethod: Forward");
|
|
for _ in materials.iter_mut() {}
|
|
for camera in &cameras {
|
|
commands.entity(camera).remove::<NormalPrepass>();
|
|
commands.entity(camera).remove::<DepthPrepass>();
|
|
commands.entity(camera).remove::<MotionVectorPrepass>();
|
|
commands.entity(camera).remove::<DeferredPrepass>();
|
|
}
|
|
}
|
|
if keys.just_pressed(KeyCode::Digit3) {
|
|
*mode = DefaultRenderMode::ForwardPrepass;
|
|
default_opaque_renderer_method.set_to_forward();
|
|
println!("DefaultOpaqueRendererMethod: Forward + Prepass");
|
|
for _ in materials.iter_mut() {}
|
|
for camera in &cameras {
|
|
commands.entity(camera).insert(NormalPrepass);
|
|
commands.entity(camera).insert(DepthPrepass);
|
|
commands.entity(camera).insert(MotionVectorPrepass);
|
|
commands.entity(camera).remove::<DeferredPrepass>();
|
|
}
|
|
}
|
|
|
|
if keys.just_pressed(KeyCode::KeyH) {
|
|
*hide_ui = !*hide_ui;
|
|
}
|
|
|
|
if !*hide_ui {
|
|
text.push_str("(H) Hide UI\n");
|
|
text.push_str("(Space) Play/Pause\n\n");
|
|
text.push_str("Rendering Method:\n");
|
|
|
|
text.push_str(&format!(
|
|
"(1) {} Deferred\n",
|
|
if let DefaultRenderMode::Deferred = *mode {
|
|
">"
|
|
} else {
|
|
""
|
|
}
|
|
));
|
|
text.push_str(&format!(
|
|
"(2) {} Forward\n",
|
|
if let DefaultRenderMode::Forward = *mode {
|
|
">"
|
|
} else {
|
|
""
|
|
}
|
|
));
|
|
text.push_str(&format!(
|
|
"(3) {} Forward + Prepass\n",
|
|
if let DefaultRenderMode::ForwardPrepass = *mode {
|
|
">"
|
|
} else {
|
|
""
|
|
}
|
|
));
|
|
}
|
|
}
|