17 releases
0.6.7 | Feb 24, 2023 |
---|---|
0.6.5 | Jan 9, 2023 |
0.6.4 | Oct 19, 2022 |
0.6.3 | Jul 7, 2022 |
0.4.0 |
|
#23 in Rendering
148 downloads per month
38KB
713 lines
wgpu-text
wgpu-text
is a wrapper over glyph-brush
for fast and easy text rendering in wgpu
.
This project was inspired by and is similar to wgpu_glyph
but has additional features and is more straightforward. Also, there is no need to include glyph-brush in your project.
Some features are directly implemented from glyph-brush, so it's recommended to go through Section docs and Section examples for better understanding of managing and adding text.
Installation
Add the following to your Cargo.toml
file:
[dependencies]
wgpu_text = "0.6.7"
Usage
use wgpu_text::section::{Section, Text, Layout, HorizontalAlign};
let brush = wgpu_text::BrushBuilder::using_font_bytes(font).unwrap()
/* .initial_cache_size((1024, 1024))) */ // use this to avoid resizing cache texture
/* .with_depth_testing(true) */ // enable/disable depth testing
.build(&device, &config);
// Directly implemented from glyph_brush.
let section = Section::default()
.add_text(Text::new("Hello World"))
.with_layout(Layout::default().h_align(HorizontalAlign::Center));
// on window resize:
brush.resize_view(config.width as f32, config.height as f32, &queue);
// window event loop:
winit::event::Event::RedrawRequested(_) => {
// Has to be queued every frame.
brush.queue(§ion);
let text_buffer = brush.draw(&device, &view, &queue);
// Has to be submitted last so text won't be overlapped.
queue.submit([some_other_encoder.finish(), text_buffer]);
frame.present();
}
Examples
For more detailed examples, look through examples.
cargo run --example <example-name>
Run examples with --release
for accurate performance.
Features
Besides basic text rendering and glyph-brush features, some features add customization:
- builtin matrix - default matrix for orthographic projection (feel free to use it for creating custom matrices)
- custom matrix - grants the ability to provide a custom matrix for purposes of custom view, rotation, etc. (the downside is that it applies to all rendered text)
- depth testing - by adding a z coordinate, text can be set on top or below other text (if enabled)
Future
- wgpu stuff: maybe change to StagingBelt instead of Queue
Contributing
All contributions are welcome.
Dependencies
~4–37MB
~624K SLoC