Monogame像素着色器 - 纹理传递完全透明

时间:2016-06-09 09:12:43

标签: graphics directx shader monogame hlsl

我正试图在游戏中为水制作失真着色器。我有屏幕的rendertarget和水面罩rendertarget,我试着简单地捕捉面具下面的像素,但我无法让它工作。当我传递纹理时,就好像它们都是完全透明的一样。我能做错什么?

着色器:

texture Screen;
texture Mask;
float2 Offset;

sampler ScreenSampler = sampler_state
{
      Texture = <Screen>;
};

sampler MaskSampler = sampler_state
{
      Texture = <Mask>;
};

float4 PixelShaderFunction(float2 texCoord: TEXCOORD0) : COLOR
{
    float4 mask = tex2D(MaskSampler, texCoord);
    float4 color = tex2D(ScreenSampler, texCoord + Offset);

    if (mask.a > 0)
    {
        return color;
    }

    return mask;
}

technique Technique0
{
    pass Pass0
    {
        PixelShader = compile ps_4_0 PixelShaderFunction();
    }
}

渲染目标:

Doldrums.Game.Graphics.GraphicsDevice.SetRenderTarget(renderTargetDistortion);
Doldrums.Game.Graphics.GraphicsDevice.Clear(Color.Transparent);

waterEffect.Parameters["Screen"].SetValue(Doldrums.RenderTarget);
waterEffect.Parameters["Mask"].SetValue(renderTargetWater);
waterEffect.Parameters["Offset"].SetValue(Doldrums.Camera.ToScreen(renderTargetPosition));

sprites.Begin(SpriteSortMode.Deferred, null, null, null, null, waterEffect);
sprites.Draw(renderTargetWater, Vector2.Zero, Color.White);
sprites.End();

最后,渲染rendertarget:

sprites.Draw(renderTargetDistortion, renderTargetPosition, Color.White);

1 个答案:

答案 0 :(得分:0)

我在开发过程中使用monogame完全相同的“问题”。这里的问题很容易解决,改变这个:

sprites.Begin(**SpriteSortMode.Deferred**, null, null, null, null, waterEffect);
sprites.Draw(renderTargetWater, Vector2.Zero, Color.White);
sprites.End();

到另一种模式:

sprites.Begin(**SpriteSortMode.Immediate**, null, null, null, null, waterEffect);
sprites.Draw(renderTargetWater, Vector2.Zero, Color.White);
sprites.End();

玩得开心:)