Skip to content

Commit 6f067d8

Browse files
committed
Add new docs about MSDF fonts
FlaxEngine/FlaxEngine#3944
1 parent 51c2040 commit 6f067d8

14 files changed

Lines changed: 94 additions & 1 deletion

manual/toc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@
345345
### [How to create UI](ui/tutorials/create-ui.md)
346346
### [How to create UI from code](ui/tutorials/create-ui-from-code.md)
347347
### [How to create a custom font material](ui/tutorials/create-font-material.md)
348+
### [How to create an outline font material](ui/tutorials/create-outline-font-material.md)
348349
### [How to create a custom GUI material](ui/tutorials/create-gui-material.md)
349350
### [How to blur UI panel background](ui/tutorials/blur-background.md)
350351
### [How to create a Main Menu](ui/tutorials/create-main-menu.md)

manual/ui/fonts/index.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
**Font Assets** are binary resources that contain information of font characters (and/or prerendered characters texture).
66
Flax performs the required importing, loading and rasterization of the font glyphs. Fonts are used by the 3D [Text Render](../text-render/index.md) actor, as well as the [UI](../index.md) system.
77

8-
Flax uses the **FreeType** library for font character rasterization and offline rendering.
8+
Flax uses the **FreeType** library for font character rasterization and offline rendering. Fonts can be rasterized into `Bitmap` or `MSDF` (Multi-channel Signed Distance Field generator from **msdfgen** library).
99

1010
## Importing fonts
1111

@@ -32,11 +32,26 @@ The font window can be used to preview and edit the font rasterization options:
3232

3333
| Property | Description |
3434
|--------|--------|
35+
| **Raster Mode** | The font rasterization mode. Possible options include: <table><tbody><tr><th>Option</th><th>Description</th></tr><tr><td>**Bitmap**</td><td>Use the default FreeType rasterizer to render font atlases.</td></tr><tr><td>**MSDF**</td><td>Use the Multi-channel Signed Distance Field (MSDF) generator to render font atlases. Need to be rendered with a compatible material.</td></tr></tbody></table> |
3536
| **Hinting** | The font hinting used when rendering characters. Possible options include: <table><tbody><tr><th>Option</th><th>Description</th></tr><tr><td>**Default**</td><td>Use the default hinting specified in the font.</td></tr><tr><td>**Auto**</td><td>Force the use of an automatic hinting algorithm (over the fonts native hinter).</td></tr><tr><td>**Auto Light**</td><td>Force the use of an automatic light hinting algorithm, optimized for non-monochrome displays.</td></tr><tr><td>**Monochrome**</td><td>Force the use of an automatic hinting algorithm optimized for monochrome displays.</td></tr><tr><td>**None**</td><td>Do not use hinting. This generally generates 'blurrier' bitmap glyphs when the glyphs are rendered in any of the anti-aliased modes.</td></tr></tbody></table> |
3637
| **Anti Aliasing** | Enables using anti-aliasing for font characters. Otherwise the font will use monochrome data. |
3738
| **Bold** | Enables an artificial embolden effect. |
3839
| **Italic** | Enables a slant effect, emulating italic style. |
3940

41+
## Bitmap vs MSDF
42+
43+
![Bitmap vs MSDF](media/font-bitma-vs-msdf.png)
44+
45+
The default workflow for fonts uses `Bitmap` mode, which rasterizes character glyphs into a image (stored inside an atlas). Then character image can be sampled directly from it inside GPU shaders (atlas texture uses `R8_UNorm` format - 8-bit red channel). This method is efficient, fast to render and simple to integrate inside graphics pipeline as text sampling requires just a single texture sample.
46+
47+
The most common limitations of this technique are isuses when using large font sizes. Then, character glyphs images need higher resolution to avoid aliasing artifacts, which increases memory usage and limits scalability across different hardware, but even tho they might be visible (eg. when using auto-sized text).
48+
49+
![SDF Font Outline](media/font-msdf-outline.png)
50+
51+
One of the solutions to this is fonts based on Multi-channel Signed Distance Field, shortened as MSDF. Instead of storing font glyph visibility inside a texture, rasterizer outputs multiple distance fields to the glyph edge and stores them in separate texture channels (`R8G8B8A8_UNorm` format - 8-bit red, green and blue channels are used). Then each pixel of such a character image contains signed distance to the glyph edge, which can be used to fill pixels that have a non-positive distance (are inside the glyph). The key advantage of this method is significantly lower resolution requrement to for image to represent fonts in high quality at large scale. Additionally, this technique provides *almost-free* way to implement more advanced text effects such as outline, glow, shadow or procedural texturing.
52+
53+
To learn how to create a custom material with shader for MSDF font see [this tutorial](../tutorials/create-outline-font-material.md).
54+
4055
## Fallback fonts
4156

