C# Visual Basic XNA高斯模糊函数

C# Visual Basic XNA高斯模糊函数,c#,vb.net,xna,blur,gaussian,C#,Vb.net,Xna,Blur,Gaussian,我花了几个小时在VB中寻找一个高斯模糊函数来模糊纹理2D。我没有找到,所以我自己做了一个,在一篇博文和一个朋友的帮助下。这是给你们中任何一个正在寻找它的人的(我知道这不是一个问题,但希望它能帮助你们中的一些人) 你还需要下载一个文件(名为GaussianBlur.fx),这里有一个提供该文件的博客链接:(只需将其放在项目内容中名为“Effects”的文件夹中)重新格式化为问题,并将解决方案作为答案发布。(很高兴看到VB的强大功能)你还需要下载一个文件(名为GaussianBlur.fx),这里有

我花了几个小时在VB中寻找一个高斯模糊函数来模糊纹理2D。我没有找到,所以我自己做了一个,在一篇博文和一个朋友的帮助下。这是给你们中任何一个正在寻找它的人的(我知道这不是一个问题,但希望它能帮助你们中的一些人)


你还需要下载一个文件(名为GaussianBlur.fx),这里有一个提供该文件的博客链接:(只需将其放在项目内容中名为“Effects”的文件夹中)重新格式化为问题,并将解决方案作为答案发布。(很高兴看到VB的强大功能)你还需要下载一个文件(名为GaussianBlur.fx),这里有一个提供该文件的博客链接:(只需将其放在项目内容中名为“Effects”的文件夹中)重新格式化为问题,并将解决方案作为答案发布。(很高兴看到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)

    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