MonoGame - BlendState - 2D SpriteBatch

2019-09-01 02:20发布

问题:

I need SpriteBatch to render images how I would expect in WPF or a normal UIKit app:

  • partially transparent PNGs render on top of one another, as you would expect
  • I have an alpha I can modify, preferably something like using Color.White and modifying the A value.

Closest has been using BlendState.NonPremultiplied, but I get weird lines where partially transparent PNGs overlap on one another.

I was having similar issues on Windows (see here), but fixed it by changing the Premultiplied setting in the XNA content project. How can I do similar for MonoGame? (I'm expecting there is a difference in OpenGL here)

回答1:

We solved it by using two different setups.

On Windows and XNA:

  • Use BlendState.NonPremultiplied
  • Change the default setting to Premultiply = False on the content project for all PNGs
  • To modify Alpha value of a sprite, use Color.White and set A value

On MonoGame and iOS:

  • Use BlendState.AlphaBlend
  • Leave PNGs as-is
  • To modify Alpha value of a sprite, use Color.White * (float)Alpha / (float)byte.MaxValue

My PNGs are not pre-multiplied and are used exactly as you'd expect coming out of Photoshop.