fix blur shader

This commit is contained in:
nilllzz 2018-02-22 23:43:27 +01:00
parent 4a3c803608
commit ce42b42806
9 changed files with 252 additions and 333 deletions

View File

@ -25889,6 +25889,7 @@
<Content Include="credits.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="Resources\Blur\BlurHandler.vb" />
<None Include="packages.config" />
<None Include="Pokemon\Items\_itemList.txt" />
<None Include="My Project\app.manifest" />
@ -27398,11 +27399,11 @@
<Compile Include="Pokemon\Monster\Shedinja.vb" />
<Compile Include="Pokemon\Wild\FrontierSpawner.vb" />
<Compile Include="Pokemon\Wild\Spawner.vb" />
<Compile Include="Resources\Blur\GaussianBlur.vb" />
<Compile Include="Resources\ContentPackManager.vb" />
<Compile Include="Resources\FontContainer.vb" />
<Compile Include="Resources\FontManager.vb" />
<Compile Include="Resources\GameModeManager.vb" />
<Compile Include="Resources\GaussianBlur.vb" />
<Compile Include="Resources\ModelManager.vb" />
<Compile Include="Resources\Models\2D\BillModel.vb" />
<Compile Include="Resources\Models\2D\CrossModel.vb" />

View File

@ -0,0 +1,91 @@
Namespace Resources.Blur
Public Class BlurHandler
Implements IDisposable
Private Const BLUR_RADIUS As Integer = 7
Private Const BLUR_AMOUNT As Single = 2.0F
Private _blurCore As GaussianBlur
Private _batch As SpriteBatch
Private _rt1 As RenderTarget2D
Private _rt2 As RenderTarget2D
Private _isDisposed As Boolean = False
Public Property IsDisposed As Boolean
Get
Return _isDisposed
End Get
Private Set(value As Boolean)
_isDisposed = value
End Set
End Property
Public Sub New(width As Integer, height As Integer)
Me.New(Core.Content.Load(Of Effect)("Effects\GaussianBlur"), New SpriteBatch(Core.GraphicsDevice), width, height)
End Sub
Public Sub New(gaussianBlurEffect As Effect, batch As SpriteBatch, width As Integer, height As Integer)
_batch = batch
_blurCore = New GaussianBlur(gaussianBlurEffect)
_blurCore.ComputeKernel(BLUR_RADIUS, BLUR_AMOUNT)
Dim renderTargetWidth = CInt(width / 2)
Dim renderTargetHeight = CInt(height / 2)
_rt1 = New RenderTarget2D(Core.GraphicsDevice, renderTargetWidth, renderTargetHeight, False,
Core.GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.None)
_rt2 = New RenderTarget2D(Core.GraphicsDevice, renderTargetWidth, renderTargetHeight, False,
Core.GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.None)
_blurCore.ComputeOffsets(renderTargetWidth, renderTargetHeight)
End Sub
Public Function Perform(texture As Texture2D) As Texture2D
Return _blurCore.PerformGaussianBlur(texture, _rt1, _rt2)
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
End Sub
Private Sub Dispose(disposing As Boolean)
If Not IsDisposed Then
If disposing Then
If _rt1 IsNot Nothing AndAlso Not _rt1.IsDisposed Then
_rt1.Dispose()
End If
If _rt2 IsNot Nothing AndAlso Not _rt2.IsDisposed Then
_rt2.Dispose()
End If
If _blurCore IsNot Nothing AndAlso Not _blurCore.IsDisposed Then
_blurCore.Dispose()
End If
If _batch IsNot Nothing AndAlso Not _batch.IsDisposed Then
_batch.Dispose()
End If
End If
_rt1 = Nothing
_rt2 = Nothing
_blurCore = Nothing
_batch = Nothing
IsDisposed = True
End If
End Sub
End Class
End Namespace

View File

