Text
Text is rendered through a custom SDF shader. The UI is built to include the correct UV coordinates so that the SDF shader properly renders text with smooth lines.
Why not use TextMeshPro?
Implementing and configuring the mesh to work properly with TextMeshPro shader is a bit of a mystery. TextMeshPro's font generation seems to skip certain characters despite the font actually supporting it. This makes creating a 1 draw call UI difficult to create.
Using the shipped font
NimGui uses Ubuntu Mono Nerd Font to support unicode characters which are used to render the following glyphs:
☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■✖✕
Ubuntu Mono Nerd Font follows the Ubuntu Font License.
Using a custom font
To load your own font, you will need to generate your own Font Texture and SerializedFontData. This is can be done through the Font Generator window.
Access the Font Generator window by clicking Tools -> NimGui -> Font Generator
.
The window will look like the following where you can set your Font Size
and the
signed distance field Distance
.
Font Size: The font size should typically be kept large to provide enough space for each glyph's signed distance field.
Distance: The x field describes the pixels that are fully within the contours, while the y distance describes the pixels that are fully outside of the contours.
After setting the values, you can update the Font Texture Preview to see how the font's texture will look.
Lastly, you can save the Texture which will also generate a SerializedFontAsset
which contains the glyph table that NimGui will reference when drawing fonts.
Font Texture
You must change your Font Texture's Texture type to Single Channel
and Channel to Alpha
.
Loading the custom font
To load the custom font call the following function in OnEnable
in a MonoBehaviour:
// Assigned via inspector
[SerializeField] Texture2D tex;
[SerializeField] SerializedFontData fontDataAsset;
// Initialize the static ImGuiRenderUtils and allocate internal memory.
void OnEnable() {
ImGuiRenderUtils.Initialize(tex, fontDataAsset);
}
// Release allocated memory.
void OnDisable() {
ImGuiRenderUtils.Release();
}
NOTE: You can only load in 1 custom font at any time. You cannot mix and match fonts together.
How is text stored?
Text is stored as a series of characters in a bump allocator. Bump allocators are initialized
with a fixed size with a stored index. This index increments everytime we request a new ImString
from a window.
This allows us to avoid having to rely strictly on string
datatypes as the ImString
contains
a pointer and a length.
NimGui is initialized to store 8192 characters in the bump allocator.
Limitations
You cannot resize the bump allocator to request more than 8192 at the moment, unless you edit the source directly.