diff --git a/examples/3d/shadow_biases.rs b/examples/3d/shadow_biases.rs index c3d3db32ff3a0..f7abde26372b0 100644 --- a/examples/3d/shadow_biases.rs +++ b/examples/3d/shadow_biases.rs @@ -1,12 +1,15 @@ //! Demonstrates how shadow biases affect shadows in a 3d scene. -use std::f32::consts::PI; +#[path = "../helpers/camera_controller.rs"] +mod camera_controller; -use bevy::{input::mouse::MouseMotion, pbr::ShadowFilteringMethod, prelude::*}; +use bevy::{pbr::ShadowFilteringMethod, prelude::*}; +use camera_controller::{CameraController, CameraControllerPlugin}; fn main() { App::new() .add_plugins(DefaultPlugins) + .add_plugins(CameraControllerPlugin) .add_systems(Startup, setup) .add_systems( Update, @@ -16,7 +19,6 @@ fn main() { adjust_point_light_biases, toggle_light, adjust_directional_light_biases, - camera_controller, ), ) .run(); @@ -135,8 +137,6 @@ fn setup( .with_children(|c| { c.spawn(TextBundle::from_sections([ TextSection::new("Controls:\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("R / Z - reset biases to default / zero\n", style.clone()), TextSection::new( "L - switch between directional and point lights [", @@ -195,7 +195,7 @@ fn toggle_light( if input.just_pressed(KeyCode::KeyL) { for mut light in &mut point_lights { light.intensity = if light.intensity == 0.0 { - example_text.single_mut().sections[5].value = "PointLight".to_string(); + example_text.single_mut().sections[3].value = "PointLight".to_string(); 100000000.0 } else { 0.0 @@ -203,7 +203,7 @@ fn toggle_light( } for mut light in &mut directional_lights { light.illuminance = if light.illuminance == 0.0 { - example_text.single_mut().sections[5].value = "DirectionalLight".to_string(); + example_text.single_mut().sections[3].value = "DirectionalLight".to_string(); 100000.0 } else { 0.0 @@ -241,9 +241,9 @@ fn adjust_light_position( 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); + example_text.sections[21].value = format!("{:.1},", light.translation.x); + example_text.sections[22].value = format!(" {:.1},", light.translation.y); + example_text.sections[23].value = format!(" {:.1}", light.translation.z); } } } @@ -270,7 +270,7 @@ fn cycle_filter_methods( ShadowFilteringMethod::Hardware2x2 } }; - example_text.single_mut().sections[8].value = filter_method_string; + example_text.single_mut().sections[6].value = filter_method_string; } } } @@ -304,8 +304,8 @@ fn adjust_point_light_biases( 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); + example_text.single_mut().sections[9].value = format!("{:.2}", light.shadow_depth_bias); + example_text.single_mut().sections[12].value = format!("{:.1}", light.shadow_normal_bias); } } @@ -338,119 +338,7 @@ fn adjust_directional_light_biases( 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); - } -} - -#[derive(Component)] -struct CameraController { - pub enabled: bool, - pub sensitivity: f32, - pub key_forward: KeyCode, - pub key_back: KeyCode, - pub key_left: KeyCode, - pub key_right: KeyCode, - pub key_up: KeyCode, - pub key_down: KeyCode, - pub key_run: KeyCode, - pub walk_speed: f32, - pub run_speed: f32, - pub friction: f32, - pub pitch: f32, - pub yaw: f32, - pub velocity: Vec3, -} - -impl Default for CameraController { - fn default() -> Self { - Self { - enabled: true, - sensitivity: 0.5, - key_forward: KeyCode::KeyW, - key_back: KeyCode::KeyS, - key_left: KeyCode::KeyA, - key_right: KeyCode::KeyD, - key_up: KeyCode::KeyE, - key_down: KeyCode::KeyQ, - key_run: KeyCode::ShiftLeft, - walk_speed: 10.0, - run_speed: 30.0, - friction: 0.5, - pitch: 0.0, - yaw: 0.0, - velocity: Vec3::ZERO, - } - } -} - -fn camera_controller( - time: Res