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

Make scroll-to-zoom a lot more responsive in 3D views #4668

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Changes from all commits
Commits
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
37 changes: 14 additions & 23 deletions crates/re_space_view_spatial/src/eye.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,6 @@ pub struct OrbitEye {
impl OrbitEye {
const MAX_PITCH: f32 = 0.999 * 0.25 * std::f32::consts::TAU;

/// Scroll wheels delta are capped out at this value per second. Anything above is smoothed out over several frames.
///
/// We generally only want this to only kick in when the user scrolls fast while we maintain very high framerate,
/// so don't go too low!
///
/// To give a sense of ballpark:
/// * measured 14.0 as the value of a single notch on a logitech mouse wheel connected to a Macbook returns in a single frame (!)
/// (so scrolling 10 notches in a tenth of a second gives a per second scroll delta of 1400)
/// * macbook trackpad is typically at max 1.0 in every given frame
const MAX_SCROLL_DELTA_PER_SECOND: f32 = 1000.0;

pub fn new(orbit_center: Vec3, orbit_radius: f32, world_from_view_rot: Quat, up: Vec3) -> Self {
OrbitEye {
orbit_center,
Expand Down Expand Up @@ -322,18 +311,20 @@ impl OrbitEye {
did_interact = true;
}

// Mouse wheels often go very large steps!
// This makes the zoom speed feel clunky, so we smooth it out over several frames.
let frame_delta = response.ctx.input(|i| i.stable_dt).at_most(0.1);
let accumulated_scroll_delta = raw_scroll_delta + self.unprocessed_scroll_delta;
let unsmoothed_scroll_per_second = accumulated_scroll_delta / frame_delta;
let scroll_dir = unsmoothed_scroll_per_second.signum();
let scroll_delta = scroll_dir
* unsmoothed_scroll_per_second
.abs()
.at_most(Self::MAX_SCROLL_DELTA_PER_SECOND)
* frame_delta;
self.unprocessed_scroll_delta = accumulated_scroll_delta - scroll_delta;
let scroll_delta;
{
// Mouse wheels often go very large steps.
// A single notch on a logitech mouse wheel connected to a Macbook returns 14.0 raw_scroll_delta.
// This makes the zoom speed feel clunky, so we smooth it out over several frames.

self.unprocessed_scroll_delta += raw_scroll_delta;

let dt = response.ctx.input(|i| i.stable_dt).at_most(0.1);
let t = egui::emath::exponential_smooth_factor(0.90, 0.1, dt); // reach _% in _ seconds
Copy link
Member Author

@emilk emilk Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really happy with the exponential_smooth_factor function ❤️

https://docs.rs/emath/latest/emath/fn.exponential_smooth_factor.html


scroll_delta = t * self.unprocessed_scroll_delta;
self.unprocessed_scroll_delta -= scroll_delta;
}

if self.unprocessed_scroll_delta.abs() > 0.1 {
// We have a lot of unprocessed scroll delta, so we need to keep calling this function.
Expand Down
Loading