mirror of
https://github.com/bevyengine/bevy
synced 2024-11-15 09:27:41 +00:00
54006b107b
# Objective A big step in the migration to required components: meshes and materials! ## Solution As per the [selected proposal](https://hackmd.io/@bevy/required_components/%2Fj9-PnF-2QKK0on1KQ29UWQ): - Deprecate `MaterialMesh2dBundle`, `MaterialMeshBundle`, and `PbrBundle`. - Add `Mesh2d` and `Mesh3d` components, which wrap a `Handle<Mesh>`. - Add `MeshMaterial2d<M: Material2d>` and `MeshMaterial3d<M: Material>`, which wrap a `Handle<M>`. - Meshes *without* a mesh material should be rendered with a default material. The existence of a material is determined by `HasMaterial2d`/`HasMaterial3d`, which is required by `MeshMaterial2d`/`MeshMaterial3d`. This gets around problems with the generics. Previously: ```rust commands.spawn(MaterialMesh2dBundle { mesh: meshes.add(Circle::new(100.0)).into(), material: materials.add(Color::srgb(7.5, 0.0, 7.5)), transform: Transform::from_translation(Vec3::new(-200., 0., 0.)), ..default() }); ``` Now: ```rust commands.spawn(( Mesh2d(meshes.add(Circle::new(100.0))), MeshMaterial2d(materials.add(Color::srgb(7.5, 0.0, 7.5))), Transform::from_translation(Vec3::new(-200., 0., 0.)), )); ``` If the mesh material is missing, previously nothing was rendered. Now, it renders a white default `ColorMaterial` in 2D and a `StandardMaterial` in 3D (this can be overridden). Below, only every other entity has a material: ![Näyttökuva 2024-09-29 181746](https://github.com/user-attachments/assets/5c8be029-d2fe-4b8c-ae89-17a72ff82c9a) ![Näyttökuva 2024-09-29 181918](https://github.com/user-attachments/assets/58adbc55-5a1e-4c7d-a2c7-ed456227b909) Why white? This is still open for discussion, but I think white makes sense for a *default* material, while *invalid* asset handles pointing to nothing should have something like a pink material to indicate that something is broken (I don't handle that in this PR yet). This is kind of a mix of Godot and Unity: Godot just renders a white material for non-existent materials, while Unity renders nothing when no materials exist, but renders pink for invalid materials. I can also change the default material to pink if that is preferable though. ## Testing I ran some 2D and 3D examples to test if anything changed visually. I have not tested all examples or features yet however. If anyone wants to test more extensively, it would be appreciated! ## Implementation Notes - The relationship between `bevy_render` and `bevy_pbr` is weird here. `bevy_render` needs `Mesh3d` for its own systems, but `bevy_pbr` has all of the material logic, and `bevy_render` doesn't depend on it. I feel like the two crates should be refactored in some way, but I think that's out of scope for this PR. - I didn't migrate meshlets to required components yet. That can probably be done in a follow-up, as this is already a huge PR. - It is becoming increasingly clear to me that we really, *really* want to disallow raw asset handles as components. They caused me a *ton* of headache here already, and it took me a long time to find every place that queried for them or inserted them directly on entities, since there were no compiler errors for it. If we don't remove the `Component` derive, I expect raw asset handles to be a *huge* footgun for users as we transition to wrapper components, especially as handles as components have been the norm so far. I personally consider this to be a blocker for 0.15: we need to migrate to wrapper components for asset handles everywhere, and remove the `Component` derive. Also see https://github.com/bevyengine/bevy/issues/14124. --- ## Migration Guide Asset handles for meshes and mesh materials must now be wrapped in the `Mesh2d` and `MeshMaterial2d` or `Mesh3d` and `MeshMaterial3d` components for 2D and 3D respectively. Raw handles as components no longer render meshes. Additionally, `MaterialMesh2dBundle`, `MaterialMeshBundle`, and `PbrBundle` have been deprecated. Instead, use the mesh and material components directly. Previously: ```rust commands.spawn(MaterialMesh2dBundle { mesh: meshes.add(Circle::new(100.0)).into(), material: materials.add(Color::srgb(7.5, 0.0, 7.5)), transform: Transform::from_translation(Vec3::new(-200., 0., 0.)), ..default() }); ``` Now: ```rust commands.spawn(( Mesh2d(meshes.add(Circle::new(100.0))), MeshMaterial2d(materials.add(Color::srgb(7.5, 0.0, 7.5))), Transform::from_translation(Vec3::new(-200., 0., 0.)), )); ``` If the mesh material is missing, a white default material is now used. Previously, nothing was rendered if the material was missing. The `WithMesh2d` and `WithMesh3d` query filter type aliases have also been removed. Simply use `With<Mesh2d>` or `With<Mesh3d>`. --------- Co-authored-by: Tim Blackbird <justthecooldude@gmail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
220 lines
8.4 KiB
Rust
220 lines
8.4 KiB
Rust
//! This example illustrates how to run a winit window in a reactive, low power mode.
|
|
//!
|
|
//! This is useful for making desktop applications, or any other program that doesn't need to be
|
|
//! running the event loop non-stop.
|
|
|
|
use bevy::{
|
|
prelude::*,
|
|
utils::Duration,
|
|
window::{PresentMode, RequestRedraw, WindowPlugin},
|
|
winit::{EventLoopProxyWrapper, WakeUp, WinitSettings},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
// Continuous rendering for games - bevy's default.
|
|
.insert_resource(WinitSettings::game())
|
|
// Power-saving reactive rendering for applications.
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
// You can also customize update behavior with the fields of [`WinitSettings`]
|
|
.insert_resource(WinitSettings {
|
|
focused_mode: bevy::winit::UpdateMode::Continuous,
|
|
unfocused_mode: bevy::winit::UpdateMode::reactive_low_power(Duration::from_millis(10)),
|
|
})
|
|
.insert_resource(ExampleMode::Game)
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
// Turn off vsync to maximize CPU/GPU usage
|
|
present_mode: PresentMode::AutoNoVsync,
|
|
..default()
|
|
}),
|
|
..default()
|
|
}))
|
|
.add_systems(Startup, test_setup::setup)
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
test_setup::cycle_modes,
|
|
test_setup::rotate_cube,
|
|
test_setup::update_text,
|
|
update_winit,
|
|
),
|
|
)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Resource, Debug)]
|
|
enum ExampleMode {
|
|
Game,
|
|
Application,
|
|
ApplicationWithRequestRedraw,
|
|
ApplicationWithWakeUp,
|
|
}
|
|
|
|
/// Update winit based on the current `ExampleMode`
|
|
fn update_winit(
|
|
mode: Res<ExampleMode>,
|
|
mut winit_config: ResMut<WinitSettings>,
|
|
event_loop_proxy: Res<EventLoopProxyWrapper<WakeUp>>,
|
|
mut redraw_request_events: EventWriter<RequestRedraw>,
|
|
) {
|
|
use ExampleMode::*;
|
|
*winit_config = match *mode {
|
|
Game => {
|
|
// In the default `WinitSettings::game()` mode:
|
|
// * When focused: the event loop runs as fast as possible
|
|
// * When not focused: the app will update when the window is directly interacted with
|
|
// (e.g. the mouse hovers over a visible part of the out of focus window), a
|
|
// [`RequestRedraw`] event is received, or one sixtieth of a second has passed
|
|
// without the app updating (60 Hz refresh rate max).
|
|
WinitSettings::game()
|
|
}
|
|
Application => {
|
|
// While in `WinitSettings::desktop_app()` mode:
|
|
// * When focused: the app will update any time a winit event (e.g. the window is
|
|
// moved/resized, the mouse moves, a button is pressed, etc.), a [`RequestRedraw`]
|
|
// event is received, or after 5 seconds if the app has not updated.
|
|
// * When not focused: the app will update when the window is directly interacted with
|
|
// (e.g. the mouse hovers over a visible part of the out of focus window), a
|
|
// [`RequestRedraw`] event is received, or one minute has passed without the app
|
|
// updating.
|
|
WinitSettings::desktop_app()
|
|
}
|
|
ApplicationWithRequestRedraw => {
|
|
// Sending a `RequestRedraw` event is useful when you want the app to update the next
|
|
// frame regardless of any user input. For example, your application might use
|
|
// `WinitSettings::desktop_app()` to reduce power use, but UI animations need to play even
|
|
// when there are no inputs, so you send redraw requests while the animation is playing.
|
|
// Note that in this example the RequestRedraw winit event will make the app run in the same
|
|
// way as continuous
|
|
redraw_request_events.send(RequestRedraw);
|
|
WinitSettings::desktop_app()
|
|
}
|
|
ApplicationWithWakeUp => {
|
|
// Sending a `WakeUp` event is useful when you want the app to update the next
|
|
// frame regardless of any user input. This can be used from outside Bevy, see example
|
|
// `window/custom_user_event.rs` for an example usage from outside.
|
|
// Note that in this example the Wakeup winit event will make the app run in the same
|
|
// way as continuous
|
|
let _ = event_loop_proxy.send_event(WakeUp);
|
|
WinitSettings::desktop_app()
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Everything in this module is for setting up and animating the scene, and is not important to the
|
|
/// demonstrated features.
|
|
pub(crate) mod test_setup {
|
|
use crate::ExampleMode;
|
|
use bevy::{
|
|
color::palettes::basic::{LIME, YELLOW},
|
|
prelude::*,
|
|
window::RequestRedraw,
|
|
};
|
|
|
|
/// Switch between update modes when the mouse is clicked.
|
|
pub(crate) fn cycle_modes(
|
|
mut mode: ResMut<ExampleMode>,
|
|
button_input: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
if button_input.just_pressed(KeyCode::Space) {
|
|
*mode = match *mode {
|
|
ExampleMode::Game => ExampleMode::Application,
|
|
ExampleMode::Application => ExampleMode::ApplicationWithRequestRedraw,
|
|
ExampleMode::ApplicationWithRequestRedraw => ExampleMode::ApplicationWithWakeUp,
|
|
ExampleMode::ApplicationWithWakeUp => ExampleMode::Game,
|
|
};
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub(crate) struct Rotator;
|
|
|
|
/// Rotate the cube to make it clear when the app is updating
|
|
pub(crate) fn rotate_cube(
|
|
time: Res<Time>,
|
|
mut cube_transform: Query<&mut Transform, With<Rotator>>,
|
|
) {
|
|
for mut transform in &mut cube_transform {
|
|
transform.rotate_x(time.delta_seconds());
|
|
transform.rotate_local_y(time.delta_seconds());
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub struct ModeText;
|
|
|
|
pub(crate) fn update_text(
|
|
mut frame: Local<usize>,
|
|
mode: Res<ExampleMode>,
|
|
mut query: Query<&mut Text, With<ModeText>>,
|
|
) {
|
|
*frame += 1;
|
|
let mode = match *mode {
|
|
ExampleMode::Game => "game(), continuous, default",
|
|
ExampleMode::Application => "desktop_app(), reactive",
|
|
ExampleMode::ApplicationWithRequestRedraw => {
|
|
"desktop_app(), reactive, RequestRedraw sent"
|
|
}
|
|
ExampleMode::ApplicationWithWakeUp => "desktop_app(), reactive, WakeUp sent",
|
|
};
|
|
let mut text = query.single_mut();
|
|
text.sections[1].value = mode.to_string();
|
|
text.sections[3].value = frame.to_string();
|
|
}
|
|
|
|
/// Set up a scene with a cube and some text
|
|
pub fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
mut event: EventWriter<RequestRedraw>,
|
|
) {
|
|
commands.spawn((
|
|
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
|
|
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
|
|
Rotator,
|
|
));
|
|
|
|
commands.spawn((
|
|
DirectionalLight::default(),
|
|
Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
));
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.0, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
event.send(RequestRedraw);
|
|
commands.spawn((
|
|
TextBundle::from_sections([
|
|
TextSection::new(
|
|
"Press space bar to cycle modes\n",
|
|
TextStyle { ..default() },
|
|
),
|
|
TextSection::from_style(TextStyle {
|
|
color: LIME.into(),
|
|
..default()
|
|
}),
|
|
TextSection::new(
|
|
"\nFrame: ",
|
|
TextStyle {
|
|
color: YELLOW.into(),
|
|
..default()
|
|
},
|
|
),
|
|
TextSection::from_style(TextStyle {
|
|
color: YELLOW.into(),
|
|
..default()
|
|
}),
|
|
])
|
|
.with_style(Style {
|
|
align_self: AlignSelf::FlexStart,
|
|
position_type: PositionType::Absolute,
|
|
top: Val::Px(12.0),
|
|
left: Val::Px(12.0),
|
|
..default()
|
|
}),
|
|
ModeText,
|
|
));
|
|
}
|
|
}
|