xna 4渲染目标与alpha通道

时间:2014-03-17 22:51:54

标签: c# xna alpha monogame rendertarget

我想做一件简单的事情:使用alpha chanel混合将2d纹理渲染到2d渲染目标。

所以我创建了渲染目标:

renderTarget = new RenderTarget2D(m_graphicsDevice, 200, 200);

然后我创建我的纹理:

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);

然后我清除我的渲染目标白色,并在渲染目标中绘制我的纹理两次,具有良好的比例,并在颜色和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);

我在屏幕上绘制了这个渲染目标,带有绿色背景和一个非预乘的渲染器,以便纹理中的alpha值作为透明度应用。

graphicsDevice.Clear(Color.Green);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
spriteBatch.Draw(renderTarget, Vector2.zero, Color.White);
spriteBatch.End();

结果如预期:

    渲染目标中的
  • ,仅适用于属于一个纹理的像素:

    • red chanel = texture red chanel(1)* render target red chanel(1)= 1
    • 绿色和蓝色chanel =纹理chanel(0)*渲染目标chanel = 0
    • alpha chanel =纹理alpha(0.5)*渲染目标alpha(1)= 0.5
    • 由于Alpha透明度设置为0.5(1/2红色+ 1/2绿色),绿色背景上方绘制的纹理会产生棕色。
  • 在渲染目标中,用于重叠像素(因此上面计算应用两次)

    • red chanel = texture red chanel(1)* texture red chanel(1)* render target red chanel(1)= 1
    • 绿色和蓝色chanel =纹理chanel(0)*纹理chanel(0)*渲染目标chanel = 0
    • alpha chanel =纹理alpha(0.5)*纹理alpha(0.5)*渲染目标alpha(1)= 0.25
    • 并且在绿色背景上方绘制的纹理由于alpha值0.25(1/4红色+ 3/4绿色)而呈现近乎绿色的颜色

现在,如果我将renderstate更改为仅应用纹理的alpha chanel,而不是让颜色乘以:

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;

我应该得到的是:

    渲染目标中的
  • ,仅适用于属于一个纹理的像素:

    • red chanel = 0 * texture red chanel(1)+ 1 * render target red chanel(1)= 1
    • 绿色和蓝色chanel = 0 *纹理chanel(0)+ 1 *渲染目标chanel(1)= 1
    • alpha chanel =纹理alpha(0.5)*渲染目标alpha(1)= 0.5
    • 并且在绿色背景上方绘制的此纹理应为浅绿色(1/2白色+ 1/2绿色)
  • 在渲染目标中,用于重叠像素(因此上面计算应用两次)

    • red chanel = 1
    • 绿色和蓝色香奈儿= 1
    • alpha chanel =纹理alpha(0.5)*纹理alpha(0.5)*渲染目标alpha(1)= 0.25
    • 并且在绿色背景上方绘制的纹理应该会产生更强烈的绿色(0.25 *白色+ 0.75 *绿色)

我得到的是纯白色纹理......

为什么我的渲染目标中的alpha混合计算在我不混合颜色时停止工作? 我的目标是只对alpha chanel进行计算,而不会获得纹理颜色。我怎么能这样做?

感谢

1 个答案:

答案 0 :(得分:0)

你说(我得到的是纯白色的纹理......)但我得到: enter image description here

相关问题