0

I'm trying to make a distortion shader for water in my game. I have the screen's rendertarget, and the water mask rendertarget, and I'm try to simply capture the pixels underneath the mask, but I can't get it to work. When I pass the textures, it's as if they're both completely transparent. What could I be doing wrong?

Shader:

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();
    }
}

Render target:

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();

Finally, rendering the rendertarget:

sprites.Draw(renderTargetDistortion, renderTargetPosition, Color.White);
user1438378
  • 141
  • 2
  • 5
  • Could you add the code with which you create the render targets (maybe you choose a format which has no alpha channel) – VB_overflow Jun 10 '16 at 16:21
  • Also have you already tried to do something more simple, like rendering a single sprite to a render target then showing this render target on screen to check if at least this worked ? – VB_overflow Jun 10 '16 at 16:27

1 Answers1

0

I had the exact same "issue"using monogame during my development. The problem here is easily fixed, change this:

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

To another mode like this:

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

Have fun :)

Cron
  • 1