//! This example shows how to render 2D objects on top of Bevy UI, by using a second camera with a higher `order` than the UI camera. use bevy::{color::palettes::tailwind, prelude::*, render::view::RenderLayers}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems(Update, rotate_sprite) .run(); } fn setup(mut commands: Commands, asset_server: Res) { // The default camera. `IsDefaultUiCamera` makes this the default camera to render UI elements to. Alternatively, you can add the `UiTargetCamera` component to root UI nodes to define which camera they should be rendered to. commands.spawn((Camera2d, IsDefaultUiCamera)); // The second camera. The higher order means that this camera will be rendered after the first camera. We will render to this camera to draw on top of the UI. commands.spawn(( Camera2d, Camera { order: 1, // Don't draw anything in the background, to see the previous camera. clear_color: ClearColorConfig::None, ..default() }, // This camera will only render entities which are on the same render layer. RenderLayers::layer(1), )); commands.spawn(( // We could also use a `UiTargetCamera` component here instead of the general `IsDefaultUiCamera`. Node { width: Val::Percent(100.), height: Val::Percent(100.), display: Display::Flex, justify_content: JustifyContent::Center, align_items: AlignItems::Center, ..default() }, BackgroundColor(tailwind::ROSE_400.into()), children![( Node { height: Val::Percent(30.), width: Val::Percent(20.), min_height: Val::Px(150.), min_width: Val::Px(150.), border: UiRect::all(Val::Px(2.)), ..default() }, BorderRadius::all(Val::Percent(25.0)), BorderColor::all(Color::WHITE), )], )); // This 2D object will be rendered on the second camera, on top of the default camera where the UI is rendered. commands.spawn(( Sprite { image: asset_server.load("textures/rpg/chars/sensei/sensei.png"), custom_size: Some(Vec2::new(100., 100.)), ..default() }, RenderLayers::layer(1), )); } fn rotate_sprite(time: Res