in webgl-threejs

NeHe Lessons 14 and 15 – Vector Fonts

Vector Font with Custom Shader

A Vector Font with Custom Shader

Introduction

This post is a “twofer” – it’s about vector fonts, but we cover them in two steps.  First, a simple vector font with solid colors (Lesson 14), then using a vector font and mapping a texture onto the font with a custom shader (Lesson15).  This is the first foray into shaders, but won’t be the last. So let’s take a look.

A Simple Vector Font

Using vector fonts with three.js is very simple:

  • Get a JSON-based vector font
  • Load the font with the three.js font-loader
  • Construct a FontGeometry, specifying a bunch of parameters
  • Create a mesh with the geometry and a material

Now let’s take a look at each of them.

Getting JSON-based Fonts

You can convert fonts to the necessary JSON format at facetype.js.  Facetype doesn’t provide any fonts – so you need to upload your fonts (in OpenType, TrueType or WOFF format) to the site and they will convert them to JSON so you can download them.

Getting fonts is easy.  There are many places to get “free” fonts.  You can go to  Google fonts (all of which are free of licensing) or Fonts101.com.   Do not simply upload the fonts on your system as most of those are protected fonts which, though they might be converted by facetype, are protected fonts in the sense that the font foundries (.g. MonoType, Adobe, etc.)  have invested a LOT of time and money in them and they are licensed to your system – not you. So go get some free fonts.

Loading the Font

Loading the font is easy, as three.js provides a “font loader”.  See the lesson for more information.

Constructing a Font Geometry

Constructing a font geometry in three.js is pretty trivial.  Here is a typical call:

var loader = new THREE.FontLoader();
loader.load( fontName, function ( font ) {

    textGeom = new THREE.TextGeometry(text, {
        size: size,                    // actually the height of the font, in user-space
        height: height,                // THICKNESS of the extruded font, in user-space
        curveSegments: curveSegments,
        font: font,                    // the THREE.font object
        bevelThickness: bevelThickness,
        bevelSize: bevelSize,
        bevelEnabled: bevelEnabled
    });

    var textMaterial = new THREE.MeshFaceMaterial(materialArray);
    var textMesh = new THREE.Mesh(textGeom, textMaterial);
}

Note that the FontLoader takes the name of the font and calls back to the load() method when the font has been loaded.  Finally, three.js uses  the outline of the glyph (letter) as a shape and they extrudes it to form the 3D shape.

Note one misleading wackiness – what three.js calls the size of the font is the font height and what it calls the height is the thickness of the extruded font.

Then one creates a mesh with an array (the example above uses an array so the face and sides are different colors but that’s a detail.

Creating a Shader Material for the Font

Finally, the second example, Lesson 15, uses a custom shader to map two different textures onto the font.  Nothing spectacular here – about the most basic  shader one could create, but it does constitute and introduction to the world of shaders.  Lots more details in the lesson itself.

You can find the Lesson 14 at Geo-F/X here.   Lesson 15 is here.  As always, the sources are on github here. Feel free to contact me at rkwright@geofx.com or comment on this article directly.

Write a Comment

Comment