微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

屏幕空间反射奇怪的结果

如何解决屏幕空间反射奇怪的结果

我正在尝试使用光线行进进行屏幕空间反射,但出现了奇怪的反射。 你能告诉我如何调试它或审查它吗? 如果您也能发现任何问题,我们将不胜感激。

enter image description here

float4 position_from_texture(float2 uv)
{

    return (position_tex.Sample(position_sampler,uv).xyzw);
}

bool Raymarch(float3 originVS,float3 dirVS,inout int iterations,out float3 hitPointSS,out int hitLayer)
{

    float resolution = 1.0;
    float max_distance = 3;
    int   steps = 20;
    float farZ = 1000;
    float thickness = 1.5;

    originVS = originVS + dirVS * resolution;

    float sceneZMin = farZ;
    hitLayer = -1;
    float deltaT = max_distance / steps;

    float t = 0.0f;
    float3 raySS = float3(-1,-1,farZ);

    bool outOfBounds = false;

    for (int i = 0; i < steps; i++)
    {
        iterations++;
        t += deltaT;
        float3 rayVS = originVS + dirVS * t;

        // convert to homogenous clip space

        float4 rayHS = mul(proj,float4(rayVS,1));
        raySS.xy = rayHS.xy / rayHS.w;
        raySS.z = rayHS.w;

        
            sceneZMin = depth_tex.SampleLevel(depth_sampler,raySS.xy,0).x;
            //outOfBounds = (sceneZMin == 0.0f);

            if(sceneZMin < 0.999989)
            {
            
            float sceneZMax = sceneZMin + thickness;

            if (((raySS.z >= sceneZMin) && (raySS.z <= sceneZMax)))
            {
                hitLayer = 1;
            }
            if (hitLayer >= 0)
                break;
            }
    }
    hitPointSS = float3(raySS.xy,sceneZMin);

    
    return (raySS.z < sceneZMin) || (sceneZMin + thickness < raySS.z);

}
PS_Output main(PS_Input input)
{
    PS_Output output;
    float thickness = 2.5;

    float2 tex_size;
    color_tex.GetDimensions(tex_size.x,tex_size.y);
    float2 tex_coord = float2(input.position.xy) / float2(tex_size);

    float3 originVS = position_from_texture(tex_coord).xyz;
    float4 originVS_xyzw = position_from_texture(tex_coord).xyzw;

    float3 normalVS = normal_tex.Sample(normal_sampler,tex_coord).xyz;

    float3 viewRayVS = normalize(originVS.xyz);

    float3 directionVs = reflect(viewRayVS,normalVS);

    float3 hitPointSS = float3(-1.0f,-1.0f,0.0f);
    int hitLayer = -1.0f;
    bool missed = true;
    int iterations = 0.0f;

    if (directionVs.z > 0.0)
    {

        missed = Raymarch(originVS,directionVs,iterations,hitPointSS,hitLayer);

    }
    float alphaBlend = 0.0f;

    if (missed)
    {
        hitPointSS.xy = float2(-1.0f,-1.0f);
    }
    else
    {
    
        float depth = depth_tex.SampleLevel(depth_sampler,hitPointSS.xy,0).x;
        if ( depth < 0.999 )
        {       
        alphaBlend = depth
                     * (hitPointSS.x < 0 || hitPointSS.x > 1 ? 0 : 1)
                     * (hitPointSS.y < 0 || hitPointSS.y > 1 ? 0 : 1);
        }
    }
    
    float4 uv = float4(0.0);
    
    float visibility = clamp(alphaBlend,0.0,1.0);
    uv.ba      = float2(visibility);
    uv.xy = hitPointSS.xy;

    output.color = uv;

    return output;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。