繁体   English   中英

如何创建添加到Alpha蒙版时要重写的Alpha蒙版?

[英]How do I create an alpha mask which is rewritten when added to?

覆盖的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;
}

我不确定100%确定采用哪种方法,因为我不知道实际结果应该是什么样。 这是我正在谈论的几个例子。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM