-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add examples for orthographic and perspective zoom #15092
Merged
alice-i-cecile
merged 12 commits into
bevyengine:main
from
bas-ie:add-zoom-projection-examples
Sep 9, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1ffd788
Add examples for orthographic and perspective zoom
bas-ie 770b70f
Fix descriptions
bas-ie 6d3a8ac
Update example list
bas-ie 6e59e76
Add instructions
bas-ie 5412d17
Pad out the comments
bas-ie c3f4e1c
Address PR feedback
bas-ie 90bf07b
Apply (some of) PR feedback
bas-ie 9f1e021
Unicode!
bas-ie 038368a
Correct name
bas-ie 095da01
Extraneous comment
bas-ie d44a972
Use deref_mut
bas-ie 84a8c56
Use correct defaults, try into_inner because reasons
bas-ie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
//! Shows how to zoom and orbit orthographic and perspective projection cameras. | ||
|
||
use std::{ | ||
f32::consts::{FRAC_PI_2, PI}, | ||
ops::Range, | ||
}; | ||
|
||
use bevy::{input::mouse::AccumulatedMouseScroll, prelude::*, render::camera::ScalingMode}; | ||
|
||
#[derive(Debug, Default, Resource)] | ||
struct CameraSettings { | ||
pub orbit_distance: f32, | ||
// Multiply keyboard inputs by this factor | ||
pub orbit_speed: f32, | ||
// Clamp fixed vertical scale to this range | ||
pub orthographic_zoom_range: Range<f32>, | ||
// Multiply mouse wheel inputs by this factor | ||
pub orthographic_zoom_speed: f32, | ||
// Clamp field of view to this range | ||
pub perspective_zoom_range: Range<f32>, | ||
// Multiply mouse wheel inputs by this factor | ||
pub perspective_zoom_speed: f32, | ||
// Clamp pitch to this range | ||
pub pitch_range: Range<f32>, | ||
} | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.init_resource::<CameraSettings>() | ||
.add_systems(Startup, (setup, instructions)) | ||
.add_systems(Update, (orbit, switch_projection, zoom)) | ||
.run(); | ||
} | ||
|
||
/// Set up a simple 3D scene | ||
fn setup( | ||
mut camera_settings: ResMut<CameraSettings>, | ||
mut commands: Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<StandardMaterial>>, | ||
) { | ||
// Perspective projections use field of view, expressed in radians. We would | ||
// normally not set it to more than π, which represents a 180° FOV. | ||
let min_fov = PI / 5.; | ||
let max_fov = PI - 0.2; | ||
|
||
// In orthographic projections, we specify sizes in world units. The below values | ||
// are very roughly similar to the above FOV settings, in terms of how "far away" | ||
// the subject will appear when used with FixedVertical scaling mode. | ||
let min_zoom = 5.0; | ||
let max_zoom = 150.0; | ||
|
||
// Limiting pitch stops some unexpected rotation past 90° up or down. | ||
let pitch_limit = FRAC_PI_2 - 0.01; | ||
|
||
camera_settings.orbit_distance = 10.0; | ||
camera_settings.orbit_speed = 1.0; | ||
camera_settings.orthographic_zoom_range = min_zoom..max_zoom; | ||
camera_settings.orthographic_zoom_speed = 1.0; | ||
camera_settings.perspective_zoom_range = min_fov..max_fov; | ||
// Changes in FOV are much more noticeable due to its limited range in radians | ||
camera_settings.perspective_zoom_speed = 0.05; | ||
camera_settings.pitch_range = -pitch_limit..pitch_limit; | ||
|
||
commands.spawn(( | ||
Name::new("Camera"), | ||
Camera3dBundle { | ||
projection: OrthographicProjection { | ||
scaling_mode: ScalingMode::FixedVertical( | ||
camera_settings.orthographic_zoom_range.start, | ||
), | ||
..OrthographicProjection::default_3d() | ||
} | ||
.into(), | ||
transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), | ||
..default() | ||
}, | ||
)); | ||
|
||
commands.spawn(( | ||
Name::new("Plane"), | ||
PbrBundle { | ||
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)), | ||
material: materials.add(StandardMaterial { | ||
base_color: Color::srgb(0.3, 0.5, 0.3), | ||
// Turning off culling keeps the plane visible when viewed from beneath. | ||
cull_mode: None, | ||
..default() | ||
}), | ||
..default() | ||
}, | ||
)); | ||
|
||
commands.spawn(( | ||
Name::new("Cube"), | ||
PbrBundle { | ||
mesh: meshes.add(Cuboid::default()), | ||
material: materials.add(Color::srgb(0.8, 0.7, 0.6)), | ||
transform: Transform::from_xyz(1.5, 0.51, 1.5), | ||
..default() | ||
}, | ||
)); | ||
|
||
commands.spawn(( | ||
Name::new("Light"), | ||
PointLightBundle { | ||
transform: Transform::from_xyz(3.0, 8.0, 5.0), | ||
..default() | ||
}, | ||
)); | ||
} | ||
|
||
fn instructions(mut commands: Commands) { | ||
commands | ||
.spawn(( | ||
Name::new("Instructions"), | ||
NodeBundle { | ||
style: Style { | ||
align_items: AlignItems::Start, | ||
flex_direction: FlexDirection::Column, | ||
justify_content: JustifyContent::Start, | ||
width: Val::Percent(100.), | ||
..default() | ||
}, | ||
..default() | ||
}, | ||
)) | ||
.with_children(|parent| { | ||
parent.spawn(TextBundle::from_section( | ||
"Scroll mouse wheel to zoom in/out", | ||
TextStyle::default(), | ||
)); | ||
parent.spawn(TextBundle::from_section( | ||
"W or S: pitch", | ||
TextStyle::default(), | ||
)); | ||
parent.spawn(TextBundle::from_section( | ||
"A or D: yaw", | ||
TextStyle::default(), | ||
)); | ||
}); | ||
} | ||
|
||
fn orbit( | ||
mut camera: Query<&mut Transform, With<Camera>>, | ||
camera_settings: Res<CameraSettings>, | ||
keyboard_input: Res<ButtonInput<KeyCode>>, | ||
time: Res<Time>, | ||
) { | ||
let mut transform = camera.single_mut(); | ||
|
||
let mut delta_pitch = 0.0; | ||
let mut delta_yaw = 0.0; | ||
|
||
if keyboard_input.pressed(KeyCode::KeyW) { | ||
delta_pitch += camera_settings.orbit_speed; | ||
} | ||
if keyboard_input.pressed(KeyCode::KeyA) { | ||
delta_yaw -= camera_settings.orbit_speed; | ||
} | ||
if keyboard_input.pressed(KeyCode::KeyS) { | ||
delta_pitch -= camera_settings.orbit_speed; | ||
} | ||
if keyboard_input.pressed(KeyCode::KeyD) { | ||
delta_yaw += camera_settings.orbit_speed; | ||
} | ||
|
||
// Incorporating the delta time between calls prevents this from being framerate-bound. | ||
delta_pitch *= time.delta_seconds(); | ||
delta_yaw *= time.delta_seconds(); | ||
|
||
// Obtain the existing pitch, yaw, and roll values from the transform. | ||
let (yaw, pitch, roll) = transform.rotation.to_euler(EulerRot::YXZ); | ||
|
||
// Establish the new yaw and pitch, preventing the pitch value from exceeding our limits. | ||
let pitch = (pitch + delta_pitch).clamp( | ||
camera_settings.pitch_range.start, | ||
camera_settings.pitch_range.end, | ||
); | ||
let yaw = yaw + delta_yaw; | ||
transform.rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, roll); | ||
|
||
// Adjust the translation to maintain the correct orientation toward the orbit target. | ||
transform.translation = Vec3::ZERO - transform.forward() * camera_settings.orbit_distance; | ||
} | ||
|
||
fn switch_projection( | ||
mut camera: Query<&mut Projection, With<Camera>>, | ||
camera_settings: Res<CameraSettings>, | ||
keyboard_input: Res<ButtonInput<KeyCode>>, | ||
) { | ||
let mut projection = camera.single_mut(); | ||
|
||
if keyboard_input.just_pressed(KeyCode::Space) { | ||
// Switch projection type | ||
*projection = match *projection { | ||
Projection::Orthographic(_) => Projection::Perspective(PerspectiveProjection { | ||
fov: camera_settings.perspective_zoom_range.start, | ||
..default() | ||
}), | ||
Projection::Perspective(_) => Projection::Orthographic(OrthographicProjection { | ||
scaling_mode: ScalingMode::FixedVertical( | ||
camera_settings.orthographic_zoom_range.start, | ||
), | ||
..OrthographicProjection::default_3d() | ||
}), | ||
} | ||
} | ||
} | ||
|
||
fn zoom( | ||
mut camera: Query<&mut Projection, With<Camera>>, | ||
camera_settings: Res<CameraSettings>, | ||
mouse_wheel_input: Res<AccumulatedMouseScroll>, | ||
) { | ||
let projection = camera.single_mut(); | ||
|
||
// Usually, you won't need to handle both types of projection. This is by way of demonstration. | ||
match projection.into_inner() { | ||
Projection::Orthographic(ref mut orthographic) => { | ||
// Get the current scaling_mode value to allow clamping the new value to our zoom range. | ||
let ScalingMode::FixedVertical(current) = orthographic.scaling_mode else { | ||
return; | ||
}; | ||
// Set a new ScalingMode, clamped to a limited range. | ||
let zoom_level = (current | ||
+ camera_settings.orthographic_zoom_speed * mouse_wheel_input.delta.y) | ||
.clamp( | ||
camera_settings.orthographic_zoom_range.start, | ||
camera_settings.orthographic_zoom_range.end, | ||
); | ||
orthographic.scaling_mode = ScalingMode::FixedVertical(zoom_level); | ||
} | ||
Projection::Perspective(ref mut perspective) => { | ||
// Adjust the field of view, but keep it within our stated range. | ||
perspective.fov = (perspective.fov | ||
+ camera_settings.perspective_zoom_speed * mouse_wheel_input.delta.y) | ||
.clamp( | ||
camera_settings.perspective_zoom_range.start, | ||
camera_settings.perspective_zoom_range.end, | ||
); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quite like the orbit showcase. I don't think we had that before :)
Maybe rename the file to
orbit_and_projection_zoom
, with the example name changing accordingly?