17cupsofcoffee / tetra

🎮 A simple 2D game framework written in Rust
MIT License
907 stars 63 forks source link

Add an option to generate premultiplied font atlases #299

Closed 17cupsofcoffee closed 2 years ago

17cupsofcoffee commented 2 years ago

Summary

When drawing with premultiplied alpha, TTF glyphs just display as white boxes, because the generated texture data is not premultiplied.

This can be easily worked around by momentarily switching back to the default blend mode, but this requires a seperate draw call, and due to #273 this state change is not easy to encapsulate.

It would be nice to have an option for glyphs to be premultiplied when they are generated, so that this overhead can be avoided.

Motivation/Examples

Premultiplied alpha, while a bit more awkward to set up, makes it way easier to get nice results when working with transparency.

In my own games, I have started trying to use premultiplied alpha throughout, but I currently cannot do that for TTF fonts.

Alternatives Considered

We could leave things as they are, but that wouldn't be nice for performance/usability.

17cupsofcoffee commented 2 years ago

While I'm at it, it would be nice to add a setting for the amount of padding on the rendered glyphs - I've been trying to write a shader for drawing borders on TTF pixel fonts, and I can't currently get it to work because the border would extend beyond the outside of the quad.

EDIT: actually this might be a bad idea due to the wasted space in the atlas - going to leave this out for now

17cupsofcoffee commented 2 years ago

Part of me wonders if I should give these options for BMFonts too - even though they have pre-baked textures, we still assemble a runtime font atlas to avoid texture swapping overhead with large fonts. So we could allow premultiplication/padding there too.

That said, given that by definition you're pre-baking your assets if you're using BMFont, it might be better to just encourage people to export as premultiplied/with the correct amount of padding.

17cupsofcoffee commented 2 years ago

Premultiplied glyphs are now working on the 0.7 branch.