@ -0,0 +1,143 @@
Imports Microsoft.VisualBasic
Namespace Resources.Blur
Public Class GaussianBlur
Implements IDisposable
Private _effect As Effect
Private _spriteBatch As SpriteBatch
Private _radius As Integer
Private _amount As Single
Private _sigma As Single
Private _kernel As Single()
Private _offsetHoriz As Vector2()
Private _offsetVert As Vector2()
Private _isDisposed As Boolean = False
Public Property IsDisposed As Boolean
Get
Return _isDisposed
End Get
Private Set(value As Boolean)
_isDisposed = value
End Set
End Property
Public Sub New(gaussianBlurEffect As Effect)
_effect = gaussianBlurEffect
_spriteBatch = New SpriteBatch(Core.GraphicsDevice)
End Sub
Friend Sub ComputeKernel(blurRadius As Integer, blurAmount As Single)
_radius = blurRadius
_amount = blurAmount
_kernel = Nothing
_kernel = New Single(_radius * 2) {}
_sigma = _radius / _amount
Dim twoSigmaSquare As Single = 2.0F * _sigma * _sigma
Dim sigmaRoot As Single = CSng(Math.Sqrt(twoSigmaSquare * Math.PI))
Dim total As Single = 0F
Dim distance As Single = 0F
Dim index As Integer = 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
End Sub
Friend Sub ComputeOffsets(textureWidth As Single, textureHeight As Single)
_offsetHoriz = Nothing
_offsetHoriz = New Vector2(_radius * 2) {}
_offsetVert = Nothing
_offsetVert = New Vector2(_radius * 2) {}
Dim index As Integer = 0
Dim xOffset As Single = 1.0F / textureWidth
Dim yOffset As Single = 1.0F / textureHeight
For i As Integer = -_radius To _radius
index = i + _radius
_offsetHoriz(index) = New Vector2(i * xOffset, 0F)
_offsetVert(index) = New Vector2(0F, i * yOffset)
Next
End Sub
Friend Function PerformGaussianBlur(srcTexture As Texture2D, renderTarget1 As RenderTarget2D, renderTarget2 As RenderTarget2D) As Texture2D
If _effect Is Nothing Then
Throw New InvalidOperationException("Blur effect not loaded")
End If
Dim outputTexture As Texture2D = Nothing
Dim srcRect = New Rectangle(0, 0, srcTexture.Width, srcTexture.Height)
Dim destRect1 = New Rectangle(0, 0, renderTarget1.Width, renderTarget1.Height)
Dim destRect2 = New Rectangle(0, 0, renderTarget2.Width, renderTarget2.Height)
' perform horizontal blur
GraphicsDevice.SetRenderTarget(renderTarget1)
_effect.CurrentTechnique = _effect.Techniques("GaussianBlur")
_effect.Parameters("weights").SetValue(_kernel)
_effect.Parameters("colorMapTexture").SetValue(srcTexture)
_effect.Parameters("offsets").SetValue(_offsetHoriz)
_spriteBatch.Begin(effect:=_effect)
_spriteBatch.Draw(srcTexture, destRect1, Color.White)
_spriteBatch.End()
' perform vertical blur
GraphicsDevice.SetRenderTarget(renderTarget2)
outputTexture = CType(renderTarget1, Texture2D)
_effect.Parameters("colorMapTexture").SetValue(outputTexture)
_effect.Parameters("offsets").SetValue(_offsetVert)
_spriteBatch.Begin(effect:=_effect)
_spriteBatch.Draw(outputTexture, destRect2, Color.White)
_spriteBatch.End()
GraphicsDevice.SetRenderTarget(Nothing)
Return renderTarget2
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
End Sub
Private Sub Dispose(disposing As Boolean)
If Not IsDisposed Then
If disposing Then
If _spriteBatch IsNot Nothing AndAlso Not _spriteBatch.IsDisposed Then
_spriteBatch.Dispose()
End If
End If
_effect = Nothing
_spriteBatch = Nothing
IsDisposed = True
End If
End Sub
End Class
End Namespace

