Mesh Writer

Generate letters in BABYLON meshes.


Writer = BABYLON.MeshWriter(scene, {scale:scale});
text1 = new Writer(
"anchor": "center",
"letter-height": 50,
"color": "#1C3870",
"position": {
"z": 20

PG: Mesh Writer Example 1

Getting the code

Copy or and build it into your load sequence.

If you prefer npm

npm i meshwriter

Import in your React/Angular component:

import { MeshWriter } from "meshwriter";

With meshwriter you can use MeshWriter directly like this:

let Writer = MeshWriter(this.scene, { scale: .25, defaultFont: "Arial" });
let textMesh = new Writer("Hello World", {
"font-family": "Arial",
"letter-height": 30,
"letter-thickness": 12,
color: "#1C3870",
anchor: "center",
colors: {
diffuse: "#F0F0F0",
specular: "#000000",
ambient: "#F0F0F0",
emissive: "#ff00f0"
position: {
x: 0,
y: 10,
z: 0

Superconstructor - BABYLON.MeshWriter()

After MeshWriter is loaded, BABYLON.MeshWriter is defined. It is called with one or two parameters.

  • scene   required

  • preferences   optional   The preferences object may specify up to three values

    default-font Helvetica
    scale 1
    letter-origin "letter-center"

The call to BABYLON.MeshWriter returns a constructor. Call it "Writer".

Constructor - new Writer()

new Writer() is called with a string and an (optional) options parameter.  The options object conforms to normalBabylon.js structures and terminology.

font-family default-font
anchor left
letter-height 100
letter-thickness 1
color #808080 # hits emissive only
alpha 1
x 0
y 0
z 0
colors # if you need to control more than just emissive
diffuse #F0F0F0
specular #000000
ambient #F0F0F0
emissive color # from option field 'color' above

new Writer() builds a mesh with material that is inserted into the scene.  This is a multi-step process with interim meshes and holes per letter.  These meshes are sucked into an SPS and then disposed.  At the end, one mesh, one material and one SPS have been added to the scene.

new Writer() also returns a writer instance with useful methods.  See below.


Each writer instance has methods to allow one to retrieve theBabylon.js objects or to get/set attributes of the SPS.

color # sets or gets color but no change to material
alpha # sets or gets alpha but no change to material
setColor # set emissive color and change color value
setAlpha # change value and material
overrideOpac # change material but not value
resetOpac # sets material to current value

Usage Hints

If you wish to do extensive things with position, rotation or animation, retrieve the meshes and materials from the instance using the methods shown above.  The output from new Writer() is an SPS with one particle for each character.

Example: PG: Mesh Writer Example 2

const SCALE =.25
const TEXT_SIZE = 35
Writer = BABYLON.MeshWriter(scene, {scale:SCALE,defaultFont:"Arial"});
textMesh = new Writer(
anchor: "center",
"letter-height": TEXT_SIZE,
"letter-thickness": TEXT_THICKNESS,
color: "#1C3870",
diffuse :"#F0F0F0",
specular :"#000000",
ambient :"#F0F0F0",
emissive :"#ff00f0"
position: {
z: 70
//Text Writer create SPS with Particle for each letter
SPS = textMesh.getSPS()
//Update animation
SPS.updateParticle = (particle)=> {
particle.rotation.x -= 0.06;
scene.registerBeforeRender( ()=> {
//sps.mesh.rotation.y = k;

Colors:  With most lighting, it is enough just to use the "color" field to specify the letter coloring.  However, programmers may specify all four color types by putting a "colors" object in the options object.

Unless you have a specific need, do not specify a font.  The default font, Helvetica, has the most extensive characters and the fewest faces; it will be the most efficient if you have a lot of text.  Jura was added because the author likes it for numbers.