看起来一切都配置正确:6 个视图矩阵、1 个投影矩阵、一个将图元转换为光源空间并将 6 个纹理写入深度缓冲区立方体贴图的几何着色器。仅当视图矩阵中的近平面和 lightPerspectiveValues 来源的矩阵不同(分别为 0.1f 和 1.0f)时,它才起作用。但结果并不相同 - 远处的物体不会被遮挡,只有那些距离点光源很近的物体才会被遮挡。我在 HLSL 开发书籍和这篇文章中看到了该技术: https: //habr.com/ru/articles/259679/
void TestApplication::PrepareLightViewMatrixes(glm::vec3 lightPos)
{
glm::vec3 vectors[] =
{
glm::vec3(0.f, 0.f, 0.f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(0.f, 0.f, 0.f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f),
glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f),
glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f)
};
m_DepthCaptureViews =
{
glm::lookAtLH(lightPos, lightPos + vectors[1], vectors[2]),
glm::lookAtLH(lightPos, lightPos + vectors[4], vectors[5]),
glm::lookAtLH(lightPos, lightPos + vectors[7], vectors[8]),
glm::lookAtLH(lightPos, lightPos + vectors[10], vectors[11]),
glm::lookAtLH(lightPos, lightPos + vectors[13], vectors[14]),
glm::lookAtLH(lightPos, lightPos + vectors[16], vectors[17])
};
}
glm::mat4 lightProjMat = glm::perspectiveLH(XM_PIDIV2, 1.f, 0.1f, 100.f);
///// Передаю lightPerspectiveValues и матрицы через константный буфер /////
XMMATRIX matPointProj = XMMatrixPerspectiveFovLH(XM_PIDIV2, 1.0, 1.f, 100.f);
XMFLOAT4X4 matPointProjFloat;
XMStoreFloat4x4(&matPointProjFloat, matPointProj);
lightCb.lightPerspectiveValues = glm::vec2(matPointProjFloat.m[2][2], matPointProjFloat.m[3][2]);
lightCb.cubeView[0] = glm::transpose(m_DepthCaptureViews[0]);
lightCb.cubeView[1] = glm::transpose(m_DepthCaptureViews[1]);
lightCb.cubeView[2] = glm::transpose(m_DepthCaptureViews[2]);
lightCb.cubeView[3] = glm::transpose(m_DepthCaptureViews[3]);
lightCb.cubeView[4] = glm::transpose(m_DepthCaptureViews[4]);
lightCb.cubeView[5] = glm::transpose(m_DepthCaptureViews[5]);
lightCb.cubeProj = glm::transpose(lightProjMat);
///// Геометрический шейдер /////
struct GS_OUTPUT
{
float4 Pos : SV_POSITION;
uint RTIndex : SV_RenderTargetArrayIndex;
};
[maxvertexcount(18)]
void GSMain(triangle float4 InPos[3] : SV_Position, inout TriangleStream<GS_OUTPUT> OutStream)
{
for (int iFace = 0; iFace < 6; iFace++)
{
GS_OUTPUT output;
output.RTIndex = iFace;
for (int v = 0; v < 3; v++)
{
output.Pos = mul(InPos[v], CubeView[iFace]);
output.Pos = mul(output.Pos, CubeProj);
OutStream.Append(output);
}
OutStream.RestartStrip();
}
}
///// Пиксельный шейдер, который берет из Depth Cubemap значения глубины и рассчитывает тени /////
float SpotShadowPCF(float3 ToPixel)
{
float3 ToPixelAbs = abs(ToPixel);
float Z = max(ToPixelAbs.x, max(ToPixelAbs.y, ToPixelAbs.z));
float Depth = (lightPerspectiveValues.x * Z + lightPerspectiveValues.y) / Z;
return depthMap.SampleCmpLevelZero(comparisonSampler, ToPixel, Depth);
}
float3 CalcLight(float3 position, float4 lightSpacePosition, float3 normal, float4 diffuseColor)
{
float3 ToLight = normalize(lightPos.xyz - position.xyz);
float NDotL = saturate(dot(ToLight, normal));
float3 finalColor = diffuseColor.rgb * NDotL;
float shadowAtt = SpotShadowPCF(position.xyz - lightPos.xyz);
finalColor *= shadowAtt;
return finalColor;
}
float4 PSMain(Input input) : SV_Target
{
float4 diffuseColor = albedoMap.Sample(textureSampler, input.uv);
if (diffuseColor.a < 0.1f)
discard;
float3 ambient = diffuseColor.xyz * 0.1;
float3 color = CalcLight(input.fragPos, input.lightViewPosition, input.normal, diffuseColor);
return float4(ambient + color, 1.f);
}