Skip to content

Commit

Permalink
feat: zoom through objects
Browse files Browse the repository at this point in the history
  • Loading branch information
m-edlund committed Jun 25, 2024
1 parent 10f3979 commit d1f82e2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/zoom_through.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! A minimal example demonstrating zooming through objects.

use bevy::prelude::*;
use bevy_editor_cam::prelude::*;

fn main() {
App::new()
.add_plugins((
DefaultPlugins,
bevy_mod_picking::DefaultPickingPlugins,
DefaultEditorCamPlugins,
))
.add_systems(Startup, (setup_camera, setup_scene))
.run()
}

fn setup_camera(mut commands: Commands) {
commands.spawn((
Camera3dBundle::default(),
EditorCam {
minimum_distance: Some(1.5), // If an object is 1.5m away from the camera, begin zooming through it.
..default()
},
));
}

//
// --- The below code is not important for the example ---
//

fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let material = materials.add(Color::rgba(0.1, 0.1, 0.9, 0.5));
let mesh = meshes.add(Cuboid::from_size(Vec3::new(3.0, 3.0, 0.25)));

for i in 1..5 {
commands.spawn(PbrBundle {
mesh: mesh.clone(),
material: material.clone(),
transform: Transform::from_xyz(0.0, 0.0, -5.0 * i as f32),
..default()
});
}
}
18 changes: 18 additions & 0 deletions src/controller/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub struct EditorCam {
pub perspective: PerspectiveSettings,
/// Settings used when the camera has an orthographic [`Projection`].
pub orthographic: OrthographicSettings,
/// How close can the camera get to object before zooming through it. None
/// disables zooming through objects.
pub minimum_distance: Option<f64>,
/// Managed by the camera controller, though you may want to change this when spawning or
/// manually moving the camera.
///
Expand All @@ -89,6 +92,7 @@ impl Default for EditorCam {
orthographic: Default::default(),
enabled_motion: Default::default(),
current_motion: Default::default(),
minimum_distance: Default::default(),
last_anchor_depth: -2.0,
}
}
Expand Down Expand Up @@ -220,6 +224,20 @@ impl EditorCam {
return;
}
let anchor = self.maybe_update_anchor(anchor);

// Extend the anchor if a minimum distance for zooming is set
let anchor = if let Some(minimum_distance) = self.minimum_distance {
if anchor.length() > minimum_distance {
anchor
} else if let Some(norm_anchor) = anchor.try_normalize() {
norm_anchor * minimum_distance
} else {
anchor
}
} else {
anchor
};

// Inherit current camera velocity
let zoom_inputs = match self.current_motion {
CurrentMotion::Stationary | CurrentMotion::Momentum { .. } => InputQueue::default(),
Expand Down

0 comments on commit d1f82e2

Please sign in to comment.