XNA 2D 着色器和 SpriteSortMode

发布于 2024-12-28 00:55:41 字数 629 浏览 2 评论 0原文

因此,我一直在尝试了解 XNA 中的 2D 着色器。

http://msdn.microsoft.com/en- us/library/bb313868(v=xnagamestudio.31).aspx

上面的链接指出我需要使用 SpriteSortMode.Immediate,这对我来说是一个问题,因为我开发了一个依赖于延迟渲染的视差系统(SpriteSortMode.BackToFront)。

有人可以向我解释 SpriteSortMode 在着色器中的重要性,如果立即是强制性的,我可以做些什么来维护我当前的系统,以及其他任何一般可以帮助我更好地理解这一点?

此外,我的游戏运行多个 SpriteBatch.Begin()/End() 调用(用于绘制背景,然后是游戏,然后是前景和 HUD 等)。我注意到 Bloom 示例在这种情况下不起作用,我猜测它与此有关。

总的来说,我在理解这些概念时遇到了一些困难。我知道什么是着色器以及它如何工作,但我不知道它如何与 XNA 交互以及那里发生了什么。我真的很感激一些启发。 :)

谢谢!

So I've been trying to wrap my head around shaders in 2D in XNA.

http://msdn.microsoft.com/en-us/library/bb313868(v=xnagamestudio.31).aspx

This link above stated that I needed to use SpriteSortMode.Immediate, which is a problem for me because I have developed a parallax system that relies on deferred rendering (SpriteSortMode.BackToFront).

Can someone explain to me the significance of SpriteSortMode in shaders, if Immediate is mandatory, anything I can do to maintain my current system, and anything else in general to help me understand this better?

In addition, my game runs off of multiple SpriteBatch.Begin()/End() calls (for drawing background, then the game, then the foreground and HUD and etc). I've noticed that the Bloom example does not work in that case, and I am guessing it has something to do with this.

In general, I've been having some trouble understanding these concepts. I know what a shader is and how it works, but I don't know how it interacts with XNA and what goes on there. I would really appreciate some enlightenment. :)

Thanks SO!

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

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

发布评论

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

评论(2

蓝天 2025-01-04 00:55:41

如果您尝试执行一些重要的操作(例如渲染分层透明度或使用深度缓冲区),则排序模式将很重要。我假设您想做一些不平凡的事情,因为您想使用像素着色器来完成它。

SpriteSortMode.Immediate 将完全按照绘制调用的顺序绘制事物。如果您使用其他模式,如果可以的话,SpriteBatch 将按纹理对视频卡的绘制调用进行分组。这是出于性能原因。

请记住,每次调用 SpriteBatch.Begin 时,您都会应用新的像素着色器并丢弃之前设置的像素着色器。 (即使新的只是 SpriteBatch 的标准像素着色器,它应用了 Tint 颜色。)此外,请记住,通过调用 SpriteBatch.End 您是在告诉视频卡执行所有当前的 SpriteBatch 命令。

这意味着,如果您的精美像素着色器的范围有限,您可能会保留现有的排序方法。换句话说,使用一种效果绘制背景,然后使用另一种效果绘制前景和角色。对 SpriteBatch 的每个 Begin/End 调用都可以单独处理。

如果您的目标是将一种效果(例如热浪或绽放)应用于屏幕上的所有内容,您还有另一种选择。您可以选择将所有图形渲染到您创建的 RenderTarget 上,而不是直接渲染到视频卡的后缓冲区。如果执行此操作,则在渲染部分的末尾,您可以调用 GraphicsDevice.SetRenderTarget(null) 并使用适用于整个场景的自定义着色器将完成的图像绘制到后台缓冲区。

The sort mode will matter if you are attempting to do something non-trivial like render layered transparency or make use of a depth buffer. I'm going to assume you want to do something non-trivial since you want to use a pixel shader to accomplish it.

SpriteSortMode.Immediate will draw things in exactly the order of the draw calls. If you use another mode, SpriteBatch will group the draw calls to the video card by texture if it can. This is for performance reasons.

Keep in mind that every time you call SpriteBatch.Begin you are applying a new pixel shader and discarding the one previously set. (Even if the new one is just SpriteBatch's standard pixel shader that applies a Tint color.) Additionally, remember that by calling SpriteBatch.End you are telling the video card to execute all of the current SpriteBatch commands.

This means that you could potentially keep your existing sorting method, if your fancy pixel shaders are of limited scope. In other words, draw your background with one Effect and then your foreground and characters with another. Each Begin/End call to SpriteBatch can be treated separately.

If your goal is to apply one Effect (such as heat waves or bloom) to everything on the screen you have another option. You could choose to render all of your graphics onto a RenderTarget that you create instead of directly to the video card's backbuffer. If you do this, at the end of your rendering section you can call GraphicsDevice.SetRenderTarget(null) and paint your completed image to the backbuffer with a custom shader that applies to the entire scene.

陪我终i 2025-01-04 00:55:41

我不能百分百确定精灵排序模式对着色器的影响有多大,我认为它会根据您使用着色器的用途而有所不同。

至于绽放,如果您使用多个开始和结束(如果可以的话,您确实希望将其最小化)。您可以创建屏幕大小的渲染目标,按照现在的方式绘制所有内容。然后在最后,取回渲染目标(使用graphicsdevice.SetRenderTarget(null);),然后使用绽放着色器绘制全屏渲染目标(在0,0位置),这样你就会绽放整个场景,无论使用各种排序模式对场景的组件进行排序。

I'm not one hundred percent sure on how much the sprite sorting mode effects shaders, i would think it would vary depending on what you were using the shader for.

as for bloom if you're using multiple begin and ends (which you really want to minimise if you can). you can create a render target the size of the screen, draw everything as you are now. then at the very end, take that render target back (using graphicsdevice.SetRenderTarget(null);) then draw your full screen render target (at 0,0 position) with the bloom shader, that way you will bloom the entire scene, regardless of the components of the scene using various sort modes.

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