【问题标题】:HLSL: Gaussian Blur EffectHLSL:高斯模糊效果
【发布时间】:2016-03-30 08:50:40
【问题描述】:

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

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

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 外观,如下所示:

我做错了什么?

【问题讨论】:

    标签: directx hlsl pixel-shader post-processing


    【解决方案1】:

    我认为您需要使用如下着色器代码分别渲染水平和垂直通道,但方向不同(请参阅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

    【讨论】:

    • 你的意思是我需要第三次渲染通道?
    • 据我了解,所提供文章的作者展示了一些性能优化方法。但不是经典的高斯模糊,只是经过多次传递后的相似结果。
    猜你喜欢
    • 2016-07-08
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2020-03-20
    • 2021-10-02
    • 2018-05-01
    相关资源
    最近更新 更多