0
\$\begingroup\$

I'm working on a shader but it has a weird tint to it and I'm not sure why. Right now it's extremely simple, it just sets a rendertarget, draws to it, passes it as a texture to the shader then oututs the pixels to a fullscreen quad. If I remove the rendertarget/texture part and just directly pass it into the shader it comes out properly, but once I start including rendertargets the colors get a little weird.

Here are two images showing what I mean:

This is how it should look normally

This is how it should look normally

This is how it looks with the rendertargets This is how it looks with the rendertargets I've included the code for the shader and the draw method below:

Shader:

float4x4 World;
float4x4 View;
float4x4 Projection;

Texture glowTexture;

sampler glowSampler = sampler_state 
{ 
texture = <glowTexture>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = clamp;
AddressV = clamp;
};

struct VertexShaderInput
{
float4 Position : POSITION0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
};

VertexShaderOutput VSOutline(VertexShaderInput input)
{
VertexShaderOutput output;

output.Position = input.Position;

return output;
}

float4 PSOutline(VertexShaderOutput input, float2 pixel:VPOS) : COLOR0
{
float2 uvPixel = (pixel + 0.5) * float2(1.0 / 1280, 1.0 / 770);

float4 finalColor = tex2D(glowSampler, uvPixel);

return finalColor;
}

technique Outline
{
pass Pass1
{
    AlphaBlendEnable = TRUE;
    DestBlend = INVSRCALPHA;
    SrcBlend = SRCALPHA;

    VertexShader = compile vs_3_0 VSOutline();
    PixelShader = compile ps_3_0 PSOutline();
}
}

Draw method:

            GraphicsDevice.SetRenderTarget(renderTarget);
            GraphicsDevice.Clear(Color.Black);

            scene.RenderGrid();
            playerList.Render();
            scene.RenderObjects();

            GraphicsDevice.SetRenderTarget(null);


            renderTargetTexture = (Texture2D)renderTarget;

            for (int l = 0; l < resourceLoader.GlowShader.CurrentTechnique.Passes.Count; l++)
            {
                resourceLoader.GlowShader.Parameters["glowTexture"].SetValue(renderTargetTexture);

                resourceLoader.GlowShader.CurrentTechnique.Passes[l].Apply();
                renderQuad.Render();
            }
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think that you only haven't clean rendertarget to black before drawing. \$\endgroup\$
    – Vodáček
    Sep 22, 2013 at 17:16

1 Answer 1

2
\$\begingroup\$

I think it is either that the SpriteBatch changes the GraphicsDevice states or that you haven't cleared your rendertarget like Vodáček suggested.

For more information on the device states see BlendState and the others

\$\endgroup\$
2
  • \$\begingroup\$ Aha, thanks you two, it was because I wasn't clearing it. I didn't realise I had to clear it on both rendertargets and the second wasn't being cleared \$\endgroup\$ Sep 22, 2013 at 19:11
  • \$\begingroup\$ Search for "xna purple background" and there will be tons of information. Purple background is always a hint from XNA that something went wrong while changing render targets. \$\endgroup\$
    – 0xBADF00D
    Sep 24, 2013 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .