我花了好几个小时在VB中寻找高斯模糊函数来模糊texture2Ds。我找不到一个,所以我在博客文章和朋友的帮助下自己做了一个。在这里,对于你们中的任何人来说(我知道这不是一个问题,但它应该有希望帮助你们中的一些人)
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)
If effect Is Nothing Then Throw New InvalidOperationException("GaussianBlur.fx effect not loaded.")
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