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 projectionFields§
§position: Vec2camera position in world space
zoom: f32zoom level (1.0 = 1:1, 2.0 = 2x zoom)
rotation: f32rotation 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
impl Camera
Sourcepub fn at_position(x: f32, y: f32) -> Self
pub fn at_position(x: f32, y: f32) -> Self
create a camera at the given position
Sourcepub fn projection_matrix(
&self,
window_width: u32,
window_height: u32,
) -> [f32; 16]
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 uploadSourcepub fn projection_matrix_for_layer(
&self,
layer: i32,
window_width: u32,
window_height: u32,
) -> [f32; 16]
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.
Sourcepub fn set_layer_parallax(&mut self, layer: i32, offset: Vec2)
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.
Sourcepub fn clear_layer_parallax(&mut self, layer: i32)
pub fn clear_layer_parallax(&mut self, layer: i32)
remove the parallax offset for a layer, reverting to normal camera tracking.
Sourcepub fn screen_to_world(
&self,
screen: Vec2,
window_width: u32,
window_height: u32,
) -> Vec2
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
}Sourcepub fn world_to_screen(
&self,
world: Vec2,
window_width: u32,
window_height: u32,
) -> Vec2
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).
Sourcepub fn set_target_aspect(&mut self, width: u32, height: u32) -> &mut Self
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§
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConditionalSend for Twhere
T: Send,
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> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self using default().
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