0

我想在 3D 空间中的特定位置绘制文本和图像。场景以 3D 渲染,我想在 XYZ 坐标上显示渲染的 2D 文本和图像。

我有来自场景和视口的世界、视图、投影矩阵。我不想渲染真正的 3D 字体,也不想显示带有纹理顶点的图像。

我尝试了一些矩阵乘法与变换矩阵,我还尝试使用基本效果作为开始方法的参数。但没有一个对我有用。

        eff.World = Graph3DGame.Current.currentWorld;
        eff.View = Graph3DGame.Current.currentView;
        eff.Projection = Graph3DGame.Current.currentPerspective;

        spriteBatch.Begin(0, null, null, null, null, eff);
        spriteBatch.DrawString(Fonts.Math, "hello, world!", new Vector2(100,100), Color.Blue);
        spriteBatch.End();

希望任何人都可以提供帮助。

4

1 回答 1

2

如果您有 x,y,z 坐标,您可以通过使用视口投影 3d 坐标,在屏幕上找到实际对应的 2d 坐标。基本上这应该工作:

var position3d = new Vector3(1,1,1);

var viewport = GraphicsDevice.Viewport;
var position2d = viewport.Project(position3d, projectionMatrix, viewMatrix, worldMatrix);

position2d 值将具有 az 值,如果需要,可以忽略它。

然后,您可以使用它来绘制文本(如果您希望 i 居中):

var text = "Hello, world!";
var measure = myFont.Measure("Hello, world!");

var centeredPosition = new Vector2(position2d.X - measure.X / 2, position2d.Y - measure.Y / 2);

spriteBatch.Begin();
spriteBatch.DrawString(myFont, text, centeredPosition, Color.Blue);

希望有帮助。

于 2018-10-28T17:00:49.790 回答