mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
shadow_biases: Support moving the light position and resetting biases (#10185)
# Objective - Make it possible to move the light position around in the `shadow_biases` example - Also support resetting the depth/normal biases to the engine defaults, or zero. ## Solution - The light position is displayed in the text overlay. - The light position can be adjusted with left/right/up/down/pgup/pgdown. - The depth/normal biases can be reset to defaults by pressing R, or to zero by pressing Z. --------- Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com>
This commit is contained in:
parent
219e2ac6e1
commit
15c54b5542
1 changed files with 169 additions and 86 deletions
|
@ -12,6 +12,7 @@ fn main() {
|
||||||
Update,
|
Update,
|
||||||
(
|
(
|
||||||
cycle_filter_methods,
|
cycle_filter_methods,
|
||||||
|
adjust_light_position,
|
||||||
adjust_point_light_biases,
|
adjust_point_light_biases,
|
||||||
toggle_light,
|
toggle_light,
|
||||||
adjust_directional_light_biases,
|
adjust_directional_light_biases,
|
||||||
|
@ -21,6 +22,9 @@ fn main() {
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Lights;
|
||||||
|
|
||||||
/// set up a 3D scene to test shadow biases and perspective projections
|
/// set up a 3D scene to test shadow biases and perspective projections
|
||||||
fn setup(
|
fn setup(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
@ -44,8 +48,17 @@ fn setup(
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
commands.spawn(PointLightBundle {
|
let light_transform = Transform::from_xyz(5.0, 5.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y);
|
||||||
transform: Transform::from_xyz(5.0, 5.0, 0.0),
|
commands
|
||||||
|
.spawn((
|
||||||
|
SpatialBundle {
|
||||||
|
transform: light_transform,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
Lights,
|
||||||
|
))
|
||||||
|
.with_children(|builder| {
|
||||||
|
builder.spawn(PointLightBundle {
|
||||||
point_light: PointLight {
|
point_light: PointLight {
|
||||||
intensity: 0.0,
|
intensity: 0.0,
|
||||||
range: spawn_plane_depth,
|
range: spawn_plane_depth,
|
||||||
|
@ -57,8 +70,7 @@ fn setup(
|
||||||
},
|
},
|
||||||
..default()
|
..default()
|
||||||
});
|
});
|
||||||
|
builder.spawn(DirectionalLightBundle {
|
||||||
commands.spawn(DirectionalLightBundle {
|
|
||||||
directional_light: DirectionalLight {
|
directional_light: DirectionalLight {
|
||||||
illuminance: 100000.0,
|
illuminance: 100000.0,
|
||||||
shadow_depth_bias: 0.0,
|
shadow_depth_bias: 0.0,
|
||||||
|
@ -66,14 +78,9 @@ fn setup(
|
||||||
shadows_enabled: true,
|
shadows_enabled: true,
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
transform: Transform::from_rotation(Quat::from_euler(
|
|
||||||
EulerRot::ZYX,
|
|
||||||
0.0,
|
|
||||||
PI / 2.,
|
|
||||||
-PI / 4.,
|
|
||||||
)),
|
|
||||||
..default()
|
..default()
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// camera
|
// camera
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
|
@ -90,7 +97,15 @@ fn setup(
|
||||||
commands.spawn(PbrBundle {
|
commands.spawn(PbrBundle {
|
||||||
mesh: sphere_handle.clone(),
|
mesh: sphere_handle.clone(),
|
||||||
material: white_handle.clone(),
|
material: white_handle.clone(),
|
||||||
transform: Transform::from_xyz(0.0, spawn_height, z_i32 as f32),
|
transform: Transform::from_xyz(
|
||||||
|
0.0,
|
||||||
|
if z_i32 % 4 == 0 {
|
||||||
|
spawn_height
|
||||||
|
} else {
|
||||||
|
sphere_radius
|
||||||
|
},
|
||||||
|
z_i32 as f32,
|
||||||
|
),
|
||||||
..default()
|
..default()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -106,18 +121,33 @@ fn setup(
|
||||||
font_size: 20.,
|
font_size: 20.,
|
||||||
..default()
|
..default()
|
||||||
};
|
};
|
||||||
commands.spawn(
|
commands
|
||||||
TextBundle::from_sections([
|
.spawn(NodeBundle {
|
||||||
|
style: Style {
|
||||||
|
position_type: PositionType::Absolute,
|
||||||
|
padding: UiRect::all(Val::Px(5.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
z_index: ZIndex::Global(i32::MAX),
|
||||||
|
background_color: Color::BLACK.with_a(0.75).into(),
|
||||||
|
..default()
|
||||||
|
})
|
||||||
|
.with_children(|c| {
|
||||||
|
c.spawn(TextBundle::from_sections([
|
||||||
TextSection::new("Controls:\n", style.clone()),
|
TextSection::new("Controls:\n", style.clone()),
|
||||||
TextSection::new("WSAD - forward/back/strafe left/right\n", style.clone()),
|
TextSection::new("WSAD - forward/back/strafe left/right\n", style.clone()),
|
||||||
TextSection::new("E / Q - up / down\n", style.clone()),
|
TextSection::new("E / Q - up / down\n", style.clone()),
|
||||||
|
TextSection::new("R / Z - reset biases to default / zero\n", style.clone()),
|
||||||
TextSection::new(
|
TextSection::new(
|
||||||
"L - switch between directional and point lights [",
|
"L - switch between directional and point lights [",
|
||||||
style.clone(),
|
style.clone(),
|
||||||
),
|
),
|
||||||
TextSection::new("DirectionalLight", style.clone()),
|
TextSection::new("DirectionalLight", style.clone()),
|
||||||
TextSection::new("]\n", style.clone()),
|
TextSection::new("]\n", style.clone()),
|
||||||
TextSection::new("F - switch between filter methods [", style.clone()),
|
TextSection::new(
|
||||||
|
"F - switch directional light filter methods [",
|
||||||
|
style.clone(),
|
||||||
|
),
|
||||||
TextSection::new("Hardware2x2", style.clone()),
|
TextSection::new("Hardware2x2", style.clone()),
|
||||||
TextSection::new("]\n", style.clone()),
|
TextSection::new("]\n", style.clone()),
|
||||||
TextSection::new("1/2 - change point light depth bias [", style.clone()),
|
TextSection::new("1/2 - change point light depth bias [", style.clone()),
|
||||||
|
@ -135,14 +165,25 @@ fn setup(
|
||||||
),
|
),
|
||||||
TextSection::new("0.0", style.clone()),
|
TextSection::new("0.0", style.clone()),
|
||||||
TextSection::new("]\n", style.clone()),
|
TextSection::new("]\n", style.clone()),
|
||||||
])
|
TextSection::new(
|
||||||
.with_style(Style {
|
"left/right/up/down/pgup/pgdown - adjust light position (looking at 0,0,0) [",
|
||||||
position_type: PositionType::Absolute,
|
style.clone(),
|
||||||
top: Val::Px(12.0),
|
),
|
||||||
left: Val::Px(12.0),
|
TextSection::new(
|
||||||
..default()
|
format!("{:.1},", light_transform.translation.x),
|
||||||
}),
|
style.clone(),
|
||||||
);
|
),
|
||||||
|
TextSection::new(
|
||||||
|
format!(" {:.1},", light_transform.translation.y),
|
||||||
|
style.clone(),
|
||||||
|
),
|
||||||
|
TextSection::new(
|
||||||
|
format!(" {:.1}", light_transform.translation.z),
|
||||||
|
style.clone(),
|
||||||
|
),
|
||||||
|
TextSection::new("]\n", style.clone()),
|
||||||
|
]));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn toggle_light(
|
fn toggle_light(
|
||||||
|
@ -154,7 +195,7 @@ fn toggle_light(
|
||||||
if input.just_pressed(KeyCode::L) {
|
if input.just_pressed(KeyCode::L) {
|
||||||
for mut light in &mut point_lights {
|
for mut light in &mut point_lights {
|
||||||
light.intensity = if light.intensity == 0.0 {
|
light.intensity = if light.intensity == 0.0 {
|
||||||
example_text.single_mut().sections[4].value = "PointLight".to_string();
|
example_text.single_mut().sections[5].value = "PointLight".to_string();
|
||||||
100000000.0
|
100000000.0
|
||||||
} else {
|
} else {
|
||||||
0.0
|
0.0
|
||||||
|
@ -162,7 +203,7 @@ fn toggle_light(
|
||||||
}
|
}
|
||||||
for mut light in &mut directional_lights {
|
for mut light in &mut directional_lights {
|
||||||
light.illuminance = if light.illuminance == 0.0 {
|
light.illuminance = if light.illuminance == 0.0 {
|
||||||
example_text.single_mut().sections[4].value = "DirectionalLight".to_string();
|
example_text.single_mut().sections[5].value = "DirectionalLight".to_string();
|
||||||
100000.0
|
100000.0
|
||||||
} else {
|
} else {
|
||||||
0.0
|
0.0
|
||||||
|
@ -171,6 +212,42 @@ fn toggle_light(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn adjust_light_position(
|
||||||
|
input: Res<Input<KeyCode>>,
|
||||||
|
mut lights: Query<&mut Transform, With<Lights>>,
|
||||||
|
mut example_text: Query<&mut Text>,
|
||||||
|
) {
|
||||||
|
let mut offset = Vec3::ZERO;
|
||||||
|
if input.just_pressed(KeyCode::Left) {
|
||||||
|
offset.x -= 1.0;
|
||||||
|
}
|
||||||
|
if input.just_pressed(KeyCode::Right) {
|
||||||
|
offset.x += 1.0;
|
||||||
|
}
|
||||||
|
if input.just_pressed(KeyCode::Up) {
|
||||||
|
offset.z -= 1.0;
|
||||||
|
}
|
||||||
|
if input.just_pressed(KeyCode::Down) {
|
||||||
|
offset.z += 1.0;
|
||||||
|
}
|
||||||
|
if input.just_pressed(KeyCode::PageDown) {
|
||||||
|
offset.y -= 1.0;
|
||||||
|
}
|
||||||
|
if input.just_pressed(KeyCode::PageUp) {
|
||||||
|
offset.y += 1.0;
|
||||||
|
}
|
||||||
|
if offset != Vec3::ZERO {
|
||||||
|
let mut example_text = example_text.single_mut();
|
||||||
|
for mut light in &mut lights {
|
||||||
|
light.translation += offset;
|
||||||
|
light.look_at(Vec3::ZERO, Vec3::Y);
|
||||||
|
example_text.sections[23].value = format!("{:.1},", light.translation.x);
|
||||||
|
example_text.sections[24].value = format!(" {:.1},", light.translation.y);
|
||||||
|
example_text.sections[25].value = format!(" {:.1}", light.translation.z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn cycle_filter_methods(
|
fn cycle_filter_methods(
|
||||||
input: Res<Input<KeyCode>>,
|
input: Res<Input<KeyCode>>,
|
||||||
mut filter_methods: Query<&mut ShadowFilteringMethod>,
|
mut filter_methods: Query<&mut ShadowFilteringMethod>,
|
||||||
|
@ -193,7 +270,7 @@ fn cycle_filter_methods(
|
||||||
ShadowFilteringMethod::Hardware2x2
|
ShadowFilteringMethod::Hardware2x2
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
example_text.single_mut().sections[7].value = filter_method_string;
|
example_text.single_mut().sections[8].value = filter_method_string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,24 +285,27 @@ fn adjust_point_light_biases(
|
||||||
for mut light in &mut query {
|
for mut light in &mut query {
|
||||||
if input.just_pressed(KeyCode::Key1) {
|
if input.just_pressed(KeyCode::Key1) {
|
||||||
light.shadow_depth_bias -= depth_bias_step_size;
|
light.shadow_depth_bias -= depth_bias_step_size;
|
||||||
example_text.single_mut().sections[10].value =
|
|
||||||
format!("{:.2}", light.shadow_depth_bias);
|
|
||||||
}
|
}
|
||||||
if input.just_pressed(KeyCode::Key2) {
|
if input.just_pressed(KeyCode::Key2) {
|
||||||
light.shadow_depth_bias += depth_bias_step_size;
|
light.shadow_depth_bias += depth_bias_step_size;
|
||||||
example_text.single_mut().sections[10].value =
|
|
||||||
format!("{:.2}", light.shadow_depth_bias);
|
|
||||||
}
|
}
|
||||||
if input.just_pressed(KeyCode::Key3) {
|
if input.just_pressed(KeyCode::Key3) {
|
||||||
light.shadow_normal_bias -= normal_bias_step_size;
|
light.shadow_normal_bias -= normal_bias_step_size;
|
||||||
example_text.single_mut().sections[13].value =
|
|
||||||
format!("{:.1}", light.shadow_normal_bias);
|
|
||||||
}
|
}
|
||||||
if input.just_pressed(KeyCode::Key4) {
|
if input.just_pressed(KeyCode::Key4) {
|
||||||
light.shadow_normal_bias += normal_bias_step_size;
|
light.shadow_normal_bias += normal_bias_step_size;
|
||||||
example_text.single_mut().sections[13].value =
|
|
||||||
format!("{:.1}", light.shadow_normal_bias);
|
|
||||||
}
|
}
|
||||||
|
if input.just_pressed(KeyCode::R) {
|
||||||
|
light.shadow_depth_bias = PointLight::DEFAULT_SHADOW_DEPTH_BIAS;
|
||||||
|
light.shadow_normal_bias = PointLight::DEFAULT_SHADOW_NORMAL_BIAS;
|
||||||
|
}
|
||||||
|
if input.just_pressed(KeyCode::Z) {
|
||||||
|
light.shadow_depth_bias = 0.0;
|
||||||
|
light.shadow_normal_bias = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
example_text.single_mut().sections[11].value = format!("{:.2}", light.shadow_depth_bias);
|
||||||
|
example_text.single_mut().sections[14].value = format!("{:.1}", light.shadow_normal_bias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,24 +319,27 @@ fn adjust_directional_light_biases(
|
||||||
for mut light in &mut query {
|
for mut light in &mut query {
|
||||||
if input.just_pressed(KeyCode::Key5) {
|
if input.just_pressed(KeyCode::Key5) {
|
||||||
light.shadow_depth_bias -= depth_bias_step_size;
|
light.shadow_depth_bias -= depth_bias_step_size;
|
||||||
example_text.single_mut().sections[16].value =
|
|
||||||
format!("{:.2}", light.shadow_depth_bias);
|
|
||||||
}
|
}
|
||||||
if input.just_pressed(KeyCode::Key6) {
|
if input.just_pressed(KeyCode::Key6) {
|
||||||
light.shadow_depth_bias += depth_bias_step_size;
|
light.shadow_depth_bias += depth_bias_step_size;
|
||||||
example_text.single_mut().sections[16].value =
|
|
||||||
format!("{:.2}", light.shadow_depth_bias);
|
|
||||||
}
|
}
|
||||||
if input.just_pressed(KeyCode::Key7) {
|
if input.just_pressed(KeyCode::Key7) {
|
||||||
light.shadow_normal_bias -= normal_bias_step_size;
|
light.shadow_normal_bias -= normal_bias_step_size;
|
||||||
example_text.single_mut().sections[19].value =
|
|
||||||
format!("{:.1}", light.shadow_normal_bias);
|
|
||||||
}
|
}
|
||||||
if input.just_pressed(KeyCode::Key8) {
|
if input.just_pressed(KeyCode::Key8) {
|
||||||
light.shadow_normal_bias += normal_bias_step_size;
|
light.shadow_normal_bias += normal_bias_step_size;
|
||||||
example_text.single_mut().sections[19].value =
|
|
||||||
format!("{:.1}", light.shadow_normal_bias);
|
|
||||||
}
|
}
|
||||||
|
if input.just_pressed(KeyCode::R) {
|
||||||
|
light.shadow_depth_bias = DirectionalLight::DEFAULT_SHADOW_DEPTH_BIAS;
|
||||||
|
light.shadow_normal_bias = DirectionalLight::DEFAULT_SHADOW_NORMAL_BIAS;
|
||||||
|
}
|
||||||
|
if input.just_pressed(KeyCode::Z) {
|
||||||
|
light.shadow_depth_bias = 0.0;
|
||||||
|
light.shadow_normal_bias = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
example_text.single_mut().sections[17].value = format!("{:.2}", light.shadow_depth_bias);
|
||||||
|
example_text.single_mut().sections[20].value = format!("{:.1}", light.shadow_normal_bias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue