shadow artifacts

Started by
4 comments, last by michaelmk86 11 years, 10 months ago
hi guys,
does any one can provide any clues on why i get horizontal lines on my shadowed scene?

22120506.png

and the shadow map, as seen by the light
19673483.png

and the HLSL file:

float4x4 xWorldViewProjection;
float4x4 xLightsWorldViewProjection;
float4x4 xWorld;
float3 xLightPos;
float xLightPower;
float xAmbient;
float4 xColor = float4(1, 0.8, 0.8, 0);
float xMaxDepth;

Texture xTexture;

sampler TextureSampler = sampler_state { texture = <xTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = Wrap; AddressV = Wrap;};

Texture xShadowMap;

sampler ShadowMapSampler = sampler_state { texture = <xShadowMap> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = clamp; AddressV = clamp;};

float DotProduct(float3 lightPos, float3 pos3D, float3 normal)
{
float3 lightDir = normalize(pos3D - lightPos);
return dot(-lightDir, normal);
}

struct SMapVertexToPixel
{
float4 Position : POSITION;
float4 Position2D : TEXCOORD0;
};

struct SMapPixelToFrame
{
float4 Color : COLOR0;
};


SMapVertexToPixel ShadowMapVertexShader( float4 inPos : POSITION)
{
SMapVertexToPixel Output = (SMapVertexToPixel)0;

Output.Position = mul(inPos, xLightsWorldViewProjection);
Output.Position2D = Output.Position;

return Output;
}

SMapPixelToFrame ShadowMapPixelShader(SMapVertexToPixel PSIn)
{
SMapPixelToFrame Output = (SMapPixelToFrame)0;

Output.Color = PSIn.Position2D.z/xMaxDepth; //Output.Color = PSIn.Position2D.z/PSIn.Position2D.w;

return Output;
}


technique ShadowMap
{
pass Pass0
{
VertexShader = compile vs_2_0 ShadowMapVertexShader();
PixelShader = compile ps_2_0 ShadowMapPixelShader();
}
}


struct SSceneVertexToPixel
{
float4 Position : POSITION;
float4 Pos2DAsSeenByLight : TEXCOORD0;

float2 TexCoords : TEXCOORD1;
float3 Normal : TEXCOORD2;
float4 Position3D : TEXCOORD3;

};

struct SScenePixelToFrame
{
float4 Color : COLOR0;
};


SSceneVertexToPixel ShadowedSceneVertexShader( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0, float3 inNormal : NORMAL)
{
SSceneVertexToPixel Output = (SSceneVertexToPixel)0;

Output.Position = mul(inPos, xWorldViewProjection);
Output.Pos2DAsSeenByLight = mul(inPos, xLightsWorldViewProjection);
Output.Normal = normalize(mul(inNormal, (float3x3)xWorld));
Output.Position3D = mul(inPos, xWorld);
Output.TexCoords = inTexCoords;

return Output;
}

SScenePixelToFrame ShadowedScenePixelShader(SSceneVertexToPixel PSIn)
{
SScenePixelToFrame Output = (SScenePixelToFrame)0;

float2 ProjectedTexCoords;
ProjectedTexCoords[0] = PSIn.Pos2DAsSeenByLight.x/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;
ProjectedTexCoords[1] = -PSIn.Pos2DAsSeenByLight.y/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;

float diffuseLightingFactor = 0;
if ((saturate(ProjectedTexCoords).x == ProjectedTexCoords.x) && (saturate(ProjectedTexCoords).y == ProjectedTexCoords.y))
{
float depthStoredInShadowMap = tex2D(ShadowMapSampler, ProjectedTexCoords).r;
//float realDistance = PSIn.Pos2DAsSeenByLight.z/PSIn.Pos2DAsSeenByLight.w;
float realDistance = PSIn.Pos2DAsSeenByLight.z/xMaxDepth;
if ((realDistance - 1.0f/100.0f) <= depthStoredInShadowMap)
{
diffuseLightingFactor = DotProduct(xLightPos, PSIn.Position3D, PSIn.Normal);
diffuseLightingFactor = saturate(diffuseLightingFactor);
diffuseLightingFactor *= xLightPower;
}
}

float4 baseColor = tex2D(TextureSampler, PSIn.TexCoords);
Output.Color = baseColor*(diffuseLightingFactor + xAmbient)*xColor;

return Output;
}


technique Lighting
{
pass Pass0
{
VertexShader = compile vs_2_0 ShadowedSceneVertexShader();
PixelShader = compile ps_2_0 ShadowedScenePixelShader();
}
}
Advertisement
That looks like a typical case of shadow map acne - where the resolution of your shadow map isn't sufficiently high for your scene. If you change the resolution of the map, does it get better with higher res?
What is the format and bit depth of the shadow texture?
The gradient in the dark area near the light is easily visible in the image, and that is causing your artifacts. A large gap between shades of grey.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


