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

Fix always redrawing in the presence of a 3D space view #2900

Merged
merged 2 commits into from
Aug 3, 2023
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
67 changes: 44 additions & 23 deletions crates/re_space_view_spatial/src/ui_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ impl View3DState {

if let Some(target_orbit) = &cam_interpolation.target_orbit {
*orbit_camera = cam_interpolation.start.lerp(target_orbit, t);
} else if let Some(target_camera) = &cam_interpolation.target_eye {
let camera = cam_interpolation.start.to_eye().lerp(target_camera, t);
} else if let Some(target_eye) = &cam_interpolation.target_eye {
let camera = cam_interpolation.start.to_eye().lerp(target_eye, t);
orbit_camera.copy_from_eye(&camera);
} else {
self.eye_interpolation = None;
Expand All @@ -163,32 +163,44 @@ impl View3DState {
}

fn interpolate_to_eye(&mut self, target: Eye) {
if let Some(start) = self.orbit_eye {
let target_time = EyeInterpolation::target_time(&start.to_eye(), &target);
self.spin = false; // the user wants to move the camera somewhere, so stop spinning
self.eye_interpolation = Some(EyeInterpolation {
elapsed_time: 0.0,
target_time,
start,
target_orbit: None,
target_eye: Some(target),
});
if let Some(start) = self.orbit_eye.as_mut() {
// the user wants to move the camera somewhere, so stop spinning
self.spin = false;

if let Some(target_time) = EyeInterpolation::target_time(&start.to_eye(), &target) {
self.eye_interpolation = Some(EyeInterpolation {
elapsed_time: 0.0,
target_time,
start: *start,
target_orbit: None,
target_eye: Some(target),
});
} else {
start.copy_from_eye(&target);
}
} else {
// shouldn't really happen (`self.orbit_eye` is only `None` for the first frame).
}
}

fn interpolate_to_orbit_eye(&mut self, target: OrbitEye) {
if let Some(start) = self.orbit_eye {
let target_time = EyeInterpolation::target_time(&start.to_eye(), &target.to_eye());
self.spin = false; // the user wants to move the camera somewhere, so stop spinning
self.eye_interpolation = Some(EyeInterpolation {
elapsed_time: 0.0,
target_time,
start,
target_orbit: Some(target),
target_eye: None,
});
// the user wants to move the camera somewhere, so stop spinning
self.spin = false;

if let Some(target_time) =
EyeInterpolation::target_time(&start.to_eye(), &target.to_eye())
{
self.eye_interpolation = Some(EyeInterpolation {
elapsed_time: 0.0,
target_time,
start,
target_orbit: Some(target),
target_eye: None,
});
} else {
self.orbit_eye = Some(target);
}
} else {
self.orbit_eye = Some(target);
}
Expand All @@ -205,14 +217,23 @@ struct EyeInterpolation {
}

impl EyeInterpolation {
pub fn target_time(start: &Eye, stop: &Eye) -> f32 {
pub fn target_time(start: &Eye, stop: &Eye) -> Option<f32> {
// Take more time if the rotation is big:
let angle_difference = start
.world_from_rub_view
.rotation()
.angle_between(stop.world_from_rub_view.rotation());

egui::remap_clamp(angle_difference, 0.0..=std::f32::consts::PI, 0.2..=0.7)
// Threshold to avoid doing pointless interpolations that trigger frame requests.
if angle_difference < 0.01 && start.pos_in_world().distance(stop.pos_in_world()) < 0.0001 {
None
} else {
Some(egui::remap_clamp(
angle_difference,
0.0..=std::f32::consts::PI,
0.2..=0.7,
))
}
}
}

Expand Down