chore(text): update text mesh crate

Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
Schmarni
2025-09-06 04:55:58 +02:00
parent 7314428ce7
commit ae68d0e135
2 changed files with 42 additions and 45 deletions

6
Cargo.lock generated
View File

@@ -622,7 +622,7 @@ dependencies = [
[[package]] [[package]]
name = "bevy-mesh-text-3d" name = "bevy-mesh-text-3d"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/Schmarni-Dev/bevy-mesh-text-3d#1215d6195b153b1dcd5f0e5cab2fb6f8ae834aa4" source = "git+https://github.com/Schmarni-Dev/bevy-mesh-text-3d#a151272ce771c6856811bb21bff41e46b72b90a3"
dependencies = [ dependencies = [
"bevy", "bevy",
"cosmic-text 0.14.2", "cosmic-text 0.14.2",
@@ -4045,7 +4045,7 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77e878c846a8abae00dd069496dbe8751b16ac1c3d6bd2a7283a938e8228f90d" checksum = "77e878c846a8abae00dd069496dbe8751b16ac1c3d6bd2a7283a938e8228f90d"
dependencies = [ dependencies = [
"proc-macro-crate 1.3.1", "proc-macro-crate 3.3.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.104", "syn 2.0.104",
@@ -6718,7 +6718,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]

View File

@@ -11,18 +11,17 @@ use crate::{
}, },
nodes::{ nodes::{
Node, Node,
drawable::XAlign, drawable::{TextFit, XAlign},
spatial::{Spatial, SpatialNode}, spatial::{Spatial, SpatialNode},
}, },
}; };
use bevy::{platform::collections::HashMap, prelude::*}; use bevy::{platform::collections::HashMap, prelude::*};
use bevy_mesh_text_3d::{ use bevy_mesh_text_3d::{
Align, Attrs, MeshTextPlugin, Settings as FontSettings, generate_meshes, Align, Attrs, HorizontalAnchorPoint, MeshTextPlugin, Settings as FontSettings, VerticalAlign,
text_glyphs::TextGlyphs, VerticalAnchorPoint, generate_meshes,
}; };
use color_eyre::eyre::eyre; use color_eyre::eyre::eyre;
use core::f32; use core::f32;
use cosmic_text::Metrics;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ffi::OsStr, mem, path::PathBuf, sync::Arc}; use std::{ffi::OsStr, mem, path::PathBuf, sync::Arc};
@@ -34,7 +33,7 @@ impl Plugin for TextNodePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
// Text init stuff // Text init stuff
// 1.0 for font size in meters // 1.0 for font size in meters
app.add_plugins(MeshTextPlugin::new(1.0)); app.add_plugins(MeshTextPlugin);
app.world_mut() app.world_mut()
.resource_mut::<FontSettings>() .resource_mut::<FontSettings>()
.font_system .font_system
@@ -72,21 +71,33 @@ fn spawn_text(
super::XAlign::Center => Align::Center, super::XAlign::Center => Align::Center,
super::XAlign::Right => Align::Left, super::XAlign::Right => Align::Left,
}); });
let vertical_alignment = Some(match style.text_align_y {
super::YAlign::Top => VerticalAlign::Top,
super::YAlign::Center => VerticalAlign::Middle,
super::YAlign::Bottom => VerticalAlign::Bottom,
});
let text_string = text.text.lock().clone(); let text_string = text.text.lock().clone();
let mut text_glyphs = TextGlyphs::new(
Metrics {
font_size: style.character_height,
line_height: style.character_height,
},
[(text_string.as_str(), attrs.clone())],
&attrs,
&mut font_settings.font_system,
alignment,
);
let max_width = style.bounds.as_ref().map(|v| v.bounds.x); let max_width = style.bounds.as_ref().map(|v| v.bounds.x);
let max_height = style.bounds.as_ref().map(|v| v.bounds.x); let max_height = style.bounds.as_ref().map(|v| v.bounds.y);
let (width, height) = let horizontal_anchor_point = style
text_glyphs.measure(max_width, max_height, &mut font_settings.font_system); .bounds
.as_ref()
.map(|v| match v.anchor_align_x {
XAlign::Left => HorizontalAnchorPoint::Left,
XAlign::Center => HorizontalAnchorPoint::Middle,
XAlign::Right => HorizontalAnchorPoint::Right,
})
.unwrap_or(HorizontalAnchorPoint::Middle);
let vertical_anchor_point = style
.bounds
.as_ref()
.map(|v| match v.anchor_align_y {
YAlign::Top => VerticalAnchorPoint::Top,
YAlign::Center => VerticalAnchorPoint::Middle,
YAlign::Bottom => VerticalAnchorPoint::Bottom,
})
.unwrap_or(VerticalAnchorPoint::Middle);
let wrap = matches!(style.bounds.as_ref().map(|v| v.fit), Some(TextFit::Wrap));
let char_meshes = generate_meshes( let char_meshes = generate_meshes(
bevy_mesh_text_3d::InputText::Simple { bevy_mesh_text_3d::InputText::Simple {
text: text_string, text: text_string,
@@ -109,43 +120,29 @@ fn spawn_text(
bevy_mesh_text_3d::Parameters { bevy_mesh_text_3d::Parameters {
extrusion_depth: 0.0, extrusion_depth: 0.0,
font_size: style.character_height, font_size: style.character_height,
line_height: style.character_height, line_height: style.character_height * 1.1,
alignment, alignment,
max_width, max_width: wrap.then_some(0).and(max_width),
max_height, max_height: wrap.then_some(0).and(max_height),
vertical_alignment,
horizontal_anchor_point,
vertical_anchor_point,
}, },
&mut meshes, &mut meshes,
); );
if let Some(db) = old_db { if let Some(db) = old_db {
mem::swap(font_settings.font_system.db_mut(), db); mem::swap(font_settings.font_system.db_mut(), db);
} }
let Ok(char_meshes) = let Ok((char_meshes, _text_size)) =
char_meshes.inspect_err(|err| error!("unable to create text meshes: {err}")) char_meshes.inspect_err(|err| error!("unable to create text meshes: {err}"))
else { else {
continue; continue;
}; };
// TODO: text align
let letters = char_meshes let letters = char_meshes
.into_iter() .into_iter()
.map(|v| { .map(|v| {
cmds.spawn(( cmds.spawn((Mesh3d(v.mesh), MeshMaterial3d(v.material), v.transform))
Mesh3d(v.mesh),
MeshMaterial3d(v.material),
Transform::from_xyz(
// -dist +
match style.text_align_x {
XAlign::Left => 0.0,
XAlign::Center => width * -0.5,
XAlign::Right => -width,
},
match style.text_align_y {
YAlign::Top => height,
YAlign::Center => height * 0.5,
YAlign::Bottom => 0.0,
},
0.0,
) * v.transform,
))
.id() .id()
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();