Java libGDX使用贴花到3d面向摄影机绘制文本
我有绘制贴花的工作代码:Java libGDX使用贴花到3d面向摄影机绘制文本,java,text,3d,libgdx,draw,Java,Text,3d,Libgdx,Draw,我有绘制贴花的工作代码: 初始化: Decal decal = Decal.newDecal(1, 1, new TextureRegion(new Texture(Gdx.files.internal("2d/gui/badlogic.jpg"))) ); decal.setPosition(10, 10, 10); decal.setScale(3); decals.add(decal); 绘制方法: for (in
初始化:
Decal decal = Decal.newDecal(1, 1,
new TextureRegion(new Texture(Gdx.files.internal("2d/gui/badlogic.jpg"))) );
decal.setPosition(10, 10, 10);
decal.setScale(3);
decals.add(decal);
绘制方法:
for (int i = 0; i < decals.size; i++) {
Decal decal = decals.get(i);
decal.lookAt(stage3d.getCamera().position, stage3d.getCamera().up);
batch.add(decal);
}
batch.flush();
spriteBatch.setProjectionMatrix(tmpMat4.set(camera.combined).mul(textTransform));
spriteBatch.begin();
font.draw(spriteBatch, "Testing 1 2 3", 0, 0);
spriteBatch.end();
但是我很难写出一篇正面的文章
谢谢我不会尝试贴花方法,因为它不是为文本设置的。已为文本设置SpriteBatch (从理论上讲,贴花方法的性能更好,因为您不需要为每个文本字符串单独调用draw。但是,您必须推出与贴花兼容的自己版本的BitmapFont和BitmapFontCache。当然,如果您这样做,您可以提交拉取请求并将其添加到libgdx。) SpriteBatch代码看起来很熟悉。:)基本上,您需要做的是修改
textTransform
矩阵,使其旋转对象,使其面向相机。SpriteBatch设置为绘制面向Z方向的平面对象。所以你需要旋转一个Z向量来面对摄像机
首先,您需要一个可以重用的矢量3
private static Vector3 tmpVec3 = new Vector3();
然后,您需要找到从文本中心指向摄影机的向量。我假设您正在将文本在3D空间中的位置存储在一个名为textPosition
的向量3中:
tmpVec3.set(camera.position).sub(textPosition);
//tmpVec3 is now a vector pointing from the text to the camera.
现在,您可以定位对象的矩阵,然后将其旋转以面向摄影机,如下所示:
textTransform.setToTranslation(textPosition).rotate(Vector3.Z, tmpVec3);
现在,您可以像您发布的代码一样使用textcransform
。确保将位图字体的对齐方式设置为HAlignment.center,否则文本将围绕文本字符串的左端而不是中心旋转。对于三维图形,您可能还希望将integer
参数设置为false