从纹理生成alpha蒙版

时间:2014-10-11 15:03:40

标签: c# xna alpha

我正在尝试在我的一个辅助类中创建一个函数,它将采用纹理(左)然后生成一个alpha掩码(右)

image mask

这是我到目前为止所拥有的:

public Texture2D CreateAlphaMask(Texture2D texture)
{
    if (texture == null)
        return null;
    {
        RenderTarget2D target = new RenderTarget2D(Device, texture.Width, texture.Height);

        Device.SetRenderTarget(target);
        Device.Clear(Color.Black);

        using (SpriteBatch batch = new SpriteBatch(Device))
        {
            BlendState blendState = new BlendState()
            {
                AlphaBlendFunction = BlendFunction.Max,
                AlphaSourceBlend = Blend.One,
                AlphaDestinationBlend = Blend.One,

                ColorBlendFunction = BlendFunction.Add,
                ColorSourceBlend = Blend.InverseDestinationColor,
                ColorDestinationBlend = Blend.Zero,

                BlendFactor = Color.White,
                ColorWriteChannels = ColorWriteChannels.All
            };

            batch.Begin(0, blendState);
            batch.Draw(texture, Vector2.Zero, Color.White);
            batch.End();
        }

        Device.SetRenderTarget(null);
        return target;
    }
}

应该发生的是,如果alpha = 0,则像素为黑色,如果alpha = 1,则像素为白色(如果需要,则在这些值之间进行插值)。

然而,我似乎无法让它比左边的基本图像更“白”。也就是说,如果我将它设置为混合白色,那么最多它会转到我所拥有的灰色色调,但从不更亮。这不是我可以提前创建的,因为它必须在游戏中计算。

0 个答案:

没有答案
相关问题