Is it possible to permanently change the alpha value of a texture without rendering it into another surface ?
Asked
Active
Viewed 2,837 times
2 Answers
4
You could use Texture2D.GetData<Color>()
and Texture2D.SetData<Color>()
and overwrite the alpha value.
Edit:
This method could be a bit tricky when using Textures with premultiplied alpha.
-
1This would be a perfect candidate for a parallel operation, no? – cwharris Nov 29 '11 at 20:01
-
but wouldn't that be crazy slow ? – pixartist Nov 29 '11 at 23:53
-
Yes changing the color value in spritebatch.draw is much faster. – ClassicThunder Nov 30 '11 at 06:21
-
That's true but you have to do this one time. – Felix K. Nov 30 '11 at 09:45
-
1It's worth pointing out that you'll also have to also modify RGB values, if you are using premultiplied-alpha textures (the default), and you want to modulate the texture's transparency. – Andrew Russell Dec 01 '11 at 03:31
-
@FelixK Actually you don't need to worry about the format the texture is in (compression is part of the format, but premultiplication is not). You specify the `
` type for the data, and `GetData` and `SetData` will handle the conversion for you. Additionally, it's not so much "problematic" as you simply have to do the premultiplication step. `Color.FromNonPremultiplied` is useful here. – Andrew Russell Dec 01 '11 at 12:55
0
If you need to change the transparency in game simply multiply Color.White by the amount you want it to be transparent. This has almost no additional overhead as the pixels color values are already being multiplied by what color you pass into the draw function. Its simply 1 additional flop per loop.
spriteBatch.Draw(texture, position, sourceRect, Color.White * 0.5f, .......)

ClassicThunder
- 1,896
- 16
- 25
-
This does not change the texture alpha, it just renders the texture with a certain alpha. I used this to render it from one texture to another and back again, in order to reduce the alpha, but this will give artifacts (I think due to premultiplied alpha). Things will never get 100% transparent, they will get grey instead, no matter how often I .Draw() them. (At least this is the case with high alpha values like .9 - .99) – pixartist Nov 30 '11 at 08:15