I would like to do a simple thing : render a 2d texture to a 2d render target with alpha chanel blending.
So i create my render targer :
renderTarget = new RenderTarget2D(m_graphicsDevice, 200, 200);
then I create my texture :
texture = new Texture2D(_device, 1, 1, false, SurfaceFormat.Color);
Color clr = Color.Red;
// set half transparency on my texture
clr.A = 128;
Color[] bitmap = new Color[1] {clr};
texture.SetData(bitmap);
then I clear my rendertarget white, and draw my texture twice in the render target, with a good scale, and with a multiplicative operation on color and alpha chanel.
graphicsDevice.SetRenderTarget(renderTarget);
graphicsDevice.Clear(Color.White);
BlendState myState = new BlendState();
// multiplicative blending on color
myState.ColorSourceBlend = Blend.Zero;
myState.ColorDestinationBlend = Blend.SourceColor;
// multiplicative blending on alpha
myState.AlphaSourceBlend = Blend.Zero;
myState.AlphaDestinationBlend = Blend.SourceAlpha;
spriteBatch.Begin(SpriteSortMode.Immediate, myState);
// draw my texture twice, with an overlaping part
spriteBatch.Draw(texture, new Vector2(0, 0), null, Color.White, 0, Vector2.Zero, 100, SpriteEffects.None, 0);
spriteBatch.Draw(texture, new Vector2(50, 50), null, Color.White, 0, Vector2.Zero, 100, SpriteEffects.None, 0);
spriteBatch.End();
graphicsDevice.SetRenderTarget(null);
and I draw this render target on screen, with a green background, and a nonPremultiplied renderstate so that alpha value in the texture is applied as transparency.
graphicsDevice.Clear(Color.Green);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
spriteBatch.Draw(renderTarget, Vector2.zero, Color.White);
spriteBatch.End();
the result is as expected :
in render target, for pixels belonging to one texture only :
in render target, for overlaping pixels (so above computation applied twice)
now if I change the renderstate to just let the alpha chanel of the texture be applied, and not letting multiplication on colors :
BlendState myState = new BlendState();
// texture color is ignored, dest color remain unchanged
myState.ColorSourceBlend = Blend.Zero;
myState.ColorDestinationBlend = Blend.One;
// alpha chanel are still multiplied
myState.AlphaSourceBlend = Blend.Zero;
myState.AlphaDestinationBlend = Blend.SourceAlpha;
what I should get is :
in render target, for pixels belonging to one texture only :
in render target, for overlaping pixels (so above computation applied twice)
and what I get is a plain white texture...
why does the alpha blending computation in my render target stops working when I don't blend colors ? My goal is to only have computation on alpha chanel, without getting the texture color. How can I do that ?
thanks
You said (and what I get is a plain white texture...) but I get:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.