Sign in to follow this  

ShadowMaps looking bad

This topic is 3846 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

I'm trying to implement shadow using shadowmaps in direct3d using vertex,pixel shaders.While it works ,the results are quite bad..First of all when the light is near an object a lot of noise occurs around the object,the shadow starts a bit far away from the object and also i get i small shadow in 'negative' as you can see in the pics..The problems become apparent from the time i position the light where you can already see the shadows..Any suggestions?I have tried various values for the SHADOW_EPSILON(when comparing the depth from the shadow map to the current z-value)but doesn't seem to improve anything.. From light's point of view Camera's view Here's the effect file
#define SHADOW_EPSILON 0.00005f
#define SMAP_SIZE 1024
float4x4 	g_worldViewProjectionMatrix4x4,
			g_worldLightViewLightProjectionMatrix4x4;
			
texture g_shadowMapTexture;

sampler g_shadowMapSampler =
sampler_state
{
	texture = <g_shadowMapTexture>;
	
};
texture g_modelTexture;
sampler g_modelTextureSampler =
sampler_state
{
	texture = <g_modelTexture>;
	MinFilter = Linear;
	MagFilter = Linear;
};
void LightDepthVertexShader(	in float4 iPos : POSITION,in float4 iColor : COLOR,in float2 iTex[3] : TEXCOORD,out float4 oPos : POSITION,out float2 oTex[3] : TEXCOORD,out float2 oDepth : TEXCOORD3)
{
	oPos = mul(iPos, g_worldLightViewLightProjectionMatrix4x4);
	oDepth = oPos.zw;
	oTex = iTex;
}



void LightDepthPixelShader(in float2 iDepth : TEXCOORD3,out float4 oColor : COLOR)
{
	
	
	oColor = iDepth.x/iDepth.y;
	
}

void RenderSceneVertexShader(in float4 iPos : POSITION,in float4 iColor : COLOR,
in float2 iTex[3] : TEXCOORD,out float4 oPos : POSITION,out float4 oColor : COLOR,out float4 oPosLightProj : TEXCOORD3,out float2 oTex[3] : TEXCOORD)
{
	oPos=mul(iPos, g_worldViewProjectionMatrix4x4);
	
	oColor = iColor;
	oTex = iTex;
	oPosLightProj = mul(iPos,g_worldLightViewLightProjectionMatrix4x4);
	oPosLightProj.xy/=oPosLightProj.w;
	oPosLightProj.z/=oPosLightProj.w;
	oPosLightProj.w /=oPosLightProj.w;
	
}

void RenderScenePixelShader(in float4 iColora : COLOR,in float4 oPosLightProj : TEXCOORD3,in float2 oTex[3] : TEXCOORD,out vector oColor : COLOR)
{
oColor = iColora*tex2D(g_modelTextureSampler, oTex[0])*2;
float2 shadowMapLookUpCoords = 0.5 * oPosLightProj.xy + 0.5;
shadowMapLookUpCoords.y = 1 - shadowMapLookUpCoords.y;

float zvalue = 	tex2D(g_shadowMapSampler,shadowMapLookUpCoords );

float currentz = oPosLightProj.z;

if(shadowMapLookUpCoords.x <0 ||shadowMapLookUpCoords.x>1||shadowMapLookUpCoords.y<0||shadowMapLookUpCoords.y<0){
oColor =oColor*0.3;
}
else if((zvalue+SHADOW_EPSILON) <(currentz)){
oColor =oColor*0.7;
}

}


technique ShadowMap {

	pass P0 {
	
		VertexShader = compile vs_2_0 LightDepthVertexShader();
		PixelShader = compile ps_2_0 LightDepthPixelShader();
	}
	pass P1 {
		VertexShader = compile vs_2_0 RenderSceneVertexShader();
		PixelShader = compile ps_2_0 RenderScenePixelShader();
	}

}

Share this post


Link to post
Share on other sites
Ok I've looked it over. The drawing of the shadowmap looks ok. I don't really understand your comparisment though. It's too late here to really go into detail what might be wrong (and your syntax is strange :)). I'm just going to leave you with a comparisment that works on the values you put into the shadowmap.


float4 CompareShadowMap(float4 shadowTextureUV, uniform sampler2D shadowSampler)
{
float shadow = 1;
if (shadowTextureUV.w > 0)
{
float4 projected = shadowTextureUV / shadowTextureUV.w;

// Sample depth
float depth = tex2D(shadowSampler, projected.xy).x;

// Find if in shadow
shadow = (projected.z <= (depth + shadowDepthOffset));
}

// Duplicate shadow across all components
return max(float4(shadow.xxx, 1), float4(MinimumShadowDarkness.xxx, 1));
}



ps: Check that you use the right matrices at the right time. Might also be that.

Share this post


Link to post
Share on other sites
Thanks for the reply..I was dividing the projected vertex with the w component at the vertex shader...i changed it and did it in the pixel shader before the sampling and works a lot better...Anyway thanks again

Share this post


Link to post
Share on other sites

This topic is 3846 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this