2D cameras
Set up Camera2d: orthographic projection and scaling modes, zoom, following a player, cursor to world coordinates, pixel-perfect rendering and 2D bloom.
On this page
Camera2d is an orthographic camera set up for 2D: it looks down the -Z axis, draws sprites, 2D meshes and Text2d, and sees everything up to 1000 units in front of or behind it on the Z axis. Spawning it adds the Camera, Projection, Transform and other components it needs.
commands.spawn(Camera2d);Everything that applies to cameras in general, such as viewports, render order, render layers and clear colours, is covered in Cameras. This page covers the 2D specifics.
Projection and scaling#
A 2D camera's Projection is Projection::Orthographic(OrthographicProjection). Its scaling_mode decides how many world units fit on screen:
ScalingMode | What you see |
|---|---|
WindowSize | The default. One world unit is one logical pixel, so a bigger window shows more of the world. |
FixedVertical { viewport_height } | Always viewport_height units tall; the width follows the aspect ratio. Good for games that should look the same on any screen. |
FixedHorizontal { viewport_width } | Always viewport_width units wide. |
AutoMin { min_width, min_height } | At least this much of the world, keeping the aspect ratio. |
AutoMax { max_width, max_height } | At most this much of the world, keeping the aspect ratio. |
Fixed { width, height } | Exactly this area, stretched to the window. |
use neo::{camera::ScalingMode, prelude::*};
commands.spawn((
Camera2d,
Projection::Orthographic(OrthographicProjection {
scaling_mode: ScalingMode::FixedVertical { viewport_height: 720.0 },
..OrthographicProjection::default_2d()
}),
));Start from OrthographicProjection::default_2d(), which keeps the 2D near plane, rather than default().
Zoom#
Change the projection's scale: below 1.0 zooms in, above 1.0 zooms out.
fn zoom(mut projection: Single<&mut Projection, With<Camera2d>>, keys: Res<ButtonInput<KeyCode>>) {
if let Projection::Orthographic(ortho) = &mut **projection {
if keys.pressed(KeyCode::Equal) { ortho.scale *= 0.98; }
if keys.pressed(KeyCode::Minus) { ortho.scale *= 1.02; }
}
}Follow a player#
Move the camera's Transform. Smoothing the motion keeps it from feeling stiff:
fn follow(
time: Res<Time>,
player: Single<&Transform, (With<Player>, Without<Camera2d>)>,
mut camera: Single<&mut Transform, With<Camera2d>>,
) {
let target = player.translation.truncate().extend(camera.translation.z);
camera.translation.smooth_nudge(&target, 4.0, time.delta_secs());
}For game cameras with blends, shake and priorities, use NEO's camera director; see Camera.
Cursor to world#
Camera::viewport_to_world_2d turns a position in the window into a world position, which is how you click on things or aim at the cursor:
fn cursor_world(
camera: Single<(&Camera, &GlobalTransform)>,
window: Single<&Window>,
mut gizmos: Gizmos,
) {
let (camera, camera_transform) = *camera;
if let Some(cursor) = window.cursor_position()
&& let Ok(world) = camera.viewport_to_world_2d(camera_transform, cursor)
{
gizmos.circle_2d(world, 10.0, Color::WHITE);
}
}Camera::world_to_viewport goes the other way. For clicking sprites and meshes, picking does the hit testing for you.
Pixel-perfect rendering#
To make low-resolution art snap to its pixel grid even while things rotate, render the game to a small image and show that image scaled up:
- Create an image with
Image::new_target_texture(width, height, TextureFormat::Bgra8UnormSrgb, None)and add it toAssets<Image>. - Spawn a
Camera2dwithRenderTarget::Image(handle.clone().into()),Camera { order: -1, .. },Msaa::Offand aRenderLayersfor the game world. - Spawn a
Sprite::from_image(handle)on a second layer and a secondCamera2dwithMsaa::Offthat only sees that layer. - Scale the outer camera's projection so the canvas fills the window in whole-number steps.
Also set ImagePlugin::default_nearest() so textures aren't filtered. The pixel_grid_snap example does all of this.
Bloom#
Bloom makes bright pixels glow. In 2D, add it to the camera and give sprites or materials colours above 1.0:
use neo::{
core_pipeline::tonemapping::{DebandDither, Tonemapping},
post_process::bloom::Bloom,
prelude::*,
};
commands.spawn((
Camera2d,
Camera { clear_color: ClearColorConfig::Custom(Color::BLACK), ..default() },
Tonemapping::TonyMcMapface,
Bloom::default(),
DebandDither::Enabled,
));
commands.spawn(Sprite {
image: asset_server.load("branding/icon.png"),
color: Color::srgb(5.0, 5.0, 5.0),
..default()
});Bloom works in the browser too. Its settings are covered in Post-processing.
Examples#
| Example | What it shows |
|---|---|
2d_viewport_to_world | Cursor to world with a moving, zooming camera and a custom viewport. |
rotate_to_cursor | Turning a sprite to face the cursor. |
pixel_grid_snap | Pixel-perfect rendering through a low-resolution image. |
bloom_2d | Bloom on sprites and meshes. |
projection_zoom | Orthographic and perspective zoom. |
Run one from the engine core's workspace: cargo run --example pixel_grid_snap.