Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I wanted to subtract a white image with black circle background from a picture to get a lighting circle here is the white image White Background and Black Circle So I wrote this code

spriteBatch.Begin(SpriteSortMode.Deferred, new BlendState
{
      ColorBlendFunction = BlendFunction.Subtract,
      AlphaBlendFunction = BlendFunction.Subtract
});
//Draw the white image with black circle
spriteBatch.Draw(test, new Vector2(), Color.White);
spriteBatch.End();

I was aiming to get difference between it and the background image rendered enter image description here to get something like this enter image description here

But the result that the first image is only drawn while second is below it

share|improve this question
    
Couldn't you simply do this with alpha blending? –  Andrew Russell Feb 23 '12 at 14:18

2 Answers 2

up vote 1 down vote accepted

Reposing my answer from here.

enter image description here

Just set the below as the BlendState in SpriteBatch.Begin(). Also worth noting is that if the SortMode is SpriteSortMode.Immediate you can alter the all of the GraphicsDevice's properties without calling Spritebatch.Begin again.

        GraphicsDevice.BlendState = new BlendState {

            ColorSourceBlend = Blend.Zero,
            ColorDestinationBlend = Blend.InverseSourceColor,

            AlphaSourceBlend = Blend.Zero,
            AlphaDestinationBlend = Blend.InverseSourceColor

        };

enter image description here

share|improve this answer
    
wow thanks that works :) –  Amidos2006 Feb 24 '12 at 8:56

Try inverting the image, in substraction what happens is that (for every channel (RGB) of every pixel) the following calculation is done: result = clip( source - filter, 0, 255 ).

With filter the white image you posted. Meaning that the white parts of the source becoming black, and the black parts staying the same.

share|improve this answer
    
what do u mean? making the source is the destination and destination is the source ?? –  Amidos2006 Feb 23 '12 at 12:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.