pub struct Camera {
pub projection: Projection,
pub center: Vec3,
pub distance: f32,
pub orientation: Quat,
pub fov_y: f32,
pub aspect: f32,
pub znear: f32,
pub zfar: f32,
}Expand description
Arcball camera for the 3D viewport.
The camera orbits around a center point. orientation is a quaternion that
maps camera-space axes to world-space: eye = center + orientation * (Z * distance).
Pan translates the center in camera-space. Zoom adjusts the distance.
Using a quaternion avoids gimbal lock and allows full 360° orbit in any direction. All matrices are computed right-handed (wgpu NDC convention).
Fields§
§projection: ProjectionProjection mode (perspective or orthographic).
center: Vec3Orbit target point in world space.
distance: f32Distance from center to eye (zoom).
orientation: QuatCamera orientation as a unit quaternion. eye = center + orientation * (Vec3::Z * distance). Identity = looking along -Z from +Z position (standard front view).
fov_y: f32Vertical field of view in radians.
aspect: f32Viewport width / height ratio — updated each frame from viewport rect.
znear: f32Near clipping plane distance.
zfar: f32Far clipping plane distance.
Implementations§
Source§impl Camera
impl Camera
Sourcepub const MIN_DISTANCE: f32 = 0.01
pub const MIN_DISTANCE: f32 = 0.01
Minimum allowed camera distance (zoom limit).
Sourcepub const MAX_DISTANCE: f32 = 1.0e6
pub const MAX_DISTANCE: f32 = 1.0e6
Maximum allowed camera distance (zoom limit).
Sourcepub fn center(&self) -> Vec3
pub fn center(&self) -> Vec3
Return the orbit target center in world space.
Forward-compatible accessor — equivalent to reading self.center.
Sourcepub fn set_center(&mut self, center: Vec3)
pub fn set_center(&mut self, center: Vec3)
Set the orbit target center in world space.
Sourcepub fn distance(&self) -> f32
pub fn distance(&self) -> f32
Return the camera distance (zoom).
Forward-compatible accessor — equivalent to reading self.distance.
Sourcepub fn set_distance(&mut self, d: f32)
pub fn set_distance(&mut self, d: f32)
Set the camera distance, clamped to [MIN_DISTANCE, MAX_DISTANCE].
Sourcepub fn orientation(&self) -> Quat
pub fn orientation(&self) -> Quat
Return the camera orientation quaternion.
Forward-compatible accessor — equivalent to reading self.orientation.
Sourcepub fn set_orientation(&mut self, q: Quat)
pub fn set_orientation(&mut self, q: Quat)
Set the camera orientation, normalizing the quaternion.
Sourcepub fn set_aspect_ratio(&mut self, width: f32, height: f32)
pub fn set_aspect_ratio(&mut self, width: f32, height: f32)
Set the aspect ratio from pixel dimensions.
If height is zero or negative, aspect is set to 1.0.
Sourcepub fn set_clip_planes(&mut self, znear: f32, zfar: f32)
pub fn set_clip_planes(&mut self, znear: f32, zfar: f32)
Set the near and far clipping plane distances.
Sourcepub fn orbit(&mut self, yaw: f32, pitch: f32)
pub fn orbit(&mut self, yaw: f32, pitch: f32)
Orbit the camera by yaw and pitch radians.
Applies Quat::from_rotation_y(-yaw) * orientation * Quat::from_rotation_x(-pitch).
The sign convention matches all examples: positive yaw rotates counter-clockwise
when viewed from above, positive pitch tilts up.
Sourcepub fn pan_world(&mut self, right_delta: f32, up_delta: f32)
pub fn pan_world(&mut self, right_delta: f32, up_delta: f32)
Pan the camera by world-space deltas.
right_delta subtracts from center along the camera right vector;
up_delta adds to center along the camera up vector. This sign
convention matches mouse pan: dragging right moves the scene left.
Sourcepub fn pan_pixels(&mut self, delta_pixels: Vec2, viewport_height: f32)
pub fn pan_pixels(&mut self, delta_pixels: Vec2, viewport_height: f32)
Pan the camera by pixel-space deltas.
Computes pan_scale = 2 * distance * tan(fov_y/2) / viewport_height
then delegates to pan_world.
Sourcepub fn zoom_by_factor(&mut self, factor: f32)
pub fn zoom_by_factor(&mut self, factor: f32)
Zoom by multiplying the distance by factor, clamped to [MIN_DISTANCE, MAX_DISTANCE].
Sourcepub fn zoom_by_delta(&mut self, delta: f32)
pub fn zoom_by_delta(&mut self, delta: f32)
Zoom by adding delta to the distance, clamped to [MIN_DISTANCE, MAX_DISTANCE].
Sourcepub fn frame_sphere(&mut self, center: Vec3, radius: f32)
pub fn frame_sphere(&mut self, center: Vec3, radius: f32)
Frame the camera to contain a bounding sphere, applying the result directly.
Sourcepub fn frame_aabb(&mut self, aabb: &Aabb)
pub fn frame_aabb(&mut self, aabb: &Aabb)
Frame the camera to contain an AABB, applying the result directly.
Sourcepub fn fit_sphere_target(&self, center: Vec3, radius: f32) -> CameraTarget
pub fn fit_sphere_target(&self, center: Vec3, radius: f32) -> CameraTarget
Compute a CameraTarget that would frame a bounding sphere,
preserving the current orientation.
Sourcepub fn fit_aabb_target(&self, aabb: &Aabb) -> CameraTarget
pub fn fit_aabb_target(&self, aabb: &Aabb) -> CameraTarget
Compute a CameraTarget that would frame an AABB,
preserving the current orientation.
Sourcepub fn eye_position(&self) -> Vec3
pub fn eye_position(&self) -> Vec3
Eye (camera) position in world space.
Sourcepub fn view_matrix(&self) -> Mat4
pub fn view_matrix(&self) -> Mat4
Right-handed view matrix (world -> camera space).
Sourcepub fn proj_matrix(&self) -> Mat4
pub fn proj_matrix(&self) -> Mat4
Right-handed projection matrix (wgpu depth 0..1).
In orthographic mode the viewing volume is derived from distance and
fov_y so that switching between perspective and orthographic at the
same distance produces a similar framing.
Sourcepub fn view_proj_matrix(&self) -> Mat4
pub fn view_proj_matrix(&self) -> Mat4
Combined view-projection matrix for use in the camera uniform buffer. Note: projection * view (column-major order: proj applied after view).
Sourcepub fn frustum(&self) -> Frustum
pub fn frustum(&self) -> Frustum
Extract the view frustum from the current view-projection matrix.
Sourcepub fn fit_sphere(&self, center: Vec3, radius: f32) -> (Vec3, f32)
pub fn fit_sphere(&self, center: Vec3, radius: f32) -> (Vec3, f32)
Compute (center, distance) that would frame a bounding sphere in view,
preserving the current orientation.
A 1.2× padding factor is applied so the object doesn’t touch the edges.
Sourcepub fn fit_aabb(&self, aabb: &Aabb) -> (Vec3, f32)
pub fn fit_aabb(&self, aabb: &Aabb) -> (Vec3, f32)
Compute (center, distance) that would frame an AABB in view,
preserving the current orientation.
Converts the AABB to a bounding sphere and delegates to fit_sphere.
Sourcepub fn center_on_domain(&mut self, nx: f32, ny: f32, nz: f32)
pub fn center_on_domain(&mut self, nx: f32, ny: f32, nz: f32)
Center the camera on a domain of the given extents, adjusting distance and clipping planes so the entire domain is visible.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Camera
impl RefUnwindSafe for Camera
impl Send for Camera
impl Sync for Camera
impl Unpin for Camera
impl UnsafeUnpin for Camera
impl UnwindSafe for Camera
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.