在 XNA 中使 Texture2D 的部分透明

发布于 2024-09-26 07:10:36 字数 526 浏览 0 评论 0原文

我刚刚开始游戏开发,我认为像《坦克大战》或《百战天虫》这样的游戏会很不错。 到目前为止,我能想到的最难的部分是使地形可破坏,我想在做简单的部分之前知道它是如何完成的。

我认为爆炸可能有一个面具纹理,可以针对不同的武器进行缩放。然后使用该蒙版,我应该使底层地形透明(并可选择绘制深色边框)。
(来源:mikakolari.fi

我该如何实现这一目标? 我是否必须逐个像素地更改 alpha 值,或者我可以使用某种屏蔽技术吗?在地形顶部绘制蓝色圆圈不是一种选择。

我有 XNA 3.1 和 4.0 版本。

I'm just starting game development and I thought a game like Tank wars or Worms would be nice.
The hardest part I can think of so far is making the terrain destructible and I want to know how it's done before doing the easy parts.

I thought that explosion could have a mask texture which could be scaled for different weapons. Then using that mask I should make underlying terrain transparent (and optionally draw a dark border).

(source: mikakolari.fi)

How do I achieve that?
Do I have to change the alpha value pixel by pixel or can I use some kind of masking technique? Drawing a blue circle on top of the terrain isn't an option.

I have versions 3.1 and 4.0 of XNA.

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

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

发布评论

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

评论(3

高速公鹿 2024-10-03 07:10:36

本教程就是您正在搜索的内容:
http://www.riemers.net/eng/Tutorials/XNA/Csharp /series2d.php

第 20 章:添加爆炸坑

简而言之:
您有 2 个纹理:1 个颜色纹理(可见)​​、1 个碰撞纹理(不可见)
您从碰撞纹理中减去爆炸图像。
要获得深色边框:展开爆炸纹理并加深该区域的颜色。
现在生成一个新的颜色纹理(旧颜色 - 碰撞 = 新颜色)。

This tutorial is what you are searching:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/series2d.php

Capter 20: Adding explosion craters

In short:
You have 2 textures: 1 Color Texture (visible), 1 Collision Texture (invisible)
You substract the explosion image from your collision texture.
To get the dark border: expand the explosion texture and darken the color in this area.
Now you generate a new Color Texture (old color - collison = new color).

錯遇了你 2024-10-03 07:10:36

这是一个很难回答的问题 - 因为有很多方法可以做到这一点。每种方法都有优点和缺点。我只给出一个概述:

作为整体设计,您需要跟踪:原始纹理、应用的“黑暗度”和应用的“透明度”。我几乎可以肯定地说的一件事是,你想在某个地方“累积”爆炸的结果 - 你不想做的是维护曾经发生过的所有爆炸的列表。

因此,您可以拥有具有纹理、黑暗度和透明度的表面。您可以将黑暗和透明度合并到一个表面中,并使用一个存储“正常”、“黑暗”(或一定程度的黑暗)和“透明”的通道。

因为您可能不希望暗环在它们相交的地方逐渐变暗,所以当您使用 max 函数(Math.Max C#)。

要生成最终纹理,您可以将黑暗/透明纹理写入原始纹理或其副本(您只需要更新每次爆炸接触的区域)。

或者您可以使用像素着色器来组合它们 - 其细节超出了本问题的范围。 (此外,像素着色器无法在 Windows Phone 7 上的 XNA 4.0 上运行。)

This is a difficult question to answer - because there are many ways you could do it. And there are pros and cons to each method. I'll just give an overview:

As an overall design, you need to keep track of: the original texture, the "darkness" applied, and the "transparency" applied. One thing I can say almost for sure is you want to "accumulate" the results of the explosions somewhere - what you don't want to be doing is maintaining a list of all explosions that have ever happened.

So you have surfaces for texture, darkness and transparency. You could probably merge darkness and transparency into a single surface with a single channel that stores "normal", "dark" (or a level of darkness) and "transparent".

Because you probably don't want the dark rings to get progressively darker where they intersect, when you apply an explosion to your darkness layer with the max function (Math.Max in C#).

To produce your final texture you could just write from the darkness/transparency texture to your original texture or a copy of it (you only need to update the area that each explosion touches).

Or you could use a pixel shader to combine them - the details of which are beyond the scope of this question. (Also a pixel shader won't work on XNA 4.0 on Windows Phone 7.)

吹梦到西洲 2024-10-03 07:10:36

您应该使用所需像素的颜色创建一个新的 Texure2D。Alpha = 0。

Color[] bits = new Color[Texture.Width * Texture.Height];
Texture.GetData(bits);

foreach(Vector2D pixel in overlapedArea)
{
   int x = (int)(pixel.X);
   int y = (int)(pixel.Y);

   bits[x + y * texture.Width] = Color.FromNonPremultiplied(0,0,0,0));
}

Texture2D newTexture = new Texture2D(texture.GraphicsDevice, texture.Width, texture.Height);
newTexture.SetData(bits);

现在用最后一个纹理替换新的 Texture2D,就可以开始了!

有关碰撞或更改纹理像素颜色的更多代码,请访问此页面的代码:
http://www.codeproject.com/Articles/328894 /XNA-Sprite-Class-with-useful-methods

You should Make a new Texure2D with the Color of desired pixels.Alpha = 0.

Color[] bits = new Color[Texture.Width * Texture.Height];
Texture.GetData(bits);

foreach(Vector2D pixel in overlapedArea)
{
   int x = (int)(pixel.X);
   int y = (int)(pixel.Y);

   bits[x + y * texture.Width] = Color.FromNonPremultiplied(0,0,0,0));
}

Texture2D newTexture = new Texture2D(texture.GraphicsDevice, texture.Width, texture.Height);
newTexture.SetData(bits);

Now replace the new Texture2D with the Last Texture and you're good to go!

For more code about Collision, or changing texture pixels color go to this page for codes:
http://www.codeproject.com/Articles/328894/XNA-Sprite-Class-with-useful-methods

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