**Ready for review. Examples migration progress: 100%.** # Objective - Implement https://github.com/bevyengine/bevy/discussions/15014 ## Solution This implements [cart's proposal](https://github.com/bevyengine/bevy/discussions/15014#discussioncomment-10574459) faithfully except for one change. I separated `TextSpan` from `TextSpan2d` because `TextSpan` needs to require the `GhostNode` component, which is a `bevy_ui` component only usable by UI. Extra changes: - Added `EntityCommands::commands_mut` that returns a mutable reference. This is a blocker for extension methods that return something other than `self`. Note that `sickle_ui`'s `UiBuilder::commands` returns a mutable reference for this reason. ## Testing - [x] Text examples all work. --- ## Showcase TODO: showcase-worthy ## Migration Guide TODO: very breaking ### Accessing text spans by index Text sections are now text sections on different entities in a hierarchy, Use the new `TextReader` and `TextWriter` system parameters to access spans by index. Before: ```rust fn refresh_text(mut query: Query<&mut Text, With<TimeText>>, time: Res<Time>) { let text = query.single_mut(); text.sections[1].value = format_time(time.elapsed()); } ``` After: ```rust fn refresh_text( query: Query<Entity, With<TimeText>>, mut writer: UiTextWriter, time: Res<Time> ) { let entity = query.single(); *writer.text(entity, 1) = format_time(time.elapsed()); } ``` ### Iterating text spans Text spans are now entities in a hierarchy, so the new `UiTextReader` and `UiTextWriter` system parameters provide ways to iterate that hierarchy. The `UiTextReader::iter` method will give you a normal iterator over spans, and `UiTextWriter::for_each` lets you visit each of the spans. --------- Co-authored-by: ickshonpe <david.curthoys@googlemail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
		
			
				
	
	
		
			159 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			159 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! Showcases wireframe rendering.
 | 
						|
//!
 | 
						|
//! Wireframes currently do not work when using webgl or webgpu.
 | 
						|
//! Supported platforms:
 | 
						|
//! - DX12
 | 
						|
//! - Vulkan
 | 
						|
//! - Metal
 | 
						|
//!
 | 
						|
//! This is a native only feature.
 | 
						|
 | 
						|
use bevy::{
 | 
						|
    color::palettes::css::*,
 | 
						|
    pbr::wireframe::{NoWireframe, Wireframe, WireframeColor, WireframeConfig, WireframePlugin},
 | 
						|
    prelude::*,
 | 
						|
    render::{
 | 
						|
        render_resource::WgpuFeatures,
 | 
						|
        settings::{RenderCreation, WgpuSettings},
 | 
						|
        RenderPlugin,
 | 
						|
    },
 | 
						|
};
 | 
						|
 | 
						|
fn main() {
 | 
						|
    App::new()
 | 
						|
        .add_plugins((
 | 
						|
            DefaultPlugins.set(RenderPlugin {
 | 
						|
                render_creation: RenderCreation::Automatic(WgpuSettings {
 | 
						|
                    // WARN this is a native only feature. It will not work with webgl or webgpu
 | 
						|
                    features: WgpuFeatures::POLYGON_MODE_LINE,
 | 
						|
                    ..default()
 | 
						|
                }),
 | 
						|
                ..default()
 | 
						|
            }),
 | 
						|
            // You need to add this plugin to enable wireframe rendering
 | 
						|
            WireframePlugin,
 | 
						|
        ))
 | 
						|
        // Wireframes can be configured with this resource. This can be changed at runtime.
 | 
						|
        .insert_resource(WireframeConfig {
 | 
						|
            // The global wireframe config enables drawing of wireframes on every mesh,
 | 
						|
            // except those with `NoWireframe`. Meshes with `Wireframe` will always have a wireframe,
 | 
						|
            // regardless of the global configuration.
 | 
						|
            global: true,
 | 
						|
            // Controls the default color of all wireframes. Used as the default color for global wireframes.
 | 
						|
            // Can be changed per mesh using the `WireframeColor` component.
 | 
						|
            default_color: WHITE.into(),
 | 
						|
        })
 | 
						|
        .add_systems(Startup, setup)
 | 
						|
        .add_systems(Update, update_colors)
 | 
						|
        .run();
 | 
						|
}
 | 
						|
 | 
						|
/// set up a simple 3D scene
 | 
						|
