RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-652731

Aaron JJ's questions

Martin Hope
Aaron JJ
Asked: 2025-01-05 23:48:25 +0000 UTC

为什么无法在 DirectX11 上实现全向阴影贴图?

  • 6

看起来一切都配置正确: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);
}
разработка-игр
  • 1 个回答
  • 51 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5