
# Objective As discussed in #14275, Bevy is currently too prone to panic, and makes the easy / beginner-friendly way to do a large number of operations just to panic on failure. This is seriously frustrating in library code, but also slows down development, as many of the `Query::single` panics can actually safely be an early return (these panics are often due to a small ordering issue or a change in game state. More critically, in most "finished" products, panics are unacceptable: any unexpected failures should be handled elsewhere. That's where the new With the advent of good system error handling, we can now remove this. Note: I was instrumental in a) introducing this idea in the first place and b) pushing to make the panicking variant the default. The introduction of both `let else` statements in Rust and the fancy system error handling work in 0.16 have changed my mind on the right balance here. ## Solution 1. Make `Query::single` and `Query::single_mut` (and other random related methods) return a `Result`. 2. Handle all of Bevy's internal usage of these APIs. 3. Deprecate `Query::get_single` and friends, since we've moved their functionality to the nice names. 4. Add detailed advice on how to best handle these errors. Generally I like the diff here, although `get_single().unwrap()` in tests is a bit of a downgrade. ## Testing I've done a global search for `.single` to track down any missed deprecated usages. As to whether or not all the migrations were successful, that's what CI is for :) ## Future work ~~Rename `Query::get_single` and friends to `Query::single`!~~ ~~I've opted not to do this in this PR, and smear it across two releases in order to ease the migration. Successive deprecations are much easier to manage than the semantics and types shifting under your feet.~~ Cart has convinced me to change my mind on this; see https://github.com/bevyengine/bevy/pull/18082#discussion_r1974536085. ## Migration guide `Query::single`, `Query::single_mut` and their `QueryState` equivalents now return a `Result`. Generally, you'll want to: 1. Use Bevy 0.16's system error handling to return a `Result` using the `?` operator. 2. Use a `let else Ok(data)` block to early return if it's an expected failure. 3. Use `unwrap()` or `Ok` destructuring inside of tests. The old `Query::get_single` (etc) methods which did this have been deprecated.
94 lines
2.8 KiB
Rust
94 lines
2.8 KiB
Rust
//! Test rendering of many cameras and lights
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
use bevy::{
|
|
math::ops::{cos, sin},
|
|
prelude::*,
|
|
render::camera::Viewport,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, rotate_cameras)
|
|
.run();
|
|
}
|
|
|
|
const CAMERA_ROWS: usize = 4;
|
|
const CAMERA_COLS: usize = 4;
|
|
const NUM_LIGHTS: usize = 5;
|
|
|
|
/// set up a simple 3D scene
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
window: Query<&Window>,
|
|
) -> Result {
|
|
// circular base
|
|
commands.spawn((
|
|
Mesh3d(meshes.add(Circle::new(4.0))),
|
|
MeshMaterial3d(materials.add(Color::WHITE)),
|
|
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
|
|
));
|
|
|
|
// cube
|
|
commands.spawn((
|
|
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
|
|
MeshMaterial3d(materials.add(Color::WHITE)),
|
|
Transform::from_xyz(0.0, 0.5, 0.0),
|
|
));
|
|
|
|
// lights
|
|
for i in 0..NUM_LIGHTS {
|
|
let angle = (i as f32) / (NUM_LIGHTS as f32) * PI * 2.0;
|
|
commands.spawn((
|
|
PointLight {
|
|
color: Color::hsv(angle.to_degrees(), 1.0, 1.0),
|
|
intensity: 2_000_000.0 / NUM_LIGHTS as f32,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
Transform::from_xyz(sin(angle) * 4.0, 2.0, cos(angle) * 4.0),
|
|
));
|
|
}
|
|
|
|
// cameras
|
|
let window = window.single()?;
|
|
let width = window.resolution.width() / CAMERA_COLS as f32 * window.resolution.scale_factor();
|
|
let height = window.resolution.height() / CAMERA_ROWS as f32 * window.resolution.scale_factor();
|
|
let mut i = 0;
|
|
for y in 0..CAMERA_COLS {
|
|
for x in 0..CAMERA_ROWS {
|
|
let angle = i as f32 / (CAMERA_ROWS * CAMERA_COLS) as f32 * PI * 2.0;
|
|
commands.spawn((
|
|
Camera3d::default(),
|
|
Camera {
|
|
viewport: Some(Viewport {
|
|
physical_position: UVec2::new(
|
|
(x as f32 * width) as u32,
|
|
(y as f32 * height) as u32,
|
|
),
|
|
physical_size: UVec2::new(width as u32, height as u32),
|
|
..default()
|
|
}),
|
|
order: i,
|
|
..default()
|
|
},
|
|
Transform::from_xyz(sin(angle) * 4.0, 2.5, cos(angle) * 4.0)
|
|
.looking_at(Vec3::ZERO, Vec3::Y),
|
|
));
|
|
i += 1;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn rotate_cameras(time: Res<Time>, mut query: Query<&mut Transform, With<Camera>>) {
|
|
for mut transform in query.iter_mut() {
|
|
transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(time.delta_secs()));
|
|
}
|
|
}
|