MonoGame.Extended icon indicating copy to clipboard operation
MonoGame.Extended copied to clipboard

Drawing opaque texture above Tiled map

Open Jace-Beaudoin opened this issue 8 years ago • 11 comments

Hey guys,

I just started using MonoGame about a month ago, so I'm sorry in advance if this is something simple I glanced over. I'm trying to draw a Texture2D on top of a Tiled map, but drawing the Texture2D with 50% opacity so both are visible. I've found a code snippet that is the intended solution, and works for other people, but unfortunately not me.

The code to change opacity on the Texture2D: spriteBatch.Draw(tile, new Vector2(0, 0), Color.White * .5f);

Even when I change the SpriteBatch parameter to BlendState.AlphaBlend it doesn't seem to work: spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, Matrix.CreateScale(Map.myScale));

The map section with no Texture2D looks like this: monogameissue1

And with the Texture2D drawn it looks like this: monogameissue2

When the spriteBatch.Draw .5f is removed, there is no difference between that and the second image I provided. Is there something else I'm missing? Thanks

Jace-Beaudoin avatar Mar 01 '18 05:03 Jace-Beaudoin

What Texture2D are you trying to draw over the map?

stefanrbk avatar Mar 01 '18 11:03 stefanrbk

I just tried with the 1.0 version of SpriteBatch.Draw with the single argument for just a Sprite and set Sprite.Alpha = 0.5f and got this image

That overload doesn't exist in 2.0 and the code just multiplies Sprite.Color * Sprite.Alpha is 1.0 which i feel would behave the same way your supplied code would work... I did try multiplying Sprite.Color * 0.5f and it rendered clear... I'll look into it more after work.

stefanrbk avatar Mar 01 '18 19:03 stefanrbk

Sorry, the lighter blue square in the top left is the Texture2D

Jace-Beaudoin avatar Mar 02 '18 01:03 Jace-Beaudoin

No worries, I couldn't tell if that was it or if that was a chunk of map missing!

On Thu, Mar 1, 2018, 7:11 PM Jace-Beaudoin [email protected] wrote:

Sorry, the lighter blue square in the top left is the Texture2D

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/craftworkgames/MonoGame.Extended/issues/479#issuecomment-369786589, or mute the thread https://github.com/notifications/unsubscribe-auth/AWCsVMofTDco_loN6JHHNzBNcUz3N_-mks5taJw1gaJpZM4SXxCi .

stefanrbk avatar Mar 02 '18 01:03 stefanrbk

I just tried a few different things with the Sprite I draw in my test game, and I can't reproduce that issue... Tell me more about your texture2d. What format is it in? Does the format support alpha? etc...

stefanrbk avatar Mar 03 '18 19:03 stefanrbk

The texture is PNG, which from what I can tell from research has an alpha channel. I didn't think it could be an issue with the format. I'll try to see if I can reproduce the issue with other image formats.

Jace-Beaudoin avatar Mar 04 '18 04:03 Jace-Beaudoin

PNGs do have alpha channels as that is what my texture I posted above has to allow for the non-square shape of my player. If you are familiar with the debugger, see if you can pull some more stats about the Texture2D when the game is running. Maybe PNGs can be saved without alpha to save on space if your image doesn't use it, or maybe the graphics engine removes the alpha channel if your texture doesn't have it to save GRAM, I'm not certain. Graphics cards are weird at times...

stefanrbk avatar Mar 04 '18 05:03 stefanrbk

If you check your Texture2D variable, look at ._texture.Description.Format. On mine, it is listed as R8G8B8A8_UNorm where the "A8" in the middle means there is an alpha channel present when the Texture2D loaded.

stefanrbk avatar Mar 04 '18 06:03 stefanrbk

Not super proficient with the debugger, but managed to find the variable you're talking about. Value seems to be SharpDX.DXGI.Format.R8G8B8A8_UNorm just like yours. Tried to see if I had better luck with jpeg or bmp, but no luck. I also discovered the option "Include Alpha Channel" on the program I'm using (GraphicsGale) but after retesting all previously stated formats, no luck. Could it be a Visual Studio package I'm missing?

Jace-Beaudoin avatar Mar 05 '18 00:03 Jace-Beaudoin

It shouldn't be a package, could be something else though

stefanrbk avatar Mar 05 '18 20:03 stefanrbk

Huzzah! I actually found the problem! Well, sort of. I still don't know why my workaround works, but it does.

Below is a picture of 3 squares in the top left showing an alpha of 1.0f, 0.5f, and 0.0f monogameissue3

What I did was I wrapped the tiles I wanted with transparency into their own spriteBatch.Begin() and spriteBatch,End(). I have no idea why this works, I used the same parameters as my other Begin() and End(), so I think it's either how End() handles layering or MapRenderer, as I'm using Tiled. The order of drawing doesn't seem to matter in my scenario, so I'm not sure how I had a bunch of problems while others seem to be fine.

Anyway, I consider this a workaround and closed issue until I find the source of the problem. Will possibly investigate deeper down the road if other people report a similar issue. Thanks for your help!

Jace-Beaudoin avatar Mar 26 '18 22:03 Jace-Beaudoin