ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-10 13:55:30  阅读:234  来源: 互联网

标签:c xna paint sprite


我正在使用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):

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

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;

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

标签:c,xna,paint,sprite
来源: https://codeday.me/bug/20190610/1212288.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有