Slow the camera zoom in/out

This commit is contained in:
Lillian Vixe 2024-02-18 06:04:11 -08:00
parent 6febfecde2
commit 3ddfe54b97
2 changed files with 13 additions and 14 deletions

View file

@ -3,11 +3,10 @@ pub mod orbit_camera {
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
use bevy::window::*;
use bevy::*;
/// Tags an entity as capable of panning and orbiting.
#[derive(Component)]
struct PanOrbitCamera {
pub struct PanOrbitCamera {
/// The "focus point" to orbit around. It is automatically updated when panning the camera
pub focus: Vec3,
pub radius: f32,
@ -26,7 +25,7 @@ pub mod orbit_camera {
/// Pan the camera with middle mouse click, zoom with scroll wheel, orbit with right mouse click.
pub fn pan_orbit_camera(
window_query: &Query<&Window, With<PrimaryWindow>>,
window_query: Query<&Window, With<PrimaryWindow>>,
mut ev_motion: EventReader<MouseMotion>,
mut ev_scroll: EventReader<MouseWheel>,
input_mouse: Res<Input<MouseButton>>,
@ -42,17 +41,17 @@ pub mod orbit_camera {
let mut orbit_button_changed = false;
if input_mouse.pressed(orbit_button) {
for ev in ev_motion.iter() {
for ev in ev_motion.read() {
rotation_move += ev.delta;
}
} else if input_mouse.pressed(pan_button) {
// Pan only if we're not rotating at the moment
for ev in ev_motion.iter() {
for ev in ev_motion.read() {
pan += ev.delta;
}
}
for ev in ev_scroll.iter() {
scroll += ev.y;
for ev in ev_scroll.read() {
scroll += ev.y * 0.0125;
}
if input_mouse.just_released(orbit_button) || input_mouse.just_pressed(orbit_button) {
orbit_button_changed = true;
@ -69,7 +68,7 @@ pub mod orbit_camera {
let mut any = false;
if rotation_move.length_squared() > 0.0 {
any = true;
let window = get_primary_window_size(window_query);
let window = get_primary_window_size(&window_query);
let delta_x = {
let delta = rotation_move.x / window.x * std::f32::consts::PI * 2.0;
if pan_orbit.upside_down {
@ -86,7 +85,7 @@ pub mod orbit_camera {
} else if pan.length_squared() > 0.0 {
any = true;
// make panning distance independent of resolution and FOV,
let window = get_primary_window_size(window_query);
let window = get_primary_window_size(&window_query);
if let Projection::Perspective(projection) = projection {
pan *= Vec2::new(projection.fov * projection.aspect_ratio, projection.fov)
/ window;

View file

@ -7,7 +7,7 @@ pub mod mesh_plugin {
},
};
use crate::orbit_camera::orbit_camera::spawn_camera;
use crate::orbit_camera::orbit_camera::*;
#[derive(Event)]
pub struct SendMeshEvent {}
@ -16,6 +16,7 @@ pub mod mesh_plugin {
impl Plugin for PluginInitializer {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, pan_orbit_camera);
}
}
@ -89,7 +90,7 @@ pub mod mesh_plugin {
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
indices.extend([len + 2, len + 3, len + 1, len + 1, len + 3, len]);
}
if render_py {
@ -113,7 +114,6 @@ pub mod mesh_plugin {
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
// ^ this is the only one that we know for sure is correct
}
if render_ny {
@ -136,7 +136,7 @@ pub mod mesh_plugin {
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
indices.extend([len + 2, len + 3, len + 1, len + 1, len + 3, len]);
}
if render_pz {
@ -182,7 +182,7 @@ pub mod mesh_plugin {
color.as_rgba_f32(),
color.as_rgba_f32(),
]);
indices.extend([len, len + 3, len + 1, len + 1, len + 3, len + 2]);
indices.extend([len + 2, len + 3, len + 1, len + 1, len + 3, len]);
}
}