MSDF TextRenderer - Mitigation option against alpha fluctuation on 1px text strokes

Issue:
At high camera distances where text strokes become 1 pixel wide, alpha aliasing causes harsh pixel-to-pixel brightness fluctuations.

Feature Request:

  • An automatic or toggleable rendering mode optimized for small text at a distance.
  • Or options to customize the distance/texture read to output alpha curve.
1 Like

cc @ryantrem

Does it help in your case to increase thicknessControl when the text is further away? Do you have a Playground you can share that clearly demonstrates the issue?

I think @ryantrem’s suggestion to try thicknessControl is a useful first test, but it would help to distinguish thin strokes from temporal aliasing.

When a glyph stem is projected onto roughly one screen pixel, even a small subpixel movement can change its coverage quite noticeably. Making the stroke thicker may improve readability without necessarily eliminating the brightness fluctuation during camera movement.

One thing I’d compare in the Playground is the same text at a fixed camera position versus a very slow camera movement. If it only flickers while moving, that would point more toward a screen-space sampling issue than a simple thickness problem.

I’d also try a slightly positive thicknessControl value and repeat the test at a higher rendering resolution, keeping the camera and text geometry unchanged.

For an automatic solution, I wonder whether projected glyph size in pixels would be a better input than camera distance alone. The same text at the same distance can occupy very different numbers of pixels depending on the FOV and rendering resolution.

If thickness adjustment improves visibility but doesn’t stabilize the edges, a screen-space-aware smoothing approach might be worth investigating.

Does the fluctuation happen when the camera is completely stationary too, or mainly during movement?

Thickness control can improve the readability somewhat, but it stays significantly less readable than a text mesh.
It can shift the stroke variation from a 0–1 pixel range to a 1–2 pixel range for the smallest text, where this inconstancy in stroke width affects the readability. Increasing the thickness more, changed the looks of the text quite a bit, plus if the text is small enough caused empty space between the strokes to be eliminated.

For performance reasons, we prefer MSDF text over text meshes and small MSDF text can be quite readable, as demonstrated in this Three.js example: MSDF Font Generator.

After loading a font, it shows a “small text” option at the left bottom of the page. You can see how it makes small text perfectly readable, with the tradeoff that the text becomes more translucent.
Applying it to larger text shows its effect workings. It creates a significantly bigger transition in alpha from the edge of the text to the center of the text.

Since we ran into the blocking issue of readability when the camera and text are completely stationary, we have not looked yet into the effects when there is movement.

@kay.goossen I tried to create a repro from your description here: https://playground.babylonjs.com/#8WN19Q#0

Does that demonstrate the problem you are describing? Can you also take a look at this Babylon Lite Playground? Babylon Lite Playground

Babylon Lite has a completely different text rendering solution. It is based on slug, a text rendering technique used in a lot of AAA games. The patent was lifted on it earlier this year so we created an implementation in Babylon Lite. We have plans to port it back to BJS as well, but haven’t done so yet, so I’m curious if the issues you are facing are solved by a slug-based text renderer. Generally, slug is faster, uses far less gpu memory, and is higher quality compared to MSDF.

This appear to be what I encounter as well. For now I just increase text size with distance, but this is far from a good solution. Thanks for the good work on the comparison!

Thank you. That demonstration captures our exact issue. While adjusting text thickness can create an okay looking text in the 5x zoom view, in the 1x zoom view the smallest two text sizes stay compromised in readability. Next to this it also shows another issue I was running against: The okay thickness range for small text is smaller than other text rendering options, making it harder to visually separate bold from non-bold text.

While slug is a nice alternative visually, I assume rendering performance will be noticeable worse than MSDF. We have thousands of active text labels, so we were looking into switching our text meshes to MSDF text, to improve performance. Slug might be good enough (I don’t know how the rendering performance compares with text meshes), but the option to improve MSDF small text readability would be ideal for us.

Clamping the screen-space partial derivatives (fwidth) and adding a sub-pixel supersampling bias in the fragment shader makes a huge difference for 1px technical strokes in MSDF text rendering!

We run into the exact same alpha shimmer when rendering ISO 3098 engineering dimension callouts and GD&T tolerance frames from 2D/3D DXF drawings inside WebGL viewports during slow camera pans. Pairing MSDF glyph atlases with analytic screen-space stroke width clamping (max(screenPxDistance, 0.65)) keeps thin technical annotations crisp even at 100% browser zoom without needing 4x MSAA on the entire render target. For anyone mapping CAD text entities (TEXT / MTEXT group codes and stroke fonts) into WebGL/Babylon text renderers, we maintain a reference on DXF Entity Group Codes & Technical Typography Specifications.

Thanks @cadguide, do you have code you are willing to share? If there is something relatively simple we can do to improve msdf without bloating the bundle or significantly impacting the perf, we could for sure improve it. Otherwise, I think porting slug text rendering back to BJS could be a good option as well. It’s true that slug text rendering is more costly on the gpu than the current msdf implementation, but for small amounts of text they are both very fast, and slug has a lot of advantages over msdf.