Text 3D
Text rendering in 3D is done through bevy_rich_text3d crate which Bevy_Lunex re-exports. This is also gated
behind a text3d feature if you have disabled default features.
Similar to 2D text, we use Text3d component in conjunction with the Window ui layout.
UiLayout- Specifies position and anchor only, size is ignored.UiTextSize- Specifies the height of the text in proportion to parent node.Text3d- Specifies the actual text.
Important
Text3drequires some necessary setup. You have to add these 2 components with some “default” values for it to work.
MeshMaterial3d- Required material to work. Recommended:StandardMaterial { base_color_texture: Some(TextAtlas::DEFAULT_IMAGE), alpha_mode: AlphaMode::Blend, unlit: true, ..Default::default() }Mesh3d- Required empty defaultMesh3d::default()component to work.
Example
ui.spawn((
Name::new("Panel"),
// Set the layout of this mesh
UiLayout::window().pos(Rl(50.0)).anchor(Anchor::Center).pack(),
// This controls the height of the text, so 10% of the parent's node height
UiTextSize::from(Rh(10.0)),
// Set the text value
Text3d::new("Hello 3D UI!"),
// Style the 3D text
Text3dStyling {
size: 64.0,
color: Srgba::new(1., 1., 1., 1.),
align: TextAlign::Center,
font: Arc::from("Rajdhani"),
weight: Weight::BOLD,
..Default::default()
},
// Provide a material to this mesh
MeshMaterial3d(materials.add(
StandardMaterial {
base_color_texture: Some(TextAtlas::DEFAULT_IMAGE),
alpha_mode: AlphaMode::Blend,
unlit: true,
..Default::default()
}
)),
// Requires an empty mesh
Mesh3d::default(),
));
Warning
Text3dcomponent can ONLY be rendered withCamera3d. 2D text is a separate matter.
Note
bevy_rich_text3dworks a bit differently thanbevy_text. When styling a text, you don’t provide a font handle. Instead, the font must be loaded into afontdbthrough a resource and then you can reference it by using the font name..insert_resource(LoadFonts { font_directories: vec!["assets/fonts".to_owned()], ..Default::default() })