Vb.net 单一游戏高斯模糊效果产生纯色纹理2D
我正在用monogame开发一个游戏,最近从XNA转换过来,我的模糊效果停止了工作。在成功地使其无错误运行(并且不更改任何代码逻辑)后,应该模糊的图像将变成纯色 图像: 之前: 在(XNA)之后: 在(单一游戏)之后: 以下是我的模糊函数的逻辑(在VB中): ` ` 下面是包含在guassian模糊效果文件中的代码: `Vb.net 单一游戏高斯模糊效果产生纯色纹理2D,vb.net,xna,blur,monogame,gaussian,Vb.net,Xna,Blur,Monogame,Gaussian,我正在用monogame开发一个游戏,最近从XNA转换过来,我的模糊效果停止了工作。在成功地使其无错误运行(并且不更改任何代码逻辑)后,应该模糊的图像将变成纯色 图像: 之前: 在(XNA)之后: 在(单一游戏)之后: 以下是我的模糊函数的逻辑(在VB中): ` ` 下面是包含在guassian模糊效果文件中的代码: ` #定义半径2 #定义内核大小(半径*2+1) //--------------------------------------------------------------
#定义半径2
#定义内核大小(半径*2+1)
//-----------------------------------------------------------------------------
//全球的。
//-----------------------------------------------------------------------------
浮动权重[内核大小];
float2偏移量[内核大小];
//-----------------------------------------------------------------------------
//纹理。
//-----------------------------------------------------------------------------
纹理颜色贴图纹理;
sampler2D colorMap=采样器状态
{
纹理=;
MipFilter=线性;
MinFilter=线性;
磁滤波器=线性;
};
//-----------------------------------------------------------------------------
//像素着色器。
//-----------------------------------------------------------------------------
float4 PS_GaussianBlur(float2 texCoord:texCoord):颜色0
{
float4 color=float4(0.0f,0.0f,0.0f,0.0f);
for(int i=0;i
`您可能想发布前后效果的屏幕截图。我刚刚添加了图像,非常感谢!经过数小时的研究,基本问题是texture2D没有被传递到fx文件中。显然,单游戏开发者决定不再让你这么做了。我发现的一个解决方法涉及一个名为BaseTexture的预构建变量,并建议使用一个命令:“paremeters”(“BaseTexture”).setParemeters(img)-这在VB中不存在。我所拥有的只是setvalue。帮助?我怀疑MonoGame团队故意这么做。在github上提出错误报告。
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
#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();
}
}