This repo provides an integration with MSDF, created by Viktor Chlumský. The integration primary uses Rust with bindings generated for C#. An example of the MSDF Atlas can be seen below:
It generates a font atlas like above with the proper UVs for you to do Font Rendering. Information such as
- Face Info
- Ascender
- Descender
- Line height
- Units per em
- Glyph Info
- Bearings
- Advance
- Metrics / Size
- Uvs
are provided through the SerializedFontData
. By default, the SerializedFontData
is sorted by unicode (smallest to largest).
This allows you to use whatever search algorithm you want.
MSDF rendering provides a smaller texture foot print for high quality fonts compared to SDF font rendering. Take a look at the images above comparing how SDF handles sharp corners and with how MSDF handles sharp corners.
To get a better quality for font rendering with SDF, you would need to scale the texture up and provide more info. This is how TextMeshPro provides higher quality fonts. For more information, please read Viktor Chlumský's master thesis.
- OpenUPM integration is coming a bit later for now, add this through your package manager via git URL.
To access the above menu, go to Tools -> InitialPrefabs -> Atlas Generator
in the toolbar.
- Select a directory using the Select Export Directory.
- Select a font asset
- Add in the default characters you want to include in the atlas
- a. Your font must support the characters you want to include. If the characters don't exist in the font, then glyph information cannot be extracted!
- (Optional) If you want to scale the texture to the nearest power of 2, click on Scale Texture.
- a. Please note that scaling the texture may leave undesired empty space.
- Select your DownScale option, the bigger the #, the smaller the glyphs will be in the atlas.
- Select your Max Atlas Width, this supports 128, 256, 512, 1024, 2048, 4096 as a max atlas width.
- (Optional). Add padding between your glyphs, via the Padding field.
- Select your Range. A bigger # means that you will have smoother transitions of the glyphs' edges. A smaller number provides a more focused distance field, but loses detail the further you are away from the edge.
- Select the UV Space.
One Minus V
is the default for Unity, so that the glyphs aline the rendering engine's sampler. - Select your Color Type. Currently supported are: Simple, Ink Trap, and Distance.
- Select your Degrees. This helps the algorithm determine what's considered a corner in the character when generating the glyphs.
- Select the # of Threads to execute the work on.
- a. Be mindful of selecting the total # of threads relative to the # of glyphs/chars you want to goenerate. Each thread is responsible for
total_num_of_glyphs / thread_count
.
- a. Be mindful of selecting the total # of threads relative to the # of glyphs/chars you want to goenerate. Each thread is responsible for
InitialPrefabs.MSDF makes use of InitialPrefabs.ImportOverrides. This allows for easy configuration of the TextureImporterSettings.
A default FontAtlasImportConfig
is provided which
- Removes the alpha channel
- Removes mipmaps
- Removes filtering
- Removes compression
This is queried by: FontAtlasTextureImporter
, which attempts to get the first primary FontAtlasImportConfig
. You can create your own for your
project by right clicking in the project and going to Create -> InitialPrefabs -> MSDF -> FontAtlasImportConfig
.
- To make your custom
FontAtlasImportConfig
the primary config, toggle the Is Primary field on. - Add any file pattern to the File Pattern field to post process any incoming textures.
- a. By default the Atlas Generator names all generated files with _MSDSAtlas.png suffix.
- b. This field is a regex field.
- Add any
TextureImporterSettings
andTextureImporterPlatformSettings
to the Per Platform Settings array.- a. The default one only configures Standalone in the repo.
- If you don't want the importer to do any post processing, add the define:
DISABLE_MSDF_IMPORT
to your project's scripting define. - If you want binary serialization for the
SerializedFontData
, add the defineMSDF_BINARY
to your project's scripting define.
An example shader and glyph rendering is provided in this repo, but is by no means a good way of handling text rendering with layouts. It's simply a demo to provide
basic details on how text rendering can work. You can view this in com.initialprefabs.msdfgen/Example/Scenes/SampleScene.unity
.
The msdf-atlas
is a rust project which generates a dll. You are welcome to use that dll and call get_glyph_data_utf16
to generate an atlas. Please keep in mind
that the glyph data is returned as pointer so you will need to reinterpret it back to its structured data.
Please view byte_buffer.rs
tests and source code to see how to interpret a pointer at an index to GlyphData
.
- Viktor Chlumský for the master thesis and providing MSDFGen to play around with.
- Cysharp's csbindgen for an automated code generation of bindings from Rust -> C#.