What is the format and bit depth of the shadow texture?
The gradient in the dark area near the light is easily visible in the image, and that is causing your artifacts. A large gap between shades of grey.


L. Spiro

Thanks,
changing the format from D3DFMT_R5G6B5 to D3DFMT_X8R8G8B8 completely fix my problem.smile.png

I just have one more question, I require the light source to be very far away (to look like it come from the sky), but the problem is that objects that are far away get very pixelate(and not very accurate) shadows. the first thing that came to my mind is to blur the shadow(to look like soft shadows?) not sure what approach need to follow in order to get good quality shadows with the light source being far away.
Neither of those formats have enough precision for a shadow map. At the very least you want to use something like D3DFMT_R16F, although D3DFMT_R32F would be even better. Ideally you don't want to use a render target texture at all and just sample the depth buffer, but doing that requires "driver hacks" so you can worry about that later.

For filtering shadows you can look into Percentage Closer Filtering, often abbreviated as PCF. There are also more advanced techniques like Variance Shadow Maps which open up new possibilities for filtering.

Either way, at a certain point no amount of filtering is going to make up for really low resolution shadows and all you will do is blur the shadow detail out of existence. You appear to be using a spot light with a perspective projection, which means at a certain distance you will have inadequate resolution to capture the details of your scene. Usually this isn't a problem, since typically your spotlights have a range attenuation which means they only have a limited area of effect to begin with. Directional lights are different since they are global light sources, and techniques like cascaded shadow maps are typically used to get adequate resolution across the entire viewable range.

Neither of those formats have enough precision for a shadow map. At the very least you want to use something like D3DFMT_R16F, although D3DFMT_R32F would be even better. Ideally you don't want to use a render target texture at all and just sample the depth buffer, but doing that requires "driver hacks" so you can worry about that later.

For filtering shadows you can look into Percentage Closer Filtering, often abbreviated as PCF. There are also more advanced techniques like Variance Shadow Maps which open up new possibilities for filtering.

Either way, at a certain point no amount of filtering is going to make up for really low resolution shadows and all you will do is blur the shadow detail out of existence. You appear to be using a spot light with a perspective projection, which means at a certain distance you will have inadequate resolution to capture the details of your scene. Usually this isn't a problem, since typically your spotlights have a range attenuation which means they only have a limited area of effect to begin with. Directional lights are different since they are global light sources, and techniques like cascaded shadow maps are typically used to get adequate resolution across the entire viewable range.


As far Percentage Closer Filtering I try the Brute-Force Implementation as seen in (11.3 http://http.develope...ugems_ch11.html) but i didn't manege to get it right.


SScenePixelToFrame ShadowedScenePixelShader(SSceneVertexToPixel PSIn)
{
SScenePixelToFrame Output = (SScenePixelToFrame)0;

float2 ProjectedTexCoords;
ProjectedTexCoords[0] = PSIn.Pos2DAsSeenByLight.x/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;
ProjectedTexCoords[1] = -PSIn.Pos2DAsSeenByLight.y/PSIn.Pos2DAsSeenByLight.w/2.0f +0.5f;

float shadowCoeff = 0;
float diffuseLightingFactor = 0;
if ((saturate(ProjectedTexCoords).x == ProjectedTexCoords.x) && (saturate(ProjectedTexCoords).y == ProjectedTexCoords.y))
{
float depthStoredInShadowMap = tex2D(ShadowMapSampler, ProjectedTexCoords).r;
float realDistance = PSIn.Pos2DAsSeenByLight.z/xMaxDepth;
if ((realDistance - 1.0f/100.0f) <= depthStoredInShadowMap)
{
diffuseLightingFactor = DotProduct(xLightPos, PSIn.Position3D, PSIn.Normal);
diffuseLightingFactor = saturate(diffuseLightingFactor);
diffuseLightingFactor *= xLightPower;
}
}else{
float sum = 0;
float x, y;
for (y = -1.5; y <= 1.5; y += 1.0)
for (x = -1.5; x <= 1.5; x += 1.0)
sum += offset_lookup(ShadowMapSampler, PSIn.Position3D, float2(x, y));
shadowCoeff = sum / 16.0;
}

float4 baseColor = tex2D(TextureSampler, PSIn.TexCoords);
Output.Color = baseColor*(diffuseLightingFactor + shadowCoeff + xAmbient)*xColor;

return Output;
}


will be nice if you point out some hints on how to use "offset lookup" method.

This topic is closed to new replies.

Advertisement