SpotLight shadow projection

Started by
11 comments, last by Tasaq 11 years, 8 months ago
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 smile.png
But also I want to know why does it extrude projected texture towards light camera and how to reapir it smile.png
Thank You in advance smile.png
Advertisement
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?
Perception is when one imagination clashes with another

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?

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
It's definitely interesting, and shadowing issues are notoriously difficult to debug sad.png
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: (http://mynameismjp.wordpress.com/samples-tutorials-tools/pix-with-xna-tutorial/)
Perception is when one imagination clashes with another

Your light looks to be over the hood of the car, but it's shading areas behind the light

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).

As far as PIX goes, MJP has a really good tutorial for it: (http://mynameismjp.w...h-xna-tutorial/)

Thanks! I was thinking about asking for tutorial, but you were faster :) And I like MJP tutorials, they are very clear :)
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.
Perception is when one imagination clashes with another
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 smile.png ).
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?
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!
Perception is when one imagination clashes with another

To the PIX!

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.
Ah that's the problem, that's not what Clamp does.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.textureaddressmode.aspx

Essentially if you read at address 1.25, -.25, your address will be changed to 1.0f, 0.0f.
Perception is when one imagination clashes with another

This topic is closed to new replies.

Advertisement