3
votes

this is a very difficult problem to describe (I'm using MonoGame, Windows 8).

I want to be able to render a semi-transparent 'sprite' image, such as a .png, not onto the screen but onto a clear image, do this several times to in effect create a new sprite image by blending together various images... then, I want to be able to render this new sprite image to the screen with the areas that I haven't drawn to remaining transparent (i.e. around the edge mainly).

Sure, I can load in a semi-transparent 'sprite' .png image and 'paste' it onto the RenderTarget; I can do this several times and the 'sprites' will blend nicely. I can then 'paste' (i.e. draw/render) that composite image, the RenderTarget, onto the screen.

The problem is, when the RenderTarget is drawn onto the screen, it has a solid block around it.

Presumably, the default alpha value for each pixel in the RenderTarget is solid. Any pixel that I don't draw to, I want to remain fully transparent, just as if you grabbed a paintbrush and painted not onto paper but onto glass. If you then placed that sheet of glass on, say, your swirly carpet, everywhere that doesn't have paint you would see swirly carpet. But that's not what I see, I just see black, as though I'd 'painted onto black paper' rather than 'painted onto glass'.

If I could render an image onto another image directly there wouldn't be a problem (can this be done?): I'd create an 'empty' .png image, perhaps in PhotoShop, i.e. not draw anything then save it with transparency enabled, and I'd render my images onto that, then save the result. But a RenderTarget seems to be already opaque.

I hope the above is clear. I've searched and found lots of answers on manipulating the alpha of Texture2D objects, but I can't find anything on manipulating the alpha of a RenderTarget2D.

Thanks for any help!

1

1 Answers

2
votes

I've finally figured out the solution so I'm going to answer my own question, in case it's useful to anybody else.

It's very simple. You can in fact turn the RenderTarget2D transparent, because it is, apparently, basically a Texture2D. You can clear it like this:

GraphicsDevice.Clear(Color.Transparent);

It seems to start solid as default, which is what confused me.