Sprite Alpha ignored over 3d with Effect

I’m trying to implement a HUD-over-3D. Everything was fine until I added Effects (ie, SpriteSortMode.Immediate). The below image shows my problem:

As you can see, this little Greyscale effect works as intended, but loses the alpha. This image below is with SpriteSortMode.Deferred:

All the alphas work fine, but I can’t use Effects.

Things I’ve tried:

  • Different sprite batches (one Deferred, one Immediate): the effects batch draws underneath the Deferred batch, tinting the fx sprites. Played with layer order, tried all the BlendStates, nothing helped.
  • NonPremultiply in the content pipeline: no change
  • Declare AlphaBlendEnable = TRUE in my base shader: no change
  • Many other things which didn’t change anything!

Here’s my drawing code (‘currentSortMode’ lets me toggle between Deferred & Immediate):

 GraphicsDevice.Clear(Color.Black);
 Globals.spriteBatch.Begin(currentSortMode, BlendState.AlphaBlend, null, DepthStencilState.DepthRead);

 //sprites are instantiated with a base shader, ie, return tex2D(SpriteTextureSampler,input.TextureCoordinates) * input.Color;
 //these 2 lines are within the base 2d object class's Draw method:
 effect.CurrentTechnique.Passes[0].Apply();
 Globals.spriteBatch.Draw(myModel, new Rectangle((int)(pos.X), (int)(pos.Y), (int)dims.X, (int)dims.Y), null, color, rot, new Vector2(myModel.Bounds.Width/2, myModel.Bounds.Height/2), new SpriteEffects(), 0);

 //...and back to Game.cs:
 Globals.spriteBatch.End();
 //Draw Skybox

I picked up Monogame maybe 2 weeks ago, so any clues to what I’m not considering would be greatly appreciated.

Hi @mujadaddy, Welcome to the Community!

Have you joined the Discord yet? You may get a quicker response on this matter there, simply share this post in the Help channel and it should get a faster response.

Happy Coding!

In your SpriteBatch.Begin change your BlendState to NonPremultiplied instead of AlphaBlend when using custom FX. Set alpha (return float4) in pixel shader.

Already tried the BlendState, no effect.

Everything I know about shaders I learned the last two weeks, so can you tell me what would need to change here? It looks like I’m already trying to return the alpha in the float4…

struct VertexShaderOutput
{
	float4 Position : SV_POSITION;
	float4 Color : COLOR0;
	float2 TextureCoordinates : TEXCOORD0;
};

float4 MainPS(VertexShaderOutput input) : COLOR
{
	return tex2D(SpriteTextureSampler,input.TextureCoordinates) * input.Color;
}

@MrValentine Joined, waiting 10 minutes to post.

… I’ve played with the Graphics.Clear color, and it turns out it’s writing whatever the the Clear color is, but still ignoring the Alpha.

1 Like

Hmm. Are you using render targets (specifically, switching RTs)? Is your DepthBuffer enabled (should only be an issue for 3D models when mixing with 2D drawing)? I don’t think we have enough puzzle pieces to solve this particular issue. If you could upload a basic project showing this (clean project with the minimal amount needed to reproduce), we could help much better/faster.

@TheKelsam I can do that, it may take a while though; it will be easier for me to start a new project and see if I can recreate the issue.

Note, though, that I ONLY have this issue when I’m drawing over my 3D space, not when drawing 2d-over-2d:

Fixed! See below.

Ok, I was trying to do WAY too much.

I had the SpriteBatch.Begin BEFORE any 3D drawing, including the Skybox.

I’ve moved the Begin call after the skybox, and now it works the way I’m expecting it. Thanks to folks in the Discord for talking me through their approaches!

Here’s the final view:
spritebatchissueFixed

2 Likes