mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +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(); ```
160 lines
4.7 KiB
Rust
160 lines
4.7 KiB
Rust
//! Shows how to render to a texture. Useful for mirrors, UI, or exporting images.
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
use bevy::{
|
|
core_pipeline::clear_color::ClearColorConfig,
|
|
prelude::*,
|
|
render::{
|
|
camera::RenderTarget,
|
|
render_resource::{
|
|
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
|
|
},
|
|
view::RenderLayers,
|
|
},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_startup_system(setup)
|
|
.add_system(cube_rotator_system)
|
|
.add_system(rotator_system)
|
|
.run();
|
|
}
|
|
|
|
// Marks the first pass cube (rendered to a texture.)
|
|
#[derive(Component)]
|
|
struct FirstPassCube;
|
|
|
|
// Marks the main pass cube, to which the texture is applied.
|
|
#[derive(Component)]
|
|
struct MainPassCube;
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
mut images: ResMut<Assets<Image>>,
|
|
) {
|
|
let size = Extent3d {
|
|
width: 512,
|
|
height: 512,
|
|
..default()
|
|
};
|
|
|
|
// This is the texture that will be rendered to.
|
|
let mut image = Image {
|
|
texture_descriptor: TextureDescriptor {
|
|
label: None,
|
|
size,
|
|
dimension: TextureDimension::D2,
|
|
format: TextureFormat::Bgra8UnormSrgb,
|
|
mip_level_count: 1,
|
|
sample_count: 1,
|
|
usage: TextureUsages::TEXTURE_BINDING
|
|
| TextureUsages::COPY_DST
|
|
| TextureUsages::RENDER_ATTACHMENT,
|
|
},
|
|
..default()
|
|
};
|
|
|
|
// fill image.data with zeroes
|
|
image.resize(size);
|
|
|
|
let image_handle = images.add(image);
|
|
|
|
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 4.0 }));
|
|
let cube_material_handle = materials.add(StandardMaterial {
|
|
base_color: Color::rgb(0.8, 0.7, 0.6),
|
|
reflectance: 0.02,
|
|
unlit: false,
|
|
..default()
|
|
});
|
|
|
|
// This specifies the layer used for the first pass, which will be attached to the first pass camera and cube.
|
|
let first_pass_layer = RenderLayers::layer(1);
|
|
|
|
// The cube that will be rendered to the texture.
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: cube_handle,
|
|
material: cube_material_handle,
|
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
|
|
..default()
|
|
},
|
|
FirstPassCube,
|
|
first_pass_layer,
|
|
));
|
|
|
|
// Light
|
|
// NOTE: Currently lights are shared between passes - see https://github.com/bevyengine/bevy/issues/3462
|
|
commands.spawn(PointLightBundle {
|
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 10.0)),
|
|
..default()
|
|
});
|
|
|
|
commands.spawn((
|
|
Camera3dBundle {
|
|
camera_3d: Camera3d {
|
|
clear_color: ClearColorConfig::Custom(Color::WHITE),
|
|
..default()
|
|
},
|
|
camera: Camera {
|
|
// render before the "main pass" camera
|
|
priority: -1,
|
|
target: RenderTarget::Image(image_handle.clone()),
|
|
..default()
|
|
},
|
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0))
|
|
.looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
},
|
|
first_pass_layer,
|
|
));
|
|
|
|
let cube_size = 4.0;
|
|
let cube_handle = meshes.add(Mesh::from(shape::Box::new(cube_size, cube_size, cube_size)));
|
|
|
|
// This material has the texture that has been rendered.
|
|
let material_handle = materials.add(StandardMaterial {
|
|
base_color_texture: Some(image_handle),
|
|
reflectance: 0.02,
|
|
unlit: false,
|
|
..default()
|
|
});
|
|
|
|
// Main pass cube, with material containing the rendered first pass texture.
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: cube_handle,
|
|
material: material_handle,
|
|
transform: Transform::from_xyz(0.0, 0.0, 1.5)
|
|
.with_rotation(Quat::from_rotation_x(-PI / 5.0)),
|
|
..default()
|
|
},
|
|
MainPassCube,
|
|
));
|
|
|
|
// The main pass camera.
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(0.0, 0.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
/// Rotates the inner cube (first pass)
|
|
fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<FirstPassCube>>) {
|
|
for mut transform in &mut query {
|
|
transform.rotate_x(1.5 * time.delta_seconds());
|
|
transform.rotate_z(1.3 * time.delta_seconds());
|
|
}
|
|
}
|
|
|
|
/// Rotates the outer cube (main pass)
|
|
fn cube_rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<MainPassCube>>) {
|
|
for mut transform in &mut query {
|
|
transform.rotate_x(1.0 * time.delta_seconds());
|
|
transform.rotate_y(0.7 * time.delta_seconds());
|
|
}
|
|
}
|