使用深度測試轉譯場景

新增深度測試至頂點 (或幾何形狀) 著色器和像素著色器,建立陰影效果。 《逐步解說:使用 Direct3D 11 中的深度緩衝區實作陰影體》第 3 部分。

包含光線範圍的轉換

頂點著色器必須計算每個頂點的已轉換光線空間位置。 使用常數緩衝區提供光線空間模型、檢視和投影矩陣。 您也可以使用此常數緩衝區,提供光線位置和法線來進行光源計算。 深度測試期間將會使用光線空間的已轉換位置。

PixelShaderInput main(VertexShaderInput input)
{
    PixelShaderInput output;
    float4 pos = float4(input.pos, 1.0f);

    // Transform the vertex position into projected space.
    float4 modelPos = mul(pos, model);
    pos = mul(modelPos, view);
    pos = mul(pos, projection);
    output.pos = pos;

    // Transform the vertex position into projected space from the POV of the light.
    float4 lightSpacePos = mul(modelPos, lView);
    lightSpacePos = mul(lightSpacePos, lProjection);
    output.lightSpacePos = lightSpacePos;

    // Light ray
    float3 lRay = lPos.xyz - modelPos.xyz;
    output.lRay = lRay;
    
    // Camera ray
    output.view = eyePos.xyz - modelPos.xyz;

    // Transform the vertex normal into world space.
    float4 norm = float4(input.norm, 1.0f);
    norm = mul(norm, model);
    output.norm = norm.xyz;
    
    // Pass through the color and texture coordinates without modification.
    output.color = input.color;
    output.tex = input.tex;

    return output;
}

接下來,像素著色器會使用頂點著色器提供的插補光線空間位置,測試像素是否在陰影中。

測試位置是否位在光線範圍內

首先,將 X 和 Y 座標標準化,檢查該像素是否在光線的檢視範圍內。 如果兩者都在 [0, 1] 範圍內,表示像素有可能在陰影中。 或者,您可以略過深度測試。 如要快速測試這點,著色器可以呼叫 Saturate 並與原始值比較結果。

// Compute texture coordinates for the current point's location on the shadow map.
float2 shadowTexCoords;
shadowTexCoords.x = 0.5f + (input.lightSpacePos.x / input.lightSpacePos.w * 0.5f);
shadowTexCoords.y = 0.5f - (input.lightSpacePos.y / input.lightSpacePos.w * 0.5f);
float pixelDepth = input.lightSpacePos.z / input.lightSpacePos.w;

float lighting = 1;

// Check if the pixel texture coordinate is in the view frustum of the 
// light before doing any shadow work.
if ((saturate(shadowTexCoords.x) == shadowTexCoords.x) &&
    (saturate(shadowTexCoords.y) == shadowTexCoords.y) &&
    (pixelDepth > 0))
{

對陰影圖進行深度測試

使用範例比較函式 (SampleCmpSampleCmpLevelZero),根據深度圖測試光線空間的像素深度。 計算標準化光線空間深度值 (即 z / w),然後將值傳遞至比較函式。 由於我們使用取樣工具的 LessOrEqual 比較測試,因此比較測試通過時,內建函式會傳回零;這表示像素位於陰影內。

// Use an offset value to mitigate shadow artifacts due to imprecise 
// floating-point values (shadow acne).
//
// This is an approximation of epsilon * tan(acos(saturate(NdotL))):
float margin = acos(saturate(NdotL));
#ifdef LINEAR
// The offset can be slightly smaller with smoother shadow edges.
float epsilon = 0.0005 / margin;
#else
float epsilon = 0.001 / margin;
#endif
// Clamp epsilon to a fixed range so it doesn't go overboard.
epsilon = clamp(epsilon, 0, 0.1);

// Use the SampleCmpLevelZero Texture2D method (or SampleCmp) to sample from 
// the shadow map, just as you would with Direct3D feature level 10_0 and
// higher.  Feature level 9_1 only supports LessOrEqual, which returns 0 if
// the pixel is in the shadow.
lighting = float(shadowMap.SampleCmpLevelZero(
    shadowSampler,
    shadowTexCoords,
    pixelDepth + epsilon
    )
    );

計算陰影內或外的光源

如果像素不在陰影中,像素著色器應計算直接光源,並將其新增至像素值。

return float4(input.color * (ambient + DplusS(N, L, NdotL, input.view)), 1.f);
float3 DplusS(float3 N, float3 L, float NdotL, float3 view)
{
    const float3 Kdiffuse = float3(.5f, .5f, .4f);
    const float3 Kspecular = float3(.2f, .2f, .3f);
    const float exponent = 3.f;

    // Compute the diffuse coefficient.
    float diffuseConst = saturate(NdotL);

    // Compute the diffuse lighting value.
    float3 diffuse = Kdiffuse * diffuseConst;

    // Compute the specular highlight.
    float3 R = reflect(-L, N);
    float3 V = normalize(view);
    float3 RdotV = dot(R, V);
    float3 specular = Kspecular * pow(saturate(RdotV), exponent);

    return (diffuse + specular);
}

否則,像素著色器應使用環境光源來計算像素值。

return float4(input.color * ambient, 1.f);

請參閱本逐步解說的下一章節,瞭解如何支援各種硬體的陰影對應