簡體   English   中英

將SpriteBatch移至3D空間坐標

[英]Move SpriteBatch to 3D Space Coordinate

我想在3D空間中的特定位置繪制文本和圖像。 場景以3D渲染,我想在XYZ坐標上顯示渲染的2D文本和圖像。

我有來自場景和ViewPort的“世界”,“視圖”,“投影矩陣”。 我不想渲染真實的3D字體,也不想顯示帶有紋理頂點的圖像。

我已經嘗試過使用轉換矩陣進行一些矩陣乘法,並且還嘗試使用基本效果作為begin-method的參數。 但是他們都不為我工作。

        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();

希望任何人都能提供幫助。

如果具有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值將具有z值,如果需要,可以忽略該值。

然后,您可以使用此文字(如果您希望我居中)來繪制文字:

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);

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM