Skip to main content

Camera

Struct Camera 

Source
pub struct Camera {
    pub projection: Projection,
    pub center: Vec3,
    pub distance: f32,
    pub orientation: Quat,
    pub fov_y: f32,
    pub aspect: f32,
    pub znear: Option<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 deg orbit in any direction. All matrices are computed right-handed (wgpu NDC convention).

Fields§

§projection: Projection

Projection mode (perspective or orthographic).

§center: Vec3

Orbit target point in world space.

§distance: f32

Distance from center to eye (zoom).

§orientation: Quat

Camera orientation as a unit quaternion. eye = center + orientation * (Vec3::Z * distance). Identity = looking along -Z from +Z position (top view in a Z-up world).

§fov_y: f32

Vertical field of view in radians.

§aspect: f32

Viewport width / height ratio : updated each frame from viewport rect.

§znear: Option<f32>

Near clipping plane distance. None (the default) lets the library manage depth precision automatically. Set to Some(value) only when you need exact control over the near plane, e.g. to avoid clipping geometry that is very close to the camera.

§zfar: f32

Far clipping plane distance.

Implementations§

Source§

impl Camera

Source

pub const MIN_DISTANCE: f32 = 0.01

Minimum allowed camera distance (zoom limit).

Source

pub const MAX_DISTANCE: f32 = 1.0e6

Maximum allowed camera distance (zoom limit).

Source

pub fn center(&self) -> Vec3

Return the orbit target center in world space.

Forward-compatible accessor : equivalent to reading self.center.

Source

pub fn set_center(&mut self, center: Vec3)

Set the orbit target center in world space.

Source

pub fn distance(&self) -> f32

Return the camera distance (zoom).

Forward-compatible accessor : equivalent to reading self.distance.

Source

pub fn set_distance(&mut self, d: f32)

Set the camera distance, clamped to [MIN_DISTANCE, MAX_DISTANCE].

Source

pub fn orientation(&self) -> Quat

Return the camera orientation quaternion.

Forward-compatible accessor : equivalent to reading self.orientation.

Source

pub fn set_orientation(&mut self, q: Quat)

Set the camera orientation, normalizing the quaternion.

Source

pub fn set_fov_y(&mut self, fov_y: f32)

Set the vertical field of view in radians.

Source

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.

Source

pub fn set_clip_planes(&mut self, znear: f32, zfar: f32)

Set the near and far clipping plane distances.

Source

pub fn orbit(&mut self, yaw: f32, pitch: f32)

Orbit the camera by yaw and pitch radians.

Applies Quat::from_rotation_z(-yaw) * orientation * Quat::from_rotation_x(-pitch). The sign convention matches all examples: positive yaw rotates counter-clockwise when viewed from above (Z-up), positive pitch tilts up.

Source

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.

Source

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.

Source

pub fn zoom_by_factor(&mut self, factor: f32)

Zoom by multiplying the distance by factor, clamped to [MIN_DISTANCE, MAX_DISTANCE].

Source

pub fn zoom_by_delta(&mut self, delta: f32)

Zoom by adding delta to the distance, clamped to [MIN_DISTANCE, MAX_DISTANCE].

Source

pub fn frame_sphere(&mut self, center: Vec3, radius: f32)

Frame the camera to contain a bounding sphere, applying the result directly.

Source

pub fn frame_aabb(&mut self, aabb: &Aabb)

Frame the camera to contain an AABB, applying the result directly.

Source

pub fn fit_sphere_target(&self, center: Vec3, radius: f32) -> CameraTarget

Compute a CameraTarget that would frame a bounding sphere, preserving the current orientation.

Source

pub fn fit_aabb_target(&self, aabb: &Aabb) -> CameraTarget

Compute a CameraTarget that would frame an AABB, preserving the current orientation.

Source

pub fn eye_position(&self) -> Vec3

Eye (camera) position in world space.

Source

pub fn view_matrix(&self) -> Mat4

Right-handed view matrix (world -> camera space).

Source

pub fn effective_zfar(&self) -> f32

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. Effective far-plane distance : zfar floored to distance * 3 so the far plane always extends past the orbit centre regardless of zoom level.

Source

pub fn effective_znear(&self) -> f32

Effective near-plane distance.

Keeps the near/far ratio at most 10,000:1 regardless of how far the camera has zoomed out. At close range the value is znear unchanged. At large distances it grows automatically so that f32 depth precision does not collapse and cause objects behind opaque surfaces to bleed through. Consumers do not need to adjust znear themselves.

Source

pub fn proj_matrix(&self) -> Mat4

Returns the projection matrix for the current camera state.

Source

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).

Source

pub fn right(&self) -> Vec3

Camera right vector in world space (used for pan).

Source

pub fn up(&self) -> Vec3

Camera up vector in world space (used for pan).

Source

pub fn frustum(&self) -> Frustum

Extract the view frustum from the current view-projection matrix.

Source

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.2x padding factor is applied so the object doesn’t touch the edges.

Source

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.

Source

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§

Source§

impl Clone for Camera

Source§

fn clone(&self) -> Camera

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Camera

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Camera

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Camera

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts 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>

Converts 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)

Converts &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)

Converts &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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more