Skip to main content

Camera

Struct Camera 

Source
pub struct Camera {
    pub position: Vec2,
    pub zoom: f32,
    pub rotation: f32,
    pub viewport: Option<(u32, u32)>,
    pub layer_parallax: FxHashMap<i32, Vec2>,
    pub target: Option<RenderTargetId>,
}
Expand description

camera resource, affects how the render queue is projected.

when no camera resource exists, rendering uses world-space anchored at origin. when present, the orthographic projection is offset and scaled accordingly.

§example

use lunar_render::Camera;
use lunar_math::Vec2;

// camera centered at (400, 300), letterboxed to an 800x600 viewport
let cam = Camera {
    position: Vec2::new(400.0, 300.0),
    zoom: 1.0,
    rotation: 0.0,
    viewport: Some((800, 600)),
    layer_parallax: Default::default(),
    target: None,
};

// use cam.projection_matrix(window_w, window_h) for the render projection

Fields§

§position: Vec2

camera position in world space

§zoom: f32

zoom level (1.0 = 1:1, 2.0 = 2x zoom)

§rotation: f32

rotation in radians

§viewport: Option<(u32, u32)>

viewport size in pixels (None = full window)

§layer_parallax: FxHashMap<i32, Vec2>

per-layer offset for parallax scrolling (layer id → world offset)

§target: Option<RenderTargetId>

render target to draw into (None = swapchain / window)

Implementations§

Source§

impl Camera

Source

pub fn new() -> Self

create a new camera at the origin with default settings

Source

pub fn at_position(x: f32, y: f32) -> Self

create a camera at the given position

Source

pub fn projection_matrix( &self, window_width: u32, window_height: u32, ) -> [f32; 16]

compute the orthographic projection matrix incorporating camera transforms. returns a 4x4 column-major matrix as a flat array of 16 f32s. for per-layer parallax, use Camera::projection_matrix_for_layer instead.

§example
use lunar_render::Camera;

let cam = Camera::default();
let proj = cam.projection_matrix(800, 600);
// proj is a [f32; 16] suitable for wgpu uniform upload
Source

pub fn projection_matrix_for_layer( &self, layer: i32, window_width: u32, window_height: u32, ) -> [f32; 16]

compute the orthographic projection matrix with a per-layer parallax offset. the layer’s parallax offset is subtracted from the camera position before computing the transform, so layers can scroll at different speeds. set per-layer offsets via Camera::set_layer_parallax.

Source

pub fn set_layer_parallax(&mut self, layer: i32, offset: Vec2)

set a parallax offset for a specific layer. the offset is a world-space Vec2 subtracted from the camera position when rendering that layer. to scroll a background at half speed, pass camera.position * 0.5 as the offset each frame.

Source

pub fn clear_layer_parallax(&mut self, layer: i32)

remove the parallax offset for a layer, reverting to normal camera tracking.

Source

pub fn screen_to_world( &self, screen: Vec2, window_width: u32, window_height: u32, ) -> Vec2

convert a screen-space pixel position to world-space coordinates.

accounts for camera position, zoom, rotation, and viewport letterboxing. screen origin is top-left, y-down. world is top-left, y-down.

§example
fn my_system(camera: Res<Camera>, input: Res<InputState>) {
    let (mx, my) = input.mouse_position();
    let world = camera.screen_to_world(Vec2::new(mx, my), 800, 600);
    // spawn something at the mouse position in world space
}
Source

pub fn world_to_screen( &self, world: Vec2, window_width: u32, window_height: u32, ) -> Vec2

convert a world-space position to screen-space pixel coordinates.

inverse of screen_to_world. the result is in screen pixel coordinates (top-left origin, y-down).

Source

pub fn set_target_aspect(&mut self, width: u32, height: u32) -> &mut Self

enable or disable viewport letterboxing.

when enabled, the projection auto-computes black-bar offsets when the window aspect ratio doesn’t match the viewport aspect ratio. this is called internally by set_target_aspect.

returns &mut Self for chaining.

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 Resource for Camera
where Self: Send + Sync + 'static,

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> ConditionalSend for T
where T: Send,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

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> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. 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<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> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of this value.
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,