0

覆盖的 Alpha 蒙版

我正在尝试在应用程序中创建类似雾的效果,其中纹理(在上面的示例中为白色)在精灵周围变得透明(显示下面的黑色纹理),并且密度渐变远离它们的中心。但是,每当我向 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;
}
4

1 回答 1

1

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

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

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

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

于 2013-07-22T17:09:12.597 回答