shadow mapping for directional lights, strange artefacts

Started by
4 comments, last by swiftcoder 15 years, 9 months ago
Hi, I have a problem with my shadow mapping for directional lights and just can't find a way to solve it. I've already implemented shadow mapping for spotlights and it works fine, but my directional light causes some very strange artefacts. But they don't seem to be usual shadow acne, but (i guess) some kind of discontinuity in the z-distribution since i can get rid of the black stripes by adding enough offset to the shadowmap. But this pushes the parts between the stripes so far away that geometry very close to the floor doesn't cast any shadow anymore. Also the stripes appear to get broader the further away they are from the lightsource. Screenshot I'm setting up the camera and projection matrices by hand but they seem to be correct because creating the matrices using DirectX functions produces the same results. You can also see the shadowmap in the upper left corner of the screenshot and it also seems to be correct. So i assume my shaders for rendering the final scene must be wrong. Some code: Shader for rendering to the shadowmap:

void VRenderToShadowmap(in VertexShaderIn In, out VertexShaderOut Out)
{
	Out.Position = mul(In.Position, WorldViewProjMatrix);
	Out.Depth = Out.Position.z + 0.001f;
}


float4 PShader(float Depth : TEXCOORD0) : COLOR
{
	return float4(Depth, Depth, Depth, 1.0f);
}


Shader that render scene (simplified):

void VShaderDirectional(in VertexShaderIn In, out VertexShaderOut1 Out)
{
	Out.Position = mul(In.Position, WorldViewProjMatrix);
	Out.ShadowMapCoords = mul(In.Position, LightWorldViewProjMatrix);

	Out.ShadowMapCoords.xy *= float2(0.5f, -0.5f);
	Out.ShadowMapCoords.xy += 0.5f;

	// transforming lightdirection to tangentspace

	Out.Tex0 = In.Tex0;
}


float4 NormalMappingPShaderDirectional(in VertexShaderOut1 In) : COLOR
{
	clip(In.ShadowMapCoords.xyz);
	clip(1.0f - In.ShadowMapCoords.xyz);

	float Depth = tex2D(Shadowmap, In.ShadowMapCoords.xy).z;
	float4 Color = (float4)0;
	
	if(In.ShadowMapCoords.z <= Depth)
	{
		// lighting...
	}
	else
	{
		clip(-1);
	}

	return Color;
}


I hope someone has encountered a similar/the same problem and can help me. Thanks in advance for your time and help.
Advertisement
Just looks like a biasing problem to me. As you already noted adding a bias should help. You can also try rendering only back-faces into the shadow map, that should help with biasing.
Thanks for the quick reply. But adding enough bias to make the stripes completely disappear will result in a shadow looking like this:

Screenshot

Geometry to close to the floor won't cast shadows anymore. Drawing only backfaces into the shadowmap will even worsen the problem since they are generally closer to the floor.

The strange thing is, even without any bias at all there is no z-fighting or shadow acne, just clear stripes on the floor.
Check your shadow-buffer depth - if you have fallen back to an 8-bit buffer, these stripes can occur.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Thank you so much. You just made my day!
I was using an 24 Bit depth-buffer, but didn't consider that I store the depth in one of the textures 8-Bit color channels...
Quote:Original post by DraganO
Thank you so much. You just made my day!
I was using an 24 Bit depth-buffer, but didn't consider that I store the depth in one of the textures 8-Bit color channels...
No problem - the same thing happened to me about a week ago :)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement