TextRenderBehavior
is a rendering behavior that enables MSDF (Multi-channel Signed Distance Field) text rendering in a WebGPU-powered game engine.
It supports dynamic text updates, color changes, and transformations.
The TextRenderBehavior
class extends RenderBehavior
and integrates the MSDF font rendering system. It consists of:
A Signed Distance Field (SDF) is a texture where each pixel stores the shortest distance to the nearest edge of a shape. Positive values represent points inside the shape, and negative values represent points outside. This approach allows rendering text at multiple resolutions without the need for high-resolution bitmaps.
The Multi-Channel Signed Distance Field (MSDF) extends SDF by encoding distance information in multiple channels (RGB). This technique helps preserve corners and fine details of glyphs, addressing the common blurring and deformation issues seen in traditional SDF.
To render MSDF-based text, a fragment shader reconstructs the glyph shape using the RGB distance values. The approach is:
asyncInit()
Loads the font asynchronously and initializes text rendering resources.
refreshText()
Recalculates the formatted text whenever the text string or properties change.
createFont()
Loads the font texture, character data, and kernings from the font JSON file. A kerning is the space between two characters in a font.
formatText()
Converts the given string into a buffer of vertex data for rendering.
measureText()
Computes text dimensions for layout and alignment.