mirror of
https://github.com/bevyengine/bevy
synced 2024-11-23 05:03:47 +00:00
01aedc8431
# Objective Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands). ## Solution All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input: ```rust // before: commands .spawn() .insert((A, B, C)); world .spawn() .insert((A, B, C); // after commands.spawn((A, B, C)); world.spawn((A, B, C)); ``` All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api. By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`). This improves spawn performance by over 10%: ![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png) To take this measurement, I added a new `world_spawn` benchmark. Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main. **Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).** --- ## Changelog - All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input - All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api - World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior. ## Migration Guide ```rust // Old (0.8): commands .spawn() .insert_bundle((A, B, C)); // New (0.9) commands.spawn((A, B, C)); // Old (0.8): commands.spawn_bundle((A, B, C)); // New (0.9) commands.spawn((A, B, C)); // Old (0.8): let entity = commands.spawn().id(); // New (0.9) let entity = commands.spawn_empty().id(); // Old (0.8) let entity = world.spawn().id(); // New (0.9) let entity = world.spawn_empty(); ```
166 lines
4.6 KiB
Rust
166 lines
4.6 KiB
Rust
//! A test to confirm that `bevy` allows setting the window to arbitrary small sizes
|
|
//! This is run in CI to ensure that this doesn't regress again.
|
|
|
|
use bevy::{core_pipeline::clear_color::ClearColorConfig, prelude::*};
|
|
|
|
// The smallest size reached is 1x1, as X11 doesn't support windows with a 0 dimension
|
|
// TODO: Add a check for platforms other than X11 for 0xk and kx0, despite those currently unsupported on CI.
|
|
const MAX_WIDTH: u16 = 401;
|
|
const MAX_HEIGHT: u16 = 401;
|
|
const MIN_WIDTH: u16 = 1;
|
|
const MIN_HEIGHT: u16 = 1;
|
|
const RESIZE_STEP: u16 = 4;
|
|
|
|
#[derive(Resource)]
|
|
struct Dimensions {
|
|
width: u16,
|
|
height: u16,
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(WindowDescriptor {
|
|
width: MAX_WIDTH.try_into().unwrap(),
|
|
height: MAX_HEIGHT.try_into().unwrap(),
|
|
scale_factor_override: Some(1.),
|
|
title: "Resizing".into(),
|
|
..Default::default()
|
|
})
|
|
.insert_resource(Dimensions {
|
|
width: MAX_WIDTH,
|
|
height: MAX_HEIGHT,
|
|
})
|
|
.add_plugins(DefaultPlugins)
|
|
.insert_resource(Phase::ContractingY)
|
|
.add_system(change_window_size)
|
|
.add_system(sync_dimensions)
|
|
.add_system(bevy::window::close_on_esc)
|
|
.add_startup_system(setup_3d)
|
|
.add_startup_system(setup_2d)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
enum Phase {
|
|
ContractingY,
|
|
ContractingX,
|
|
ExpandingY,
|
|
ExpandingX,
|
|
}
|
|
|
|
use Phase::*;
|
|
|
|
fn change_window_size(
|
|
mut windows: ResMut<Dimensions>,
|
|
mut phase: ResMut<Phase>,
|
|
mut first_complete: Local<bool>,
|
|
) {
|
|
// Put off rendering for one frame, as currently for a frame where
|
|
// resizing happens, nothing is presented.
|
|
// TODO: Debug and fix this if feasible
|
|
if !*first_complete {
|
|
*first_complete = true;
|
|
return;
|
|
}
|
|
let height = windows.height;
|
|
let width = windows.width;
|
|
match *phase {
|
|
Phase::ContractingY => {
|
|
if height <= MIN_HEIGHT {
|
|
*phase = ContractingX;
|
|
} else {
|
|
windows.height -= RESIZE_STEP;
|
|
}
|
|
}
|
|
Phase::ContractingX => {
|
|
if width <= MIN_WIDTH {
|
|
*phase = ExpandingY;
|
|
} else {
|
|
windows.width -= RESIZE_STEP;
|
|
}
|
|
}
|
|
Phase::ExpandingY => {
|
|
if height >= MAX_HEIGHT {
|
|
*phase = ExpandingX;
|
|
} else {
|
|
windows.height += RESIZE_STEP;
|
|
}
|
|
}
|
|
Phase::ExpandingX => {
|
|
if width >= MAX_WIDTH {
|
|
*phase = ContractingY;
|
|
} else {
|
|
windows.width += RESIZE_STEP;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn sync_dimensions(dim: Res<Dimensions>, mut windows: ResMut<Windows>) {
|
|
if dim.is_changed() {
|
|
windows.get_primary_mut().unwrap().set_resolution(
|
|
dim.width.try_into().unwrap(),
|
|
dim.height.try_into().unwrap(),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// A simple 3d scene, taken from the `3d_scene` example
|
|
fn setup_3d(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
// plane
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
|
|
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
|
|
..default()
|
|
});
|
|
// cube
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
..default()
|
|
});
|
|
// light
|
|
commands.spawn(PointLightBundle {
|
|
point_light: PointLight {
|
|
intensity: 1500.0,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
..default()
|
|
});
|
|
// camera
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
/// A simple 2d scene, taken from the `rect` example
|
|
fn setup_2d(mut commands: Commands) {
|
|
commands.spawn(Camera2dBundle {
|
|
camera: Camera {
|
|
// render the 2d camera after the 3d camera
|
|
priority: 1,
|
|
..default()
|
|
},
|
|
camera_2d: Camera2d {
|
|
// do not use a clear color
|
|
clear_color: ClearColorConfig::None,
|
|
},
|
|
..default()
|
|
});
|
|
commands.spawn(SpriteBundle {
|
|
sprite: Sprite {
|
|
color: Color::rgb(0.25, 0.25, 0.75),
|
|
custom_size: Some(Vec2::new(50.0, 50.0)),
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
}
|