0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ Couldn't you simply do this with alpha blending? \$\endgroup\$ – Andrew Russell Feb 23 '12 at 14:18
1
\$\begingroup\$

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

\$\endgroup\$
0
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ what do u mean? making the source is the destination and destination is the source ?? \$\endgroup\$ – Amidos2006 Feb 23 '12 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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