#bevy #gamedev #pixelart #text-image #text

bevy_pxtxt

基于位图源图像创建字体并在bevy中渲染文本

5个版本

0.2.0 2024年7月22日
0.1.3 2024年7月6日
0.1.2 2024年7月5日
0.1.1 2024年7月5日
0.1.0 2024年7月4日

#168 in 游戏开发

Download history 260/week @ 2024-07-01 26/week @ 2024-07-08 139/week @ 2024-07-22 46/week @ 2024-07-29

236 每月下载量

MIT 许可证

105KB
813

Bevy Pxtxt

你可以称它为“像素字体”、“图像字体”、“纹理字体”等。无论如何,这个库都有。 bevy_pxtxt 允许用户根据他们自己绘制的图像创建字体和渲染文本。

默认情况下,图像是 .png 文件,但可以使用相应的功能标志与 .bmp.jpeg.gif.tiff

示例

资源/moonshock.ron

(
    name: "Moonshock",
    image: "moonshock.png",
    glyph_width: Varied (
        max: 5,
        min: 1,
    ),
    spacing: 1,
    ascender: 7,
    descender: 2,
)

examples/sections.rs

use bevy::prelude::*;
use bevy_asset_loader::asset_collection::{AssetCollection, AssetCollectionApp};
use bevy_pxtxt::{plugin::PxtxtPlugin, pxfont::PxFont, pxtext::{PxText, PxTextBundle, PxTextSection}};

#[derive(AssetCollection, Resource)]
struct PxFontCollection {
    #[asset(path = "moonshock.ron")]
    pub moonshock: Handle<PxFont>,
}

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, PxtxtPlugin::default()))
        .init_collection::<PxFontCollection>()
        .insert_resource(Msaa::Off)
        .add_systems(Startup, setup)
        .run();
}

fn setup(
    fonts: Res<PxFontCollection>,
    mut commands: Commands,
) {
    commands.spawn(Camera2dBundle::default());
    commands.spawn(PxTextBundle {
        text: PxText::from_sections(
            vec![
                PxTextSection::new("Here's a ")
                    .with_color(Color::WHITE),
                PxTextSection::new("c")
                    .with_color(Color::hsl(0., 0.9, 0.7)).underlined(),
                PxTextSection::new("o")
                    .with_color(Color::hsl(70., 0.9, 0.7)).underlined(),
                PxTextSection::new("l")
                    .with_color(Color::hsl(170., 0.9, 0.7)).underlined(),
                PxTextSection::new("o")
                    .with_color(Color::hsl(220., 0.9, 0.7)).underlined(),
                PxTextSection::new("r")
                    .with_color(Color::hsl(280., 0.9, 0.7)).underlined(),
                PxTextSection::new("ful example of some pixel text.\n\n")
                    .with_color(Color::WHITE),
                PxTextSection::new("(Using sections for different colors)")
                    .with_color(Color::GRAY),
            ], fonts.moonshock.clone()
        ).with_line_spacing(5),
        transform: Transform::from_scale(Vec3::splat(4.0)),
        ..Default::default()
    });
}

结果:示例结果,显示“这是一个彩色像素文本的例子。(使用部分进行不同颜色。)

PxFontData 文件

字体加载到 PxFont 上,它使用 PxFontData 进行反序列化。这是 .ron 文件写入的结构。

必填字段:

  • name - 字体的名称
  • image - 字体图像的路径,相对于 assets 文件夹
  • glyph_width - 每个字符的宽度
    • Varied - 在 maxmin 值之间自动检测字符宽度
    • Monospace - 所有字符宽度相同
  • ascender - 基线以上的像素数
  • descender - 基线以下的像素数

可选字段:

  • char_layout - 图像上显示的字符
    • StartingAt - 字符从给定的字符开始,按Unicode值升序排列
    • Ranges - 字符作为一系列范围给出
    • Listed - 字符单独列出
    • 默认: StartingAt(' ')
  • spacing - 单词内部每个字符之间的空格
    • 默认: 1
  • padding - 图像中字符之间的填充
    • 默认: (0, 0)

输入

可以接收 左键点击右键点击悬停 事件的段落。当实体具有 PxText 时,PickableText 会感知这些事件。

要接收这些事件,请使用 EventReader<PxTextEvent> (了解事件)。

示例

查看 examples/bounded.rs 了解如何使用边界框。

查看 examples/picking.rs 了解如何使用输入。

查看 examples/sections.rs(上面也有)了解如何使用不同的段落。

兼容性

Bevy Pxtxt 版本 0.1 与 Bevy 版本 0.13 兼容。

许可证

本库,包括示例和资产,受 MIT 许可证 许可。

依赖项

~36–73MB
~1.5M SLoC