Skip to content

Commit

Permalink
Upgrade bevy to 0.12.0 (#31) (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
vav-dev authored Nov 19, 2023
1 parent 5ad8ac7 commit dd84a97
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 67 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_text_mesh"
version = "0.7.0"
version = "0.8.0"
edition = "2021"
description = "A bevy 3D text mesh generator for displaying text"
repository = "https://github.com/blaind/bevy_text_mesh"
Expand All @@ -16,15 +16,15 @@ anyhow = "1.0"
glyph_brush_layout = "0.2.3"

[dependencies.bevy]
version = "0.11.0"
version = "0.12.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.11.0"
version = "0.12.0"
default-features = false
features = [
"bevy_winit",
Expand All @@ -34,6 +34,7 @@ features = [
"bevy_core_pipeline",
"bevy_sprite",
"x11",
"tonemapping_luts",
]

[features]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Consider this as a preview of the plugin for gathering feedback about the API:

| bevy | bevy_text_mesh |
| ---- | -------------- |
| 0.12 | 0.8.0 |
| 0.11 | 0.7.0 |
| 0.10 | 0.6.0 |
| 0.9 | 0.5.0 |
Expand Down Expand Up @@ -61,7 +62,7 @@ Add to Cargo.toml:

```
[dependencies]
bevy_text_mesh = "0.7.0"
bevy_text_mesh = "0.8.0"
```

Include the library:
Expand Down Expand Up @@ -91,7 +92,7 @@ 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:
Expand Down
23 changes: 4 additions & 19 deletions examples/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,14 @@ use bevy_text_mesh::prelude::*;
fn main() {
App::new()
.insert_resource(Msaa::Sample4)
.add_plugins(
(
DefaultPlugins,
TextMeshPlugin,
)
)
.add_systems(Startup,
(
setup,
setup_text_mesh.after(setup),
)
)
.add_systems(Update,
(
update_text_mesh,
rotate_camera,
)
)
.add_plugins((DefaultPlugins, TextMeshPlugin))
.add_systems(Startup, (setup, setup_text_mesh.after(setup)))
.add_systems(Update, (update_text_mesh, rotate_camera))
.run();
}

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 {
Expand Down
24 changes: 6 additions & 18 deletions examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::time::Duration;

use bevy::{
diagnostic::{
Diagnostic, DiagnosticId, Diagnostics, DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin, RegisterDiagnostic
Diagnostic, DiagnosticId, Diagnostics, DiagnosticsStore, FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin, RegisterDiagnostic,
},
prelude::*,
render::camera::Camera,
Expand Down Expand Up @@ -34,22 +35,9 @@ fn main() {
FrameTimeDiagnosticsPlugin::default(),
LogDiagnosticsPlugin::default(),
))
.register_diagnostic(
Diagnostic::new(TEXT_MESH_UPDATES, "text_mesh_updates", 20)
)
.add_systems(Startup,
(
setup,
setup_text_mesh,
)
)
.add_systems(Update,
(
spawn_meshes,
update_text_mesh,
rotate_camera,
)
)
.register_diagnostic(Diagnostic::new(TEXT_MESH_UPDATES, "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)
.run();
}
Expand Down Expand Up @@ -87,7 +75,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 Down
47 changes: 34 additions & 13 deletions src/font_loader.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
use bevy::asset::AsyncReadExt;
use std::error::Error;
use std::fmt::Display;

use anyhow::Result;
use bevy::asset::{AssetLoader, BoxedFuture, LoadContext, LoadedAsset};
use bevy::asset::io::Reader;
use bevy::asset::{Asset, AssetLoader, BoxedFuture, LoadContext};
use bevy::reflect::{TypePath, TypeUuid};
use bevy::text::Font;

#[derive(Debug)]
pub struct FontLoaderError;

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())
}
}

#[derive(Default)]
pub struct FontLoader;

impl AssetLoader for FontLoader {
type Asset = TextMeshFont;
type Settings = ();
type Error = FontLoaderError;

fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<()>> {
reader: &'a mut Reader,
_: &'a Self::Settings,
_: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
// standard bevy_text/src/font_loader code
let font = Font::try_from_bytes(bytes.into())?;
load_context.set_default_asset(LoadedAsset::new(font));
let mut bytes = Vec::new();
reader
.read_to_end(&mut bytes)
.await
.expect("unable to read font");

// ttf fontloading
let font = TextMeshFont {
ttf_font: ttf2mesh::TTFFile::from_buffer_vec(bytes.to_vec()).unwrap(),
ttf_font: ttf2mesh::TTFFile::from_buffer_vec(bytes)
.expect("unable to decode asset"),
};

load_context.set_labeled_asset("mesh", LoadedAsset::new(font));

Ok(())
Ok(font)
})
}

Expand All @@ -33,7 +54,7 @@ impl AssetLoader for FontLoader {
}
}

#[derive(TypeUuid, TypePath)]
#[derive(TypeUuid, TypePath, Asset)]
#[uuid = "5415ac03-d009-471e-89ab-dc0d4e31a8c4"]
pub struct TextMeshFont {
pub(crate) ttf_font: ttf2mesh::TTFFile,
Expand Down
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@ pub mod prelude {
pub use glyph_brush_layout::{HorizontalAlign, VerticalAlign};
}

use font_loader::FontLoader;
use mesh_cache::MeshCache;
pub use prelude::*;

pub struct TextMeshPlugin;

impl Plugin for TextMeshPlugin {
fn build(&self, app: &mut App) {
app.add_asset::<font_loader::TextMeshFont>()
.add_systems(Update,
(
mesh_system::text_mesh,
mesh_system::font_loaded,
)
)
app.register_asset_loader(FontLoader)
.init_asset::<font_loader::TextMeshFont>()
.add_systems(Update, (mesh_system::text_mesh, mesh_system::font_loaded))
.insert_resource(MeshCache::default())
.init_asset_loader::<font_loader::FontLoader>();
}
Expand Down
10 changes: 5 additions & 5 deletions src/mesh_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,19 @@ pub(crate) fn font_loaded(
// FIXME: this event system is triggered any time a new text is rendered
// by AssetEvent::Modified caused by font.get_mut(). Improve performance?

for event in events.iter() {
for event in events.read() {
match event {
AssetEvent::Created { handle } => {
AssetEvent::LoadedWithDependencies { id } => {
for (mut state, text_mesh) in query.iter_mut() {
if handle == &text_mesh.style.font {
if id == &text_mesh.style.font.id() {
state.font_loaded = Some(true);
}
}
}
AssetEvent::Removed { handle } => {
AssetEvent::Removed { id } => {
// why would this happen? handling anyway
for (mut state, text_mesh) in query.iter_mut() {
if handle == &text_mesh.style.font {
if id == &text_mesh.style.font.id() {
state.font_loaded = Some(false);
}
}
Expand Down

0 comments on commit dd84a97

Please sign in to comment.