Drawing textures, but ONLY where current render-target is NOT transperant! [SOLVED]

I want to super-impose a texture on top of my tiles.

I need my texture to be drawn only where the current render target is NOT transparent.

the current render target starts out transparent, then gets level geometry drawn, and I want to overlay a texture ONLY where geometry is already drawn, NOT the transparent bits.
-NOTE that level geometry can have any shape, so I need overlay - functionality, or some sort of masking.

-Is there some blend-state setup for this? Or would I need some sort of pixel shader?

Easiest way to do it would probably be using a shader. Check if the alpha channel has any value for each pixel

That won’t work, he is asking to be able to test the destination render target which you can’t do in a shader

You could use a blend function, but I can’t think of one that would work off the top of my head.

Or a stencil test would work

But really why I can’t see why you want to do this

If you are rendering the level geometry , why not pass in the overlay to the same shader and no the level geometry and overlay in a single shader

You should be able to do it with a custom blend state. I think this is what you want:

new BlendState("InvDestAlpha", Blend.InverseDestinationAlpha, Blend.DestinationAlpha);
1 Like

Please, tell me more about this stencil test… Sounds like something I could use.

thanks markus

Hey Thank you! I have tinkered with what you gave me, but I could not achieve results…

I can use blend-states, I just don’t know how to configure this one.

Can you help by being more specific:
What parameters of a new blend-state do I need to set, and to what values?

Oh sorry, the constructor I was using is private. The idea was to set the following parameters:

ColorBlendFunction = BlendFunction.Add;
ColorSourceBlend = Blend.InverseDestinationAlpha;
ColorDestinationBlend = Blend.DestinationAlpha;

That should give you the color output you’re looking for. You can also control the alpha output, there are the same 3 parameters starting with Alpha. I guess you want to leave the alpha values untouched, this should do it:

AlphaBlendFunction = BlendFunction.Add;
AlphaSourceBlend = Blend.Zero;
AlphaDestinationBlend = Blend.One;
1 Like

SO CLOSE :slight_smile: Your values are cool, and I am saving them for something else, BUT they produce the opposite result of what I wanted - They super-impose on everything that IS transparent, while I am looking to super-impose on everything that IS NOT transparent. Can you advise how to get the reverse? I will post if I guess the correct values.

It worked, I tweaked the values to following:

ColorBlendFunction = BlendFunction.Add,
ColorSourceBlend = Blend.DestinationAlpha,
ColorDestinationBlend = Blend.DestinationAlpha,
AlphaBlendFunction = BlendFunction.Add,
AlphaSourceBlend = Blend.Zero,
AlphaDestinationBlend = Blend.One,


Hey markus! I have noticed a problem :frowning: So I think the additive color blending means, means that a black overlay will not draw at all, and MOST images will be slightly translucent (darker = more translucent), … I was hoping for a way to superimpose any image, even if it has black on it.

Any thoughts?