How to make a Texture2D 50% transparent? XNA

爱⌒轻易说出口 提交于 2019-12-04 08:57:36

问题


I'm using SpriteBatch to draw a Texture2D on the screen and was wondering how I could manipulate the the images opacity? Anyone know the best way in accomplishing this?


回答1:


Assuming you are using XNA 4.0 with premultiplied alpha. In your spritebatch.draw multiply the color by a float, 0.5f for 50% transparency, and draw as you would normally. If you are not using premultiplied alpha I suggest you do for performance reasons and its more intuitive after you get used to it.

Example:

_spriteBatch.Draw(texture, location, Color.White * 0.5f);

Edit: Also make sure you set your blend state to BlendState.AlphaBlend, or another blend state that supports alpha and is not NonPremultiplied.

Example:

_spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);



回答2:


Just use color as new Color(RGBA); where:

  • R is Red
  • G is Green
  • B is Blue
  • A is Alpha

For instance:

new Color(100, 100, 100, 100);


来源:https://stackoverflow.com/questions/6632723/how-to-make-a-texture2d-50-transparent-xna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!