libktx / ktx

Kotlin extensions for the libGDX game framework
https://libktx.github.io/
Creative Commons Zero v1.0 Universal
1.36k stars 71 forks source link

[Feature Request] Asset Manager loader for Distance Field fonts #377

Open rubybrowncoat opened 3 years ago

rubybrowncoat commented 3 years ago

A loader for asset manager in the vein of what was done with ktx-freetype but for distance field fonts: https://github.com/libgdx/libgdx/wiki/Distance-field-fonts

A loader for this type of fonts might be desirable, I couldn't figure out how to integrate the loading method specified in the wiki with my asset manager setup.


Alternatively to that, I have used https://github.com/maltaisn/msdf-gdx to load msdf fonts seamlessly like this, because the library includes a loader and I just needed to load it onto the Asset Manager:

val assetManager = AssetManager()
assetManager.setLoader(MsdfFontLoader(InternalFileHandleResolver()))

enum class DistanceFieldFontAssets(val path: String) {
    DankMono32("skin/dank-mono-48-msdf.fnt"),
}

fun AssetManager.load(asset: DistanceFieldFontAssets) = load<MsdfFont>(asset.path)
operator fun AssetManager.get(asset: DistanceFieldFontAssets) = getAsset<MsdfFont>(asset.path)

DistanceFieldFontAssets.values().forEach { assetManager.load(it).finishLoading() }

I'm not sure about all the differences between the distance field fonts described on the libgdx wiki and the ones used by the msdf library, so I can't really say if using that library is sufficient to cover the use cases set forth by the libgdx wiki.

czyzby commented 3 years ago

Seems like the font requires a custom shader, which means we would either have to extend BitmapFont to ensure it is called before rendering, or document this for the users. The examples show how to implement outlines and shadows, so we probably should consider adding these as well. Either way, an extension for msdf-gdx would be cleaner, but the library does not seem to be updated anymore. I might tinker with it in a while, but no promises.