如何在 SpriteBatch Draw XNA (2D) 中将两个精灵相乘
我正在 XNA 3.1 中为动作角色扮演游戏编写简单的十六进制引擎。我想照亮英雄和火把附近的地面,就像在暗黑破坏神 II 中点燃它们一样。我认为最好的方法是计算视野,隐藏玩家看不到的任何图块及其内容,并在任何光源顶部绘制特殊的“光”纹理:黑白色的纹理,中心有一个模糊的圆圈。
我想将此纹理与背景相乘(如在混合模式中:相乘),但不幸的是,我在 SpriteBatch 中看不到执行此操作的选项。有人能指出我正确的方向吗?
或者也许还有其他更好的方法来实现暗黑破坏神 II 中的光照模型?
I am writing simple hex engine for action-rpg in XNA 3.1. I want to light ground near hero and torches just as they were lighted in Diablo II. I though the best way to do so was to calculate field-of-view, hide any tiles and their's content that player can't see and draw special "Light" texture on top of any light source: Texture that is black with white, blurred circle in it's center.
I wanted to multiply this texture with background (as in blending mode: multiply), but - unfortunately - I do not see option for doing that in SpriteBatch. Could someone point me in right direction?
Or perhaps there is other - better - way to achive lighting model as in Diablo II?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要将光纹理与场景相乘,您将使该区域变暗,而不是使其变亮。
您可以尝试使用附加混合进行渲染;这看起来不太正确,但是很简单并且可以接受。您必须使用相当低的 Alpha 来绘制光线,以使光纹理不至于使图像的该部分过度饱和。
另一种更复杂的照明方法是将所有灯光纹理(场景中的所有灯光)添加到第二个渲染目标上,然后将此纹理与场景相乘。这应该提供更真实的光照,但性能开销更大并且更复杂。
初始化:
绘制:
If you were to multiply your light texture with the scene, you will darken the area, not brighten it.
You could try rendering with additive blending; this won't quite look right, but is easy and may be acceptable. You will have to draw your light with a fairly low alpha for the light texture to not just over saturate that part of the image.
Another, more complicated, way of doing lighting is to draw all of your light textures (for all the lights in the scene) additively onto a second render target, and then multiply this texture with your scene. This should give much more realistic lighting, but has a larger performance overhead and is more complex.
Initialisation:
Draw:
据我所知,如果不使用您自己的自定义着色器,就无法做到这一点。
为此,自定义着色器的工作方式如下:
这将输出一个明亮的场景,但不会产生任何阴影。如果您想要阴影,我建议您遵循来自 Catalin Zima 的这个优秀示例
As far as I know there is no way to do this without using your own custom shaders.
A custom shader for this would work like so:
This will output a lit scene, but it won't do any shadows. If you want shadows I'd suggest following this excellent sample from Catalin Zima
也许使用与 BloomEffect 组件 中相同的技术可能是一个想法。
基本上,该效果的作用是抓取渲染的场景,从场景中最亮的区域计算光晕图像,进行模糊并将两者组合起来。结果是根据颜色突出显示区域。
这里可以使用相同的方法。它会更简单,因为您不必根据背景计算光晕图像,而只需根据角色的位置。
您甚至可以进一步重复使用它来为其他光源(例如火炬、魔法效果等)提供突出显示。
Perhaps using the same technique as in the BloomEffect component could be an idea.
Basically what the effect does is grabbing the rendered scene, calculates a bloom image from the brightest areas in the scene, the blurs and combines the two. The result is highlighting areas depending on color.
The same approach could be used here. It will be simpler since you won't have to calculate the bloom image based on the background, only based on the position of the character.
You could even reuse this further to provide highlighting for other light sources as well, such as torches, magic effects and whatnot.