0

I've been trying to learn screen-space techniques, specifically Ray-marching ones but I have been struggling to get a single working example to continue learning from and solidify my knowledge. I'm implementing Screen-space shadows following this article but my result just seems to be a white image and I cannot seem to understand why. The code makes sense to me but the result does not seem to be right. I can't seem to understand where I might have gone wrong while attempting this screen-space ray-marching technique and would appreciate any insight to that will help me continue learning.

Using Vulkan + GLSL

Full shader: screen_space_shadows.glsl


// calculate screen space shadows
float computeScreenSpaceShadow()
{
    vec3 FragPos = texture(gPosition, uvCoords).rgb;
    vec4 ViewSpaceLightPosition = camera.view * light.LightPosition;
    vec3 LightDirection = ViewSpaceLightPosition.xyz - FragPos.xyz;
        
    // Ray position and direction in view-space.
    vec3 RayPos = texture(gPosition, uvCoords).xyz; // ray start position
    vec3 RayDirection = normalize(-LightDirection.xyz);
    
    // Save original depth of the position
    float DepthOriginal = RayPos.z;
    // Ray step
    vec3 RayStep = RayDirection * STEP_LENGTH;
    
    float occlusion = 0.0;

    for(uint i = 0; i < MAX_STEPS; i++)
    {
        RayPos += RayStep;
        vec2 Ray_UV = ViewToScreen(RayPos);


        // Make sure the UV is inside screen-space
        if(!ValidRay(Ray_UV)){
            return 1.0;
        }
        
        // Compute difference between ray and cameras depth
        float DepthZ = linearize_depth(texture(depthMap, Ray_UV).x);
        float DepthDelta = RayPos.z - DepthZ;

        // Check if camera cannot see the ray. Ray depth must be larger than camera depth = positive delta
        bool canCameraSeeRay = (DepthDelta > 0.0) && (DepthDelta < THICKNESS);
        bool occludedByOriginalPixel = abs(RayPos.z - DepthOriginal) < MAX_DELTA_FROM_ORIGINAL_DEPTH;
        if(canCameraSeeRay && occludedByOriginalPixel)
        {
            // Mark as occluded 
            occlusion = 1.0;

            break;
        }
    } 
    return 1.0 - occlusion;
}

Output

enter image description here

Krellex
  • 613
  • 2
  • 7
  • 20
  • What have you tried to debug your shader? Try making it output something other than what's meant to be the final colour, for example your ray direction. Then you can check the direction make sense. You can repeat this approach for various variables and see if any of them don't match expectations. – Andrea Sep 07 '22 at 21:02

0 Answers0