Cascading Shadow Mapping - Texture Search

I am trying to implement cascading shadow rendering in my engine, but I am somewhat stuck on the last step. For testing purposes, I made sure that all of the cascades span my entire scene. The result is currently:

CSM Different cascade intensities

The different intensities of the cascades are not , in fact, a problem.

This is how I view the texture for the shadow maps inside the fragment shader:

layout(std140) uniform CSM
{
    vec4 csmFard; // far distances for each cascade
    mat4 csmVP[4]; // View-Projection Matrix
    int numCascades; // Number of cascades to use. In this example it 4.
};

uniform sampler2DArrayShadow csmTextureArray; // The 4 shadow maps

in vec4 csmPos[4]; // Vertex position in shadow MVP space

float GetShadowCoefficient()
{
    int index = numCascades -1;
    vec4 shadowCoord;
    for(int i=0;i<numCascades;i++)
    {
        if(gl_FragCoord.z < csmFard[i])
        {
            shadowCoord = csmPos[i];
            index = i;
            break;
        }
    }
    shadowCoord.w = shadowCoord.z;
    shadowCoord.z = float(index);
    shadowCoord.x = shadowCoord.x *0.5f +0.5f;
    shadowCoord.y = shadowCoord.y *0.5f +0.5f;
    return shadow2DArray(csmTextureArray,shadowCoord).x;
}

      

Then I use the return value and just multiply it by the diffuse color. This explains the different intensities of the cascades as I grab the depth value directly from the texture. I've tried doing a depth comparison instead, but with limited success:

    [...] // Same code as above
    shadowCoord.w = shadowCoord.z;
    shadowCoord.z = float(index);
    shadowCoord.x = shadowCoord.x *0.5f +0.5f;
    shadowCoord.y = shadowCoord.y *0.5f +0.5f;
    float z = shadow2DArray(csmTextureArray,shadowCoord).x;
    if(z < shadowCoord.w)
        return 0.25f;
    return 1.f;
}

      

Although this gives me the same shadow value everywhere, it only works for the first cascade, all others are empty:

CSM only works for first cascade - others are blank

(I colored the cascades because otherwise the transitions won't be visible in this case)

What am I missing here?

+3


source to share





All Articles