Skip to content

Commit

Permalink
Fix issue with orthographic cameras, add ortho example
Browse files Browse the repository at this point in the history
  • Loading branch information
Plonq committed May 26, 2023
1 parent 25fdce8 commit 439ae4f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
56 changes: 56 additions & 0 deletions examples/orthographic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Demonstrates usage with an orthographic camera
use bevy::prelude::*;
use bevy::render::camera::ScalingMode;
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(PanOrbitCameraPlugin)
.add_startup_system(setup)
.run();
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Ground
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
// Cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
// Camera
commands.spawn((
Camera3dBundle {
// Put the camera further away so clipping is less likely
transform: Transform::from_translation(Vec3::new(0.0, 5.5, 15.0)),
projection: Projection::Orthographic(OrthographicProjection {
scaling_mode: ScalingMode::FixedVertical(2.0),
..default()
}),
..default()
},
PanOrbitCamera::default(),
));
}
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct PanOrbitCamera {
/// Defaults to `Vec3::ZERO`.
pub focus: Vec3,
/// The radius of the orbit, or the distance from the `focus` point.
/// For orthographic projection, this is converted the the scale factor of the projection.
/// For orthographic projection, this is ignored, and the projection's `scale` is used instead.
/// If set to `None`, it will be calculated from the camera's current position during
/// initialization.
/// Automatically updated.
Expand Down Expand Up @@ -425,14 +425,13 @@ fn pan_orbit_camera(
has_moved = true;
}
} else if scroll.abs() > 0.0 {
pan_orbit.radius = pan_orbit
.radius
// Prevent zoom to zero otherwise we can get stuck there
.map(|radius| f32::max(radius - scroll * radius * 0.2, 0.05));
if let Projection::Orthographic(ref mut p) = *projection {
if let Some(radius) = pan_orbit.radius {
p.scale = radius;
}
p.scale = f32::max(p.scale - scroll * p.scale * 0.2, 0.05);
} else {
pan_orbit.radius = pan_orbit
.radius
// Prevent zoom to zero otherwise we can get stuck there
.map(|radius| f32::max(radius - scroll * radius * 0.2, 0.05));
}
has_moved = true;
}
Expand Down

0 comments on commit 439ae4f

Please sign in to comment.