mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
25bfa80e60
# 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.
86 lines
2.6 KiB
Rust
86 lines
2.6 KiB
Rust
//! Shows how to render simple primitive shapes with a single color.
|
|
//!
|
|
//! You can toggle wireframes with the space bar except on wasm. Wasm does not support
|
|
//! `POLYGON_MODE_LINE` on the gpu.
|
|
|
|
use bevy::prelude::*;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
use bevy::sprite::{Wireframe2dConfig, Wireframe2dPlugin};
|
|
|
|
fn main() {
|
|
let mut app = App::new();
|
|
app.add_plugins((
|
|
DefaultPlugins,
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
Wireframe2dPlugin,
|
|
))
|
|
.add_systems(Startup, setup);
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
app.add_systems(Update, toggle_wireframe);
|
|
app.run();
|
|
}
|
|
|
|
const X_EXTENT: f32 = 900.;
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
) {
|
|
commands.spawn(Camera2d);
|
|
|
|
let shapes = [
|
|
meshes.add(Circle::new(50.0)),
|
|
meshes.add(CircularSector::new(50.0, 1.0)),
|
|
meshes.add(CircularSegment::new(50.0, 1.25)),
|
|
meshes.add(Ellipse::new(25.0, 50.0)),
|
|
meshes.add(Annulus::new(25.0, 50.0)),
|
|
meshes.add(Capsule2d::new(25.0, 50.0)),
|
|
meshes.add(Rhombus::new(75.0, 100.0)),
|
|
meshes.add(Rectangle::new(50.0, 100.0)),
|
|
meshes.add(RegularPolygon::new(50.0, 6)),
|
|
meshes.add(Triangle2d::new(
|
|
Vec2::Y * 50.0,
|
|
Vec2::new(-50.0, -50.0),
|
|
Vec2::new(50.0, -50.0),
|
|
)),
|
|
];
|
|
let num_shapes = shapes.len();
|
|
|
|
for (i, shape) in shapes.into_iter().enumerate() {
|
|
// Distribute colors evenly across the rainbow.
|
|
let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
|
|
|
|
commands.spawn((
|
|
Mesh2d(shape),
|
|
MeshMaterial2d(materials.add(color)),
|
|
Transform::from_xyz(
|
|
// Distribute shapes from -X_EXTENT/2 to +X_EXTENT/2.
|
|
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
|
|
0.0,
|
|
0.0,
|
|
),
|
|
));
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
commands.spawn(
|
|
TextBundle::from_section("Press space to toggle wireframes", TextStyle::default())
|
|
.with_style(Style {
|
|
position_type: PositionType::Absolute,
|
|
top: Val::Px(12.0),
|
|
left: Val::Px(12.0),
|
|
..default()
|
|
}),
|
|
);
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn toggle_wireframe(
|
|
mut wireframe_config: ResMut<Wireframe2dConfig>,
|
|
keyboard: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
if keyboard.just_pressed(KeyCode::Space) {
|
|
wireframe_config.global = !wireframe_config.global;
|
|
}
|
|
}
|