如何创建一个在添加时重写的alpha掩码?

时间:2013-07-22 16:17:17

标签: c# xna

Overwritten Alpha Mask

我正在尝试在一个应用程序中创建一个类似雾的效果,其中纹理(在白色上方的示例中)变得透明(在下面显示黑色纹理)精灵周围,密度渐变远离其中心。但是,每当我向alpha蒙版添加一个新圆圈时,它都会覆盖前面添加到蒙版纹理中,从而产生上图所示的震动效果。我一直试图在面具纹理上添加新的功能,以便与以前的添加很好地融合,但我无法理解如何做到这一点。

我已经尝试过为我的面具创建一个面具来混合,但它总是没有显示任何东西,或者使用我的alpha着色器fx文件绘制到我的alpha蒙版(这会产生一些非常奇怪的视觉效果)

如何将添加到alpha蒙版的内容混合在一起,以免相互覆盖?

非常感谢任何帮助。

-------------代码:-------------

使用此方法更新alpha蒙版:

public void createLightSource(Vector2 RemovePosition, Texture2D circleTexture)
    {

            // Create a render target, which we will draw to instead of the screen
            RenderTarget2D target = new RenderTarget2D(graphics.GraphicsDevice, mainTexture.Width, mainTexture.Height);

            // set the RenderTarget2D as the target for all future Draw calls untill we say otherwise
            graphics.GraphicsDevice.SetRenderTarget(target);

            // start our batch as usual..
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);

            // start with a transparent canvas
            graphics.GraphicsDevice.Clear(Color.Transparent);

            // add in the previously drawn dots from the current alpha map.
            spriteBatch.Draw(alphaMask, new Vector2(0, 0), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);

            // add a new dot to the map. 
            play.game.spriteBatch.Draw(circleTexture,
                    new Vector2((float)(RemovePosition.X - mainTexture.x), (float)(RemovePosition.Y - mainTexture.y)),
                    null,
                    Color.White,
                    0f,
                    new Vector2(circleTexture.Width / 2f, circleTexture.Height / 2f),
                    1f,
                    SpriteEffects.None,
                    1f);

            // end the draw call
            spriteBatch.End();

            // start drawing to the screen again
            graphics.GraphicsDevice.SetRenderTarget(null);

            // set our Texture2D Alpha Mask to equal the current render target (the new mask).
            // RenderTarget2D can be cast to a Texture2D without a problem
            alphaMask = target;
        }

在此方法中绘制到主纹理:

public void mainTextureDraw()
        {
            //alpha shader is the fx file (below)
            alphaShader.Parameters["MaskTexture"]
                                .SetValue(alphaMask);
        // start a spritebatch for our effect
        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
            null, null, null, alphaShader);

        play.game.spriteBatch.Draw(mainTexture,
            position,
            null, Color.White, 0f,
            new Vector2(0, 0),
            1f, SpriteEffects.None, 1f);

        spriteBatch.End();
    }

fx文件中的重要方法:

float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR
{
    // we retrieve the color in the original texture at 
    // the current coordinate remember that this function 
    // is run on every pixel in our texture.
    float4 color = tex2D(mainTexture, inCoord);

    // Since we are using a black and white mask the black 
    // area will have a value of 0 and the white areas will 
    // have a value of 255. Hence the black areas will subtract
    // nothing from our original color, and the white areas of
    // our mask will subtract all color from the color.
    color.rgba = color.rgba - tex2D(alphaMask, inCoord).r;

    // return the new color of the pixel.
    return color;
}

1 个答案:

答案 0 :(得分:1)

我不能100%确定接近这个方向,因为我不知道实际结果应该是什么样子。以下是我所谈论的几个例子。

https://plus.google.com/photos/117280483756658540406/albums/5903487202722042913?authkey=CK-_kZvo2Iqy9wE

但是,我认为最好的方法是查看PixelShaderFunction。您已在color中获得现有纹理,并且您正在使用tex2D()的红色组件。我想知道它是否有用的方法是使用tex2D()。r并查看它是否大于color.r,如果是,则使用color的值而不是减去tex2D()。

基本上,您有一个想要维护的阈值。您需要在像素处测试您的条件,然后决定如何以及是否要更改它。