Support Warning
WebGPU is currently only supported on Chrome starting with version 113, and only on desktop. If they don't work on your configuration, you can check the WebGL2 examples
here.
Support for WebGPU in Bevy hasn't been released yet, this example has been compiled using the main branch.
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
window::{CursorGrabMode, PresentMode, WindowLevel},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "I am a window!".into(),
resolution: (500., 300.).into(),
present_mode: PresentMode::AutoVsync,
fit_canvas_to_parent: true,
prevent_default_event_handling: false,
..default()
}),
..default()
}))
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin)
.add_systems(
Update,
(
change_title,
toggle_cursor,
toggle_vsync,
cycle_cursor_icon,
switch_level,
),
)
.run();
}
fn toggle_vsync(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
if input.just_pressed(KeyCode::V) {
let mut window = windows.single_mut();
window.present_mode = if matches!(window.present_mode, PresentMode::AutoVsync) {
PresentMode::AutoNoVsync
} else {
PresentMode::AutoVsync
};
info!("PRESENT_MODE: {:?}", window.present_mode);
}
}
fn switch_level(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
if input.just_pressed(KeyCode::T) {
let mut window = windows.single_mut();
window.window_level = match window.window_level {
WindowLevel::AlwaysOnBottom => WindowLevel::Normal,
WindowLevel::Normal => WindowLevel::AlwaysOnTop,
WindowLevel::AlwaysOnTop => WindowLevel::AlwaysOnBottom,
};
info!("WINDOW_LEVEL: {:?}", window.window_level);
}
}
fn change_title(mut windows: Query<&mut Window>, time: Res<Time>) {
let mut window = windows.single_mut();
window.title = format!(
"Seconds since startup: {}",
time.elapsed().as_secs_f32().round()
);
}
fn toggle_cursor(mut windows: Query<&mut Window>, input: Res<Input<KeyCode>>) {
if input.just_pressed(KeyCode::Space) {
let mut window = windows.single_mut();
window.cursor.visible = !window.cursor.visible;
window.cursor.grab_mode = match window.cursor.grab_mode {
CursorGrabMode::None => CursorGrabMode::Locked,
CursorGrabMode::Locked | CursorGrabMode::Confined => CursorGrabMode::None,
};
}
}
fn cycle_cursor_icon(
mut windows: Query<&mut Window>,
input: Res<Input<MouseButton>>,
mut index: Local<usize>,
) {
let mut window = windows.single_mut();
const ICONS: &[CursorIcon] = &[
CursorIcon::Default,
CursorIcon::Hand,
CursorIcon::Wait,
CursorIcon::Text,
CursorIcon::Copy,
];
if input.just_pressed(MouseButton::Left) {
*index = (*index + 1) % ICONS.len();
} else if input.just_pressed(MouseButton::Right) {
*index = if *index == 0 {
ICONS.len() - 1
} else {
*index - 1
};
}
window.cursor.icon = ICONS[*index];
}