C# XNA在绘制文本时进行奇怪的3D绘制
我正在用XNA3.1制作一个3D宇宙飞船游戏 我试图将我的绘画代码与游戏逻辑分开(Altound XNA几乎做到了这一点) 我正在使用一个特殊的静态类在屏幕上绘制我的模型。。。主游戏类在绘制时使用以下代码:C# XNA在绘制文本时进行奇怪的3D绘制,c#,xna,sprite,paint,C#,Xna,Sprite,Paint,我正在用XNA3.1制作一个3D宇宙飞船游戏 我试图将我的绘画代码与游戏逻辑分开(Altound XNA几乎做到了这一点) 我正在使用一个特殊的静态类在屏幕上绘制我的模型。。。主游戏类在绘制时使用以下代码: protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.Black); // Draws the stage (the skybox
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
// Draws the stage (the skybox and objects like rocks)
stageManager.Draw(gameTime, GraphicsDevice, camera);
// Draws each player and each Enemy on stage given the camera
foreach (Player p in players)
p.Draw(camera);
foreach(Enemy e in enemies)
e.Draw(camera);
if(Configuration.Debug)
col.renderColBoundings(GraphicsDevice, camera);
GraphicHelper.drawOverlayText("50", "10"); // "Error" line...
base.Draw(gameTime);
}
但当我画文本时,一些奇怪的事情发生了。。。这是一个图像():
正如您所看到的,所有内容看起来都是重叠的(但在适当的位置)。。。就像宇宙飞船的涡轮机
graphicshelper.drawOverlyText
中的代码如下:
public static void drawOverlayText(String p1Hp, String currEnemies)
{
string text1 = "Player1: " + p1Hp;
string text2 = "Enemies: " + currEnemies + "/10";
string text3 = "Space Hogs Beta";
spriteBatch.Begin();
// Draw the string twice to create a drop shadow, first colored black
// and offset one pixel to the bottom right, then again in white at the
// intended position. This makes text easier to read over the background.
spriteBatch.DrawString(font, text1, new Vector2(651, 11), Color.Gray);
spriteBatch.DrawString(font, text1, new Vector2(650, 10), Color.White);
spriteBatch.DrawString(font, text2, new Vector2(851, 11), Color.Gray);
spriteBatch.DrawString(font, text2, new Vector2(850, 10), Color.White);
spriteBatch.DrawString(font, text3, new Vector2(741, 611), Color.Gray);
spriteBatch.DrawString(font, text3, new Vector2(740, 610), Color.White);
spriteBatch.End();
}
该静态类具有以下属性:
static ContentManager content;
static GraphicsDevice graphics;
static Camera camera;
static Dictionary<String, Model> models = new Dictionary<string, Model>();
static SpriteBatch spriteBatch;
static SpriteFont font;
public static void initHelper(ContentManager c, GraphicsDevice g, Camera cam)
{
content = c;
graphics = g;
camera = cam;
spriteBatch = new SpriteBatch(g);
font = c.Load<SpriteFont>("Fonts/main");
}
静态ContentManager内容;
静态图形设备图形;
静态摄像机;
静态字典模型=新字典();
静态SpriteBatch SpriteBatch;
静态字体;
公共静态void initHelper(ContentManager c、GraphicsDevice g、摄像头)
{
内容=c;
图形=g;
摄像机=凸轮;
spriteBatch=新spriteBatch(g);
font=c.Load(“字体/main”);
}
希望您能帮助我:)您看到的奇怪渲染是因为深度缓冲区已关闭。当您使用SpriteBatch时,它会被关闭。这是XNA3.1API已知的一个奇怪之处。xna4.0至少让这一点变得更加明显 。和 解决方案基本上是在使用SpriteBatch后将渲染状态设置回您想要的状态。在这种情况下,至少设置:
GraphicsDevice.RenderState.DepthBufferEnable = true;
(可能还有其他一些州你想改回去。)谢谢安德鲁,成功了!我知道这和图形设备有关,但不完全是什么:P