Text
Keywords:
tutorial,
X3D,
world,
rendering
Author(s): Sabine Webel, Yvonne Jung
Date: 2007-03-02
Summary: This tutorial shows how to use Text in a 3D scene.
Introduction
It's often useful to add text to the scene, e.g. for the labeling of objects or marking of nameplates. Instant Player supports different ways to put text in a scene. There are two types of Text nodes which are processed as geometry nodes: SolidText and PixelText.
Another possibility is the use of a TextTexture node that is a texture filled by the given text-string. The FontStyle node is another important node you need to know about in order to adapt the text to the given use case. This tutorial shows you how to utilize the different types of Text nodes.
Text basics
The easiest way to add text in your scene is to use a Text geometry. This type of text is processed as geometry. Therefore it is placed in the geometry field of a Shape node. If you put a Text node (without specifying the type of text to use) in the Shape, SolidText is used as default. The text strings are contained in the string field. Each string is displayed as a seperate line of text. The appearance of the text is defined in the Appearance of the Shape.
Code: A default text geometry
Shape { appearance Appearance { diffuseColor 1 0 0 } geometry Text { string [ "Default Text." "New line of default Text." ] } }
In order to modify some font attributes of the text you must put a FontStyle into the fontStyle field of your text node. The FontStyle node defines the font size, font family (e.g. SANS), style (e.g. ITALIC), the direction of the text string (horizontal or vertical), how the text should be justified, and any language-specific rendering techniques that must be used for non-English text.
The following example shows how to use a FontStyle. A more detailed discussion of its usage can be found in the text component section of the X3D specification.
Code: A text geometry with a font style
Shape { appearance Appearance { diffuseColor 1 0 0 } geometry Text { string "Italic Typewriter Font" fontStyle FontStyle { family "TYPEWRITER" style "ITALIC" size 2.5 } } }
SolidText and PixelText
As described above, the SolidText geometry is the default text, that is used when no explicit type is given. This text consists of a 3D geometry. The depth of the text (i.e. its dimension in the z direction) is indicated by the depth field.
The PixelText is a special kind of geometry containing a texture, that provides all letters of the alphabet, and a rectangular geometry for each letter of the text where the corresponding part from the texture is mapped onto. This type of text is usually found in computer games, because it is very efficient, but it has a lower visual quality than the SolidText. An example file can be found below.
TextTexture
Slightly different but often more general applicable is the TextTexture node. Unlike the other types this type of test is not a geometry but a material; more precisely it is a Texture, which is filled by the text string using the given fontStyle.
Code: Text in a texture
Shape { appearance Appearance { texture TextTexture { string "TextTexture example" fontStyle FontStyle { family "Arial" style "PLAIN" } } } geometry IndexedFaceSet { ... } }
The TextTexture has one interesting MFVec2f output field, the textureBounds. Because you can't know the image size in advance due to the fact, that the texture is assempled during runtime based on the chosen text, font style etc., the text will probably look skewed when mapping on an arbitrary geometry. Therefore the textureBounds field contains an SFVec2f with the offset of the upper left corner of the image from the coordinate origin in pixels, and another one with the width and height of the text image in pixels. With this information an appropriate texture (or similiar) transformation can then be calculated.
Files: