Jump to content
  • Advertisement
Sign in to follow this  

Shadow Map Bias issue

This topic is 2018 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Recently I managed to get shadow mapping working within my application, however, a lot of the occluders are getting occluded, that is a LOT of self-shadowing


Now in my hlsl code I DO have a bias however it is simply NOT working. What I mean is no matter how much I increase or decrease the bias, the shadows stay static.


It seems like a bias value of 1 or 0 seem to only make a difference


    float2 projectTexCoord = float2(0.0f, 0.0f);
    float  bias            = 0.006f;
    float4 textureMap      = colorMap[0].Sample(colorSampler, texel.tex);
    float  lightIntensity  = 0.0f;
    float  depthValue      = 0.0f;
    float  lightDepthValue = 0.0f;
    float4 color           = float4(0.0f, 0.0f, 0.0f, 0.0f);
    float4 lightObject     = light[5].ambient;
    float4 specular        = float4(0.0f, 0.0f, 0.0f, 0.0f);
    bool   lightHit        = false;

    //Normal Mapping
    float4 bumpMap    = colorMap[1].Sample(colorSampler, texel.tex);
    bumpMap           = (bumpMap * 2.0f) - 1.0f;
    float bumpNormal = texel.norm + bumpMap.x * texel.tang + bumpMap.y * texel.binorm;
    float lightItensity2 = saturate(dot(bumpNormal, -1.5f));

    //Specular Mapping
    float3 reflection = normalize(2 * lightItensity2 * bumpNormal - light[5].dir);
    specular   = pow(saturate(dot(reflection, texel.viewDir)), 2.5f);
    specular += float4(0.02f, 0.02f, 0.06f, 0.0f);
    float4 specTex = colorMap[3].Sample(colorSampler, texel.tex);
    specular *= specTex;

    //Grab the lightPosition at the vertex
    texel.lightPos = light[5].dir.xyz - texel.wPos.xyz;
    texel.lightPos = normalize(texel.lightPos);

    // input.lpos.xyz /= input.lpos.w;

     //Create The texture coordinates for the projecting of the shadowMap
    projectTexCoord.x =  texel.lightViewP.x / texel.lightViewP.w  /  2.0f + 0.5f;
    projectTexCoord.y =  texel.lightViewP.y / texel.lightViewP.w / -2.0f + 0.5f;

    // Determine if the projected coordinates are in the 0 to 1 range.  If so then this pixel is in the view of the light
    if((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y))

        // Sample the shadow map depth value from the depth texture using the sampler at the projected texture coordinate location.
        depthValue = colorMap[2].Sample(sampleTypeClamp, projectTexCoord).r;

        // Calculate the depth of the light.
        lightDepthValue = texel.lightViewP.z / texel.lightViewP.w;

        // Subtract the bias from the lightDepthValue.
        lightDepthValue = (lightDepthValue - bias);

        // Compare the depth of the shadow map value and the depth of the light to determine whether to shadow or to light this pixel.
        // If the light is in front of the object then light the pixel, if not then shadow this pixel since an object (occluder) is casting a shadow on it.
        if(lightDepthValue > depthValue)

             lightIntensity = saturate(dot(texel.norm, texel.lightPos));

            // Calculate the amount of light on this pixel.
            if(lightIntensity > 0.0f)

                lightObject = light[5].diffuse;
                color += float4(0.0f, 2.4f, 0.0f, 0.0f);

                lightHit    = true;

    float4 color2 = lightItensity2 * lightObject;
    color = saturate(color2 + lightObject) * textureMap;
    color = saturate(color + specular);

    //color += float4(0.5f, 0.0f, 0.0f, 1.0f);
    return color;


I just can't seem to figure this out. I tried working with the sampler, I tried to see if my depth shader was correct (it is). But I constantly get the same results of the self shadowing.


If someone could just point me in the direction UI should be looking to solve this problem I would be so grateful. Im just so lost right now.


And if anybody needs any code not shown here let me know


Any response will be appreciated


-Marcus Hansen


Share this post

Link to post
Share on other sites

Can you show an image of the issue?  If your depth bias isn't working, then it sounds like your transformation to light space is somehow not correct.  Is the shadowing working properly everywhere else?

Share this post

Link to post
Share on other sites

Thanks for the quick reply!




As you can see Im rendering the depthBuffer on a texture. If you notice I am not parsing the ground through the depth shader. The reason for that is the same as with the rest of the issues. Self-Shadowing. If i parse the ground through the shader (all the individual tiles with a mag of 2) it all gets self-shadowed like the brick hedges surrounding the level.


"Is the shadowing working properly everywhere else?"


Somewhat, the shadows do look nice, and do look like their doing their offset correctly.


I also checked the tile offset for my level generation, but it seemed to check out.


If you want I can post my light code.


Thanks for the quick reply! I needed some input before I went insane!


Marcus Hansen

Share this post

Link to post
Share on other sites

Any ideas?


Here is my code for setting up of the view and projection matrices for the light


light.pos     = XMFLOAT3(-90.0f, 60.0f, -25.0f);
light.diffuse = XMFLOAT4(0.18f, 0.18f, 0.50f, 0.4f);
light.ambient = XMFLOAT4(0.10f, 0.10f, 0.40f, 0.3f);
//View Matrix
XMFLOAT3 o = XMFLOAT3(90,10,-25.0f), up = XMFLOAT3(0,1,0);
XMVECTOR e = XMLoadFloat3(&light[5].pos), fp = XMLoadFloat3(&o), u = XMLoadFloat3(&up);
lView.lightView = XMMatrixLookAtLH( e, fp, u );
//Projection Matrix
fieldOfView = (float)3.14 / 4.0f;
screenAspect = 1024 / 768;

 // Create the projection matrix for the light.
lProj.lightProj = XMMatrixPerspectiveFovLH(fieldOfView, screenAspect, 0.1f, 1000.0f);
lView.lightView = XMMatrixTranspose(lView.lightView);
lProj.lightProj = XMMatrixTranspose(lProj.lightProj);


It seems to me like i'm missing one important step.


I setup a clamp sampler for the depthBuffer Texture


Marcus Hansen

Share this post

Link to post
Share on other sites

I realized that a less then operator was facing the wrong direction in  a condition (My Shader) and I switched it around. Now this is how my shadows look




Is this a problem with my depthTexture!?


Should I review my Creation of the DepthStencilView and Texture? Does the depthTexture look correct? (Im not rendering the ground until I reprimand the issue with the brick hedges)


I'm not looking for any solution to be handed to me, I just want input, I'm lost :p


Marcus Hansen

Share this post

Link to post
Share on other sites

I got it to work.


I had to tear apart the shader. Also I thought about it. I was creating a renderTargetView. I don't know why, maybe because rastertek did it. So I removed the RenderTargetView since I only needed depth.


And, before I had a pixelShader that only did a return. So I removed the PS and just set it as null in the pipeline.


I wonder which one of the above things was causing it to perform the severe amount of shadow ance. My guess is the shaders. Or having a renderTargetView for a pass where I only render depth.


Marcus Hansen

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!