在一个批次中绘制所有 DrawableGameComponents

发布于 2024-10-09 12:48:38 字数 1198 浏览 0 评论 0原文

这是一个 XNA 问题...

我的游戏中有大量的 DrawableGameComponents 对象,它们都共享相同的 SpriteBatch。但是,我每次都必须调用 SpriteBatch.Begin() 和 .End() 来绘制每个组件。我想在一个批次中绘制所有内容以获得更好的性能,但我不知道 .Begin() 和 .End() 可以去哪里,因为 .Draw() 被自动调用(因为它们是 DrawableGameComponents)

有谁知道什么我可以让它们全部在单个 .Begin() 和 .End() 调用之间绘制,同时将它们保留为 DrawableGameComponents 吗?

编辑:

好吧,从这个网站 http://www.progware .org/Blog/post/XNA-Game-Development-(Decentralize).aspx 我认为它是在主游戏循环的 base.Draw() 调用中调用的...所以我可以用 Begin 来包装它和 End 方法

protected override void Draw(GameTime gameTime) {
    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.Begin(
            SpriteSortMode.BackToFront, BlendState.AlphaBlend, 
            SamplerState.AnisotropicClamp, null, null, null,
            gamePlay.Camera.GetTransformation()
        );
    }

    base.Draw(gameTime);

    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.End();
    }
}

,它现在可以工作,但这样做似乎意味着我想要在不同 SpriteBatch 中绘制的所有其他内容将始终绘制在该方法下方(因为它们首先绘制),因为此 SpriteBatch 的 .End() 始终是将被最后调用。

有什么办法解决这个问题吗?

This is an XNA question...

I have a large number of objects that are DrawableGameComponents in my game and they all share the same SpriteBatch. However, I have to call SpriteBatch.Begin() and .End() everytime to draw each component. I want to draw everything in a single batch for better performance but I don't know where .Begin() and .End() can go since the .Draw() is being automatically called (as they are DrawableGameComponents)

Does anyone know what I can do to make them all be drawn between a single .Begin() and .End() call whilst keeping them as DrawableGameComponents?

Edit:

Alright from this website http://www.progware.org/Blog/post/XNA-Game-Development-(Decentralize).aspx I figured that it's called at the main game loop's base.Draw() call... so I can just wrap that with the Begin and End methods

protected override void Draw(GameTime gameTime) {
    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.Begin(
            SpriteSortMode.BackToFront, BlendState.AlphaBlend, 
            SamplerState.AnisotropicClamp, null, null, null,
            gamePlay.Camera.GetTransformation()
        );
    }

    base.Draw(gameTime);

    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.End();
    }
}

and it works now but doing it this way seems to mean that everything else that I want drawn in a different SpriteBatch will always be drawn below this (as they are drawn first) because the .End() of this SpriteBatch is always going to be called last.

Any solution to this?

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

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

发布评论

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

评论(1

樱娆 2024-10-16 12:48:38

首先,除非您已经知道您的批次受到限制,否则可能不值得这样做。如果每个 .Draw 调用都从相同的纹理中提取,那么您可能会看到速度有所提高。如果它们是不同的纹理,那么切换到单个开始/结束将没有区别。 SpriteBatch 无论如何都会分解你的绘制调用。

正如您发现的那样,这样做也会使您的代码变得非常混乱 - 您必须在组件之间进行跟踪。

当前问题的唯一解决方案是使 SpriteBatch Immediate,以便它在您调用绘制调用时进行绘制,而不是在 End 调用时进行绘制。然而,这会破坏你的性能,因为根本没有进行批处理。

就我个人而言,我会坚持每个组件都有一个开始/结束,直到遇到性能问题。假设您没有 5000 个组件,在这种情况下您可能需要稍微不同地构建事物。

Firstly, unless you already know that you are batch limited its probably not worth doing this. If every single .Draw call is pulling from the same texture then you might see some speed up. If they are different textures then switching to a single Begin/End will have no difference. SpriteBatch will break up your draw calls anyway.

As you found doing this makes your code pretty confusing too - you have to keep track between components.

The only solution to your current problem is to make the SpriteBatch Immediate so that it draws as you call the draw calls instead of drawing at the End call. However this will kill your perf as no batching is done at all.

Personally I would stick to a Begin/End per component until you hit a perf problem. Assuming you don't have 5000 components that is in which case you probably need to architect things a little differently.

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