Skip to content

Commit

Permalink
Update to Bevy 0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyannide committed Nov 21, 2024
1 parent 159e6d6 commit c297a03
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_text_mesh"
version = "0.9.0"
version = "0.11.0"
edition = "2021"
description = "A bevy 3D text mesh generator for displaying text"
repository = "https://github.com/blaind/bevy_text_mesh"
Expand All @@ -10,21 +10,21 @@ license = "MIT"
readme = "README.md"

[dependencies]
ttf2mesh = "0.2.0"
ttf2mesh = "0.2.1"
bitflags = "2.1"
anyhow = "1.0"
glyph_brush_layout = "0.2.3"

[dependencies.bevy]
version = "0.13.0"
version = "0.14.0"
default-features = false
features = ["bevy_render", "bevy_text", "bevy_pbr", "bevy_asset", "bevy_sprite"]

[dev-dependencies]
rand = "0.8.4"

[dev-dependencies.bevy]
version = "0.13.0"
version = "0.14.0"
default-features = false
features = [
"bevy_winit",
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Consider this as a preview of the plugin for gathering feedback about the API:

| bevy | bevy_text_mesh |
| ---- | -------------- |
| 0.14 | 0.11.0 |
| 0.13 | 0.10.0 |
| 0.12 | 0.9.0 |
| 0.11 | 0.7.0 |
| 0.10 | 0.6.0 |
Expand Down Expand Up @@ -62,7 +64,7 @@ Add to Cargo.toml:

```
[dependencies]
bevy_text_mesh = "0.9.0"
bevy_text_mesh = "0.11.0"
```

Include the library:
Expand Down Expand Up @@ -92,14 +94,14 @@ Next, you are ready to spawn a text in your scene at a system:
First, load a font asset:

```rust
let font: Handle<TextMeshFont> = asset_server.load("fonts/FiraSans-Medium.ttf#mesh");
let font: Handle<TextMeshFont> = asset_server.load("fonts/FiraSans-Medium.ttf");
```

Then, spawn a textmesh bundle:

```rust
commands.spawn(TextMeshBundle {
text_mesh: TextMesh::new_with_color("Hello Bevy", font, Color::rgb(1., 1., 0.)),
text_mesh: TextMesh::new_with_color("Hello Bevy", font, Color::srgb(1., 1., 0.)),
transform: Transform::from_xyz(-1., 1.75, 0.),
..Default::default()
});
Expand All @@ -114,7 +116,7 @@ commands.spawn(TextMeshBundle {
style: TextMeshStyle {
font,
font_size: SizeUnit::NonStandard(36.),
color: Color::rgb(1.0, 1.0, 0.0),
color: Color::srgb(1.0, 1.0, 0.0),
font_style: FontStyle::UPPERCASE, // only UPPERCASE & LOWERCASE implemented currently
mesh_quality: Quality::Low,
..Default::default()
Expand Down
12 changes: 6 additions & 6 deletions examples/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ fn main() {
}

fn setup_text_mesh(mut commands: Commands, asset_server: Res<AssetServer>) {
let font: Handle<TextMeshFont> = asset_server.load("fonts/FiraMono-Medium.ttf#mesh");
let font: Handle<TextMeshFont> = asset_server.load("fonts/FiraMono-Medium.ttf");

commands.spawn(TextMeshBundle {
text_mesh: TextMesh {
text: String::from("Time since startup"),
style: TextMeshStyle {
font: font.clone(),
font_size: SizeUnit::NonStandard(9.),
color: Color::rgb(0.0, 0.0, 0.0),
color: Color::srgb(0.0, 0.0, 0.0),
..Default::default()
},
size: TextMeshSize {
Expand All @@ -43,7 +43,7 @@ fn setup_text_mesh(mut commands: Commands, asset_server: Res<AssetServer>) {
style: TextMeshStyle {
font: font.clone(),
font_size: SizeUnit::NonStandard(36.),
color: Color::rgb(0.0, 1.0, 0.0),
color: Color::srgb(0.0, 1.0, 0.0),
mesh_quality: Quality::Custom(128),
..Default::default()
},
Expand Down Expand Up @@ -77,7 +77,7 @@ fn update_text_mesh(
) {
if timer.timer.tick(time.delta()).just_finished() {
for mut text_mesh in text_meshes.iter_mut() {
let updated_text = String::from(format!("Time = {:.3}", time.elapsed_seconds_f64()));
let updated_text = format!("Time = {:.3}", time.elapsed_seconds_f64());

if text_mesh.text != updated_text {
text_mesh.text = updated_text;
Expand Down Expand Up @@ -110,14 +110,14 @@ fn setup(
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(Plane3d::default().mesh().size(5.0, 5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..Default::default()
});
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(Cuboid {
half_size: Vec3::new(1.0, 0.5, 1.0),
})),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
material: materials.add(Color::srgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
Expand Down
14 changes: 7 additions & 7 deletions examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
.add_plugins((
DefaultPlugins,
TextMeshPlugin,
FrameTimeDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin::default(),
))
.register_diagnostic(Diagnostic::new(TEXT_MESH_UPDATES).with_max_history_length(20)) // , "text_mesh_updates", 20))
Expand Down Expand Up @@ -74,7 +74,7 @@ fn setup_text_mesh(
asset_server: Res<AssetServer>,
) {
let state = SceneState {
font: asset_server.load("fonts/FiraMono-Medium.ttf#mesh"),
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
text_count: 0,
text_update_count: 0,
material: materials.add(StandardMaterial {
Expand All @@ -90,7 +90,7 @@ fn setup_text_mesh(
text: String::from("FPS"),
style: TextMeshStyle {
font: state.font.clone(),
color: Color::rgb(0., 1., 0.),
color: Color::srgb(0., 1., 0.),
font_size: SizeUnit::NonStandard(48.),
..Default::default()
},
Expand All @@ -108,7 +108,7 @@ fn setup_text_mesh(
style: TextMeshStyle {
font: state.font.clone(),
font_size: SizeUnit::NonStandard(18.),
color: Color::rgb(1., 1., 1.),
color: Color::srgb(1., 1., 1.),
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -193,7 +193,7 @@ fn update_text_mesh(
let mut update_count = 0;
if timer.text_update_timer.tick(time.delta()).just_finished() {
for mut text_mesh in text_meshes.iter_mut() {
let updated_text = String::from(format!("Time = {:.3}", time.elapsed_seconds_f64()));
let updated_text = format!("Time = {:.3}", time.elapsed_seconds_f64());

if text_mesh.text != updated_text {
text_mesh.text = updated_text;
Expand Down Expand Up @@ -245,7 +245,7 @@ fn update_frame_rate(
}

let camera_entity = camera_entity.iter().next().unwrap();
let camera_transform = transform_query.get_mut(camera_entity).unwrap().clone();
let camera_transform = *transform_query.get_mut(camera_entity).unwrap();
let mut transform = transform_query.get_mut(text_mesh_entity).unwrap();

// eh - why negative?
Expand All @@ -261,7 +261,7 @@ fn setup(
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(Plane3d::default().mesh().size(5.0, 5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..Default::default()
});
commands.spawn(PointLightBundle {
Expand Down
28 changes: 14 additions & 14 deletions src/font_loader.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use bevy::asset::AsyncReadExt;
use bevy::text::Font;
use std::error::Error;
use std::fmt::Display;

use anyhow::Result;
use bevy::asset::io::Reader;
use bevy::asset::{Asset, AssetLoader, BoxedFuture, LoadContext};
use bevy::asset::{
Asset, AssetLoader, AsyncReadExt, LoadContext,
io::Reader,
};
use bevy::utils::ConditionalSendFuture;
use bevy::reflect::TypePath;

#[derive(Debug)]
Expand All @@ -15,24 +16,27 @@ impl Error for FontLoaderError {}

impl Display for FontLoaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.to_string())
f.write_str("FontLoaderError")
}
}

#[derive(Default)]
pub struct FontLoader;

impl AssetLoader for FontLoader {
type Asset = Font;
/// Bevy doesn't allow multiple [`AssetLoader`]s for the same extension anymore.
/// So now we're returning our own type so Bevy can determine which loader to
/// use without binding an extension to our loader.
type Asset = TextMeshFont;
type Settings = ();
type Error = FontLoaderError;

fn load<'a>(
&'a self,
reader: &'a mut Reader,
_: &'a Self::Settings,
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
_load_context: &'a mut LoadContext,
) -> impl ConditionalSendFuture<Output = Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader
Expand All @@ -46,16 +50,12 @@ impl AssetLoader for FontLoader {
.expect("unable to decode asset"),
};

load_context.add_labeled_asset("mesh".into(), font);

let original_font = Font::try_from_bytes(bytes.into()).expect("unable to read font");

Ok(original_font)
Ok(font)
})
}

fn extensions(&self) -> &[&str] {
&["ttf"]
&[]
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/mesh_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn text_mesh(
if let Some(asset_mesh) = meshes.get_mut(mesh) {
new_mesh = false;
let ttf2_mesh =
generate_text_mesh(&text_mesh, &mut font.ttf_font, Some(&mut cache));
generate_text_mesh(text_mesh, &mut font.ttf_font, Some(&mut cache));
apply_mesh(ttf2_mesh, asset_mesh);
}
}
Expand All @@ -75,19 +75,19 @@ pub(crate) fn text_mesh(
RenderAssetUsages::RENDER_WORLD,
);

let ttf2_mesh = generate_text_mesh(&text_mesh, &mut font.ttf_font, Some(&mut cache));
let ttf2_mesh = generate_text_mesh(text_mesh, &mut font.ttf_font, Some(&mut cache));
apply_mesh(ttf2_mesh, &mut mesh);

commands.entity(entity).insert(PbrBundle {
mesh: meshes.add(mesh),
material: material.map(|m| m.clone()).unwrap_or_else(|| {
material: material.cloned().unwrap_or_else(|| {
materials.add(StandardMaterial {
base_color: text_mesh.style.color,
..Default::default()
})
}),
transform: transform.clone(),
global_transform: global_transform.clone(),
transform: *transform,
global_transform: *global_transform,
..Default::default()
});
}
Expand Down

0 comments on commit c297a03

Please sign in to comment.