Is there a way to change the opacity or transparency for a texture or sprite?
2 Answers
Yes. You can make a semi-transparent version of any colour by multiplying it by a floating point number. For example:
Color halfOpacityWhite = Color.White * 0.5f; spriteBatch.Draw(myTexture, Vector2.Zero, halfOpacityWhite); Yes!
You need to use this overload of the SpriteBatch (or another overload that has the blend state option) with the blend state set to the Alpha Blend blend state. Then in the SpriteBatch.Draw method use a white color with the alpha (transparency) value you would like to have. You can use the Color.FromARGB() method to construct a semi transparent white color.
- \$\begingroup\$ It's worth pointing out that
BlendState.AlphaBlendis the default, so you don't need to specify it. And thatColor.FromARGB(as far as I can tell) doesn't exist. I think you might have meantFromNonPremultiplied(noting that the defaultAlphaBlendblend mode requires colours to be in premultiplied format). \$\endgroup\$Andrew Russell– Andrew Russell2012-08-06 12:56:28 +00:00Commented Aug 6, 2012 at 12:56 - \$\begingroup\$ Andrew is totally right, I've worked mostly with XNA 3 so I keep forgetting about premultiplied colours. \$\endgroup\$Roy T.– Roy T.2012-08-06 16:09:58 +00:00Commented Aug 6, 2012 at 16:09