0

我目前正在开发一款游戏,我们需要将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。游戏因延迟而无法玩。

4

1 回答 1

3

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

于 2010-12-16T11:03:31.340 回答