
# Objective Yet another PR for migrating stuff to required components. This time, cameras! ## Solution As per the [selected proposal](https://hackmd.io/tsYID4CGRiWxzsgawzxG_g#Combined-Proposal-1-Selected), deprecate `Camera2dBundle` and `Camera3dBundle` in favor of `Camera2d` and `Camera3d`. Adding a `Camera` without `Camera2d` or `Camera3d` now logs a warning, as suggested by Cart [on Discord](https://discord.com/channels/691052431525675048/1264881140007702558/1291506402832945273). I would personally like cameras to work a bit differently and be split into a few more components, to avoid some footguns and confusing semantics, but that is more controversial, and shouldn't block this core migration. ## Testing I ran a few 2D and 3D examples, and tried cameras with and without render graphs. --- ## Migration Guide `Camera2dBundle` and `Camera3dBundle` have been deprecated in favor of `Camera2d` and `Camera3d`. Inserting them will now also insert the other components required by them automatically.
39 lines
991 B
Rust
39 lines
991 B
Rust
//! This example demonstrates how to use the `Camera::viewport_to_world_2d` method.
|
|
|
|
use bevy::{color::palettes::basic::WHITE, math::Isometry2d, prelude::*};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, draw_cursor)
|
|
.run();
|
|
}
|
|
|
|
fn draw_cursor(
|
|
camera_query: Query<(&Camera, &GlobalTransform)>,
|
|
windows: Query<&Window>,
|
|
mut gizmos: Gizmos,
|
|
) {
|
|
let (camera, camera_transform) = camera_query.single();
|
|
|
|
let Ok(window) = windows.get_single() else {
|
|
return;
|
|
};
|
|
|
|
let Some(cursor_position) = window.cursor_position() else {
|
|
return;
|
|
};
|
|
|
|
// Calculate a world position based on the cursor's position.
|
|
let Ok(point) = camera.viewport_to_world_2d(camera_transform, cursor_position) else {
|
|
return;
|
|
};
|
|
|
|
gizmos.circle_2d(Isometry2d::from_translation(point), 10., WHITE);
|
|
}
|
|
|
|
fn setup(mut commands: Commands) {
|
|
commands.spawn(Camera2d);
|
|
}
|