4257
When font used to draw text doesn't contains certain characters (eg. non-Latin set, emoji or icon) then fallback fonts can be used. Flax supports using one or more fallbacks when the primary font lacks a glyph to be displayed.
819 KB
Loading
19.7 KB
Loading

manual/ui/high-dpi/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ For example, with a DPI scale of 200%, all UI pixels become twice as large as th
66

77
For the most part, you don't have to do anything and Flax takes care of everything for you. However, one thing to watch out for is translating vectors, like the mouse position, between the different 'coordinate spaces'.
88

9+
> [!Tip]
10+
> Running editor or game with `-lowdpi` command line will disable High DPI awareness support.
11+
912
## Screen Space
1013

1114
One pixel in screen space is exactly one pixel on the screen. Screen space also stretches across all monitors.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# HOWTO: How to create an outline font material
2+
3+
In this tutorial, you will learn how to create a font material that can be used to draw MSDF fonts with an outline effect. A similar approach can be used to implement shadow, glow, or procedural texturing using Sign Distant Field data stored for font character glyphs.
4+
5+
## 1. Create a new material asset
6+
7+
**Right click** in the **Content Window** and select option **New -> Material -> Material**. Type a name and confirm with Enter. Double-click on created asset and start editing material.
8+
9+
![New Material](media/outline-font-step-1.png)
10+
11+
## 2. Create a texture parameter named **Font**
12+
13+
![New Parameter 1](media/outline-font-step-2.png)
14+
15+
Scroll down the material properties panel and select the new parameter type **Texture**, then press **Add parameter**. Next **double-click** on a label with created parameter name and rename it to **Font** (Flax uses a parameter named `Font` to bind the font atlas texture during rendering).
16+
17+
## 3. Change Domain to GUI
18+
19+
![Material Domain to GUI](media/outline-font-step-3.png)
20+
21+
Set Material Domain to **GUI** if you want to use this material inside UI Controls. If you want to use this material on Models or Text Render then leave it as default Surface (but use other plug than *Opacity* if material is opaque).
22+
23+
## 4. Set up the material graph
24+
25+
![Material Shader](media/outline-font-step-4.png)
26+
27+
In this step you need to create a material using shader code blocks as shown on a picture above.
28+
29+
Add a new **Custom Global Code** node and set it;s **Location** to **Includes**, then write the following code there:
30+
31+
```
32+
#include "./Flax/GUICommon.hlsl"
33+
```
34+
35+
This will ensure that various GUI and font sampling utilities will be included to use inside a shader file.
36+
37+
Then add similar node **Custom Code** with the following code:
38+
39+
```
40+
// Get closest distance to the font glyph
41+
Output0 = GetFontMSDFMedian(Input0).xxxx;
42+
```
43+
44+
Finally, drag and drop `Font` parameter (from proprties panel on the right) to sample the font texture. Plug the font `Color` output into the first `Input0` of the Custom Code node, then connect `Output0` from that node to the output `Opacity` of the material.
45+
46+
## 5. Assign the material
47+
48+
![Set up label](media/outline-font-step-5.png)
49+
50+
Now, create a new `Label` control, set it's Font to asset that uses `MSDF` as `Raster Mode` and plug the created material into the `Material` property. Ensure that font size is not too small to ensure SDF data is correct. Values around 20 are usually a good starting point.
51+
52+
Now, you should see a basic SDF font rendering that will look like a shadow-like gradient of the text:
53+
54+
![SDF Font Text Gradient](media/outline-font-step-6.png)
55+
56+
## 6. Make an outline
57+
58+
![SDF Font Text Gradient](media/outline-font-step-7.png)
59+
60+
Finally, let's pump up the shader to draw an outline of the text and use color from vertex, which will be passed by rendering system directly from the control rendering.
61+
62+
Edit the *Custom Code* node:
63+
64+
```
65+
// Get closest distance to the font glyph
66+
float thickness = 4; // Can be exposed as a parameter
67+
Texture2D font = In1; // Hack to get Font texture parameter
68+
Output0 = SampleFontMSDFOutline(font, input.TexCoord, thickness);
69+
```
70+
71+
Final result:
72+
73+
![SDF Font with Outline](../fonts/media/font-msdf-outline.png)

manual/ui/tutorials/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [How to create UI](create-ui.md)
44
* [How to create UI from code](create-ui-from-code.md)
55
* [How to create a custom font material](create-font-material.md)
6+
* [How to create an outline font material](create-outline-font-material.md)
67
* [How to create a custom GUI material](create-gui-material.md)
78
* [How to blur UI panel background](blur-background.md)
89
* [How to create a Main Menu](create-main-menu.md)
49.8 KB
Loading
293 KB
Loading
116 KB
Loading

0 commit comments

Comments
 (0)