Nazariglez / notan

Cross-platform multimedia layer
https://nazariglez.github.io/notan-web/
Apache License 2.0
802 stars 55 forks source link

Blurry text when transform matrix is large #326

Closed dgulotta closed 2 months ago

dgulotta commented 3 months ago

It seems that the transform matrix is not taken into account when computing the resolution for rendering text. In the following example, the code changes the transform matrix so that 1 unit represents 10 pixels, and draws a piece of text that is 6 units = 60 pixels tall. It seems that the text is rendered to a 6 pixel texture that is then blown up by a factor of 10, making it very blurry.

Example:

use notan::draw::*;
use notan::math::{Mat3, Vec3};
use notan::prelude::*;

#[derive(AppState)]
struct State {
    font: Font,
}

#[notan_main]
fn main() -> Result<(), String> {
    notan::init_with(setup)
        .add_config(DrawConfig)
        .draw(draw)
        .build()
}

fn setup(gfx: &mut Graphics) -> State {
    let font = gfx
        .create_font(include_bytes!("assets/Ubuntu-B.ttf"))
        .unwrap();
    State { font }
}

fn draw(gfx: &mut Graphics, state: &mut State) {
    let mut draw = gfx.create_draw();
    draw.clear(Color::BLACK);

    draw.transform()
        .push(Mat3::from_diagonal(Vec3::new(10.0, 10.0, 1.0)));

    draw.text(&state.font, "Hello World!")
        .position(40.0, 30.0)
        .size(6.0)
        .color(Color::ORANGE)
        .h_align_center()
        .v_align_middle();

    gfx.render(&draw);
}
Nazariglez commented 3 months ago

Hello! I think this is expected, text rendering is getting the letters from an image, changing the matrix will not change the number of pixels. I guess that we can calculate the scale from the matrix and apply it to the text size, however a workaround is just multiply the size of the text and skip the matrix that scales for the text to get a sharp text.

Although I am open for PRs is you want to make this improvement.

Nazariglez commented 2 months ago

Feel free to reopen if you think there is something to do here.