mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
484721be80
# Objective Fixes #14883 ## Solution Pretty simple update to `EntityCommands` methods to consume `self` and return it rather than taking `&mut self`. The things probably worth noting: * I added `#[allow(clippy::should_implement_trait)]` to the `add` method because it causes a linting conflict with `std::ops::Add`. * `despawn` and `log_components` now return `Self`. I'm not sure if that's exactly the desired behavior so I'm happy to adjust if that seems wrong. ## Testing Tested with `cargo run -p ci`. I think that should be sufficient to call things good. ## Migration Guide The most likely migration needed is changing code from this: ``` let mut entity = commands.get_or_spawn(entity); if depth_prepass { entity.insert(DepthPrepass); } if normal_prepass { entity.insert(NormalPrepass); } if motion_vector_prepass { entity.insert(MotionVectorPrepass); } if deferred_prepass { entity.insert(DeferredPrepass); } ``` to this: ``` let mut entity = commands.get_or_spawn(entity); if depth_prepass { entity = entity.insert(DepthPrepass); } if normal_prepass { entity = entity.insert(NormalPrepass); } if motion_vector_prepass { entity = entity.insert(MotionVectorPrepass); } if deferred_prepass { entity.insert(DeferredPrepass); } ``` as can be seen in several of the example code updates here. There will probably also be instances where mutable `EntityCommands` vars no longer need to be mutable.
191 lines
5.9 KiB
Rust
191 lines
5.9 KiB
Rust
//! A scene showcasing screen space ambient occlusion.
|
|
|
|
use bevy::{
|
|
core_pipeline::experimental::taa::{TemporalAntiAliasBundle, TemporalAntiAliasPlugin},
|
|
pbr::{
|
|
ScreenSpaceAmbientOcclusionBundle, ScreenSpaceAmbientOcclusionQualityLevel,
|
|
ScreenSpaceAmbientOcclusionSettings,
|
|
},
|
|
prelude::*,
|
|
render::camera::TemporalJitter,
|
|
};
|
|
use std::f32::consts::PI;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(AmbientLight {
|
|
brightness: 1000.,
|
|
..default()
|
|
})
|
|
.add_plugins((DefaultPlugins, TemporalAntiAliasPlugin))
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, update)
|
|
.run();
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
commands
|
|
.spawn(Camera3dBundle {
|
|
camera: Camera {
|
|
hdr: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(-2.0, 2.0, -2.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
msaa: Msaa::Off,
|
|
..default()
|
|
})
|
|
.insert(ScreenSpaceAmbientOcclusionBundle::default())
|
|
.insert(TemporalAntiAliasBundle::default());
|
|
|
|
let material = materials.add(StandardMaterial {
|
|
base_color: Color::srgb(0.5, 0.5, 0.5),
|
|
perceptual_roughness: 1.0,
|
|
reflectance: 0.0,
|
|
..default()
|
|
});
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Cuboid::default()),
|
|
material: material.clone(),
|
|
transform: Transform::from_xyz(0.0, 0.0, 1.0),
|
|
..default()
|
|
});
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Cuboid::default()),
|
|
material: material.clone(),
|
|
transform: Transform::from_xyz(0.0, -1.0, 0.0),
|
|
..default()
|
|
});
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Cuboid::default()),
|
|
material,
|
|
transform: Transform::from_xyz(1.0, 0.0, 0.0),
|
|
..default()
|
|
});
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: meshes.add(Sphere::new(0.4).mesh().uv(72, 36)),
|
|
material: materials.add(StandardMaterial {
|
|
base_color: Color::srgb(0.4, 0.4, 0.4),
|
|
perceptual_roughness: 1.0,
|
|
reflectance: 0.0,
|
|
..default()
|
|
}),
|
|
..default()
|
|
},
|
|
SphereMarker,
|
|
));
|
|
|
|
commands.spawn(DirectionalLightBundle {
|
|
directional_light: DirectionalLight {
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_rotation(Quat::from_euler(
|
|
EulerRot::ZYX,
|
|
0.0,
|
|
PI * -0.15,
|
|
PI * -0.15,
|
|
)),
|
|
..default()
|
|
});
|
|
|
|
commands.spawn(
|
|
TextBundle::from_section("", TextStyle::default()).with_style(Style {
|
|
position_type: PositionType::Absolute,
|
|
bottom: Val::Px(12.0),
|
|
left: Val::Px(12.0),
|
|
..default()
|
|
}),
|
|
);
|
|
}
|
|
|
|
fn update(
|
|
camera: Query<
|
|
(
|
|
Entity,
|
|
Option<&ScreenSpaceAmbientOcclusionSettings>,
|
|
Option<&TemporalJitter>,
|
|
),
|
|
With<Camera>,
|
|
>,
|
|
mut text: Query<&mut Text>,
|
|
mut sphere: Query<&mut Transform, With<SphereMarker>>,
|
|
mut commands: Commands,
|
|
keycode: Res<ButtonInput<KeyCode>>,
|
|
time: Res<Time>,
|
|
) {
|
|
let mut sphere = sphere.single_mut();
|
|
sphere.translation.y = (time.elapsed_seconds() / 1.7).sin() * 0.7;
|
|
|
|
let (camera_entity, ssao_settings, temporal_jitter) = camera.single();
|
|
|
|
let mut commands = commands
|
|
.entity(camera_entity)
|
|
.insert_if(
|
|
ScreenSpaceAmbientOcclusionSettings {
|
|
quality_level: ScreenSpaceAmbientOcclusionQualityLevel::Low,
|
|
},
|
|
|| keycode.just_pressed(KeyCode::Digit2),
|
|
)
|
|
.insert_if(
|
|
ScreenSpaceAmbientOcclusionSettings {
|
|
quality_level: ScreenSpaceAmbientOcclusionQualityLevel::Medium,
|
|
},
|
|
|| keycode.just_pressed(KeyCode::Digit3),
|
|
)
|
|
.insert_if(
|
|
ScreenSpaceAmbientOcclusionSettings {
|
|
quality_level: ScreenSpaceAmbientOcclusionQualityLevel::High,
|
|
},
|
|
|| keycode.just_pressed(KeyCode::Digit4),
|
|
)
|
|
.insert_if(
|
|
ScreenSpaceAmbientOcclusionSettings {
|
|
quality_level: ScreenSpaceAmbientOcclusionQualityLevel::Ultra,
|
|
},
|
|
|| keycode.just_pressed(KeyCode::Digit5),
|
|
);
|
|
if keycode.just_pressed(KeyCode::Digit1) {
|
|
commands = commands.remove::<ScreenSpaceAmbientOcclusionSettings>();
|
|
}
|
|
if keycode.just_pressed(KeyCode::Space) {
|
|
if temporal_jitter.is_some() {
|
|
commands.remove::<TemporalJitter>();
|
|
} else {
|
|
commands.insert(TemporalJitter::default());
|
|
}
|
|
}
|
|
|
|
let mut text = text.single_mut();
|
|
let text = &mut text.sections[0].value;
|
|
text.clear();
|
|
|
|
let (o, l, m, h, u) = match ssao_settings.map(|s| s.quality_level) {
|
|
None => ("*", "", "", "", ""),
|
|
Some(ScreenSpaceAmbientOcclusionQualityLevel::Low) => ("", "*", "", "", ""),
|
|
Some(ScreenSpaceAmbientOcclusionQualityLevel::Medium) => ("", "", "*", "", ""),
|
|
Some(ScreenSpaceAmbientOcclusionQualityLevel::High) => ("", "", "", "*", ""),
|
|
Some(ScreenSpaceAmbientOcclusionQualityLevel::Ultra) => ("", "", "", "", "*"),
|
|
_ => unreachable!(),
|
|
};
|
|
|
|
text.push_str("SSAO Quality:\n");
|
|
text.push_str(&format!("(1) {o}Off{o}\n"));
|
|
text.push_str(&format!("(2) {l}Low{l}\n"));
|
|
text.push_str(&format!("(3) {m}Medium{m}\n"));
|
|
text.push_str(&format!("(4) {h}High{h}\n"));
|
|
text.push_str(&format!("(5) {u}Ultra{u}\n\n"));
|
|
|
|
text.push_str("Temporal Antialiasing:\n");
|
|
text.push_str(match temporal_jitter {
|
|
Some(_) => "(Space) Enabled",
|
|
None => "(Space) Disabled",
|
|
});
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct SphereMarker;
|