diff --git a/examples/camera/projection_zoom.rs b/examples/camera/projection_zoom.rs index 7ed8dcc5738c2..8776c4161874d 100644 --- a/examples/camera/projection_zoom.rs +++ b/examples/camera/projection_zoom.rs @@ -4,22 +4,36 @@ use std::{f32::consts::PI, ops::Range}; use bevy::{input::mouse::AccumulatedMouseScroll, prelude::*, render::camera::ScalingMode}; -#[derive(Debug, Default, Resource)] +#[derive(Debug, Resource)] struct CameraSettings { - // Clamp fixed vertical scale to this range + /// The height of the viewport in world units when the orthographic camera's scale is 1 + pub orthographic_viewport_height: f32, + /// Clamp the orthographic camera's scale to this range pub orthographic_zoom_range: Range, - // Multiply mouse wheel inputs by this factor + /// Multiply mouse wheel inputs by this factor when using the orthographic camera pub orthographic_zoom_speed: f32, - // Clamp field of view to this range + /// Clamp perspective camera's field of view to this range pub perspective_zoom_range: Range, - // Multiply mouse wheel inputs by this factor + /// Multiply mouse wheel inputs by this factor when using the perspective camera pub perspective_zoom_speed: f32, } fn main() { App::new() .add_plugins(DefaultPlugins) - .init_resource::() + .insert_resource(CameraSettings { + orthographic_viewport_height: 5., + // In orthographic projections, we specify camera scale relative to a default value of 1, + // in which one unit in world space corresponds to one pixel. + orthographic_zoom_range: 0.1..10.0, + // This value was hand-tuned to ensure that zooming in and out feels smooth but not slow. + orthographic_zoom_speed: 0.2, + // Perspective projections use field of view, expressed in radians. We would + // normally not set it to more than π, which represents a 180° FOV. + perspective_zoom_range: (PI / 5.)..(PI - 0.2), + // Changes in FOV are much more noticeable due to its limited range in radians + perspective_zoom_speed: 0.05, + }) .add_systems(Startup, (setup, instructions)) .add_systems(Update, (switch_projection, zoom)) .run(); @@ -28,35 +42,23 @@ fn main() { /// Set up a simple 3D scene fn setup( asset_server: Res, - mut camera_settings: ResMut, + camera_settings: Res, mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { - // 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; - - 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; - commands.spawn(( Name::new("Camera"), Camera3d::default(), Projection::from(OrthographicProjection { + // We can set the scaling mode to FixedVertical to keep the viewport height constant as its aspect ratio changes. + // The viewport height is the height of the camera's view in world units when the scale is 1. scaling_mode: ScalingMode::FixedVertical { - viewport_height: camera_settings.orthographic_zoom_range.start, + viewport_height: camera_settings.orthographic_viewport_height, }, + // This is the default value for scale for orthographic projections. + // To zoom in and out, change this value, rather than `ScalingMode` or the camera's position. + scale: 1., ..OrthographicProjection::default_3d() }), Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), @@ -120,7 +122,7 @@ fn switch_projection( }), Projection::Perspective(_) => Projection::Orthographic(OrthographicProjection { scaling_mode: ScalingMode::FixedVertical { - viewport_height: camera_settings.orthographic_zoom_range.start, + viewport_height: camera_settings.orthographic_viewport_height, }, ..OrthographicProjection::default_3d() }), @@ -133,32 +135,32 @@ fn zoom( camera_settings: Res, mouse_wheel_input: Res, ) { - // Usually, you won't need to handle both types of projection. This is by way of demonstration. + // Usually, you won't need to handle both types of projection, + // but doing so makes for a more complete example. match *camera.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 { viewport_height } = orthographic.scaling_mode else { - return; - }; - // Set a new ScalingMode, clamped to a limited range. - let zoom_level = (viewport_height - + 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 { - viewport_height: zoom_level, - }; + // We want scrolling up to zoom in, decreasing the scale, so we negate the delta. + let delta_zoom = -mouse_wheel_input.delta.y * camera_settings.orthographic_zoom_speed; + // When changing scales, logarithmic changes are more intuitive. + // To get this effect, we add 1 to the delta, so that a delta of 0 + // results in no multiplicative effect, positive values result in a multiplicative increase, + // and negative values result in multiplicative decreases. + let multiplicative_zoom = 1. + delta_zoom; + + orthographic.scale = (orthographic.scale * multiplicative_zoom).clamp( + camera_settings.orthographic_zoom_range.start, + camera_settings.orthographic_zoom_range.end, + ); } Projection::Perspective(ref mut perspective) => { + // We want scrolling up to zoom in, decreasing the scale, so we negate the delta. + let delta_zoom = -mouse_wheel_input.delta.y * camera_settings.perspective_zoom_speed; + // 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, - ); + perspective.fov = (perspective.fov + delta_zoom).clamp( + camera_settings.perspective_zoom_range.start, + camera_settings.perspective_zoom_range.end, + ); } } }