Text facing the camera

Hi, I have a scene set up with some text labels in 3D. I now want all the textfields to always face the camera’s position/rotation, so that the text is always in front it, and readable. I tried some things that were not really working like what I had in mind. So my question is if anybody knows a good/simple way for achieving this in JME3



Thanks in advance

1 Like

Which node are you attaching the “text labels in 3D”?

I attach them together with a 3D box into a new Node that is then placed at a specific coördinate. These new Nodes are all direct children of the rootNode

You shouldn’t. There’s a “guiNode” exactly to it. Create a BitmapText and attach it to the “gui node”.

Yes I know about the guiNode, but this doesn’t seem the way to achieve what I need, because I want the textlabels to always be above the 3D boxes in my scene. Or is there some easy way to map the 3D positions of the boxes to the 2D gui plane?

1 Like

Then you have to play with the Camera.getWorldCoordinate() to get the model’s transform relative to the camera. Look at the TestMousePick.

Not sure, but would maybe work if you set the localtranslation on the text to 0,0,0

then in the text set the 2d position to be 0,10 or whatever high you need by text.setPosition(0, 10);

@snake: i know what you mean, you must set UpVector for a 3d label. Best is to hold it in your extended class, then to use like: node.lookAt(upVector, camera.getTranslation())



i hope you know what i mean. Its the same like particles Facing mode

Thanks for the quick answers guys!



@oxplay2 this is indeed what i’m looking for. In fact, the following lines of code give me what I need:



[java]Quaternion q = new Quaternion();

q.lookAt(cam.getLocation(),cam.getUp());

txt.setLocalRotation(q);[/java]



The problem is that this only works if the bitmaptext (txt variable) is placed in (0,0,0)

Any ideas why this happens?

ok the solution was even easier than I thought.



[java]lastTxt.lookAt(cam.getLocation(), cam.getUp());[/java]



Solved :slight_smile:

you dont want to set cam.getUp() really. This upVector is Vector for a camera, that is why it work in (0,0,0)



trully(proper) you should set its own upVector



upVector - this is vector where object is Actually looking. (if i good understand)



you need to create you own class like:



[java]class myVirtualText{

Camera cam;

myVirtualText(Camera cam){

this.cam = cam;

}

Vector3f upVector = new Vector3f(1,0,0);

Node textnode = new Node();

public void attachBitmapToNode(Bitmaptext text){

textnode.attach(text);

}

public void setPosition(Vector3f pos){

textnode.setLocalTranslation(pos);

upVector = textnode.lookAt(cam.getLocation(),upVector);

//update upVector to get Normal Vector of it (normal vector is unit vector) - but i dont know if you need normal vector



}

}[/java]





@normen: i think you should add some lessons of it(i mean lookAt and upVector) to “scenegraph for dummies”

Thats “Math for Dummies” and the link is to the right :stuck_out_tongue:

@normen: there are nothin about lookAt and upVector :slight_smile:



so i just propose to add it

Yes there is.

i looked again(here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies) and it seems im blind 8O

BillboardControl?

Maybe a Billboard? I use something like this to attach a text over a scene object.

[java]BitmapText hudText = new BitmapText(guiFont, false);

[…]

BillboardControl control = new BillboardControl();

hudText.addControl(control);

myObj.attachChild(hudText);[/java]