1.0.0 • Published 1 year ago

three-msdf-text v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

three-msdf-text

build version

Demo - Documentation - Repository

Provide geometry and material for MSDF (multi-channel signed distance fields) in THREE.js.

Features:

  • Many advanced attributes
  • Support all THREE.js Material
  • Typescript
  • No dependencies
  • Geometry subdivision

How to use

;(async () => {
  const textureLoader = new THREE.TextureLoader()
  const fontLoader = new THREE.FontLoader()

  const atlas = textureLoader.loadAsync('./font/font.png')
  const font = fontLoader.loadAsync('./font/font.fnt')

  const geometry = new TextGeometry({
    font,
    text: 'Hello world',
  })

  const material = extendMSDFMaterial(
    new THREE.MeshBasicMaterial({
      color: 0xffffff,
      transparent: true,
    }),
    { atlas },
  )

  const mesh = new THREE.Mesh(geometry, material)
})()

Attributes

You can use a lot of different attributes depending on your needs :

AttributeSizeRequiredEnabledDescription
id1truetrueUsed for face indexing
position3truetrueThe classic position attribute used in three.js
charUv2truetrueUV coords used in the font atlas
uv2falsefalseThe classic uv attribute used in three.js
normal3falsefalseThe classic normal attribute used in three.js
lineIndex1falsefalseSee schema below
charIndex1falsefalseSee schema below
wordIndex1falsefalseSee schema below
charSize2falsefalseSee schema below
lineCharIndex1falsefalseSee schema below
lineWordIndex1falsefalseSee schema below
lineCharCount1falsefalseSee schema below
lineWordCount1falsefalseSee schema below

For example, with a THREE.MeshStandardMaterial material, you will need uv and normal attributes.

const geometry = new TextGeometry({
  ...options,
  uv: true,
  normal: true,
})

width, height

The properties width and height are automatically computed. The uv attribute is interpolate based on the width and height values. You may want to force the width value to wrap automatically. You may also want to force width and height values to correctly interpolate uv attribute.

Contributing

Please refer to the contribution guidelines for details.

Unlicense

This library is unlicensed.

The original code that this library was written by ogl contributors and is unlicensed.