#文本渲染 #wgpu #true-type #渲染 #2d-图形 #图形

wgpu_text

一个简单的 wgpu 2D 文本渲染器

29 个版本

0.9.0 2024 年 7 月 19 日
0.8.7 2024 年 3 月 29 日
0.8.5 2023 年 10 月 27 日
0.8.3 2023 年 7 月 24 日
0.4.0 2022 年 3 月 31 日

#17渲染

Download history 95/week @ 2024-05-04 76/week @ 2024-05-11 503/week @ 2024-05-18 299/week @ 2024-05-25 95/week @ 2024-06-01 79/week @ 2024-06-08 146/week @ 2024-06-15 130/week @ 2024-06-22 40/week @ 2024-06-29 69/week @ 2024-07-06 192/week @ 2024-07-13 186/week @ 2024-07-20 85/week @ 2024-07-27 55/week @ 2024-08-03 65/week @ 2024-08-10 26/week @ 2024-08-17

245 每月下载量
用于 pseudo_term

MIT 许可

36KB
679 代码行

wgpu-text

Build Status Licence crates.io Documentation

wgpu-textglyph-brush 的包装,用于通过 wgpu 快速且简单地渲染文本。它支持 .otf.ttf 字体。

Library Lorem Ipsum Showcase

该项目受到 wgpu_glyph 的启发,并与之类似,但具有更多功能且更直观。此外,无需在项目中包含 glyph-brush

由于 glyph-brush crate 被重新导出,且高度依赖,建议阅读 Section 文档Section 示例 以更好地理解管理和添加文本。

安装

将以下内容添加到您的 Cargo.toml 文件中

[dependencies]
wgpu_text = "0.9.0"

用法

use wgpu_text::{glyph_brush::{Section as TextSection, Text}, BrushBuilder, TextBrush};

let brush = BrushBuilder::using_font_bytes(font).unwrap()
 /* .initial_cache_size((16_384, 16_384))) */ // use this to avoid resizing cache texture
    .build(&device, config.width, config.height, config.format);

// Directly implemented from glyph_brush.
let section = TextSection::default().add_text(Text::new("Hello World"));

// on window resize:
        brush.resize_view(config.width as f32, config.height as f32, &queue);

// window event loop:
    winit::event::Event::RedrawRequested(_) => {
        // Before are created Encoder and frame TextureView.

        // Crashes if inner cache exceeds limits.
        brush.queue(&device, &queue, [&section, ...]).unwrap();

        {
            let mut rpass = encoder.begin_render_pass(...);
            brush.draw(&mut rpass);
        }

        queue.submit([encoder.finish()]);
        frame.present();
    }

示例

有关更多详细示例,请参阅 示例

  • cargorun --example <example-name>

使用 --release 运行示例以获取准确性能。

功能

除了基本的文本渲染和 glyph-brush 功能外,一些功能增加了自定义

  • 内置矩阵 - 正交投影的默认矩阵(您可以自由使用它来创建自定义矩阵)
  • 自定义矩阵 - 允许提供自定义矩阵以实现自定义视图、旋转等目的(缺点是它适用于所有渲染文本)
  • 深度测试 - 通过添加 z 坐标,文本可以设置为位于其他文本之上或之下(如果启用)。请注意在排队 文本段落 时的队列顺序。您应该从最远到最近(根据 z 坐标,越大表示越远)排队。

贡献

欢迎所有类型的贡献。

依赖关系

~12–43MB
~686K SLoC