Text gets fuzzy at large scales

This example makes the text fuzzy. Any way to fix that?

import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText
text = OnscreenText(text = 'Fuzzy', scale = 1)
run()

Yes, as long as you are willing to give up a little more texture memory.

First of all, let’s make a text object


from pandac.PandaModules import *
from direct.gui import OnscreenText
myText=OnscreenText("Blurry!",mayChange=True)
myText.setScale(.9)

now we need to make a font. Go into c:\Windows\Fonts and find a font you like, and copy it into your script directory. I’ll use ARIAL.TTF for the moment.


myFont=DynamicTextFont(Filename("ARIAL.TTF")) #this is case-sensitive
myText.setFont(myFont)

print myFont.getPixelsPerUnit() #should be 30

The problem here is that the default font size only uses 30 pixel for each character. For a sharper font we can increase this size, at the cost of texture memory


myFont.clear()                #empties out the font pages
myFont.setPixelsPerUnit(90)
myText.setText("Sharp!")      #the change doesn't take into effect unless
                              #you call setText again

This means that we are now using 9 times the texture memory for character (3*3)! Since it is dynamic, only the characters that are used will be rendered into its texture pages, so doing “Hi” with a PPU of 256 (the maximum limit) will only use two 256x256 texture pages. But if the font is regularly used for every letter of the alphabet, things could get hairy.

Finally, to make Direct items show up sharper by default, you can do the following:


#Do this as early as possible
from direct.gui import DirectGuiGlobals
defaultFont=DirectGuiGlobals.getDefaultFont()
defaultFont.clear()
defaultFont.setPixelsPerUnit(60) 
DirectGuiGlobals.setDefaultFont(defaultFont)

Again, don’t go too high with the PPU unless you have a ton of memory to throw around.