Skip to content

Commit

Permalink
Some more camera controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurBrussee committed Nov 26, 2024
1 parent 9b467a6 commit ef61edd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
17 changes: 10 additions & 7 deletions crates/brush-viewer/src/orbit_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ pub struct OrbitControls {

pan_momentum: Vec2,
rotate_momentum: Vec2,

min_radius: f32,
max_radius: f32,
}

impl OrbitControls {
pub fn new(transform: Affine3A) -> Self {
pub fn new(transform: Affine3A, min_radius: f32, max_radius: f32) -> Self {
Self {
transform,
focus: Vec3A::ZERO,
pan_momentum: Vec2::ZERO,
rotate_momentum: Vec2::ZERO,
dirty: false,
min_radius,
max_radius
}
}

Expand Down Expand Up @@ -65,15 +70,13 @@ impl OrbitControls {
self.focus += translation;
radius -= scroll * radius * 0.2;

let min = 0.25;
let max = 35.0;
// smooth clamp to min/max radius.
if radius < min {
radius = radius * 0.5 + min * 0.5;
if radius < self.min_radius {
radius = radius * 0.5 + self.min_radius * 0.5;
}

if radius > max {
radius = radius * 0.5 + max * 0.5;
if radius > self.max_radius {
radius = radius * 0.5 + self.max_radius * 0.5;
}

self.transform.translation = self.focus + rotation * Vec3A::new(0.0, 0.0, -radius);
Expand Down
6 changes: 5 additions & 1 deletion crates/brush-viewer/src/panels/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ pub(crate) struct ScenePanel {
queue: Arc<wgpu::Queue>,
device: Arc<wgpu::Device>,
renderer: Arc<EguiRwLock<Renderer>>,

zen: bool,
}

impl ScenePanel {
pub(crate) fn new(
queue: Arc<wgpu::Queue>,
device: Arc<wgpu::Device>,
renderer: Arc<EguiRwLock<Renderer>>,
zen: bool,
) -> Self {
Self {
frame: 0.0,
Expand All @@ -65,6 +68,7 @@ impl ScenePanel {
queue,
device,
renderer,
zen,
}
}

Expand Down Expand Up @@ -244,7 +248,7 @@ impl ViewerPanel for ScenePanel {
self.last_draw = Some(cur_time);

// Empty scene, nothing to show.
if !self.is_loading && self.view_splats.is_empty() && self.err.is_none() {
if !self.is_loading && self.view_splats.is_empty() && self.err.is_none() && !self.zen {
ui.heading("Load a ply file or dataset to get started.");
ui.add_space(5.0);
ui.label(
Expand Down
49 changes: 32 additions & 17 deletions crates/brush-viewer/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,26 +233,34 @@ impl DataSource {
}
}

struct CameraSettings {
focal: f64,
min_radius: f32,
max_radius: f32,
}

impl ViewerContext {
fn new(
device: WgpuDevice,
ctx: egui::Context,
up_axis: Vec3,
focal: f64,
cam_settings: CameraSettings,
controller: UnboundedReceiver<UiControlMessage>,
) -> Self {
let rotation = Quat::from_rotation_arc(Vec3::Y, up_axis);
let model_transform = Affine3A::IDENTITY;

let model_transform =
Affine3A::from_rotation_translation(rotation, Vec3::ZERO).inverse();
let controls = OrbitControls::new(Affine3A::from_translation(-Vec3::Z * 10.0));
let avg_radius = (cam_settings.min_radius + cam_settings.max_radius) / 2.0;
let controls = OrbitControls::new(
Affine3A::from_translation(-Vec3::Z * avg_radius),
cam_settings.min_radius,
cam_settings.max_radius,
);

// Camera position will be controller by controls.
let camera = Camera::new(
Vec3::ZERO,
Quat::IDENTITY,
focal,
focal,
cam_settings.focal,
cam_settings.focal,
glam::vec2(0.5, 0.5),
);

Expand All @@ -271,8 +279,7 @@ impl ViewerContext {

pub fn set_up_axis(&mut self, up_axis: Vec3) {
let rotation = Quat::from_rotation_arc(Vec3::Y, up_axis);
let model_transform =
Affine3A::from_rotation_translation(rotation, Vec3::ZERO).inverse();
let model_transform = Affine3A::from_rotation_translation(rotation, Vec3::ZERO).inverse();
self.model_transform = model_transform;
}

Expand Down Expand Up @@ -398,21 +405,29 @@ impl Viewer {
.get("focal")
.and_then(|f| f.parse().ok())
.unwrap_or(0.5);
let min_radius = search_params
.get("min_radius")
.and_then(|f| f.parse().ok())
.unwrap_or(1.0);
let max_radius = search_params
.get("max_radius")
.and_then(|f| f.parse().ok())
.unwrap_or(10.0);

let up_axis = Vec3::Y;
let context = ViewerContext::new(
device.clone(),
cc.egui_ctx.clone(),
up_axis,
let settings = CameraSettings {
focal,
controller,
);
min_radius,
max_radius,
};

let context = ViewerContext::new(device.clone(), cc.egui_ctx.clone(), settings, controller);

let mut tiles: Tiles<PaneType> = Tiles::default();
let scene_pane = ScenePanel::new(
state.queue.clone(),
state.device.clone(),
state.renderer.clone(),
zen,
);

let scene_pane_id = tiles.insert_pane(Box::new(scene_pane));
Expand Down

0 comments on commit ef61edd

Please sign in to comment.