简体   繁体   中英

How to draw a 2d overlay on a Java 3d scene?

I have a scene written in Java 3d, where the user's viewing position is set to some coordinate using the following code:

ViewingPlatform vp = simpleUniverse.getViewingPlatform();
TransformGroup steerTG = vp.getViewPlatformTransform();

Transform3D t3d = new Transform3D();
steerTG.getTransform(t3d);

t3d.lookAt(
        new Point3d(-5, 10, 25), new Point3d(0, 0, 0), new Vector3d(0, 1, 0));
t3d.invert();

steerTG.setTransform(t3d);

Now I need to put an overlay on top of the scene that is always visible, such as some text. I've tried the following, but to no avail:

PlatformGeometry pg = new PlatformGeometry();
Text2D text = new Text2D("Text to display", Cell.BLACK, "Verdana", 18, 1);
pg.addChild(text);
simpleUniverse.getViewingPlatform().setPlatformGeometry(pg);

When I run the above code, I don't see any text at all. Can anyone please help?

The problem is that you are displaying the text directly on top of the camera within the near clipping plane. You need something like this to translate -1 along the z axis.

    PlatformGeometry pg = new PlatformGeometry();

    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(0f, 0f, -1f));
    objScale.setTransform(t3d);

    Text2D text = new Text2D("Text to display", Cell.BLACK, "Verdana", 18, 1);

    objScale.addChild(text);
    pg.addChild(objScale);

    simpleUniverse.getViewingPlatform().setPlatformGeometry(pg);

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM