XNA 通过“燃烧”添加弹坑(通过 GPU)影响
我目前正在 XNA 中开发 2D“蠕虫”克隆,其中一个功能是“可变形”地形(例如,当火箭击中地形时,会发生爆炸,一大块地形消失)。
我目前的做法是使用随着接近中心而具有逐渐更高的红色值的纹理。我循环遍历“变形”纹理的每个像素,如果当前像素与地形像素重叠并且具有足够高的红色值,我会将表示地形的颜色数组修改为透明。如果当前像素没有足够高的红色值,我会将地形颜色变黑(红色值越接近阈值,地形颜色就越黑)。在此操作结束时,我使用 SetData 更新地形纹理。
我意识到这不是一个好方法,不仅因为我读过有关管道停顿等的内容,而且还因为如果同时添加大量陨石坑,它可能会变得相当滞后。我想在 GPU 上重新制作陨石坑生成,而不是使用渲染目标在目标和要修改的纹理之间“乒乓”。这不是问题,我知道该怎么做。问题是我不知道如何使用这种方法保持燃烧效果。
下面是燃烧效果现在的样子:
有人知道我如何创建类似的燃烧效果(使形成的火山口周围的边缘变暗)吗?我对着色器完全不熟悉,但如果需要的话,如果有人指导我如何做,我将非常感激。如果还有其他方法那就太好了。
I am currently working on a 2D "Worms" clone in XNA, and one of the features is "deformable" terrain (e.g. when a rocket hits the terrain, there is an explosion and a chunk of the terrain disappears).
How I am currently doing this is by using a texture that has a progressively higher Red value as it approaches the center. I cycle through every pixel of that "Deform" texture, and if the current pixel overlaps a terrain pixel and has a high enough red value, I modify the color array representing the terrain to transparent. If the current pixel does NOT have a high enough Red value, I blacken the terrain color (it gets blacker the closer the Red value is to the threshold). At the end of this operation I use SetData to update my terrain texture.
I realize this is not a good way to do it, not only because I have read about pipeline stalls and such, but also because it can become quite laggy if lots of craters are being added at the same time. I want to remake my Crater Generation on the GPU instead using Render Targets "ping-ponging" between being the target and the texture to modify. That isn't the problem, I know how to do that. the problem is I don't know how to keep my burn effect using this method.
Here's how the burn effect looks right now:
Does anybody have an idea how I would create a similar burn effect (darkening the edges around the formed crater)? I am completely unfamiliar with Shaders but if it requires it I would be really thankful if someone walked me through on how to do it. If there are any other ways that'd be great too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你在正确的方面做得很好。但是您需要手动完成很多事情,这也可以通过绘制精灵并应用正确的公式来完成。
例如:
假设你的地形被保存到一个巨大的纹理中,位于纹理的 Alpha 通道中。 1是地形,0是空无一物。
发生爆炸,地形必须变形。只需在纹理上绘制黑色透明球体(或爆炸区域)即可轻松更新纹理。地形消失了,因为黑色球体的alpha值为0。您的纹理现在是最新的,一切都是由 spriteBatch 完成的。没有什么需要检查的。
我不知道你是否也想要一个解决方案,但现在你有了一个。
用于燃烧效果
现在我们已经有了纹理中的地形,我们可以使用着色器在绘图上进行后期效果(就像你说的那样)。着色器获取纹理的 Alpha 通道,现在可以执行不同的操作来获得燃烧效果。
第二个选项的主要区别在于,您还可以选择仅绘制法线地面,并且在渲染时不调整地面纹理中的像素。
我知道这是一个很长的故事,但这是一个很好的技术。看看卡通着色器,它们也会进行边缘检测,即使它是 3D 的。
关键词:卡通着色、HLSL、后期效果、边缘检测、图像处理。
推荐阅读:http://rbwhitaker.wikidot.com/xna-tutorials
Sounds like you're good in the right way. But you're doing a lot of things by hand, which can also be done by just drawing sprites and applying the right formulas.
For example:
Suppose your terrain is saved into a giant texture in the alpha channel of the texture. 1 is terrain, 0 is nothing.
An explosion happens and the terrain has to be deformed. Update your texture easily by just drawing a black transparent sphere (or explosion area) onto your texture. The terrain is gone, because the alpha value is 0 of the black sphere. Your texture is now up to date, everything was done by the spriteBatch. And nothing had to be checked.
I don't know if you wanted a solution for this as well, but now you have one.
For the burn effect
Now that we have our terrain in a texture, we can do a post effect on the drawing by using a shader (just like you said). The shader obtains the texture's alpha channel and can now do different things to get our burn effect.
The main difference for the second option is that you can also choose to just draw your normal ground and you are not adjusting the pixel in the ground texture on rendering.
I know this is a long story, but it is a nice technique. Have a look at toon shaders, they do edge detection as well, even though it is 3D.
Keywords: Toon shading, HLSL, Post effects, edge detection, image processing.
Recommended reading: http://rbwhitaker.wikidot.com/xna-tutorials