Panda3D Manual: Text NodeThe most fundamental way to render text in Panda3D is via the To use a TextNode, simply create one and call PT(TextNode) text;
Note that the TextNode constructor takes a string name, which is not related to the text that is to be displayed. Also note that the default text color is white; we show it as black in these examples to make it easier to see on the white background. There are a large number of properties that you can specify on the TextNode to control the appearance of your text. FontPT(TextFont) cmr12=FontPool::load_font("cmss12.egg");
You may use any font you like, including a TTF file; see Text Fonts. Small Capstext.setSmallCaps(True) PT(TextFont) cmr12=FontPool::load_font("cmss12.egg");
You can also specify the relative scale of the "lowercase" letters: text->set_small_caps_scale(0.4);
Where 1.0 is exactly the same size as the capital letters, and 0.5 is half the size. The default is 0.8. Slanttext->set_slant(0.3);
Slant can be used to give an effect similar to italicizing. The parameter value is 0.0 for no slant, or 1.0 for a 45-degree rightward slant. Usually values in the range 0.2 to 0.3 give a pleasing effect. You can also use a negative number to give a reverse slant. text->set_text_color(1, 0.5, 0.5, 1);
The color is specified with its r, g, b, a components. Note that if a is not 1, the text will be slightly transparent. Shadowtext->set_shadow(0.05, 0.05);
A shadow is another copy of the text, drawn behind the original text and offset slightly to the right and down. It can help make the text stand out from its background, especially when there is not a high contrast between the text color and the background color. (The text color in this example is exactly the same pink color used in the example above, but note how much clearer it is with the shadow.) The downside of a shadow is that it doubles the number of polygons required to render the text. Setting a shadow requires two calls: WordwrapBy default, text will be formatted on one line, unless it includes newline characters. Enabling wordwrap will automatically break the text into multiple lines if it doesn't fit within the specified width. text->set_wordwrap(15.0);
The parameter to AlignmentText is left-aligned by default; that is, it starts at the position you specify with textNodePath.setPos() and goes out to the right from there. If you have multiple lines of text, you may prefer to center the text or right-align it instead: text->set_align(TextNode::A_center);
The parameter to You can also set the alignment to one of FrameYou can specify that a thin frame should be drawn around the entire text rectangle: text->set_frame_color(0, 0, 1, 1);
As with the shadow, specifying a frame requires two calls; one to specify the color, and another to specify the dimensions of the frame. The call CardFinally, you can draw a solid card behind the text rectangle: text->set_card_color(1, 1, 0.5, 1);
This can also help to make the text easier to read when it is against a similar-colored background. Often, you will want the card to be semitransparent, which you can achieve by specifying an alpha value of 0.2 or 0.3 to the The parameters to If the text is to be visible in the 3-d world (that is, parented to render instead of to render2d), then you may observe z-fighting, or flashing, between the text and the card. To avoid this, call Picking a Text NodeStrictly speaking, a TextNode has no geometry, so you can't pick it. There are two possible workarounds. (1) Create your own card to go behind the TextNode, using e.g. CardMaker. You should be able to say cardMaker.setFrame(textNode.getFrameActual()) to set the card to be the same dimensions as the text's frame. Then you will need to either offset the text a few inches in front of the card to prevent Z-fighting, or explicitly decal the text onto the card, with something like this: card = NodePath(cardMaker.generate()) (2) Instead of parenting the TextNode directly to the scene, parent the node returned by textNode.generate() instead. This will be a static node that contains the polygons that render the text. If the text changes in the future, it won't automatically update the geometry in this node; you will have to replace this node with the new result of textNode.generate(). But this node will be 100% pickable. In particular, if you have specified © Carnegie Mellon University 2010 |