View File

@ -1,309 +0,0 @@
Imports Microsoft.Xna.Framework
Namespace Resources
''' <summary>
''' A class to apply the gaussian effect to a texture.
''' </summary>
Class GaussianEffect
Private Const BLUR_RADIUS As Integer = 7
Private Const BLUR_AMOUNT As Single = 2.0F
Private _spriteBatch As SpriteBatch
Private _blurCore As GaussianBlur
Private _rt1, _rt2 As RenderTarget2D
Private _width, _height As Integer
''' <summary>
''' Creates a new instance of the effect handler class and sets its intended dimensions.
''' </summary>
Public Sub New(ByVal width As Integer, ByVal height As Integer)
_spriteBatch = New SpriteBatch(GraphicsDevice)
_blurCore = New GaussianBlur(Core.GameInstance)
_blurCore.ComputeKernel(BLUR_RADIUS, BLUR_AMOUNT)
UpdateDimensions(width, height)
End Sub
Private Sub UpdateDimensions(ByVal width As Integer, ByVal height As Integer)
_width = width
_height = height
Dim targetWidth As Integer = CInt(width / 2)
Dim targetHeight As Integer = CInt(height / 2)
_rt1 = New RenderTarget2D(GraphicsDevice,
targetWidth, targetHeight)
_rt2 = New RenderTarget2D(GraphicsDevice,
targetWidth, targetHeight)
_blurCore.ComputeOffsets(targetWidth, targetHeight)
End Sub
''' <summary>
''' Performs the effect on a texture.
''' </summary>
Public Function Perform(ByVal texture As Texture2D) As Texture2D
If texture.Width <> _width Or texture.Height <> _height Then
'fuk OpenTK
''UpdateDimensions(texture.Width, texture.Height)
End If
Return _blurCore.PerformGaussianBlur(texture, _rt1, _rt2, _spriteBatch)
End Function
Private Class GaussianBlur
'-----------------------------------------------------------------------------
' Copyright (c) 2008-2011 dhpoware. All Rights Reserved.
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
' OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
' IN THE SOFTWARE.
'-----------------------------------------------------------------------------
' Converted to VB.Net by nilllzz (C) 20XX scrollupabuildingandrevealmenu.
'-----------------------------------------------------------------------------
Private game As Microsoft.Xna.Framework.Game
Private effect As Effect
Private m_radius As Integer
Private m_amount As Single
Private m_sigma As Single
Private m_kernel As Single()
Private offsetsHoriz As Vector2()
Private offsetsVert As Vector2()
''' <summary>
''' Returns the radius of the Gaussian blur filter kernel in pixels.
''' </summary>
Public ReadOnly Property Radius() As Integer
Get
Return m_radius
End Get
End Property
''' <summary>
''' Returns the blur amount. This value is used to calculate the
''' Gaussian blur filter kernel's sigma value. Good values for this
''' property are 2 and 3. 2 will give a more blurred result whilst 3
''' will give a less blurred result with sharper details.
''' </summary>
Public ReadOnly Property Amount() As Single
Get
Return m_amount
End Get
End Property
''' <summary>
''' Returns the Gaussian blur filter's standard deviation.
''' </summary>
Public ReadOnly Property Sigma() As Single
Get
Return m_sigma
End Get
End Property
''' <summary>
''' Returns the Gaussian blur filter kernel matrix. Note that the
''' kernel returned is for a 1D Gaussian blur filter kernel matrix
''' intended to be used in a two pass Gaussian blur operation.
''' </summary>
Public ReadOnly Property Kernel() As Single()
Get
Return m_kernel
End Get
End Property
''' <summary>
''' Returns the texture offsets used for the horizontal Gaussian blur
''' pass.
''' </summary>
Public ReadOnly Property TextureOffsetsX() As Vector2()
Get
Return offsetsHoriz
End Get
End Property
''' <summary>
''' Returns the texture offsets used for the vertical Gaussian blur
''' pass.
''' </summary>
Public ReadOnly Property TextureOffsetsY() As Vector2()
Get
Return offsetsVert
End Get
End Property
''' <summary>
''' Default constructor for the GaussianBlur class. This constructor
''' should be called if you don't want the GaussianBlur class to use
''' its GaussianBlur.fx effect file to perform the two pass Gaussian
''' blur operation.
''' </summary>
Public Sub New()
End Sub
''' <summary>
''' This overloaded constructor instructs the GaussianBlur class to
''' load and use its GaussianBlur.fx effect file that implements the
''' two pass Gaussian blur operation on the GPU. The effect file must
''' be already bound to the asset name: 'Effects\GaussianBlur' or
''' 'GaussianBlur'.
''' </summary>
Public Sub New(game As Microsoft.Xna.Framework.Game)
Me.game = game
'''Requires file restructure
'effect = Content.Load(Of Effect)("SharedResources\Effects\GaussianBlur")
effect = Content.Load(Of Effect)("Effects\GaussianBlur")
'Try
' effect = game.Content.Load(Of Effect)("SharedResources\Effects\GaussianBlur")
'Catch generatedExceptionName As ContentLoadException
' effect = game.Content.Load(Of Effect)("GaussianBlur")
'End Try
End Sub
''' <summary>
''' Calculates the Gaussian blur filter kernel. This implementation is
''' ported from the original Java code appearing in chapter 16 of
''' "Filthy Rich Clients: Developing Animated and Graphical Effects for
''' Desktop Java".
''' </summary>
''' <param name="blurRadius">The blur radius in pixels.</param>
''' <param name="blurAmount">Used to calculate sigma.</param>
Public Sub ComputeKernel(blurRadius As Integer, blurAmount As Single)
m_radius = blurRadius
m_amount = blurAmount
m_kernel = Nothing
m_kernel = New Single(m_radius * 2) {}
m_sigma = m_radius / m_amount
Dim twoSigmaSquare As Single = 2.0F * m_sigma * m_sigma
Dim sigmaRoot As Single = CSng(Math.Sqrt(twoSigmaSquare * Math.PI))
Dim total As Single = 0F
Dim distance As Single = 0F
Dim index As Integer = 0
For i As Integer = -m_radius To m_radius
distance = i * i
index = i + m_radius
m_kernel(index) = CSng(Math.Exp(-distance / twoSigmaSquare)) / sigmaRoot
total += m_kernel(index)
Next
For i As Integer = 0 To m_kernel.Length - 1
m_kernel(i) /= total
Next
End Sub
''' <summary>
''' Calculates the texture coordinate offsets corresponding to the
''' calculated Gaussian blur filter kernel. Each of these offset values
''' are added to the current pixel's texture coordinates in order to
''' obtain the neighboring texture coordinates that are affected by the
''' Gaussian blur filter kernel. This implementation has been adapted
''' from chapter 17 of "Filthy Rich Clients: Developing Animated and
''' Graphical Effects for Desktop Java".
''' </summary>
''' <param name="textureWidth">The texture width in pixels.</param>
''' <param name="textureHeight">The texture height in pixels.</param>
Public Sub ComputeOffsets(textureWidth As Single, textureHeight As Single)
offsetsHoriz = Nothing
offsetsHoriz = New Vector2(m_radius * 2) {}
offsetsVert = Nothing
offsetsVert = New Vector2(m_radius * 2) {}
Dim index As Integer = 0
Dim xOffset As Single = 1.0F / textureWidth
Dim yOffset As Single = 1.0F / textureHeight
For i As Integer = -m_radius To m_radius
index = i + m_radius
offsetsHoriz(index) = New Vector2(i * xOffset, 0F)
offsetsVert(index) = New Vector2(0F, i * yOffset)
Next
End Sub
''' <summary>
''' Performs the Gaussian blur operation on the source texture image.
''' The Gaussian blur is performed in two passes: a horizontal blur
''' pass followed by a vertical blur pass. The output from the first
''' pass is rendered to renderTarget1. The output from the second pass
''' is rendered to renderTarget2. The dimensions of the blurred texture
''' is therefore equal to the dimensions of renderTarget2.
''' </summary>
''' <param name="srcTexture">The source image to blur.</param>
''' <param name="renderTarget1">Stores the output from the horizontal blur pass.</param>
''' <param name="renderTarget2">Stores the output from the vertical blur pass.</param>
''' <param name="spriteBatch">Used to draw quads for the blur passes.</param>
''' <returns>The resulting Gaussian blurred image.</returns>
Public Function PerformGaussianBlur(srcTexture As Texture2D, renderTarget1 As RenderTarget2D, renderTarget2 As RenderTarget2D, spriteBatch As SpriteBatch) As Texture2D
If effect Is Nothing Then
Throw New InvalidOperationException("GaussianBlur.fx effect not loaded.")
End If
Dim outputTexture As Texture2D = Nothing
Dim srcRect As New Rectangle(0, 0, srcTexture.Width, srcTexture.Height)
Dim destRect1 As New Rectangle(0, 0, renderTarget1.Width, renderTarget1.Height)
Dim destRect2 As New Rectangle(0, 0, renderTarget2.Width, renderTarget2.Height)
' Perform horizontal Gaussian blur.
GraphicsDevice.SetRenderTarget(renderTarget1)
effect.CurrentTechnique = effect.Techniques("GaussianBlur")
effect.Parameters("weights").SetValue(m_kernel)
effect.Parameters("colorMapTexture").SetValue(srcTexture)
effect.Parameters("offsets").SetValue(offsetsHoriz)
spriteBatch.Begin(0, BlendState.Opaque, Nothing, Nothing, Nothing, effect)
spriteBatch.Draw(srcTexture, destRect1, Color.White)
spriteBatch.[End]()
' Perform vertical Gaussian blur.
GraphicsDevice.SetRenderTarget(renderTarget2)
outputTexture = DirectCast(renderTarget1, Texture2D)
effect.Parameters("colorMapTexture").SetValue(outputTexture)
effect.Parameters("offsets").SetValue(offsetsVert)
spriteBatch.Begin(0, BlendState.Opaque, Nothing, Nothing, Nothing, effect)
spriteBatch.Draw(outputTexture, destRect2, Color.White)
spriteBatch.[End]()
' Return the Gaussian blurred texture.
GraphicsDevice.SetRenderTarget(Nothing)
outputTexture = renderTarget2
Return outputTexture
End Function
End Class
End Class
End Namespace

