directx - HLSL:高斯模糊效果

标签 directx hlsl pixel-shader post-processing

我正在尝试使用后处理实现高斯模糊。我有两个渲染 channel ;第一个 channel 渲染场景,第二个 channel 用于效果。

这是我的像素着色器代码:

const float offset[] = {
    0.0, 1.0, 2.0, 3.0, 4.0
    };
    const float weight[] = {
      0.2270270270, 0.1945945946, 0.1216216216,
      0.0540540541, 0.0162162162
    };
    ppColour = SceneTexture.Sample(PointSample, ppIn.UV) * weight[0];
    float3 FragmentColor = float3(0.0f, 0.0f, 0.0f);

    for (int i = 1; i < 5; i++) {
        // Horizontal-pass
        FragmentColor +=
            SceneTexture.Sample(PointSample, ppIn.UV + float2(0.0f, offset[i]))*weight[i] +
            SceneTexture.Sample(PointSample, ppIn.UV - float2(0.0f, offset[i]))*weight[i];      
            // Vertical-pass
        FragmentColor +=
            SceneTexture.Sample(PointSample, ppIn.UV + float2(offset[i], 0.0f))*weight[i] +
            SceneTexture.Sample(PointSample, ppIn.UV - float2(offset[i], 0.0f))*weight[i];
        }
ppColour += FragmentColor;
return (ppColour,1.0);

我得到了字符串 y 的外观,如下所示: enter image description here

我做错了什么?

最佳答案

我认为您需要使用如下所示的着色器代码分别渲染水平和垂直 channel ,但方向不同(请参阅dir统一变量)。所以你需要 3 个步骤

  • 使用默认着色器将场景渲染到纹理 A
  • 使用高斯模糊着色器水平将纹理 A 渲染到纹理 B (dir={1.0,0.0})
  • 使用相同的高斯模糊着色器垂直将纹理 B 渲染到屏幕 (dir={0.0,1.0})
uniform vec2 dir;

const float offset[] = {0.0, 1.0, 2.0, 3.0, 4.0};
const float weight[] = {
  0.2270270270, 0.1945945946, 0.1216216216,
  0.0540540541, 0.0162162162
};
ppColour = SceneTexture.Sample(PointSample, ppIn.UV) * weight[0];
float3 FragmentColor = float3(0.0f, 0.0f, 0.0f);

//(1.0, 0.0) -> horizontal blur
//(0.0, 1.0) -> vertical blur
float hstep = dir.x;
float vstep = dir.y;

for (int i = 1; i < 5; i++) {
    FragmentColor +=
        SceneTexture.Sample(PointSample, ppIn.UV + float2(hstep*offset[i], vstep*offset[i]))*weight[i] +
        SceneTexture.Sample(PointSample, ppIn.UV - float2(hstep*offset[i], vstep*offset[i]))*weight[i];      
    }
ppColour += FragmentColor;
return (ppColour,1.0);

参见Efficient Gaussion Blur with Linear Sampling

关于directx - HLSL:高斯模糊效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36303950/

相关文章:

DirectX11 : Pass data from ComputeShader to VertexShader?

c++ - 如何将 DirectX 库包含到我自己的静态库(独立库)中?

c++ - Visual Studio 的图形诊断未捕获任何数据

math - 在 Direct3D 中使用像素着色器绘制旋转球体

java - 改变每个形状的颜色深浅

c++ - 为什么 "Warning X4000: use of potentially uninitialized variable"显示了多个常用方法的用法?

c++ - 地形 segmentation 和深度缓冲区

c++ - 如何使用 DirectX11 着色器将纹理应用到正交平面

arrays - 将数组从 HLSL 顶点着色器传递到像素着色器

c# - Visual Studio 图形调试器忽略工作像素着色器