Text as Texture?


#1

Hi,

is it possible to use textnodes as textures for other pandanodes?
Can it be done that the text remains dynamic, i.e. allowing to change the text dynamically?

If so can anyone show how to do that?

Thanks a lot for your help,

– Elena


Can this kind of an effect be coded using Panda's 1.7 APIs?
#2

No text nodes don’t modify the texture. All text nodes are collections of blocks that share set of textures. But what you can do is create an off screen buffer and camera. Have camera look at the text. When text changes you refresh the buffer.


#3
from pandac.PandaModules import NodePath,Texture,TextureStage,Mat4,Vec4
from direct.gui.OnscreenText import OnscreenText
from direct.task import Task
import direct.directbase.DirectStart
import random,string

def changeText(t):
    num=random.randint(1,7)
    text['text']='%s\n%s'%(num,random.choice(string.uppercase)*num)
    b3=text.getTightBounds()
    bHalf=(b3[0]+b3[1])*.5
    b=(b3[1]-b3[0])*.5
    ratio=b[0]/b[2]
    # max zoom along X if text is long,
    # else along Z if text is high
    b.setZ(b[0]) if ratio>1 else b.setX(b[2])
    textCam.setPos(bHalf) # put it exactly at text's center
    textCam.setScale(b[0],1,b[2]) # auto-zoom in to text
    buf.setActive(1) # enable RTT only when needed
    base.graphicsEngine.renderFrame() # render the text
    buf.setActive(0) # disable RTT
    return Task.again
taskMgr.doMethodLater(.5,changeText,'changeText')

textSG=NodePath('')
text=OnscreenText('X',parent=textSG,fg=(1,0,0,1),mayChange=1)

buf=base.win.makeTextureBuffer('textbuf',512,512)
buf.setActive(0) # enable it later, when needed
bufTex=buf.getTexture()
# set wrap modes to repeat so the text texture entirely covers smiley
bufTex.setWrapU(Texture.WMRepeat)
bufTex.setWrapV(Texture.WMRepeat)
# RTT camera
textCam=base.makeCamera2d(buf)
textCam.reparentTo(textSG)

smi=loader.loadModel('smiley')
smi.reparentTo(render)
ts=TextureStage('')
ts.setMode(TextureStage.MDecal)
ts.setSort(1) # to win over the default texture stage (sort=0)
smi.setTexture(ts,bufTex)
smi.setTexScale(ts,7,7)

camera.setPos(4.64, -4.32, 3.30)
camera.setHpr(47.05, -27.49, 25.59)
camera.lookAt(render)
mat=Mat4(camera.getMat())
mat.invertInPlace()
base.mouseInterfaceNode.setMat(mat)

base.bufferViewer.toggleEnable()
run()

#4

Hi ynjh_jo and treeform,

thanks for your replies and especially ynjh_jo for your code.
I will go testing and trying to understand it.

The reason for my question is: I am looking for an easy way of transferring text to arbitrary surface forms (spheres, cylinders, squares …) I thought if texts can be used as textures I simply projekt them onto these forms.
(Thats why I miss the modules ProjectionScreen or NonLinearImager in the current panda3D release, see my below post)

Does this make sense or is there a more easy way?

Thanks a lot ,

Elena


#5

Easy way of what ? Converting text to texture ? RTT is the only way. Once you get it as a texture, you can project it anyway you like, follow the manual. You don’t need projectionscreen for that.