如何去除模糊晕效果

时间:2014-06-14 22:53:51

标签: c++ directx hlsl

我试图模糊我的深度缓冲区,然后让我的流体模拟表面正常 这是我的hlsl像素着色器双边滤镜

float depth = depthTexture.Sample( defss1, pin.Tex).x;

float blurDepthFalloff = 2.0f;
float radius = 4.0f;
float blurScale = 2.0f / radius;
float sum = 0.0f;
float wsum = 0.0f;
float2 scale = (1.0f/(1920.0f), 1.0f/(1080.0f));

for(float x=-radius; x<=radius; x+=1.0f) 
{
    float cur = depthTexture.Sample( defss1, pin.Tex + x * scale).x;

    // range domain
    float r2 = (depth - cur) * blurDepthFalloff;
    float g = exp(-r2*r2);

    // spatial domain
    float r = x * blurScale;
    float w = exp(-r*r);

    sum += cur * w * g;
    wsum += w * g;
}
if (wsum > 0.0f)
{
    sum /= wsum;
}
return  float4(sum,sum,sum,1.0f);

及其结果enter image description here

如何去除边缘光环效应?


这是我的正常计算代码

float2 uv = pin.Tex;

float depth = depthTexture.Sample( defss1, uv.xy).x;


float3 eyePos = uvToEye(uv,depth);

float2 texCoord1 = float2(uv.x+xFactor,uv.y);
float2 texCoord2 = float2(uv.x-xFactor,uv.y);

float3 ddx = uvToEye(texCoord1, depthTexture.Sample( defss1,texCoord1.xy).x)-eyePos;
float3 ddx2 = eyePos-uvToEye(texCoord2, depthTexture.Sample( defss1,texCoord2.xy).x);
if(abs(ddx.z)>abs(ddx2.z))
{
    ddx = ddx2;
}

texCoord1 = float2(uv.x,uv.y+yFactor);
texCoord2 = float2(uv.x,uv.y-yFactor);

float3 ddy = uvToEye(texCoord1, depthTexture.Sample( defss1,texCoord1.xy).x)-eyePos;
float3 ddy2 = eyePos-uvToEye(texCoord2, depthTexture.Sample( defss1,texCoord2.xy).x);
if(abs(ddy.z)>abs(ddy2.z))
{
    ddy = ddy2;
}

float3 n = cross(ddx, ddy);
n = normalize(n);
n.y = -n.y;
return float4(n,1.0f);

0 个答案:

没有答案