From 25b62f95773f6da46143772f86b8198937507f95 Mon Sep 17 00:00:00 2001 From: davier Date: Fri, 10 Dec 2021 22:21:23 +0000 Subject: [PATCH] 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 --- Cargo.toml | 12 + crates/bevy_internal/Cargo.toml | 4 +- crates/bevy_internal/src/default_plugins.rs | 6 + crates/bevy_internal/src/lib.rs | 12 + examples/2d/text2d_pipelined.rs | 92 +++++ examples/README.md | 2 + examples/ui/ui_pipelined.rs | 225 ++++++++++ pipelined/bevy_core_pipeline/src/lib.rs | 1 - pipelined/bevy_render2/src/camera/mod.rs | 6 + pipelined/bevy_render2/src/lib.rs | 4 +- pipelined/bevy_sprite2/src/lib.rs | 11 +- pipelined/bevy_sprite2/src/render/mod.rs | 18 +- pipelined/bevy_text2/Cargo.toml | 33 ++ pipelined/bevy_text2/src/error.rs | 10 + pipelined/bevy_text2/src/font.rs | 46 +++ pipelined/bevy_text2/src/font_atlas.rs | 107 +++++ pipelined/bevy_text2/src/font_atlas_set.rs | 122 ++++++ pipelined/bevy_text2/src/font_loader.rs | 25 ++ pipelined/bevy_text2/src/glyph_brush.rs | 181 ++++++++ pipelined/bevy_text2/src/lib.rs | 58 +++ pipelined/bevy_text2/src/pipeline.rs | 130 ++++++ pipelined/bevy_text2/src/text.rs | 158 +++++++ pipelined/bevy_text2/src/text2d.rs | 182 +++++++++ pipelined/bevy_ui2/Cargo.toml | 35 ++ pipelined/bevy_ui2/src/anchors.rs | 46 +++ pipelined/bevy_ui2/src/entity.rs | 120 ++++++ pipelined/bevy_ui2/src/flex/convert.rs | 173 ++++++++ pipelined/bevy_ui2/src/flex/mod.rs | 293 +++++++++++++ pipelined/bevy_ui2/src/focus.rs | 157 +++++++ pipelined/bevy_ui2/src/lib.rs | 96 +++++ pipelined/bevy_ui2/src/margins.rs | 29 ++ pipelined/bevy_ui2/src/render/camera.rs | 14 + pipelined/bevy_ui2/src/render/mod.rs | 409 +++++++++++++++++++ pipelined/bevy_ui2/src/render/pipeline.rs | 132 ++++++ pipelined/bevy_ui2/src/render/render_pass.rs | 197 +++++++++ pipelined/bevy_ui2/src/render/ui.wgsl | 38 ++ pipelined/bevy_ui2/src/ui_node.rs | 288 +++++++++++++ pipelined/bevy_ui2/src/update.rs | 160 ++++++++ pipelined/bevy_ui2/src/widget/button.rs | 7 + pipelined/bevy_ui2/src/widget/image.rs | 42 ++ pipelined/bevy_ui2/src/widget/mod.rs | 7 + pipelined/bevy_ui2/src/widget/text.rs | 134 ++++++ 42 files changed, 3809 insertions(+), 13 deletions(-) create mode 100644 examples/2d/text2d_pipelined.rs create mode 100644 examples/ui/ui_pipelined.rs create mode 100644 pipelined/bevy_text2/Cargo.toml create mode 100644 pipelined/bevy_text2/src/error.rs create mode 100644 pipelined/bevy_text2/src/font.rs create mode 100644 pipelined/bevy_text2/src/font_atlas.rs create mode 100644 pipelined/bevy_text2/src/font_atlas_set.rs create mode 100644 pipelined/bevy_text2/src/font_loader.rs create mode 100644 pipelined/bevy_text2/src/glyph_brush.rs create mode 100644 pipelined/bevy_text2/src/lib.rs create mode 100644 pipelined/bevy_text2/src/pipeline.rs create mode 100644 pipelined/bevy_text2/src/text.rs create mode 100644 pipelined/bevy_text2/src/text2d.rs create mode 100644 pipelined/bevy_ui2/Cargo.toml create mode 100644 pipelined/bevy_ui2/src/anchors.rs create mode 100644 pipelined/bevy_ui2/src/entity.rs create mode 100644 pipelined/bevy_ui2/src/flex/convert.rs create mode 100644 pipelined/bevy_ui2/src/flex/mod.rs create mode 100644 pipelined/bevy_ui2/src/focus.rs create mode 100644 pipelined/bevy_ui2/src/lib.rs create mode 100644 pipelined/bevy_ui2/src/margins.rs create mode 100644 pipelined/bevy_ui2/src/render/camera.rs create mode 100644 pipelined/bevy_ui2/src/render/mod.rs create mode 100644 pipelined/bevy_ui2/src/render/pipeline.rs create mode 100644 pipelined/bevy_ui2/src/render/render_pass.rs create mode 100644 pipelined/bevy_ui2/src/render/ui.wgsl create mode 100644 pipelined/bevy_ui2/src/ui_node.rs create mode 100644 pipelined/bevy_ui2/src/update.rs create mode 100644 pipelined/bevy_ui2/src/widget/button.rs create mode 100644 pipelined/bevy_ui2/src/widget/image.rs create mode 100644 pipelined/bevy_ui2/src/widget/mod.rs create mode 100644 pipelined/bevy_ui2/src/widget/text.rs diff --git a/Cargo.toml b/Cargo.toml index c5da7c3c17b1b..d5e218da628c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,8 @@ default = [ "bevy_sprite2", "bevy_render2", "bevy_pbr2", + "bevy_ui2", + "bevy_text2", "bevy_winit", "render", "png", @@ -59,6 +61,8 @@ bevy_render2 = ["bevy_internal/bevy_render2"] bevy_sprite2 = ["bevy_internal/bevy_sprite2"] bevy_pbr2 = ["bevy_internal/bevy_pbr2"] bevy_gltf2 = ["bevy_internal/bevy_gltf2"] +bevy_ui2 = ["bevy_internal/bevy_ui2"] +bevy_text2 = ["bevy_internal/bevy_text2"] trace_chrome = ["bevy_internal/trace_chrome"] trace_tracy = ["bevy_internal/trace_tracy"] @@ -140,6 +144,10 @@ path = "examples/2d/sprite_sheet.rs" name = "text2d" path = "examples/2d/text2d.rs" +[[example]] +name = "text2d_pipelined" +path = "examples/2d/text2d_pipelined.rs" + [[example]] name = "texture_atlas" path = "examples/2d/texture_atlas.rs" @@ -506,6 +514,10 @@ path = "examples/ui/text_debug.rs" name = "ui" path = "examples/ui/ui.rs" +[[example]] +name = "ui_pipelined" +path = "examples/ui/ui_pipelined.rs" + # Window [[example]] name = "clear_color" diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index 4f994511282be..0e682aa357009 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -39,7 +39,7 @@ wayland = ["bevy_winit/wayland"] x11 = ["bevy_winit/x11"] # enable rendering of font glyphs using subpixel accuracy -subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"] +subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas", "bevy_text2/subpixel_glyph_atlas"] # enable systems that allow for automated testing on CI bevy_ci_testing = ["bevy_app/bevy_ci_testing"] @@ -74,7 +74,9 @@ bevy_dynamic_plugin = { path = "../bevy_dynamic_plugin", optional = true, versio bevy_sprite = { path = "../bevy_sprite", optional = true, version = "0.5.0" } bevy_sprite2 = { path = "../../pipelined/bevy_sprite2", optional = true, version = "0.5.0" } bevy_text = { path = "../bevy_text", optional = true, version = "0.5.0" } +bevy_text2 = { path = "../../pipelined/bevy_text2", optional = true, version = "0.5.0" } bevy_ui = { path = "../bevy_ui", optional = true, version = "0.5.0" } +bevy_ui2 = { path = "../../pipelined/bevy_ui2", optional = true, version = "0.5.0" } bevy_wgpu = { path = "../bevy_wgpu", optional = true, version = "0.5.0" } bevy_winit = { path = "../bevy_winit", optional = true, version = "0.5.0" } bevy_gilrs = { path = "../bevy_gilrs", optional = true, version = "0.5.0" } diff --git a/crates/bevy_internal/src/default_plugins.rs b/crates/bevy_internal/src/default_plugins.rs index 733fd2d419e98..26ea57f63300b 100644 --- a/crates/bevy_internal/src/default_plugins.rs +++ b/crates/bevy_internal/src/default_plugins.rs @@ -138,6 +138,12 @@ impl PluginGroup for PipelinedDefaultPlugins { #[cfg(feature = "bevy_sprite2")] group.add(bevy_sprite2::SpritePlugin::default()); + #[cfg(feature = "bevy_text2")] + group.add(bevy_text2::TextPlugin::default()); + + #[cfg(feature = "bevy_ui2")] + group.add(bevy_ui2::UiPlugin::default()); + #[cfg(feature = "bevy_pbr2")] group.add(bevy_pbr2::PbrPlugin::default()); diff --git a/crates/bevy_internal/src/lib.rs b/crates/bevy_internal/src/lib.rs index 932971b6f1046..bd973b12c0060 100644 --- a/crates/bevy_internal/src/lib.rs +++ b/crates/bevy_internal/src/lib.rs @@ -147,12 +147,24 @@ pub mod text { pub use bevy_text::*; } +#[cfg(feature = "bevy_text2")] +pub mod text2 { + //! Text drawing, styling, and font assets. + pub use bevy_text2::*; +} + #[cfg(feature = "bevy_ui")] pub mod ui { //! User interface components and widgets. pub use bevy_ui::*; } +#[cfg(feature = "bevy_ui2")] +pub mod ui2 { + //! User interface components and widgets. + pub use bevy_ui2::*; +} + #[cfg(feature = "bevy_winit")] pub mod winit { pub use bevy_winit::*; diff --git a/examples/2d/text2d_pipelined.rs b/examples/2d/text2d_pipelined.rs new file mode 100644 index 0000000000000..dc6ffdca3165c --- /dev/null +++ b/examples/2d/text2d_pipelined.rs @@ -0,0 +1,92 @@ +use bevy::{ + core::Time, + math::{Quat, Vec3}, + prelude::{App, AssetServer, Commands, Component, Query, Res, Transform, With}, + render2::{camera::OrthographicCameraBundle, color::Color}, + text2::{HorizontalAlign, Text, Text2dBundle, TextAlignment, TextStyle, VerticalAlign}, + PipelinedDefaultPlugins, +}; + +fn main() { + App::new() + .add_plugins(PipelinedDefaultPlugins) + .add_startup_system(setup) + .add_system(animate_translation) + .add_system(animate_rotation) + .add_system(animate_scale) + .run(); +} + +#[derive(Component)] +struct AnimateTranslation; +#[derive(Component)] +struct AnimateRotation; +#[derive(Component)] +struct AnimateScale; + +fn setup(mut commands: Commands, asset_server: Res) { + let font = asset_server.load("fonts/FiraSans-Bold.ttf"); + let text_style = TextStyle { + font, + font_size: 60.0, + color: Color::WHITE, + }; + let text_alignment = TextAlignment { + vertical: VerticalAlign::Center, + horizontal: HorizontalAlign::Center, + }; + // 2d camera + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + // Demonstrate changing translation + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("translation", text_style.clone(), text_alignment), + ..Default::default() + }) + .insert(AnimateTranslation); + // Demonstrate changing rotation + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("rotation", text_style.clone(), text_alignment), + ..Default::default() + }) + .insert(AnimateRotation); + // Demonstrate changing scale + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("scale", text_style, text_alignment), + ..Default::default() + }) + .insert(AnimateScale); +} + +fn animate_translation( + time: Res