extensions

Mesh Writer Introduction


Mesh Writer

Generate letters in BABYLON meshes.

Basic-Usage

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

● See playground example: https://www.babylonjs-playground.com/#PL752W#1 -


Getting the code

Copy https://github.com/BabylonJS/Extensions/blob/master/MeshWriter/meshwriter.min.js or https://github.com/BabylonJS/Extensions/blob/master/MeshWriter/meshwriter.js and build it into your load sequence.

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

        FIELD                 DEFAULT
      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 normal BabylonJS structures and terminology.

      FIELD                 DEFAULT
    font-family             default-font
    anchor                  left
    letter-height           100
    letter-thickness        1
    color                   #808080              # hits emissive only
    alpha                   1
    position
        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.

Instance

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

   getSPS
   getMesh
   getMaterial
   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
   dispose                 

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.

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.