1

我最近在我的地形项目上实现了阴影,使用 PCF 过滤,但是,地形斜坡上的阴影看起来不正确,如下所示:

在此处输入图像描述

在此处输入图像描述

如何去除地形斜坡上造成的伪影?快速搜索建议实施基于斜率的偏差,但我将如何计算它?

这是我的像素着色器:

Texture2D shadowMap : register(t0);

SamplerComparisonState SampleTypeCmp : register(s0);

cbuffer SPerLightCB : register(b0)
{
    float4 ambientColour;
    float4 diffuseColour;
};

cbuffer SPerLightPosCB : register(b1)
{
    float3 lightPos;
    float padding;
};

struct PixelIn
{
    float4 Pos    : SV_POSITION;
    float4 WPos   : TEXCOORD0;
    float4 LPos   : TEXCOORD1;
    float3 Normal : NORMAL;
};

float2 texOffset(int u, int v)
{
    return float2(u * 1.0f / 1024.0f, v * 1.0f / 1024.0f);
}

float4 main(PixelIn pin) : SV_Target
{
    //re-homogenize position after interpolation
    pin.LPos.xyz /= pin.LPos.w;

    //if position is not visible to the light - dont illuminate it
    //results in hard light frustum
    if (pin.LPos.x < -1.0f || pin.LPos.x > 1.0f ||
        pin.LPos.y < -1.0f || pin.LPos.y > 1.0f ||
        pin.LPos.z < 0.0f || pin.LPos.z > 1.0f)
    {
        return ambientColour;
    }

    //transform clip space coords to texture space coords (-1:1 to 0:1) 
    pin.LPos.x = pin.LPos.x / 2 + 0.5;
    pin.LPos.y = pin.LPos.y / -2 + 0.5;
    pin.LPos.z -= 0.001f; //apply bias

    //PCF Sampling
    float sum = 0.0f;
    float x, y;

    //perform PCF filtering
    for (y = -1.5; y <= 1.5; y += 1.0)
    {
        for (x = -1.5; x <= 1.5; x += 1.0)
        {
            sum += shadowMap.SampleCmpLevelZero(SampleTypeCmp, pin.LPos.xy + texOffset(x, y), pin.LPos.z);
        }
    }

    float shadowFactor = sum / 16.0;

    //calculate ilumination
    float3 L = normalize(lightPos - pin.WPos.xyz);
    float ndotl = dot(normalize(pin.Normal), L);
    return ambientColour + shadowFactor * diffuseColour * ndotl;
}
4

0 回答 0