View File

@ -134,7 +134,7 @@ Public Class NewInventoryScreen
Public Sub New(ByVal currentScreen As Screen, ByVal AllowedPages As Integer(), ByVal StartPageIndex As Integer, ByVal DoStuff As DoStuff)
_preScreenTarget = New RenderTarget2D(GraphicsDevice, windowSize.Width, windowSize.Height)
_blur = New Resources.GaussianEffect(windowSize.Width, windowSize.Height)
_blur = New Resources.Blur.BlurHandler(windowSize.Width, windowSize.Height)
_tabIndex = StartPageIndex
Me.AllowedPages = AllowedPages
@ -211,7 +211,7 @@ Public Class NewInventoryScreen
DrawAmount()
End Sub
Private _blur As Resources.GaussianEffect
Private _blur As Resources.Blur.BlurHandler
Private Sub DrawPrescreen()
If _preScreenTexture Is Nothing OrElse _preScreenTexture.IsContentLost Then

View File

@ -107,11 +107,11 @@ Public Class PressStartScreen
End If
End Sub
Private _blurHandler As Resources.GaussianEffect
Private _blurHandler As Resources.Blur.BlurHandler
Public Overrides Sub Draw()
If _blurHandler Is Nothing Then
_blurHandler = New Resources.GaussianEffect(windowSize.Width, windowSize.Height)
_blurHandler = New Resources.Blur.BlurHandler(windowSize.Width, windowSize.Height)
End If
GraphicsDevice.SetRenderTarget(target)
@ -123,7 +123,8 @@ Public Class PressStartScreen
GraphicsDevice.SetRenderTarget(Nothing)
_backgroundRenderer.Begin()
_backgroundRenderer.Draw(_blurHandler.Perform(target), New Rectangle(0, 0, windowSize.Width, windowSize.Height), Color.White)
Dim blurred = _blurHandler.Perform(target)
_backgroundRenderer.Draw(blurred, New Rectangle(0, 0, windowSize.Width, windowSize.Height), Color.White)
_backgroundRenderer.End()
If IsCurrentScreen() Then