fn setup(
 | 
						|
    mut commands: Commands,
 | 
						|
    mut meshes: ResMut<Assets<Mesh>>,
 | 
						|
    mut materials: ResMut<Assets<StandardMaterial>>,
 | 
						|
) {
 | 
						|
    // Red cube: Never renders a wireframe
 | 
						|
    commands.spawn((
 | 
						|
        Mesh3d(meshes.add(Cuboid::default())),
 | 
						|
        MeshMaterial3d(materials.add(Color::from(RED))),
 | 
						|
        Transform::from_xyz(-1.0, 0.5, -1.0),
 | 
						|
        NoWireframe,
 | 
						|
    ));
 | 
						|
    // Orange cube: Follows global wireframe setting
 | 
						|
    commands.spawn((
 | 
						|
        Mesh3d(meshes.add(Cuboid::default())),
 | 
						|
        MeshMaterial3d(materials.add(Color::from(ORANGE))),
 | 
						|
        Transform::from_xyz(0.0, 0.5, 0.0),
 | 
						|
    ));
 | 
						|
    // Green cube: Always renders a wireframe
 | 
						|
    commands.spawn((
 | 
						|
        Mesh3d(meshes.add(Cuboid::default())),
 | 
						|
        MeshMaterial3d(materials.add(Color::from(LIME))),
 | 
						|
        Transform::from_xyz(1.0, 0.5, 1.0),
 | 
						|
        Wireframe,
 | 
						|
        // This lets you configure the wireframe color of this entity.
 | 
						|
        // If not set, this will use the color in `WireframeConfig`
 | 
						|
        WireframeColor { color: LIME.into() },
 | 
						|
    ));
 | 
						|
 | 
						|
    // plane
 | 
						|
    commands.spawn((
 | 
						|
        Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
 | 
						|
        MeshMaterial3d(materials.add(Color::from(BLUE))),
 | 
						|
        // You can insert this component without the `Wireframe` component
 | 
						|
        // to override the color of the global wireframe for this mesh
 | 
						|
        WireframeColor {
 | 
						|
            color: BLACK.into(),
 | 
						|
        },
 | 
						|
    ));
 | 
						|
 | 
						|
    // light
 | 
						|
    commands.spawn((PointLight::default(), Transform::from_xyz(2.0, 4.0, 2.0)));
 | 
						|
 | 
						|
    // camera
 | 
						|
    commands.spawn((
 | 
						|
        Camera3d::default(),
 | 
						|
        Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
 | 
						|
    ));
 | 
						|
 | 
						|
    // Text used to show controls
 | 
						|
    commands.spawn((
 | 
						|
        Text::default(),
 | 
						|
        Style {
 | 
						|
            position_type: PositionType::Absolute,
 | 
						|
            top: Val::Px(12.0),
 | 
						|
            left: Val::Px(12.0),
 | 
						|
            ..default()
 | 
						|
        },
 | 
						|
    ));
 | 
						|
}
 | 
						|
 | 
						|
/// This system let's you toggle various wireframe settings
 | 
						|
fn update_colors(
 | 
						|
    keyboard_input: Res<ButtonInput<KeyCode>>,
 | 
						|
    mut config: ResMut<WireframeConfig>,
 | 
						|
    mut wireframe_colors: Query<&mut WireframeColor, With<Wireframe>>,
 | 
						|
    mut text: Query<&mut Text>,
 | 
						|
) {
 | 
						|
    **text.single_mut() = format!(
 | 
						|
        "Controls
 | 
						|
---------------
 | 
						|
Z - Toggle global
 | 
						|
X - Change global color
 | 
						|
C - Change color of the green cube wireframe
 | 
						|
 | 
						|
WireframeConfig
 | 
						|
-------------
 | 
						|
Global: {}
 | 
						|
Color: {:?}",
 | 
						|
        config.global, config.default_color,
 | 
						|
    );
 | 
						|
 | 
						|
    // Toggle showing a wireframe on all meshes
 | 
						|
    if keyboard_input.just_pressed(KeyCode::KeyZ) {
 | 
						|
        config.global = !config.global;
 | 
						|
    }
 | 
						|
 | 
						|
    // Toggle the global wireframe color
 | 
						|
    if keyboard_input.just_pressed(KeyCode::KeyX) {
 | 
						|
        config.default_color = if config.default_color == WHITE.into() {
 | 
						|
            DEEP_PINK.into()
 | 
						|
        } else {
 | 
						|
            WHITE.into()
 | 
						|
        };
 | 
						|
    }
 | 
						|
 | 
						|
    // Toggle the color of a wireframe using WireframeColor and not the global color
 | 
						|
    if keyboard_input.just_pressed(KeyCode::KeyC) {
 | 
						|
        for mut color in &mut wireframe_colors {
 | 
						|
            color.color = if color.color == LIME.into() {
 | 
						|
                RED.into()
 | 
						|
            } else {
 | 
						|
                LIME.into()
 | 
						|
            };
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |