Applying mask to texture

Started by
-1 comments, last by Micha?Ossowski 12 years, 1 month ago
Hi,
I have a problem with applying mask to texture. The steps are as follows:


mask = new RenderTarget2D(GraphicsDevice,
graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight,
false, SurfaceFormat.Color, DepthFormat.None);


...


GraphicsDevice.SetRenderTarget(mask);
GraphicsDevice.Clear(Color.Transparent);

spriteBatch.Begin();
spriteBatch.Draw(terrain, Vector2.Zero, Color.White);
spriteBatch.End();

BlendState bs = new BlendState();

bs.ColorSourceBlend = Blend.One;
bs.ColorDestinationBlend = Blend.One;
bs.ColorBlendFunction = BlendFunction.Add;

bs.AlphaSourceBlend = Blend.One;
bs.AlphaDestinationBlend = Blend.One;
bs.AlphaBlendFunction = BlendFunction.Add;

spriteBatch.Begin(SpriteSortMode.FrontToBack, bs);
spriteBatch.Draw(tex2, new Vector2(100f, 250f), Color.White);
spriteBatch.End();

GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.Draw((Texture2D)rt, Vector2.Zero, Color.White);
spriteBatch.End();


The mask and terrain are PNG textures with alpha channel. I just want to take alpha value of each pixel from mask and "put" them into render target (strictly, multilply alpha values of source and destination). BlendState class has (almost) no documentation but I don't want to use shaders. Do you happen to know how to configure this object?

EDIT:
Thank you for not responding. smile.png This motivated myself to investigate the problem thoroughly.
My configuration is:

bs.ColorSourceBlend = Blend.Zero;
bs.ColorDestinationBlend = Blend.SourceAlpha;
bs.ColorBlendFunction = BlendFunction.Add;

bs.AlphaSourceBlend = Blend.Zero;
bs.AlphaDestinationBlend = Blend.SourceAlpha;
bs.AlphaBlendFunction = BlendFunction.Add;

Now, it works perfectly.

This topic is closed to new replies.

Advertisement