Skip to content

Commit 25b62f9

Browse files
committed
Port bevy_ui to pipelined-rendering (#2653)
# Objective Port bevy_ui to pipelined-rendering (see #2535 ) ## Solution I did some changes during the port: - [X] separate color from the texture asset (as suggested [here](https://discord.com/channels/691052431525675048/743663924229963868/874353914525413406)) - [X] ~give the vertex shader a per-instance buffer instead of per-vertex buffer~ (incompatible with batching) Remaining features to implement to reach parity with the old renderer: - [x] textures - [X] TextBundle I'd also like to add these features, but they need some design discussion: - [x] batching - [ ] separate opaque and transparent phases - [ ] multiple windows - [ ] texture atlases - [ ] (maybe) clipping
1 parent 58474d7 commit 25b62f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3809
-13
lines changed

Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ default = [
2525
"bevy_sprite2",
2626
"bevy_render2",
2727
"bevy_pbr2",
28+
"bevy_ui2",
29+
"bevy_text2",
2830
"bevy_winit",
2931
"render",
3032
"png",
@@ -59,6 +61,8 @@ bevy_render2 = ["bevy_internal/bevy_render2"]
5961
bevy_sprite2 = ["bevy_internal/bevy_sprite2"]
6062
bevy_pbr2 = ["bevy_internal/bevy_pbr2"]
6163
bevy_gltf2 = ["bevy_internal/bevy_gltf2"]
64+
bevy_ui2 = ["bevy_internal/bevy_ui2"]
65+
bevy_text2 = ["bevy_internal/bevy_text2"]
6266

6367
trace_chrome = ["bevy_internal/trace_chrome"]
6468
trace_tracy = ["bevy_internal/trace_tracy"]
@@ -140,6 +144,10 @@ path = "examples/2d/sprite_sheet.rs"
140144
name = "text2d"
141145
path = "examples/2d/text2d.rs"
142146

147+
[[example]]
148+
name = "text2d_pipelined"
149+
path = "examples/2d/text2d_pipelined.rs"
150+
143151
[[example]]
144152
name = "texture_atlas"
145153
path = "examples/2d/texture_atlas.rs"
@@ -506,6 +514,10 @@ path = "examples/ui/text_debug.rs"
506514
name = "ui"
507515
path = "examples/ui/ui.rs"
508516

517+
[[example]]
518+
name = "ui_pipelined"
519+
path = "examples/ui/ui_pipelined.rs"
520+
509521
# Window
510522
[[example]]
511523
name = "clear_color"

crates/bevy_internal/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ wayland = ["bevy_winit/wayland"]
3939
x11 = ["bevy_winit/x11"]
4040

4141
# enable rendering of font glyphs using subpixel accuracy
42-
subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"]
42+
subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas", "bevy_text2/subpixel_glyph_atlas"]
4343

4444
# enable systems that allow for automated testing on CI
4545
bevy_ci_testing = ["bevy_app/bevy_ci_testing"]
@@ -74,7 +74,9 @@ bevy_dynamic_plugin = { path = "../bevy_dynamic_plugin", optional = true, versio
7474
bevy_sprite = { path = "../bevy_sprite", optional = true, version = "0.5.0" }
7575
bevy_sprite2 = { path = "../../pipelined/bevy_sprite2", optional = true, version = "0.5.0" }
7676
bevy_text = { path = "../bevy_text", optional = true, version = "0.5.0" }
77+
bevy_text2 = { path = "../../pipelined/bevy_text2", optional = true, version = "0.5.0" }
7778
bevy_ui = { path = "../bevy_ui", optional = true, version = "0.5.0" }
79+
bevy_ui2 = { path = "../../pipelined/bevy_ui2", optional = true, version = "0.5.0" }
7880
bevy_wgpu = { path = "../bevy_wgpu", optional = true, version = "0.5.0" }
7981
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.5.0" }
8082
bevy_gilrs = { path = "../bevy_gilrs", optional = true, version = "0.5.0" }

crates/bevy_internal/src/default_plugins.rs

+6
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ impl PluginGroup for PipelinedDefaultPlugins {
138138
#[cfg(feature = "bevy_sprite2")]
139139
group.add(bevy_sprite2::SpritePlugin::default());
140140

141+
#[cfg(feature = "bevy_text2")]
142+
group.add(bevy_text2::TextPlugin::default());
143+
144+
#[cfg(feature = "bevy_ui2")]
145+
group.add(bevy_ui2::UiPlugin::default());
146+
141147
#[cfg(feature = "bevy_pbr2")]
142148
group.add(bevy_pbr2::PbrPlugin::default());
143149

crates/bevy_internal/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,24 @@ pub mod text {
147147
pub use bevy_text::*;
148148
}
149149

150+
#[cfg(feature = "bevy_text2")]
151+
pub mod text2 {
152+
//! Text drawing, styling, and font assets.
153+
pub use bevy_text2::*;
154+
}
155+
150156
#[cfg(feature = "bevy_ui")]
151157
pub mod ui {
152158
//! User interface components and widgets.
153159
pub use bevy_ui::*;
154160
}
155161

162+
#[cfg(feature = "bevy_ui2")]
163+
pub mod ui2 {
164+
//! User interface components and widgets.
165+
pub use bevy_ui2::*;
166+
}
167+
156168
#[cfg(feature = "bevy_winit")]
157169
pub mod winit {
158170
pub use bevy_winit::*;

examples/2d/text2d_pipelined.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use bevy::{
2+
core::Time,
3+
math::{Quat, Vec3},
4+
prelude::{App, AssetServer, Commands, Component, Query, Res, Transform, With},
5+
render2::{camera::OrthographicCameraBundle, color::Color},
6+
text2::{HorizontalAlign, Text, Text2dBundle, TextAlignment, TextStyle, VerticalAlign},
7+
PipelinedDefaultPlugins,
8+
};
9+
10+
fn main() {
11+
App::new()
12+
.add_plugins(PipelinedDefaultPlugins)
13+
.add_startup_system(setup)
14+
.add_system(animate_translation)
15+
.add_system(animate_rotation)
16+
.add_system(animate_scale)
17+
.run();
18+
}
19+
20+
#[derive(Component)]
21+
struct AnimateTranslation;
22+
#[derive(Component)]
23+
struct AnimateRotation;
24+
#[derive(Component)]
25+
struct AnimateScale;
26+
27+
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
28+
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
29+
let text_style = TextStyle {
30+
font,
31+
font_size: 60.0,
32+
color: Color::WHITE,
33+
};
34+
let text_alignment = TextAlignment {
35+
vertical: VerticalAlign::Center,
36+
horizontal: HorizontalAlign::Center,
37+
};
38+
// 2d camera
39+
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
40+
// Demonstrate changing translation
41+
commands
42+
.spawn_bundle(Text2dBundle {
43+
text: Text::with_section("translation", text_style.clone(), text_alignment),
44+
..Default::default()
45+
})
46+
.insert(AnimateTranslation);
47+
// Demonstrate changing rotation
48+
commands
49+
.spawn_bundle(Text2dBundle {
50+
text: Text::with_section("rotation", text_style.clone(), text_alignment),
51+
..Default::default()
52+
})
53+
.insert(AnimateRotation);
54+
// Demonstrate changing scale
55+
commands
56+
.spawn_bundle(Text2dBundle {
57+
text: Text::with_section("scale", text_style, text_alignment),
58+
..Default::default()
59+
})
60+
.insert(AnimateScale);
61+
}
62+
63+
fn animate_translation(
64+
time: Res<Time>,
65+
mut query: Query<&mut Transform, (With<Text>, With<AnimateTranslation>)>,
66+
) {
67+
for mut transform in query.iter_mut() {
68+
transform.translation.x = 100.0 * time.seconds_since_startup().sin() as f32 - 400.0;
69+
transform.translation.y = 100.0 * time.seconds_since_startup().cos() as f32;
70+
}
71+
}
72+
73+
fn animate_rotation(
74+
time: Res<Time>,
75+
mut query: Query<&mut Transform, (With<Text>, With<AnimateRotation>)>,
76+
) {
77+
for mut transform in query.iter_mut() {
78+
transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos() as f32);
79+
}
80+
}
81+
82+
fn animate_scale(
83+
time: Res<Time>,
84+
mut query: Query<&mut Transform, (With<Text>, With<AnimateScale>)>,
85+
) {
86+
// Consider changing font-size instead of scaling the transform. Scaling a Text2D will scale the
87+
// rendered quad, resulting in a pixellated look.
88+
for mut transform in query.iter_mut() {
89+
transform.translation = Vec3::new(400.0, 0.0, 0.0);
90+
transform.scale = Vec3::splat((time.seconds_since_startup().sin() as f32 + 1.1) * 2.0);
91+
}
92+
}

examples/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Example | File | Description
8989
`sprite` | [`2d/sprite.rs`](./2d/sprite.rs) | Renders a sprite
9090
`sprite_sheet` | [`2d/sprite_sheet.rs`](./2d/sprite_sheet.rs) | Renders an animated sprite
9191
`text2d` | [`2d/text2d.rs`](./2d/text2d.rs) | Generates text in 2d
92+
`text2d_pipelined` | [`2d/text2d_pipelined.rs`](./2d/text2d_pipelined.rs) | Generates text in 2d
9293
`sprite_flipping` | [`2d/sprite_flipping.rs`](./2d/sprite_flipping.rs) | Renders a sprite flipped along an axis
9394
`texture_atlas` | [`2d/texture_atlas.rs`](./2d/texture_atlas.rs) | Generates a texture atlas (sprite sheet) from individual sprites
9495

@@ -253,6 +254,7 @@ Example | File | Description
253254
`text` | [`ui/text.rs`](./ui/text.rs) | Illustrates creating and updating text
254255
`text_debug` | [`ui/text_debug.rs`](./ui/text_debug.rs) | An example for debugging text layout
255256
`ui` | [`ui/ui.rs`](./ui/ui.rs) | Illustrates various features of Bevy UI
257+
`ui_pipelined` | [`ui/ui_pipelined.rs`](./ui/ui_pipelined.rs) | Illustrates various features of Bevy UI
256258

257259
## Window
258260

0 commit comments

Comments
 (0)