通过项目利用两次libgdx绘制文本?

问题描述

我试着画出我的3D世界中物体的2D文本与libgdx的camera.project功能,但有一个奇怪的问题。

请参阅下面的图片

enter image description here

enter image description here

可以在图片看到,一切都很好在第1张,但是当我扭转180度球的​​名称(代号1)是是绘制空格也(图2)。我无法得到的是什么问题和思考小时后决定在这里问。 请帮帮我。

我的代码是:

public static void drawNames(){
    TheGame.spriteBatch.begin();
    for(int i = 0; i < TheGame.playerMap.size; i++){
        Player ply = TheGame.playerMap.getValueAt(i);
        if(!ply.isAlive)
            continue;
        TheGame.tmpVec.set(ply.getPos().x,ply.getPos().y,ply.getPos().z);
        TheGame.cam.project(TheGame.tmpVec);
        TheGame.fontArialM.draw(TheGame.spriteBatch,ply.name,TheGame.tmpVec.x,TheGame.tmpVec.y,Align.center,false);
    }
    TheGame.spriteBatch.end();
}

解决方法

这是因为如果您投影了相机后面的东西,它仍然可以从 project 方法获得有效的屏幕坐标。 考虑以下打印两个世界坐标的屏幕坐标

        PerspectiveCamera camera = new PerspectiveCamera(60,800,600);
        camera.position.set(0,-10);
        camera.lookAt(0,0);
        camera.update();

        Vector3 temp = new Vector3();
        Vector3 p1 = new Vector3(1,10); // This is in front of the camera,slightly to the right
        Vector3 p2 = new Vector3(0,-100); // This is behind of the camera

        camera.project(temp.set(p1));
        System.out.println("p1 is at screen " + temp);
        if (camera.frustum.pointInFrustum(p1))
            System.out.println("p1 is visible to the camera");
        else
            System.out.println("p1 is not visible to the camera");

        camera.project(temp.set(p2));
        System.out.println("p2 is at screen " + temp);
        if (camera.frustum.pointInFrustum(p2))
            System.out.println("p2 is visible to the camera");
        else
            System.out.println("p2 is not visible to the camera");

在您的代码中,在渲染文本之前,您需要检查 ply.getPos() 向量是否对相机可见,如果是,则仅渲染文本。

if (TheGame.cam.frustum.pointInFrustum(ply.getPos()) {
    TheGame.tmpVec.set(ply.getPos().x,ply.getPos().y,ply.getPos().z);
    TheGame.cam.project(TheGame.tmpVec);
    TheGame.fontArialM.draw(TheGame.spriteBatch,ply.name,TheGame.tmpVec.x,TheGame.tmpVec.y,Align.center,false);
}

请注意,还有其他方法可以剔除相机后面的东西,这些方法可能对您更有效。