Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving camera controller into separate crate in examples #4458

Closed
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8365892
moved camera controller into separate crate
ShadowCurse Apr 11, 2022
8b39709
updated to use camera controller from utils
ShadowCurse Apr 11, 2022
9b408e2
updated to use camera controller from utils
ShadowCurse Apr 11, 2022
ae8f7e5
moved examples/utils into examples_utils
ShadowCurse Apr 11, 2022
d432c8d
Merge branch 'main' into camera_movement_as_a_crate
ShadowCurse Apr 12, 2022
80c15f2
shorten the name and removed check_camera_controller
ShadowCurse Apr 12, 2022
07fc29a
explicit CameraController addition
ShadowCurse Apr 12, 2022
0e3708d
added documentation and controls printout
ShadowCurse Apr 19, 2022
ba93c80
updated controls printout
ShadowCurse Apr 19, 2022
c766fac
fixed examples_utils crate name
ShadowCurse Apr 19, 2022
de5d8f6
Merge branch 'bevyengine:main' into camera_movement_as_a_crate
ShadowCurse Apr 19, 2022
0013cd5
bumped versions
ShadowCurse Apr 19, 2022
684d9c0
Merge branch 'main' into camera_movement_as_a_crate
ShadowCurse May 12, 2022
07f538f
added mouse control changes made by Sheepyhead
ShadowCurse May 12, 2022
b27aede
Update examples_utils/src/camera.rs
ShadowCurse May 13, 2022
41529c7
Update examples_utils/src/lib.rs
ShadowCurse May 13, 2022
e71d1c4
Merge branch 'main' into camera_movement_as_a_crate
ShadowCurse May 16, 2022
cf96d2f
moved examples_utils into bevy_camera
ShadowCurse May 17, 2022
1386c66
updated examples to use bevy_camera
ShadowCurse May 17, 2022
f792fc1
renamed camera to camera_controller
ShadowCurse May 17, 2022
01caad6
added camera controller to another example
ShadowCurse May 17, 2022
86dc2ce
fixed comment
ShadowCurse May 17, 2022
28ecdb8
small refactoring
ShadowCurse May 18, 2022
1581ffe
moved bevy_camera feature to render subfeaturs
ShadowCurse May 18, 2022
55a047e
Merge branch 'main' into camera_movement_as_a_crate
ShadowCurse May 31, 2022
13c4752
switched to bevy_time
ShadowCurse Jun 1, 2022
c16244e
Merge branch 'main' into camera_movement_as_a_crate
ShadowCurse Jun 2, 2022
4e2348d
fmt
ShadowCurse Jun 2, 2022
c2c66f5
Merge branch 'main' into camera_movement_as_a_crate
ShadowCurse Jun 14, 2022
2a93d4b
Merge branch 'main' into camera_movement_as_a_crate
ShadowCurse Jul 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ bytemuck = "1.7"
futures-lite = "1.11.3"
crossbeam-channel = "0.5.0"

# Example crates
examples_utils = { path = "examples_utils", version = "0.7.0-dev" }

[[example]]
name = "hello_world"
path = "examples/hello_world.rs"
Expand Down
12 changes: 8 additions & 4 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bevy::prelude::*;
use examples_utils::camera::{CameraController, CameraControllerPlugin};

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.run();
}
Expand Down Expand Up @@ -38,8 +40,10 @@ fn setup(
..default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraController::default());
}
12 changes: 8 additions & 4 deletions examples/3d/lighting.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use bevy::prelude::*;
use examples_utils::camera::{CameraController, CameraControllerPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.add_system(movement)
.add_system(animate_light_direction)
Expand Down Expand Up @@ -200,10 +202,12 @@ fn setup(
});

// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraController::default());
}

fn animate_light_direction(
Expand Down
13 changes: 9 additions & 4 deletions examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use examples_utils::camera::{CameraController, CameraControllerPlugin};

fn main() {
App::new()
Expand All @@ -7,17 +8,21 @@ fn main() {
brightness: 1.0 / 5.0f32,
})
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.add_system(animate_light_direction)
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"));
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
..default()
});
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0)
.looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
..default()
})
.insert(CameraController::default());
const HALF_SIZE: f32 = 1.0;
commands.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
Expand Down
12 changes: 8 additions & 4 deletions examples/3d/msaa.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use examples_utils::camera::{CameraController, CameraControllerPlugin};

/// This example shows how to configure Multi-Sample Anti-Aliasing. Setting the sample count higher
/// will result in smoother edges, but it will also increase the cost to render those edges. The
Expand All @@ -11,6 +12,7 @@ fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.add_system(cycle_msaa)
.run();
Expand All @@ -37,10 +39,12 @@ fn setup(
..default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-3.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-3.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraController::default());
}

fn cycle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) {
Expand Down
12 changes: 8 additions & 4 deletions examples/3d/parenting.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use bevy::prelude::*;
use examples_utils::camera::{CameraController, CameraControllerPlugin};

/// This example illustrates how to create parent->child relationships between entities how parent
/// transforms are propagated to their descendants
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.add_system(rotator_system)
.run();
Expand Down Expand Up @@ -58,8 +60,10 @@ fn setup(
..default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(5.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(5.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraController::default());
}
132 changes: 7 additions & 125 deletions examples/3d/shadow_biases.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use bevy::{input::mouse::MouseMotion, prelude::*};
use bevy::prelude::*;
use examples_utils::camera::{CameraController, CameraControllerPlugin};

fn main() {
println!(
"Controls:
WSAD - forward/back/strafe left/right
LShift - 'run'
E - up
Q - down
"
===============================
======= Scene Controls ========
===============================
L - switch between directional and point lights
1/2 - decrease/increase point light depth bias
3/4 - decrease/increase point light normal bias
Expand All @@ -15,11 +15,11 @@ fn main() {
);
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.add_system(adjust_point_light_biases)
.add_system(toggle_light)
.add_system(adjust_directional_light_biases)
.add_system(camera_controller)
.run();
}

Expand Down Expand Up @@ -201,121 +201,3 @@ fn adjust_directional_light_biases(
}
}
}

#[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::W,
key_back: KeyCode::S,
key_left: KeyCode::A,
key_right: KeyCode::D,
key_up: KeyCode::E,
key_down: KeyCode::Q,
key_run: KeyCode::LShift,
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<Time>,
mut mouse_events: EventReader<MouseMotion>,
key_input: Res<Input<KeyCode>>,
mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
) {
let dt = time.delta_seconds();

// Handle mouse input
let mut mouse_delta = Vec2::ZERO;
for mouse_event in mouse_events.iter() {
mouse_delta += mouse_event.delta;
}

for (mut transform, mut options) in query.iter_mut() {
if !options.enabled {
continue;
}

// Handle key input
let mut axis_input = Vec3::ZERO;
if key_input.pressed(options.key_forward) {
axis_input.z += 1.0;
}
if key_input.pressed(options.key_back) {
axis_input.z -= 1.0;
}
if key_input.pressed(options.key_right) {
axis_input.x += 1.0;
}
if key_input.pressed(options.key_left) {
axis_input.x -= 1.0;
}
if key_input.pressed(options.key_up) {
axis_input.y += 1.0;
}
if key_input.pressed(options.key_down) {
axis_input.y -= 1.0;
}

// Apply movement update
if axis_input != Vec3::ZERO {
let max_speed = if key_input.pressed(options.key_run) {
options.run_speed
} else {
options.walk_speed
};
options.velocity = axis_input.normalize() * max_speed;
} else {
let friction = options.friction.clamp(0.0, 1.0);
options.velocity *= 1.0 - friction;
if options.velocity.length_squared() < 1e-6 {
options.velocity = Vec3::ZERO;
}
}
let forward = transform.forward();
let right = transform.right();
transform.translation += options.velocity.x * dt * right
+ options.velocity.y * dt * Vec3::Y
+ options.velocity.z * dt * forward;

if mouse_delta != Vec2::ZERO {
// Apply look update
let (pitch, yaw) = (
(options.pitch - mouse_delta.y * 0.5 * options.sensitivity * dt).clamp(
-0.99 * std::f32::consts::FRAC_PI_2,
0.99 * std::f32::consts::FRAC_PI_2,
),
options.yaw - mouse_delta.x * options.sensitivity * dt,
);
transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, yaw, pitch);
options.pitch = pitch;
options.yaw = yaw;
}
}
}
19 changes: 13 additions & 6 deletions examples/3d/shadow_caster_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ use bevy::{
pbr::{NotShadowCaster, NotShadowReceiver},
prelude::*,
};
use examples_utils::camera::{CameraController, CameraControllerPlugin};

fn main() {
println!(
"Controls:
"
===============================
======= Scene Controls ========
===============================
C - toggle shadow casters (i.e. casters become not, and not casters become casters)
R - toggle shadow receivers (i.e. receivers become not, and not receivers become receivers)
L - switch between directional and point lights"
);
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.add_system(toggle_light)
.add_system(toggle_shadows)
Expand Down Expand Up @@ -109,11 +114,13 @@ fn setup(
});

// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-5.0, 5.0, 5.0)
.looking_at(Vec3::new(-1.0, 1.0, 0.0), Vec3::Y),
..default()
});
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-5.0, 5.0, 5.0)
.looking_at(Vec3::new(-1.0, 1.0, 0.0), Vec3::Y),
..default()
})
.insert(CameraController::default());
}

fn toggle_light(
Expand Down
12 changes: 8 additions & 4 deletions examples/3d/spherical_area_lights.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bevy::prelude::*;
use examples_utils::camera::{CameraController, CameraControllerPlugin};

fn main() {
App::new()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins)
.add_plugin(CameraControllerPlugin)
.add_startup_system(setup)
.run();
}
Expand All @@ -14,10 +16,12 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(1.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(1.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraController::default());

// plane
commands.spawn_bundle(PbrBundle {
Expand Down
Loading