XNA partially transparent pixels

Started by
5 comments, last by Monkeys 12 years, 3 months ago
Hey guys,

I have the graphic of a tree
67MmG.png

It's a Photoshop PNG with transparent background, but apparently Photoshop doesn't make alpha channels by default with PNGs.

So I made it a TGA with an alpha channel
2012-01-10_1827.png

Saved it as TGA, but we still get weird stuff that won't be fully transparent like this in XNA;
2012-01-10_1732.png

(The trees with solid black shadow were our first try in PNG, it just made partially transparent pixels solid black sad.png)

Any idea/tips on how to achieve good transparency with certain pixels being only partially transparent (i.e; for shadows)?

Thanks!

P.S: I'm really sorry for the awful sentence construction, I'm absolutely exhausted and can't brain very hard at the moment. You guys know how it is. ;)

P.S 2: We'd prefer not to use the pink background technique. It doesn't seem to work very well with partially transparent pixels (i.e; causes the pink outline issue) and as far as I know isn't the way to go. We really need some help understanding/learning how to use true Alpha. Thanks!
Araetis.com
Project Leader
Advertisement
Hi!

This rather looks like you have used alpha testing. Alpha testing makes a binary decision and gives you either full transparency or opacity.
But, perhaps this is an issue caused by the pre-multiplied alpha. This article here explains how alpha blending is done in xna 4.0.
Hey Tsus, thanks for your reply.

Could you elaborate on "alpha testing"? What do you mean by that? If we're using that, it's definitely not intentional. Could you explain how to turn it off and allow partial transparency?

Thanks.
Araetis.com
Project Leader
Hi again,

Alpha testing discards a pixel if its alpha value is smaller or greater than some reference value. If a pixel is discarded it does not reach the output merger state, so no writes will occur, which makes it faster than alpha blending. However, you can only have full transparency or opacity. Since alpha testing was formerly a fixed-function pipeline feature and is now deprecated, you either have to implement yourself in a shader or use the AlphaTestEffect. (So, it is not enabled by a state anymore.)

To use alpha blending you should first make sure that the texture content processor (you find it in the properties of your texture) has “Premultiply Alpha” enabled.
And then you can use in your rendering:
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(…);
spriteBatch.End();

That should do it. If it doesn’t could you post your png/tga texture, so that we can have a look at it and test it here?
Hey Tsus, unfortunately that didn't work.

Here's the PNG and TGA we've been trying everything on;

www.araetis.com/misc/1.png
www.araetis.com/misc/1.tga

If you have trouble downloading the TGA, I zipped both images under www.araetis.com/misc/misc.zip

Thanks again for your prompt replies and sorry for my slow ones.
Araetis.com
Project Leader
Hi!

The png looks fine, while the tga looks a little grayish.

I attached a small sample for you which loads and shows both textures with alpha blending on some grass. Perhaps you can spot the difference to your code. It works as expected on my end.

Good luck!
Update; we figured it out. A bit of old code slipped through our nets and there was a loop where a loop shouldn't be. We can display transparency great with PNGs now, thanks for your reply!
Araetis.com
Project Leader

This topic is closed to new replies.

Advertisement