I am developing a game in monogame and recently converted from XNA, and my blur effect stopped working. After successfully getting it to run without errors (and not changing any of the code logic) images that should be blurred turn into a solid color.
Images:
Before:
After (XNA):
After (monogame):
Here is the logic for my blur function (in VB): `
Public Shared Function blurImage(p As player, img As Texture2D, sb As SpriteBatch, Optional radius As Integer = 2, Optional amount As Single = 1.0F) As Texture2D
Dim sigma As Single = radius / amount : Dim kernel As Single() = New Single(radius * 2) {} : Dim index As Integer = 0
Dim twoSigmaSquare As Single = 2.0F * sigma * sigma : Dim sigmaRoot As Single = CSng(Math.Sqrt(twoSigmaSquare * Math.PI))
Dim offsetsHoriz As Vector2() = New Vector2(radius * 2) {} : Dim offsetsVert As Vector2() = New Vector2(radius * 2) {}
Dim effect As Effect = content.Load(Of Effect)("Effects\GaussianBlur")
For i As Integer = -radius To radius
index = i + radius
offsetsHoriz(index) = New Vector2(i * 1.0F / img.Width, 0F)
offsetsVert(index) = New Vector2(0F, i * 1.0F / img.Height)
Next
Dim total As Single = 0F : Dim distance As Single = 0F : index = 0
For i As Integer = -radius To radius
distance = i * i : index = i + radius
kernel(index) = CSng(Math.Exp(-distance / twoSigmaSquare)) / sigmaRoot
total += kernel(index)
Next
For i As Integer = 0 To kernel.Length - 1 : kernel(i) /= total : Next
Dim renderTarget1 As Texture2D = New RenderTarget2D(Game.graphics.GraphicsDevice, img.Width, img.Height, False, Game.graphics.GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.None)
Dim renderTarget2 As Texture2D = New RenderTarget2D(Game.graphics.GraphicsDevice, img.Width, img.Height, False, Game.graphics.GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.None)
Dim outputTexture As Texture2D = Nothing
Dim srcRect As New Rectangle(0, 0, img.Width, img.Height)
Dim destRect1 As New Rectangle(0, 0, renderTarget1.Width, renderTarget1.Height)
Dim destRect2 As New Rectangle(0, 0, renderTarget2.Width, renderTarget2.Height)
Game.graphics.GraphicsDevice.SetRenderTarget(renderTarget1)
effect.CurrentTechnique = effect.Techniques("GaussianBlur")
effect.Parameters("weights").SetValue(kernel)
effect.Parameters("colorMapTexture").SetValue(img)
effect.Parameters("offsets").SetValue(offsetsHoriz)
sb.Begin(0, BlendState.Opaque, Nothing, Nothing, Nothing, effect)
sb.Draw(img, destRect1, Color.White)
sb.[End]()
Game.graphics.GraphicsDevice.SetRenderTarget(renderTarget2)
outputTexture = DirectCast(renderTarget1, Texture2D)
effect.Parameters("colorMapTexture").SetValue(outputTexture)
effect.Parameters("offsets").SetValue(offsetsVert)
sb.Begin(0, BlendState.Opaque, Nothing, Nothing, Nothing, effect)
sb.Draw(outputTexture, destRect2, Color.White)
sb.End()
Game.graphics.GraphicsDevice.SetRenderTarget(Nothing)
outputTexture = DirectCast(renderTarget2, Texture2D)
Return outputTexture
End Function
`
And here is the code contained in the guassian blur effect file: `
#define RADIUS 2
#define KERNEL_SIZE (RADIUS * 2 + 1)
//-----------------------------------------------------------------------------
// Globals.
//-----------------------------------------------------------------------------
float weights[KERNEL_SIZE];
float2 offsets[KERNEL_SIZE];
//-----------------------------------------------------------------------------
// Textures.
//-----------------------------------------------------------------------------
texture colorMapTexture;
sampler2D colorMap = sampler_state
{
Texture = <colorMapTexture>;
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
};
//-----------------------------------------------------------------------------
// Pixel Shaders.
//-----------------------------------------------------------------------------
float4 PS_GaussianBlur(float2 texCoord : TEXCOORD) : COLOR0
{
float4 color = float4(0.0f, 0.0f, 0.0f, 0.0f);
for (int i = 0; i < KERNEL_SIZE; ++i)
color += tex2D(colorMap, texCoord + offsets[i]) * weights[i];
return color;
}
//-----------------------------------------------------------------------------
// Techniques.
//-----------------------------------------------------------------------------
technique GaussianBlur
{
pass
{
PixelShader = compile ps_4_0_level_9_1 PS_GaussianBlur();
}
}
`