-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
Simplify zoom and adapt for larger models #764
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a324a54
Use current camera position to scale zoom
jeevcat 8491017
Zoom relative to the distance from the origin
jeevcat be97b44
Remove unused function inputs
jeevcat a67c088
Adjust zoom factor to a value more appropriate for scroll wheels
jeevcat fe8a660
Separate line- and pixel-based scroll input
jeevcat 1ee378e
Merge branch 'main' into sam/zoom
jeevcat 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
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 |
---|---|---|
@@ -1,224 +1,55 @@ | ||
use std::{ | ||
collections::VecDeque, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use fj_math::Point; | ||
use fj_math::{Transform, Vector}; | ||
|
||
use crate::camera::{Camera, FocusPoint}; | ||
|
||
pub struct Zoom { | ||
events: VecDeque<(Instant, f64)>, | ||
|
||
target_speed: f64, | ||
current_speed: f64, | ||
use super::event::MouseScrollDelta; | ||
|
||
last_direction: Direction, | ||
idle_since: Option<Instant>, | ||
pub struct Zoom { | ||
accumulated_delta: f64, | ||
} | ||
|
||
impl Zoom { | ||
pub fn new(now: Instant) -> Self { | ||
pub fn new() -> Self { | ||
Self { | ||
events: VecDeque::new(), | ||
|
||
target_speed: 0.0, | ||
current_speed: 0.0, | ||
|
||
last_direction: Direction::None, | ||
idle_since: Some(now), | ||
accumulated_delta: 0.0, | ||
} | ||
} | ||
|
||
/// Push an input delta from the mouse wheel or track pad | ||
/// | ||
/// Expects the delta to be normalized, so using the mouse wheel and track | ||
/// pad lead to the same zoom feel. | ||
pub fn push_input_delta(&mut self, delta: f64, now: Instant) { | ||
let new_event = delta * 0.01; | ||
|
||
// If this input is opposite to previous inputs, discard previous inputs | ||
// to stop ongoing zoom. | ||
let can_break = match self.idle_since { | ||
Some(idle_since) => idle_since.elapsed() < BREAK_WINDOW, | ||
None => true, // ongoing movement; can always break that | ||
pub fn push(&mut self, delta: MouseScrollDelta) { | ||
// Accumulate all zoom inputs | ||
self.accumulated_delta += match delta { | ||
MouseScrollDelta::Line(delta) => ZOOM_FACTOR_LINE * delta, | ||
MouseScrollDelta::Pixel(delta) => ZOOM_FACTOR_PIXEL * delta, | ||
}; | ||
if self.last_direction.is_opposite(&Direction::from(new_event)) | ||
&& can_break | ||
{ | ||
self.events.clear(); | ||
|
||
// Make sure that this breaks the zoom instantly. | ||
self.current_speed = 0.0; | ||
|
||
return; | ||
} | ||
|
||
self.events.push_back((now, new_event)); | ||
} | ||
|
||
/// Discard zoom events that fall out of the zoom input time window | ||
/// | ||
/// See [`INPUT_WINDOW`]. | ||
pub fn discard_old_events(&mut self, now: Instant) { | ||
while let Some((time, _)) = self.events.front() { | ||
if now.duration_since(*time) > INPUT_WINDOW { | ||
self.events.pop_front(); | ||
continue; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
/// Update the zoom speed based on active zoom events | ||
pub fn update_speed( | ||
pub fn apply_to_camera( | ||
&mut self, | ||
now: Instant, | ||
delta_t: f64, | ||
focus_point: FocusPoint, | ||
camera: &Camera, | ||
camera: &mut Camera, | ||
) { | ||
self.target_speed = self.events.iter().map(|(_, event)| event).sum(); | ||
|
||
// Compute current speed from target speed. Gradually converge towards | ||
// target speed, but snap to target speed once the difference becomes | ||
// minuscule. | ||
let speed_delta = self.target_speed - self.current_speed; | ||
if speed_delta.abs() >= MIN_SPEED_DELTA { | ||
let accel = speed_delta.signum() * ACCELERATION; | ||
self.current_speed += accel * delta_t; | ||
|
||
// By just applying the acceleration, we can overshoot the target, | ||
// which can cause erratic movement, as we overshoot again every | ||
// frame. | ||
// | ||
// If you need to test this code, you can trigger the problem quite | ||
// easily by setting acceleration to a really high value. | ||
if (self.target_speed - self.current_speed).signum() | ||
!= speed_delta.signum() | ||
{ | ||
// We overshot. Snap to target speed. | ||
self.current_speed = self.target_speed; | ||
} | ||
} else { | ||
self.current_speed = self.target_speed; | ||
} | ||
|
||
// Track last zoom direction. | ||
self.last_direction = Direction::from(self.current_speed); | ||
|
||
// Limit current speed, if close to focus point and zooming in. | ||
if let Some(focus_point) = focus_point.0 { | ||
if self.last_direction == Direction::In { | ||
let d = Point::distance(&focus_point, &camera.position()); | ||
self.current_speed = | ||
-f64::min(-self.current_speed, d.into_f64() / 8.); | ||
} | ||
} | ||
|
||
// Track idle time | ||
if self.current_speed == 0.0 { | ||
if self.idle_since.is_none() { | ||
self.idle_since = Some(now); | ||
} | ||
} else { | ||
self.idle_since = None | ||
} | ||
} | ||
|
||
/// Access the current zoom speed | ||
pub fn speed(&self) -> f64 { | ||
self.current_speed | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
enum Direction { | ||
Out, | ||
In, | ||
None, | ||
} | ||
|
||
impl Direction { | ||
fn is_opposite(&self, other: &Self) -> bool { | ||
#[allow(clippy::match_like_matches_macro)] | ||
match (self, other) { | ||
(Self::Out, Self::In) => true, | ||
(Self::In, Self::Out) => true, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
impl From<f64> for Direction { | ||
fn from(speed: f64) -> Self { | ||
if speed > 0.0 { | ||
return Self::Out; | ||
} | ||
if speed < 0.0 { | ||
return Self::In; | ||
} | ||
let distance = match focus_point.0 { | ||
Some(fp) => (fp - camera.position()).magnitude(), | ||
None => camera.position().coords.magnitude(), | ||
}; | ||
let displacement = | ||
self.accumulated_delta * delta_t * distance.into_f64(); | ||
camera.translation = camera.translation | ||
* Transform::translation(Vector::from([0.0, 0.0, -displacement])); | ||
|
||
Self::None | ||
self.accumulated_delta = 0.; | ||
} | ||
} | ||
|
||
/// Time window for active zoom events | ||
/// | ||
/// This is the time window during which a zoom input event still has an effect | ||
/// on target zoom speed. | ||
/// | ||
/// Tuning notes: | ||
/// - If this value is too low, the user can't accumulate many active zooming | ||
/// events, meaning zoom speed can't get very high. | ||
/// - If this value is too high, a single zoom event will have too long of an | ||
/// effect, leading to spongy control behavior. | ||
/// | ||
/// This value should be as low as possible, giving the user precise control, | ||
/// while still accommodating high enough zoom speeds. | ||
const INPUT_WINDOW: Duration = Duration::from_millis(500); | ||
|
||
/// Time window in which opposite movement is interpreted as breaking | ||
/// | ||
/// Defines the time window after a movement ends, during which an input | ||
/// opposite to the movement is interpreted as breaking, not as a new movement. | ||
/// | ||
/// Tuning notes: | ||
/// - If this value is too low, zoom input intended to stop the previous | ||
/// movement will instead start a new, opposite movement, leading to jumpy | ||
/// zooming behavior. | ||
/// - If this value is too high, input meant to start a new zoom movement will | ||
/// not be detected, making zoom less controllable. | ||
/// | ||
/// This value should be as low as possible, while still preventing jumpy | ||
/// zooming behavior. | ||
const BREAK_WINDOW: Duration = Duration::from_millis(50); | ||
|
||
/// The minimum delta between current and target zoom speed | ||
/// Affects the speed of zoom movement given a scroll wheel input in lines. | ||
/// | ||
/// If the speed delta is below this value, the current zoom speed is snapped to | ||
/// the target zoom speed. | ||
/// | ||
/// Tuning notes: | ||
/// - If this value is too low, zoom speed will technically be non-zero, even | ||
/// though no movement is perceivable. This makes detection of last zoom speed | ||
/// and idle time inaccurate, leading to problems. | ||
/// - If this value is too high, zoom acceleration will jump to infinite in that | ||
/// last moment before reaching the target speed, which can seem jarring. | ||
/// | ||
/// This value should be as high as possible, allowing for precise detection of | ||
/// last zoom speed an idle time, while not causing jarring accelerations. | ||
const MIN_SPEED_DELTA: f64 = 0.01; | ||
/// Smaller values will move the camera less with the same input. | ||
/// Larger values will move the camera more with the same input. | ||
const ZOOM_FACTOR_LINE: f64 = 15.0; | ||
|
||
/// Acceleration value for the zoom movement | ||
/// | ||
/// Tuning notes: | ||
/// - If this value is too low, target zoom speed will be reached slowly, | ||
/// leading to less precise control. | ||
/// - If this value is too high, zoom movement seems unnatural, which can cause | ||
/// a jarring experience. | ||
/// Affects the speed of zoom movement given a scroll wheel input in pixels. | ||
/// | ||
/// This value should be as high as possible, while not causing jarring | ||
/// accelerations. | ||
const ACCELERATION: f64 = 0.5; | ||
/// Smaller values will move the camera less with the same input. | ||
/// Larger values will move the camera more with the same input. | ||
const ZOOM_FACTOR_PIXEL: f64 = 1.0; |
Oops, something went wrong.
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.
A few issues with the documentation here:
Line
is horizontal, but they're both vertical.