c# – 在绘制文本时XNA奇怪的3D绘画

我正在使用XNA 3.1开发3D Spaceship游戏

我试图将我的绘画代码与游戏逻辑分开(altought XNA几乎就是这样).

我正在使用一个特殊的静态类,在屏幕上绘制我的模型……主要的Game类在绘画中使用此代码:

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

但是当我画文字时,会出现一些奇怪的东西……这是一张图片(original):
c# – 在绘制文本时XNA奇怪的3D绘画

正如你所看到的,一切看起来都是重叠的(但在适当的位置)……就像飞船涡轮机一样.

GraphicHelper.drawOverlayText中的代码是这样的:

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

希望你能帮我 :)

解决方法:

您看到的奇怪渲染是因为深度缓冲区已关闭.使用SpriteBatch时会关闭它.这是XNA 3.1 API的一个奇怪之处. XNA 4.0至少使这种情况更加明显.

Here is an explanation of what render states are changed by SpriteBatch in XNA 3.1.和here is the same thing for XNA 4.0.

解决方案是在使用SpriteBatch之后,基本上将渲染状态设置回您想要的状态.在这种情况下,至少,设置:

GraphicsDevice.RenderState.DepthBufferEnable = true;

(在那里你可能还有其他一些州要改变.)

上一篇:Hihocoder 之 #1097 : 最小生成树一·Prim算法 (用vector二维 模拟邻接表,进行prim()生成树算法, *【模板】)


下一篇:一篇文章,学会jmeter模拟文件上传、下载操作