首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >延迟渲染中的点光源双抛物面VSM

延迟渲染中的点光源双抛物面VSM
EN

Stack Overflow用户
提问于 2018-05-23 15:40:10
回答 2查看 264关注 0票数 1

我一直在遵循this tutorial为延迟渲染中的点光源实现我的方差阴影贴图功能。

我用的是GLSL3.3,左手坐标系。这是我一直在做的事情:

我将场景渲染为双抛物面贴图,存储深度和深度*深度。

结果:

上图包含正面和背面的地图。点光源位于场景的中心,您可以看到它发出的黄色光最多。

然后,我设置了一个全屏着色器过程。我通过将教程代码从FX转换为GLSL来做到这一点。

作者的.fx代码:

代码语言:javascript
复制
    float4 TexturePS(float3 normalW : TEXCOORD0, float2 tex0 : TEXCOORD1, float3 pos : TEXCOORD2) : COLOR
    {
        float4 texColor = tex2D(TexS, tex0 * TexScale);

        pos = mul(float4(pos, 1.0f), LightView);

        float L = length(pos);
        float3 P0 = pos / L;

        float alpha = .5f + pos.z / LightAttenuation;

        P0.z = P0.z + 1;
        P0.x = P0.x / P0.z;
        P0.y = P0.y / P0.z;
        P0.z = L / LightAttenuation;

        P0.x = .5f * P0.x + .5f;
        P0.y = -.5f * P0.y + .5f;

        float3 P1 = pos / L;

        P1.z = 1 - P1.z;
        P1.x = P1.x / P1.z;
        P1.y = P1.y / P1.z;
        P1.z = L / LightAttenuation;

        P1.x = .5f * P1.x + .5f;
        P1.y = -.5f * P1.y + .5f;

        float depth;
        float mydepth;
        float2 moments;
        if(alpha >= 0.5f)
        {
            moments = tex2D(ShadowFrontS, P0.xy).xy;
            depth = moments.x;
            mydepth = P0.z;
        }
        else
        {
            moments = tex2D(ShadowBackS, P1.xy).xy;
            depth = moments.x;
            mydepth = P1.z;
        }

        float lit_factor = (mydepth <= moments[0]);

        float E_x2 = moments.y;
        float Ex_2 = moments.x * moments.x;
        float variance = min(max(E_x2 - Ex_2, 0.0) + SHADOW_EPSILON, 1.0);
        float m_d = (moments.x - mydepth);
        float p = variance / (variance + m_d * m_d); //Chebychev's inequality

        texColor.xyz *= max(lit_factor, p + .2f);

        return texColor;
    }

我翻译的GLSL代码:

代码语言:javascript
复制
    void main() {
        vec3 in_vertex = texture(scenePosTexture, texCoord).xyz; // get 3D vertex from 2D screen coordinate
        vec4 vert = lightViewMat * vec4(in_vertex, 1); // project vertex to point light space (view from light position, look target is -Z)

        float L = length(vert.xyz);

        float distance = length(lightPos - in_vertex);
        float denom = distance / lightRad + 1;
        float attenuation = 1.0 / (denom * denom);

        // to determine which paraboloid map to use
        float alpha = vert.z / attenuation + 0.5f;

        vec3 P0 = vert.xyz / L;

        P0.z = P0.z + 1;
        P0.x = P0.x / P0.z;
        P0.y = P0.y / P0.z;
        P0.z = L / attenuation;

        P0.x = .5f * P0.x + .5f;
        P0.y = -.5f * P0.y + .5f;

        vec3 P1 = vert.xyz / L;

        P1.z = 1 - P1.z;
        P1.x = P1.x / P1.z;
        P1.y = P1.y / P1.z;
        P1.z = L / attenuation;

        P1.x = .5f * P1.x + .5f;
        P1.y = -.5f * P1.y + .5f;

        // Variance shadow mapping
        float depth;
        float mydepth;
        vec2 moments;

        if(alpha >= 0.5f)
        {
            moments = texture(shadowMapFrontTexture, P0.xy).xy;
            depth = moments.x;
            mydepth = P0.z;
        }
        else
        {
            moments = texture(shadowMapBackTexture, P1.xy).xy;
            depth = moments.x;
            mydepth = P1.z;
        }

        // Original .fx code is: float lit_factor = (mydepth <= moments[0]);
        // I'm not sure my translated code belew is correct

        float lit_factor = 0;
        if (mydepth <= moments.x)
            lit_factor = mydepth;
        else
            lit_factor = moments.x;

        float E_x2 = moments.y;
        float Ex_2 = moments.x * moments.x;
        float variance = min(max(E_x2 - Ex_2, 0.0) + SHADOW_EPSILON, 1.0);
        float m_d = (moments.x - mydepth);
        float p = variance / (variance + m_d * m_d); //Chebychev's inequality

        fragColor = texture(sceneTexture, texCoord).rgb; // sample original color
        fragColor.rgb *= max(lit_factor, p + .2f);
    }

渲染结果

现在我不知道该在哪里触摸才能正确地渲染阴影。有人能帮我指出一下吗?

EN

回答 2

Stack Overflow用户

发布于 2018-05-24 08:25:45

我的一些朋友指出,Y分量是反转的,这就是为什么阴影看起来像颠倒的原因。在P0和P1的Y上加上减号后,它开始显示出相当合理的阴影:

但另一个问题是阴影的位置是错误的。

票数 1
EN

Stack Overflow用户

发布于 2018-05-23 19:20:15

为什么要重复抛物面投影计算?

你先计算'vert',然后计算'P0‘和'P1',难道不应该只计算'P0’和'P1‘吗?(原始代码在“pos”上不做这件事)。

编辑:

您的lit_factor错误,它应该是0.0或1.0。

您可以这样使用step() GLSL内部函数:

float lit_factor = step(mydepth, moments[0]);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50482538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档