Graphics 屏幕空间阴影着色器代码存在问题

Graphics 屏幕空间阴影着色器代码存在问题,graphics,3d,glsl,Graphics,3d,Glsl,我有这个GLSL函数,它应该对深度缓冲区进行光线扫描,并检测屏幕空间阴影的遮挡。在三次检查输入数据(如worldPos、light、view matrix、depth buffer等)是否正确后,我不断返回1.0,这意味着它从未找到封堵器。可能出了什么问题?我是否在深度值/比较方面做错了什么 float ScreenSpaceShadow(vec3 worldPos, DirectionalLight light) { vec4 rayPos = ubo.view * vec4(worl

我有这个GLSL函数,它应该对深度缓冲区进行光线扫描,并检测屏幕空间阴影的遮挡。在三次检查输入数据(如worldPos、light、view matrix、depth buffer等)是否正确后,我不断返回1.0,这意味着它从未找到封堵器。可能出了什么问题?我是否在深度值/比较方面做错了什么

float ScreenSpaceShadow(vec3 worldPos, DirectionalLight light) {
    vec4 rayPos = ubo.view * vec4(worldPos, 1.0);
    vec4 rayDir = ubo.view * vec4(-light.direction.xyz, 0.0);

    vec3 rayStep = rayDir.xyz * 0.05 / 16.0;
    float occlusion = 0.0f;

    for(int i = 0; i < 16; i++) {
        rayPos.xyz += rayStep;

        // get the uv coordinates
        vec4 v = ubo.projection * rayPos; // project
        vec2 rayUV = v.xy / v.w; // perspective divide
        rayUV = rayUV * vec2(0.5) + 0.5; // -1, 1 to 0, 1

        if(rayUV.x < 1.0 && rayUV.x > 0.0 && rayUV.y < 1.0 && rayUV.y > 0.0) {
            // sample current ray point depth and convert it bback  to -1 , 1
            float ndc = texture(gDepth, rayUV).r * 2.0 - 1.0;
            // get linear scene depth 
            float near = 0.1;
            float far = 10000.0;
            float linearDepth = (2.0 * near * far) / (far + near - ndc * (far - near));

            // delta between scene and ray depth
            float delta = rayPos.z - linearDepth;

            // if the scene depth is larger than the ray depth, we found an occluder
            if((linearDepth > rayPos.z) && (abs(delta) < .05)) {
                occlusion = 1.0;
                break;
              }
        }
    }

    return 1.0 - occlusion;
}
float-ScreenSpaceShadow(vec3-worldPos,方向光){
vec4 rayPos=ubo.view*vec4(worldPos,1.0);
vec4 rayDir=ubo.view*vec4(-light.direction.xyz,0.0);
vec3 rayStep=rayDir.xyz*0.05/16.0;
浮动闭塞=0.0f;
对于(int i=0;i<16;i++){
rayPos.xyz+=光线步长;
//获取uv坐标
vec4 v=ubo.projection*rayPos;//项目
vec2 rayUV=v.xy/v.w;//透视分割
rayUV=rayUV*vec2(0.5)+0.5;//-1,1到0,1
if(rayUV.x<1.0&&rayUV.x>0.0&&rayUV.y<1.0&&rayUV.y>0.0){
//对当前光线点深度进行采样,并将其B向后转换为-1,1
浮动ndc=纹理(gDepth,rayUV).r*2.0-1.0;
//获取线性场景深度
浮动近=0.1;
浮动远=10000.0;
浮动线深度=(2.0*近*远)/(远+近-ndc*(远-近));
//场景和光线深度之间的增量
浮动增量=光线位置z-直线深度;
//如果场景深度大于光线深度,我们会发现一个遮罩
如果((线性深度>光线位置z)和&(绝对值(增量)<.05)){
闭塞=1.0;
打破
}
}
}
返回1.0-遮挡;
}