gpt4 book ai didi

directx - HLSL:高斯模糊效果

转载 作者:行者123 更新时间:2023-12-02 04:11:45 27 4
gpt4 key购买 nike

我正在尝试使用后处理实现高斯模糊。我有两个渲染 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com