2
\$\begingroup\$

I am trying to implement a very simple pixel shader for some basic lighting using this tutorial http://www.xnahub.com/simple-2d-lighting-system-in-c-and-monogame/

My problem is that when the effect is applied the circular gradient ends up looking like an ellipse(left) instead of its circular shape(right).

enter image description here

The technique I use is to have two render targets in monogame and blend them together. The first one is all of the light masks textures, and the second is the render target I want to add the lighting effect to.

I believe the problem is related to the shader itself because when I use this code I get the ellipse(left picture)

//Blend both render targets together onto the backbuffer
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect1.Parameters["lightMask"].SetValue(lightsTarget);
effect1.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(mainTarget, Vector2.Zero, Color.White);
//spriteBatch.Draw(lightsTarget, Vector2.Zero, Color.White);
spriteBatch.End();

However, when I comment out the effect and just draw the light render target. I get a nice circle(right picture)

//Blend both render targets together onto the backbuffer
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
//effect1.Parameters["lightMask"].SetValue(lightsTarget);
//effect1.CurrentTechnique.Passes[0].Apply();
//spriteBatch.Draw(mainTarget, Vector2.Zero, Color.White);
spriteBatch.Draw(lightsTarget, Vector2.Zero, Color.White);
spriteBatch.End();

This is my effect file

sampler s0;

texture lightMask;
sampler lightSampler : register(s1) = sampler_state {Texture = <lightMask>;};

float4 PixelShaderFunction(float4 pos : SV_Position, float4 color1 : COLOR0, float2 coords: TEXCOORD0) : COLOR0
{
    float4 color = tex2D(s0, coords);
    float4 lightColor = tex2D(lightSampler, coords);
    return color * lightColor;
}


technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

Thanks.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This was a pretty simple error made in my initialization code for the two render targets.

The render target mainTarget had a height twice the length of the lightsTarget. When the shader was processing them it had to stretch the lightsTarget texture to get it to work.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks for coming back and posting the answer for the rest of us to benefit from. \$\endgroup\$
    – ashes999
    Jan 28, 2017 at 21:18

You must log in to answer this question.

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