Skip to content

atlmtw/three-bmfont-text-es

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

three-bmfont-text

This is basically an ES version of mattdesl's original npm package at https://jam3.github.io/three-bmfont-text/

Main fixes:

  • Allows for "import"
  • Made position to be vec3 to allow for computing bounding box

unstable

(click for demo) - (source)

Bitmap font rendering for ThreeJS, batching glyphs into a single BufferGeometry. Supports word-wrapping, letter spacing, kerning, signed distance fields with standard derivatives, multi-channel signed distance fields, multi-texture fonts, and more. About 12kb after minification.

The is version should work with Three JS Version 145

Below is an example that uses load-bmfont to parse BMFont files on the fly with XHR:

import { createTextGeometry } from 'three-bmfont-text-es';
import * as loadFont from "load-bmfont";
import { Mesh, TextureLoader, MeshBasicMaterial } from 'three';

loadFont('fonts/Arial.fnt', function(err, font) {
  // create a geometry of packed bitmap glyphs, 
  // word wrapped to 300px and right-aligned
  var geometry = createTextGeometry({
    width: 300,
    align: 'right',
    font: font
  })

  // change text and other options as desired
  // the options sepcified in constructor will
  // be used as defaults
  geometry.update('Lorem ipsum\nDolor sit amet.')
  
  // the resulting layout has metrics and bounds
  console.log(geometry.layout.height)
  console.log(geometry.layout.descender)
    
  // the texture atlas containing our glyphs
  var textureLoader = new TextureLoader();
  textureLoader.load('fonts/Arial.png', function (texture) {
    // we can use a simple ThreeJS material
    var material = new MeshBasicMaterial({
      map: texture,
      transparent: true,
      color: 0xaaffff
    })

    // now do something with our mesh!
    var mesh = new Mesh(geometry, material)
  })
})

The glyph layout is built on layout-bmfont-text.

Usage

NPM

geometry = createText(opt)

Returns a new BufferGeometry with the given options.

Note: The options set in the constructor become the defaults for any subsequent calls to update().

opt can be an options object, or a String – equivalent to { text: str }.

Options specific to ThreeJS:

  • flipY (boolean) whether the texture will be Y-flipped (default true)
  • multipage (boolean) whether to construct this geometry with an extra buffer containing page IDs. This is necessary for multi-texture fonts (default false)

The rest of the options are passed to layout-bmfont-text:

  • font (required) the BMFont definition which holds chars, kernings, etc
  • text (string) the text to layout. Newline characters (\n) will cause line breaks
  • width (number, optional) the desired width of the text box, causes word-wrapping and clipping in "pre" mode. Leave as undefined to remove word-wrapping (default behaviour)
  • mode (string) a mode for word-wrapper; can be 'pre' (maintain spacing), or 'nowrap' (collapse whitespace but only break on newline characters), otherwise assumes normal word-wrap behaviour (collapse whitespace, break at width or newlines)
  • align (string) can be "left", "center" or "right" (default: left)
  • letterSpacing (number) the letter spacing in pixels (default: 0)
  • lineHeight (number) the line height in pixels (default to font.common.lineHeight)
  • tabSize (number) the number of spaces to use in a single tab (default 4)
  • start (number) the starting index into the text to layout (default 0)
  • end (number) the ending index (exclusive) into the text to layout (default text.length)

geometry.update(opt)

Re-builds the geometry using the given options. Any options not specified here will default to those set in the constructor.

This method will recompute the text layout and rebuild the WebGL buffers.

opt can be a string, which is equivalent to:

geometry.update({ text: 'new text' })

geometry.layout

This is an instance of layout-bmfont-text. This supports metrics for descender, baseline, xHeight, width, height, capHeight, etc.

geometry.visibleGlyphs

A filtered set from geometry.layout.glyphs intended to align with the vertex data being used by the underlying BufferAttributes.

This is an array of { line, position, index, data } objects, see here. For example, this could be used to add a new BufferAttribute for line offset.

Demos

To run/build the demos:

git clone https://github.com/Jam3/three-bmfont-text.git
cd three-bmfont-text
npm install

Then choose one of the demos to run:

# 3D SDF rendering
npm run test-3d

# 2d bitmap rendering
npm run test-2d

# 2D MSDF rendering
npm run test-msdf

# multi-page rendering
npm run test-multi

# custom text shaders
npm run start

Open up localhost:9966 (it may take a few seconds for the initial bundle). Then when you save the corresponding JS file (in test/) it should re-bundle and trigger a live-reload event on the browser.

To build the distribution demo:

npm run build

Help

Asset Handling

See docs/assets.md

(Multi-)Signed Distance Field Rendering

See docs/sdf.md

Multi-Texture Rendering

See docs/multi.md

See Also

See text-modules for more text and font related tools.

License

MIT, see LICENSE.md for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published