Sign in to follow this  
Tasaq

SpotLight shadow projection

Recommended Posts

Hello,
I know, it's me again but I run into another strange problem and I am stuck in one place :\
I want to project a shadow map but renderTarget seems unable to be clamped (i set it from both xna and hlsl to pointClamp).
Even after manually clamping it in the shader It got extruded towards light position.
Picture will better explain the situation so I attach one (green line is where the projected texture should be cut off, and the ball in front of the car is lookAt point, and bellow normal buffer you see light view).
Here's HLSL code:
[source lang="cpp"]float3 DepthToPosition(float2 TexCoord)
{
float4 position;
float d = tex2D(depth, TexCoord.xy);
position.x = TexCoord.x * 2.0f - 1.0f;
position.y = -(TexCoord.y * 2.0f - 1.0f);
position.z = d;
position.w = 1.0f;
position = mul(position, InvertViewProjection);
position /= position.w;
return position;
}
float4 SpotShadow(VertexShaderOutput input) : COLOR0
{
float3 Position = DepthToPosition(input.TexCoord);
float4 texcoord = mul(float4(Position, 1.0), LightWorldViewProjection);
if(texcoord.z < 0)
return float4(1,0,0,1);
texcoord.x = ((texcoord.x / texcoord.w) * 0.5 ) + 0.5;
texcoord.y = ((texcoord.y / texcoord.w) * -0.5 ) + 0.5;
if(texcoord.x < 0 || texcoord.x > 1 || texcoord.y < 0 || texcoord.x > 1)
return float4(1,0,0,1);
float LightDistance = (tex2D(shadow, texcoord.xy));
return LightDistance;
}[/source]
I think I don't have to say i want to get rid of this if that is checking coords [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
But also I want to know why does it extrude projected texture towards light camera and how to reapir it [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
Thank You in advance [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Tasaq

Share this post


Link to post
Share on other sites
That definitely does look weird, something more sinister looks to be afoot. Have you tried taking it into PIX and seeing what is actually sampling at the extruded spots?

Share this post


Link to post
Share on other sites
[quote name='Seabolt' timestamp='1344449486' post='4967466']
That definitely does look weird, something more sinister looks to be afoot. Have you tried taking it into PIX and seeing what is actually sampling at the extruded spots?
[/quote]
To be onest I have never used PIX. I will try that in a few hours.
What You see I use with spotlight which projects light shape (black and white mask), which works fine. But I want to make a CSM and I am very concerned about this:)
It's first time I get this with spotlight o.O Edited by Tasaq

Share this post


Link to post
Share on other sites
It's definitely interesting, and shadowing issues are notoriously difficult to debug [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
It sort of looks like your shadow map test is giving false positives for sections that shouldn't be lit. Your light looks to be over the hood of the car, but it's shading areas behind the light, or at least out of the view of the light, so maybe something is going on there...

As far as PIX goes, MJP has a really good tutorial for it: ([url="http://mynameismjp.wordpress.com/samples-tutorials-tools/pix-with-xna-tutorial/"]http://mynameismjp.wordpress.com/samples-tutorials-tools/pix-with-xna-tutorial/[/url])

Share this post


Link to post
Share on other sites
[quote name='Seabolt' timestamp='1344450467' post='4967475']
Your light looks to be over the hood of the car, but it's shading areas behind the light
[/quote]
I worte that the ball in front of the car is lookAt point :) The light is at the top of that triangular shape (if you want I can reupload image with dot in light position).
[quote name='Seabolt' timestamp='1344450467' post='4967475']
As far as PIX goes, MJP has a really good tutorial for it: (http://mynameismjp.w...h-xna-tutorial/)
[/quote]
Thanks! I was thinking about asking for tutorial, but you were faster :) And I like MJP tutorials, they are very clear :)

Share this post


Link to post
Share on other sites
Ah what I was more thinking about is that your depth map doesn't seem to include the area that is distorted. So maybe your depth testing isn't clipping unlit pixels, or your depth map doesn't properly generate the full field of view for the light.

Share this post


Link to post
Share on other sites
I feel ashamed to admit it but it was a typo...
In the last comparison in if statment there's x instead of y (at least i learnt basic stuff wiht PIX [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] ).
Ok to make up for that here's what happenes with clamps made like that:
[source lang="cpp"]
//hlsl sampler declaration

texture ShadowMap;
sampler shadow = sampler_state
{
Texture = (ShadowMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};

//XNA side before draw call:

device.SamplerStates[0] = SamplerState.PointClamp;
device.SamplerStates[1] = SamplerState.PointClamp;
device.SamplerStates[2] = SamplerState.PointClamp;
device.SamplerStates[3] = SamplerState.PointClamp;[/source]
Left one is with if inside shader, right one is without. Why clamp doesn't work like that? Edited by Tasaq

Share this post


Link to post
Share on other sites
Clamp will take any texture fetches that are beyond 1.0f or lower than 0.0f and sample at 1.0f or 0.0f respectively. Now it looks like areas beyond the lit range are being shaded in the right image, and if you were to set clamp on the texture and every object in the scene sampled that texture, I could see that happening. Especially if the left hand image is the only shader that is supposed to read from that texture. But I'm not entirely certain what's happening in these images.

To the PIX!

Share this post


Link to post
Share on other sites
[quote name='Seabolt' timestamp='1344527146' post='4967819']
To the PIX!
[/quote]
PIX said it's clamped. Maybe I understand clamp the wrong way...
I will describe it how I understand clamping:
When texture's sampler state is set to uv clamp, tex2D returns transparent value when either u or v is out of [0,1] range.
When it's set to wrap, when u or v is, for instance, 1.1 tex2D reads it as 0.1 and thus we can make tiling.

Share this post


Link to post
Share on other sites
Ah that's the problem, that's not what Clamp does.

[url="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.textureaddressmode.aspx"]http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.textureaddressmode.aspx[/url]

Essentially if you read at address 1.25, -.25, your address will be changed to 1.0f, 0.0f.

Share this post


Link to post
Share on other sites
[quote name='Seabolt' timestamp='1344535071' post='4967861']
Essentially if you read at address 1.25, -.25, your address will be changed to 1.0f, 0.0f.
[/quote]

Ouch, so that's why.
So that if statement must remain there? Or is there another better solution? Edited by Tasaq

Share this post


Link to post
Share on other sites
Yes, that if statement looks to cull out the pixels that are outside the light's range. Sorry I didn't look over it very well that first time.

Share this post


Link to post
Share on other sites
[quote name='Seabolt' timestamp='1344544149' post='4967903']
Yes, that if statement looks to cull out the pixels that are outside the light's range. Sorry I didn't look over it very well that first time.
[/quote]
Okay, thanks, you helped me a lot and I learned some new things :)
I already implemented pcf and everything looks fine :D

Share this post


Link to post
Share on other sites

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