Quantcast
Channel: xna 4 render target with alpha chanel - Stack Overflow
Viewing all articles
Browse latest Browse all 2

xna 4 render target with alpha chanel

$
0
0

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 textureclr.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 colormyState.ColorSourceBlend = Blend.Zero;myState.ColorDestinationBlend = Blend.SourceColor;// multiplicative blending on alphamyState.AlphaSourceBlend = Blend.Zero;myState.AlphaDestinationBlend = Blend.SourceAlpha;spriteBatch.Begin(SpriteSortMode.Immediate, myState);// draw my texture twice, with an overlaping partspriteBatch.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 :

    • red chanel = texture red chanel (1) * render target red chanel (1) = 1
    • green and blue chanel = texture chanel (0) * render target chanel = 0
    • alpha chanel = texture alpha (0.5) * render target alpha (1) = 0.5
    • and this texture drawn above the green background gives a brown color thanks to alpha transparency set to 0.5 (1/2 red + 1/2 green)
  • in render target, for overlaping pixels (so above computation applied twice)

    • red chanel = texture red chanel (1) * texture red chanel (1) * render target red chanel (1) = 1
    • green and blue chanel = texture chanel (0) * texture chanel (0) * render target chanel = 0
    • alpha chanel = texture alpha (0.5) * texture alpha (0.5) * render target alpha (1) = 0.25
    • and this texture drawn above the green background gives a nearly green color thanks to alpha value 0.25 (1/4 red + 3/4 green)

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 unchangedmyState.ColorSourceBlend = Blend.Zero;myState.ColorDestinationBlend = Blend.One;// alpha chanel are still multipliedmyState.AlphaSourceBlend = Blend.Zero;myState.AlphaDestinationBlend = Blend.SourceAlpha;

what I should get is :

  • in render target, for pixels belonging to one texture only :

    • red chanel = 0 * texture red chanel (1) + 1 * render target red chanel (1) = 1
    • green and blue chanel = 0 * texture chanel (0) + 1 * render target chanel (1) = 1
    • alpha chanel = texture alpha (0.5) * render target alpha (1) = 0.5
    • and this texture drawn above the green background should give light green color (1/2 white + 1/2 green)
  • in render target, for overlaping pixels (so above computation applied twice)

    • red chanel = 1
    • green and blue chanel = 1
    • alpha chanel = texture alpha (0.5) * texture alpha (0.5) * render target alpha (1) = 0.25
    • and this texture drawn above the green background should give a more intense green (0.25 * white + 0.75 * green)

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


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images