Search code examples
c#monogamealphablendingspritebatch

Alpha Blending Problems With Spritebatch in Monogame


I'm using the player's own sprite to create a dynamic shadow on the floor below him.

The character's current animation frame is flipped vertically, squashed vertically, and drawn below him in black with partial alpha transparency.

spriteBatch.Draw (character_sprite[character[c].sprite, 0], r_draw, r_source, Color.Black * .3f, MathHelper.ToRadians (0), v_origin, SpriteEffects.FlipVertically, 0);

This looked great in XNA, but after porting to Monogame, I've been getting some weird results.

It's my understanding that opacity levels should range from 0f to 1f, but most values seem to produce solid black (full opacity), unless I drop it down to about .05f or lower. At this point, I get partial transparency, but the results look grey (less saturation) instead of black (darker). On a grey surface, for example, there's no shadow at all.

It almost seems like spriteBatch is using AdditiveBlend instead of AlphaBlend, but I'm definitely setting it to alpha and only setting it once:

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

What's also interesting is that if I set the color to Color.White (instead of black), the transparency looks normal (like a reflection) with no warped colors.

Does Monogame handle alpha transparency differently than XNA?

UPDATE:

I tried creating a generic, black, oval shadow and drawing it with Color.White * opacity. This also resulted in a shadow that was grey (rather than darkening the background), disproving my earlier theory about Color.White. It seems like all partial transparency in the game is being handled as additive instead of alpha.


Solution

  • After some experimentation, I realized this was happening only with character shadows, and not with any other transparent sprites.

    I finally narrowed the problem down to a logical error in my programming that was placing multiple calls to the character_draw function each frame. Anything with alpha transparency being drawn in that function was getting pasted over itself multiple times, causing both the unusually high opacity sensitivity, as well as the apparent additive (or greyed-out) effect I was seeing.