【问题标题】:XNA 4.0 - Using spriteBatch and basicEffect lagsXNA 4.0 - 使用 spriteBatch 和 basicEffect 滞后
【发布时间】:2010-12-16 04:57:08
【问题描述】:

我目前正在开发一款游戏,其中我们需要结合使用 DrawUserIndexedPrimitives 和普通 spriteBatch.Draw。没有组合,因为我们同时使用它们,但我们首先必须使用 spriteBatch 绘制一些 2d 精灵,然后我们禁用 spriteBatch 以启用 basicEffect 并绘制图元,最后再次启用 spriteBatch。下面的代码显示了发生问题的代码部分。

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));
                levelController.Draw(spriteBatch);


                if (gameReady)
                {
                    foreach (GameObject.GameObject go in gameObjects)
                    {
                        go.Draw(spriteBatch);
                    }
                    spriteBatch.End();

                    foreach (GameObject.GameObject go in gameObjects)
                    {
                        if (go is GameObject.Enemy.Enemy)
                        {
                            GameObject.Enemy.Enemy enemy = (GameObject.Enemy.Enemy)go;

                            basicEffect = new BasicEffect(graphics.GraphicsDevice);
                            basicEffect.VertexColorEnabled = true;
                            basicEffect.CurrentTechnique.Passes[0].Apply();

                            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1);
                        }
                    }
                }

                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));

如果下面的代码被引用,则延迟停止。

basicEffect = new BasicEffect(graphics.GraphicsDevice);
                        basicEffect.VertexColorEnabled = true;
                        basicEffect.CurrentTechnique.Passes[0].Apply();

                        GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1)

真的是我们不能同时使用 spriteBatch 和 basicEffect 而不会使游戏延迟很多吗?它已经在 3 台不同的计算机上进行了测试,从非常旧的笔记本电脑到全新的游戏玩家 PC。 由于出现延迟,游戏无法玩。

【问题讨论】:

    标签: c# xna draw lag xna-4.0


    【解决方案1】:

    我认为你应该在其他地方创建基本效果,然后是你的绘图程序。如果我猜对了,您将一直使用相同的基本效果,因此将其放入初始化中,因为每帧“新”(编辑:在每个对象的 foreach 中)可以降低性能。

    【讨论】:

    • 谢谢,我以为我们已经尝试过了,但我想没有:)
    • +1:既然你提到了它,我又查看了代码,继续创建新的BasicEffect 没有意义。不错。
    • 您看到的滞后是垃圾收集器运行并清理您正在创建的基本效果,因为它们是一次性对象,每个被收集的对象都需要运行其终结器。在游戏运行时使用 new 分配非值类型时要非常小心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多