XNA 4.0 - 使用 spriteBatch 和 basicEffect 滞后

发布于 2024-10-08 05:05:38 字数 2183 浏览 1 评论 0原文

我目前正在开发一款游戏,其中我们需要结合使用DrawUserIndexedPrimitives和普通的spriteBatch.Draw。没有组合,因为我们同时使用它们,但我们首先必须使用 spriteBatch 绘制一些 2d sprite,之后我们禁用 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 台不同的计算机上进行了测试,从非常旧的笔记本电脑到全新的游戏电脑。 由于出现延迟,游戏无法进行。

I am currently working on a game, in which we need to combine the use of DrawUserIndexedPrimitives and normal spriteBatch.Draw. Not combined as we use them at the same time, but we first have to draw some 2d sprites using spriteBatch, where after we disable spriteBatch to enable basicEffect and draw the primitives, and at last enable spriteBatch again. The code below shows the section of the code, where the problem is occurring.

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

If the code below gets quoted out, the lag stops.

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)

Can it really be that we can't both use spriteBatch and basicEffect without the game lags a lot? it has been tested on 3 different computers, from a very old laptop, to a brand new gamer pc.
The game is unplayable with the lag that occurs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖心男生 2024-10-15 05:05:38

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

I think you should create the basiceffect elsewhere then your drawing routine. If I guess right you will use the same basiceffect all the time, so put it into inicializing, since "new" per every frame (edit: in a foreach for each and every object) can cost performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文