pub struct Camera {
pub transform: CamTransform,
}Expand description
A 3D camera with perspective or orthographic projection.
Wraps a CamTransform that holds position, rotation, FOV, clip distances,
and pre-computed view/projection matrices. After mutating any transform
property, call pre_update to recalculate.
§Conventions
- Y-up world coordinate system.
- -Z forward — the camera looks down its local -Z axis by default.
- Euler angles in degrees, applied in XYZ order.
§Movement methods
| Direction | Method | Axis |
|---|---|---|
| Forward (in look direction) | fly_forw | Camera-local -Z |
| Backward | fly_back | Camera-local +Z |
| Left (strafe) | fly_left | Camera-local -X |
| Right (strafe) | fly_right | Camera-local +X |
| Up | fly_up | World +Y |
| Down | fly_down | World -Y |
§Rotation methods
| Method | Effect | Common name |
|---|---|---|
spin_x | Pitch (look up/down) | Tilt forward/backward |
spin_y | Yaw (look left/right) | Turn head side-to-side |
spin_z | Roll (rotate view) | Tilt horizon |
§Getters and setters
| Property | Getter | Setter |
|---|---|---|
| Field of view (degrees) | fov | set_fov, add_fov |
| Orthographic scale | ortho_scale | set_ortho_scale, add_ortho_scale |
| Projection mode | proj | set_proj |
| Clip distances | clip | set_clip, set_clip_near, set_clip_far |
| Viewport size | — | set_size |
| View/proj matrices | — | pre_update |
§Example
use optic_core::{CamProj, Size2D};
use optic_render::Camera;
let mut cam = Camera::new(Size2D::from(1920, 1080), CamProj::Persp);
cam.fly_forw(10.0); // move in look direction
cam.spin_y(-90.0); // yaw left 90°
cam.pre_update(); // recalculate matricesFields§
§transform: CamTransformImplementations§
Source§impl Camera
impl Camera
Sourcepub fn match_canvas_size(canvas: &Canvas, proj: CamProj) -> Self
pub fn match_canvas_size(canvas: &Canvas, proj: CamProj) -> Self
Creates a camera sized to match the given canvas dimensions.
Sourcepub fn new(size: Size2D, proj: CamProj) -> Self
pub fn new(size: Size2D, proj: CamProj) -> Self
Creates a camera at (0, 0, 5) with 75° FOV, default clip distances,
and the given projection type.
Sourcepub fn pre_update(&mut self)
pub fn pre_update(&mut self)
Recalculates the view and projection matrices from the current transform state.
Call this once per frame after all movement (fly_forw, etc.)
and rotation (spin_y, etc.) have been applied.
The matrices are consumed by the rendering pipeline — without this call
the camera will continue using stale matrices from the previous frame.
Sourcepub fn ortho_scale(&self) -> f32
pub fn ortho_scale(&self) -> f32
Returns the orthographic scale factor.
Sourcepub fn set_clip_near(&mut self, near: f32)
pub fn set_clip_near(&mut self, near: f32)
Sets the near clip plane distance.
Sourcepub fn set_clip_far(&mut self, far: f32)
pub fn set_clip_far(&mut self, far: f32)
Sets the far clip plane distance.
Sourcepub fn set_size(&mut self, size: Size2D)
pub fn set_size(&mut self, size: Size2D)
Sets the canvas/viewport size (used for aspect ratio calculation).
Sourcepub fn set_proj(&mut self, proj: CamProj)
pub fn set_proj(&mut self, proj: CamProj)
Switches between perspective and orthographic projection.
Sourcepub fn set_fov(&mut self, fov: f32)
pub fn set_fov(&mut self, fov: f32)
Sets the vertical field of view in degrees (clamped to ≥ 0.01).
Sourcepub fn set_ortho_scale(&mut self, value: f32)
pub fn set_ortho_scale(&mut self, value: f32)
Sets the orthographic scale factor.
Sourcepub fn add_ortho_scale(&mut self, value: f32)
pub fn add_ortho_scale(&mut self, value: f32)
Adds value to the orthographic scale factor.
Sourcepub fn fly_forw(&mut self, speed: f32)
pub fn fly_forw(&mut self, speed: f32)
Moves the camera forward (in the direction it faces).
The forward direction is derived from the camera’s current rotation
(stored as front in CamTransform). For the default orientation this
moves along world -Z.
§When to use
Call this each frame with speed * delta_time for smooth first-person
movement. Call pre_update after all movement and
rotation for the frame.
Sourcepub fn fly_back(&mut self, speed: f32)
pub fn fly_back(&mut self, speed: f32)
Moves the camera backward (opposite the direction it faces).
The inverse of fly_forw. Equivalent to
fly_forw(-speed).
Sourcepub fn fly_left(&mut self, speed: f32)
pub fn fly_left(&mut self, speed: f32)
Moves the camera left (strafe), perpendicular to the forward direction.
The strafe direction is computed as front × world_up, producing a
vector orthogonal to both the look direction and the world Y axis. This
keeps the horizon level even when pitching up or down.
Sourcepub fn fly_right(&mut self, speed: f32)
pub fn fly_right(&mut self, speed: f32)
Moves the camera right (strafe), perpendicular to the forward direction.
The inverse of fly_left. Equivalent to
fly_left(-speed).
Sourcepub fn fly_down(&mut self, speed: f32)
pub fn fly_down(&mut self, speed: f32)
Moves the camera straight down (world Y axis).
The inverse of fly_up. Equivalent to
fly_up(-speed).
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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 more