Text rendering
Text rendering turns text and font data into visual output for the GPU. The engine supports screen-space text for labels and data views, plus mesh-based 3D text for scene content. Both paths share font metrics and layout ideas so text can keep a stable position and size as it moves through the render system.
System role
The text system sits between application text and the render pipeline. It measures text, maps Unicode characters to glyphs, prepares shared font data, and exposes renderable GPU data or mesh geometry.
The system has two main paths:
- 2D text uses a font atlas, which stores glyph images and their metrics. It places glyphs in screen space and samples the atlas during a GPU draw.
- 3D text uses glyph outlines. It turns those outlines into extruded mesh geometry that can take part in normal scene rendering.
The text feature is part of the engine. The parent topic, Data and visualization assets, explains how text fits with other visual assets. 3D text can join the scene graph and Three.js integration. Text can label assets such as Gaussian splats, while text data can arrive through the data interchange formats used by visualization systems.
2D text pipeline
2D rendering changes Unicode text into positioned glyph data. The renderer combines that data with a shared atlas texture and draws it through the device render pass. A reusable code-point source can also convert each input row to display order before layout, so layout does not depend on a particular text-row container.
This diagram shows the main path from text content to GPU output.
flowchart LR
A[Text content] --> B[Font metrics and glyph lookup]
B --> C[Line layout and alignment]
C --> D[Prepared glyph data]
D --> E[Shared font atlas texture]
D --> F[GPU text model]
E --> F
F --> G[GPU render pass]
G --> H[Screen-space text]
Font atlases
A font atlas is the shared contract between font preparation, layout, and GPU sampling. It contains glyph frames, advances, line metrics, optional kerning, image pages, and the sampling settings needed to interpret bitmap, SDF, or MSDF data.
The engine can create an atlas from a browser font or load an MSDF atlas from a BMFont descriptor and its image pages. Browser builders can reuse an atlas for equal font settings and extend it when new characters are needed. A custom glyph renderer can provide source-specific raster data while keeping the same layout metrics. Browser builders need a canvas-capable environment because they measure and rasterize the source font. Atlas mappings can also carry a glyph's page and its layout offsets, which keeps the sampled image aligned with the pen and baseline.
The engine can also load glyphs from a remote range service. GlyphRangeLoader groups Unicode code points into ranges, shares concurrent requests, and keeps completed ranges in a bounded cache. The resulting glyphs can be packed into a caller-owned SDF atlas. Missing glyphs do not silently come from a local font, so the caller keeps control of the available character set.
References: modules/…/font-atlas.ts• L7-55 modules/…/text-utils.ts• L6-23 modules/…/text-utils.ts• L63-71 modules/…/browser-font-atlas.ts• L11-168 modules/…/lru-cache.ts• L5-36 modules/…/build-bitmap-font-atlas.ts• L12-29 modules/…/build-sdf-font-atlas.ts• L16-67 modules/…/build-msdf-font-atlas.ts• L46-177 modules/…/glyph-range-loader.ts• L6-143 modules/…/build-glyph-font-atlas.ts• L12-73
Text layout
Text layout consumes a row-oriented Unicode code-point source. createTextCodePointSource adapts one-line string rows and can apply a caller-supplied display-order conversion once per row; without that conversion, rows must already be in display order. Layout then walks each code point, resolves it to a glyph frame, atlas page, offset, and advance, and applies pair kerning plus the requested horizontal anchor and vertical baseline. The result includes row advances and ink bounds that callers can use for placement or culling.
A character that is not present in the atlas receives a defined fallback advance. This keeps preparation predictable while making the missing glyph visible as a data-quality issue rather than silently changing the local font. The same layout rules also provide text measurement, so reported width and bounds match the positions used for rendering.
References: modules/…/text-code-point-source.ts• L7-33 modules/…/text-layout.ts• L22-61 modules/…/text-utils.ts• L25-71 modules/…/text-glyph-walker.ts• L13-124 modules/…/text-metrics.ts• L8-40 modules/…/text-layout.ts• L94-350
GPU preparation
GPU preparation chooses a representation that matches the input and the device path. It can expand rows into glyph instances, retain compact row and glyph data for WebGPU expansion, or store one prepared glyph run for each distinct string.
The shared GPU resources upload all atlas pages once as a 2D-array texture. Multiple prepared batches and renderers can borrow that texture. The atlas render settings travel with the prepared data so the fragment shader uses the correct bitmap, SDF, or MSDF sampling behavior.
Large inputs can split into several render batches when device buffer limits require it. The system keeps each batch paired with its row data and glyph data, so one logical text set can still produce ordered draws.
References: modules/…/gpu-text-data.ts• L11-80 modules/…/gpu-text-resources.ts• L10-53 modules/…/gpu-text-types.ts• L5-120 modules/…/text-layout.ts• L94-350 modules/…/text-fragment-shaders.ts• L5-115
Text drawing and picking
TextRenderer is the stable drawing boundary for prepared 2D text. It selects the internal model that matches the prepared strategy, draws all render batches, and can draw a separate picking model when the host supplies one. It borrows prepared data, so the caller controls the lifetime of that data.
The attribute path keeps direct glyph instances with the source rows. The storage paths keep compact glyph data and row bindings for WebGPU. The dictionary path stores one glyph run per distinct string and uses row references, which avoids expanding repeated values before culling or drawing. Storage and dictionary models require WebGPU.
The public 2D surface is exported from the engine text entry point. It includes the atlas, layout, code-point source, resource, and renderer contracts. The experimental entry point exposes lower-level strategies for diagnostics, benchmarks, and host integrations that need direct control over preparation.
References: modules/…/text-renderer.ts• L12-140 modules/…/text-attribute-model.ts• L19-145 modules/…/text-storage-model.ts• L38-153 modules/…/text-dictionary-model.ts• L29-140 modules/…/dictionary-text-renderer.ts• L14-168 modules/…/experimental.ts• L5-57 modules/…/index.ts• L5-50 modules/…/index.ts• L5-44
3D text pipeline
3D text follows a geometry path because glyphs must exist in world space rather than only as screen-space quads. A typeface definition supplies glyph outlines and advances. The engine lays out lines, turns outlines into shapes, triangulates those shapes, and extrudes them into a mesh with positions, normals, and texture coordinates.
This diagram shows the 3D path from a typeface definition to scene geometry.
flowchart LR
A[Typeface data] --> B[Glyph outline lookup]
B --> C[Line layout and alignment]
C --> D[Glyph shapes]
D --> E[Triangulated faces]
E --> F[Extruded mesh]
F --> G[Scene render pipeline]
Typeface geometry
Font measures glyph advances and line height at the requested size. It supports multiple lines and left or centered alignment, then converts outline commands into shape paths. A glyph atlas can build shared geometry for the characters used by many text instances and retain each glyph's range, advance, and bounds.
TextGeometry creates one mesh for a complete string and reports its bounds. The resulting geometry can use the same scene transforms, materials, lighting, and bounds work as other mesh assets.
References: modules/…/typeface-font-data.ts• L6-43 modules/…/helvetiker.ts• L7-16 modules/…/font.ts• L18-110 modules/…/glyph-atlas.ts• L20-100 modules/…/text-geometry.ts• L13-71
Shape and extrusion flow
Glyph outlines are represented as paths made from lines and curves. Separate sub-paths become outer contours or holes. The shape stage closes and orients those contours before triangulation, then the extrusion stage creates front, back, and side surfaces with normals and UVs.
Extrusion options control depth, curve sampling, and optional bevels. This makes the same typeface useful for flat scene labels or solid 3D lettering without changing the font data.
References: modules/…/curves.ts• L12-145 modules/…/path.ts• L11-114 modules/…/shape-path.ts• L11-112 modules/…/shape-utils.ts• L11-52 modules/…/extrude.ts• L11-63
Render modes
The engine supports several text representations. The choice changes where work happens and how repeated content is stored, while all 2D modes use the same atlas-backed layout rules.
| Mode | Main use | Result passed to the GPU |
|---|---|---|
| Bitmap atlas | Direct browser-font coverage | Atlas pages and glyph positions |
| SDF atlas | Scalable single-channel glyph edges | Atlas pages and distance sampling settings |
| MSDF atlas | Scalable detailed glyph edges from external font assets | Atlas pages and multi-channel distance settings |
| Attribute 2D text | Direct row-to-glyph drawing | Expanded glyph instances and source attributes |
| Storage 2D text | Large WebGPU text sets | Compact glyph data and row bindings |
| Dictionary 2D text | Many repeated strings or filtered rows | Shared dictionary glyph runs and row references |
| 3D geometry | Text that belongs in world space | Extruded mesh attributes |
The engine exports 2D text and font helpers from the text entry points. The 2D surface also exposes a code-point source for adapting row data to layout. The 3D path has its own entry point, which lets applications choose screen-space or world-space output without changing the wider scene pipeline.
References: modules/…/index.ts• L5-50 modules/…/index.ts• L5-58 modules/…/index.ts• L7-28 modules/…/text-fragment-uniforms.ts• L7-45
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
FontAtlasmapping, page order, metrics, and render settings aligned. Layout and shader sampling use the same atlas contract. - Keep character page and layout-offset defaults compatible with older one-page mappings. Layout and atlas sampling must resolve the same glyph location and baseline.
- Keep browser atlas cache identity tied to every setting that changes glyph measurement, raster data, or distance-field sampling.
- Keep custom glyph bearings and advances aligned with the atlas mapping. Raster offsets must not change the intended baseline or row width.
- Keep SDF padding aligned with the distance-field settings. The sampling halo must not change glyph advances or baseline placement.
- Keep Unicode traversal, display-order conversion, kerning, anchor, baseline, and missing-character behavior aligned across expanded, storage, and dictionary preparation.
- Keep dictionary ranges and row references consistent when repeated-string preparation changes. GPU culling uses those ranges to select glyph runs.
- Keep each render batch paired with its row bindings and glyph buffers when device limits split a prepared state.
- Keep glyph-range URL encoding, range boundaries, and code-point selection aligned between the loader and atlas builder.
- Keep shared atlas texture lifetime aligned with every borrowing batch and renderer. Release borrowers before the shared texture.
- Keep typeface advances and line height aligned with shape generation. Geometry bounds and line placement depend on both.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Browser atlas construction runs without a 2D canvas context | Font measurement and glyph rasterization cannot start | Run browser atlas builders in a canvas-capable environment |
| A cache key omits font, size, padding, or distance-field settings | A request can reuse glyph data made for different metrics or sampling rules | Include all metric and sampling inputs in the cache identity |
| A custom atlas shares a key while its renderer metrics change | Later requests can use the wrong advances or images | Omit the key for independent custom atlases or include all renderer inputs |
| MSDF metadata, page count, or page dimensions do not match the loaded images | Glyph sampling can fail or use the wrong page coordinates | Validate the descriptor and resolve image pages from its descriptor URL |
| Text uses a character that is not in the atlas | The glyph has no image while the row still receives a fallback advance | Provide the required characters or set an intentional missing-character width |
| A glyph mapping uses the wrong page or omits a non-default layout offset | Text can sample another atlas layer or appear shifted from its baseline | Preserve page and layout-offset data when building and consuming mappings |
| A row is not in display order and no display-order processor is supplied | Glyphs render in source order instead of the intended visual order | Convert each row before layout or provide the processor to createTextCodePointSource |
| A glyph-range URL does not encode font-stack names or uses invalid range values | Requests can target the wrong font data or fail before loading | Use the loader's {fontstack} and {range} template contract |
| A storage or dictionary model runs on a non-WebGPU device | Model creation fails because its GPU path is unavailable | Use the attribute path or require WebGPU for storage-based text |
| A render batch uses row bindings or glyph data from another batch | Text can use wrong positions, styles, or draw counts | Preserve the batch-to-resource pairing during splitting and appending |
| A renderer or prepared batch is destroyed before its borrowed atlas resources | Later draws use released GPU resources | Destroy borrowing renderers and prepared batches before shared resources |
| A typeface has no glyph or usable fallback for a source character | 3D shape generation cannot resolve the character | Include the character in the typeface or provide a fallback glyph |
Key files
| File | Why to read it |
|---|---|
| modules/…/text-renderer.ts | Stable 2D drawing, picking, and ownership boundary |
| modules/…/gpu-text-data.ts | Common prepared-data contract and strategy statistics |
| modules/…/text-code-point-source.ts | Adapter for row text and optional display-order conversion |
| modules/…/text-layout.ts | Shared conversion from text rows to glyph data |
| modules/…/gpu-text-resources.ts | Shared atlas upload and GPU resource lifetime |
| modules/…/text-attribute-model.ts | Direct glyph drawing and render-batch handling |
| modules/…/text-storage-model.ts | Compact WebGPU text drawing and batch handling |
| modules/…/text-dictionary-model.ts | Repeated-string drawing through dictionary ranges |
| modules/…/font-atlas.ts | Common glyph image and metric contract |
| modules/…/text-utils.ts | Glyph mapping, page, offset, and kerning contracts |
| modules/…/browser-font-atlas.ts | Browser font measurement, packing, caching, and incremental atlas growth |
| modules/…/glyph-range-loader.ts | Remote glyph range loading and request sharing |
| modules/…/build-msdf-font-atlas.ts | External MSDF descriptor and page loading |
| modules/…/text-glyph-walker.ts | Canonical Unicode, kerning, alignment, and advance behavior |
| modules/…/font.ts | Typeface lookup, line layout, and outline shape creation |
| modules/…/glyph-atlas.ts | Shared 3D glyph geometry and positioned instances |
| modules/…/text-geometry.ts | Conversion from text shapes to renderable 3D mesh geometry |
| modules/…/extrude.ts | Shape extrusion into mesh attributes |