C# 从纹理生成alpha遮罩

C# 从纹理生成alpha遮罩,c#,xna,alpha,C#,Xna,Alpha,我试图在我的一个助手类中创建一个函数,该函数将获取纹理(左),然后生成alpha遮罩(右) 以下是我目前掌握的情况: public Texture2D CreateAlphaMask(Texture2D texture) { if (texture == null) return null; { RenderTarget2D target = new RenderTarget2D(Device, texture.Width, texture.H

我试图在我的一个助手类中创建一个函数,该函数将获取纹理(左),然后生成alpha遮罩(右)

以下是我目前掌握的情况:

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,那么像素是白色的(如果需要,可以在这些值之间插值)


然而,我似乎无法使它比左边的基础图像“更白”。也就是说,如果我将其设置为混合白色,那么它最多会变成我拥有的灰色色调,但不会更亮。这也不是我可以预先创建的,因为它必须在游戏期间计算。

我是否理解正确,您想将颜色通道r、g、b设置为源alpha值,将alpha设置为1?我想是这样。对于如何使用BlendState,我只有一个模糊的想法,而对于如何从中获得特定效果,我的想法就更少了。对我来说,着色器似乎更合适,因为你完全忽略了混合目标。您希望将源图像转换为其他图像。您可能会在