Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bevy 0.14 #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.12.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.12.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
4 changes: 2 additions & 2 deletions examples/2d_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 60.0,
color: Color::WHITE,
};
let text_alignment = TextAlignment::Center;
let text_alignment = JustifyText::Center;

commands.spawn(Camera2dBundle::default());
commands
.spawn(Text2dBundle {
text: Text::from_section("standard 2d text works too", text_style.clone())
.with_alignment(text_alignment),
.with_justify(text_alignment),
..default()
})
.insert(AnimateRotation);
Expand Down
18 changes: 10 additions & 8 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 @@ -109,13 +109,15 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
mesh: meshes.add(Mesh::from(Plane3d::default().mesh().size(5.0, 5.0))),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..Default::default()
});
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Mesh::from(Cuboid {
half_size: Vec3::new(1.0, 0.5, 1.0),
})),
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
27 changes: 13 additions & 14 deletions examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use bevy::{
diagnostic::{
Diagnostic, DiagnosticId, Diagnostics, DiagnosticsStore, FrameTimeDiagnosticsPlugin,
Diagnostic, DiagnosticPath, Diagnostics, DiagnosticsStore, FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin, RegisterDiagnostic,
},
prelude::*,
Expand Down Expand Up @@ -32,10 +32,10 @@ fn main() {
.add_plugins((
DefaultPlugins,
TextMeshPlugin,
FrameTimeDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin::default(),
))
.register_diagnostic(Diagnostic::new(TEXT_MESH_UPDATES, "text_mesh_updates", 20))
.register_diagnostic(Diagnostic::new(TEXT_MESH_UPDATES).with_max_history_length(20)) // , "text_mesh_updates", 20))
.add_systems(Startup, (setup, setup_text_mesh))
.add_systems(Update, (spawn_meshes, update_text_mesh, rotate_camera))
.add_systems(PostUpdate, update_frame_rate)
Expand Down Expand Up @@ -66,16 +66,15 @@ struct FPS;
#[derive(Component)]
struct TextCount;

pub const TEXT_MESH_UPDATES: DiagnosticId =
DiagnosticId::from_u128(1082410928401928501928509128509125);
pub const TEXT_MESH_UPDATES: DiagnosticPath = DiagnosticPath::const_new("measurement");

fn setup_text_mesh(
mut materials: ResMut<Assets<StandardMaterial>>,
mut commands: Commands,
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 @@ -91,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 @@ -109,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 @@ -194,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 All @@ -204,7 +203,7 @@ fn update_text_mesh(
}

state.text_update_count += update_count;
diagnostics.add_measurement(TEXT_MESH_UPDATES, || state.text_update_count as f64);
diagnostics.add_measurement(&TEXT_MESH_UPDATES, || state.text_update_count as f64);
}

fn rotate_camera(mut camera: Query<&mut Transform, With<Camera>>, time: Res<Time>) {
Expand Down Expand Up @@ -236,7 +235,7 @@ fn update_frame_rate(
if timer.fps_update_timer.tick(time.delta()).just_finished() {
if fps.is_some() {
let fps = diagnostics
.get_measurement(FrameTimeDiagnosticsPlugin::FPS)
.get_measurement(&FrameTimeDiagnosticsPlugin::FPS)
.unwrap();

text_mesh.text = format!("FPS={}", fps.value.round() as usize);
Expand All @@ -246,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,8 +260,8 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
mesh: meshes.add(Mesh::from(Plane3d::default().mesh().size(5.0, 5.0))),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..Default::default()
});
commands.spawn(PointLightBundle {
Expand Down
34 changes: 17 additions & 17 deletions src/font_loader.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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::reflect::{TypePath, TypeUuid};
use bevy::asset::{
Asset, AssetLoader, AsyncReadExt, LoadContext,
io::Reader,
};
use bevy::utils::ConditionalSendFuture;
use bevy::reflect::TypePath;

#[derive(Debug)]
pub struct FontLoaderError;
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,21 +50,17 @@ 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"]
&[]
}
}

#[derive(TypeUuid, TypePath, Asset)]
#[uuid = "5415ac03-d009-471e-89ab-dc0d4e31a8c4"]
#[derive(TypePath, Asset)]
// #[uuid = "5415ac03-d009-471e-89ab-dc0d4e31a8c4"]
pub struct TextMeshFont {
pub(crate) ttf_font: ttf2mesh::TTFFile,
}
Expand Down
Loading
Loading