View File

@ -36,7 +36,7 @@
SetCursorPosition(_menuIndex)
_cursorPosition = _cursorDestPosition
_blur = New Resources.GaussianEffect(windowSize.Width, windowSize.Height)
_blur = New Resources.Blur.BlurHandler(windowSize.Width, windowSize.Height)
End Sub
Private Sub ConstructMenu()
@ -61,7 +61,7 @@
_menuOptions.Add("Options")
End Sub
Private _blur As Resources.GaussianEffect
Private _blur As Resources.Blur.BlurHandler
Private Sub DrawPrescreen()
If _preScreenTexture Is Nothing OrElse _preScreenTexture.IsContentLost Then

View File

@ -88,7 +88,7 @@ Public Class PartyScreen
Public Sub New(ByVal currentScreen As Screen, ByVal Item As Item, ByVal ChoosePokemon As DoStuff, ByVal Title As String, ByVal canExit As Boolean, ByVal canChooseFainted As Boolean, ByVal canChooseEgg As Boolean, Optional ByVal _pokemonList As List(Of Pokemon) = Nothing, Optional ByVal ChooseMode As Boolean = True)
_preScreenTarget = New RenderTarget2D(GraphicsDevice, windowSize.Width, windowSize.Height, False, SurfaceFormat.Color, DepthFormat.Depth24Stencil8)
_blur = New Resources.GaussianEffect(windowSize.Width, windowSize.Height)
_blur = New Resources.Blur.BlurHandler(windowSize.Width, windowSize.Height)
Me.Item = Item
Me.POKEMON_TITLE = Title
@ -189,7 +189,7 @@ Public Class PartyScreen
End Sub
Private _blur As Resources.GaussianEffect
Private _blur As Resources.Blur.BlurHandler
Private Sub DrawPrescreen()
If _preScreenTexture Is Nothing OrElse _preScreenTexture.IsContentLost Then

View File

@ -12,27 +12,19 @@ float2 offsets[KERNEL_SIZE];
// Textures.
//-----------------------------------------------------------------------------
texture colorMapTexture;
sampler2D colorMap = sampler_state
{
Texture = <colorMapTexture>;
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
};
sampler colorMapTexture : register(s0);
//-----------------------------------------------------------------------------
// Pixel Shaders.
//-----------------------------------------------------------------------------
float4 PS_GaussianBlur(float2 texCoord : TEXCOORD) : COLOR0
float4 PS_MAIN(float4 position : SV_Position, float4 col : COLOR0, float2 uv : TEXCOORD0) : 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];
color += tex2D(colorMapTexture, uv + offsets[i]) * weights[i];
return color;
}
@ -42,8 +34,8 @@ float4 PS_GaussianBlur(float2 texCoord : TEXCOORD) : COLOR0
technique GaussianBlur
{
pass
pass Pass1
{
PixelShader = compile ps_5_0 PS_GaussianBlur();
PixelShader = compile ps_4_0 PS_MAIN();
}
}