Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Shadow Shader Gurus

Author
Message
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 4th Dec 2012 10:27
I've been implementing another shadow shader and am having some problems. This is my question. When you generate the depth map you use code like the following ...

Vertex Shader
float4 WPosP = mul(IN.Pos,World);
float4 Proj = mul(WPosP,LightProjMatrix);
OUT.OPos = Proj;
OUT.Depth = Proj.z;

Pixel Shader
return float4(smoothstep(0,75,IN.Depth),0,0,1)

Now you have your lovely depth image.

Then when you perform the lookup in the final render, you use something like the following:

Vertex Shader
OUT.wpos = mul(IN.pos,World);
float4 lPos = mul(OUT.wpos,LightProjMatrix);
OUT.lpos = mul(ViewMatrix,lPos);

Pixel Shader
float3 depthTex = tex2Dproj(texDepthmap,IN.lpos);
float depth = smoothstep(0,75,IN.lpos.z);
if (depth < depthTex.r){
}

The bit I've underlined (the view matrix multiplication) is the bit I don't get. In the shaders I've worked from (Evolved's examples) the ViewMatrix is hardcoded as the following:
matrix ViewMat={0.5,0,0,0.5,0,-0.5,0,0.5,0,0,0.5,0.5,0,0,0,1};

I want to know what this is and what it does. In my head, I can't see why that bit needs to be there. If the world multiplication and LightViewProj multiplicates are the same in the depth render and the final render, then to me that suggests that's all you need to do. My only guess is that additional ViewMatrix multiplication is something to do with the tex2DProj which happens later.

Futhermore, I've tried passing in the lights view matrix and the cameras view matrix instead of using the hard coded one, and neither of these generate the correct result, so it's obviously something different.

The reason why I'm asking is the shadow shading doesn't work when I start playing around with my light camera FOV and view range. I want a really tight FOV and narrow view range to achieve a directional light effect and a high precision depth map, but the tex2DProj lookup is wrong once I start playing with these values. Since I am passing in the LightViewProj matrix correctly for the light camera, I can only surmize it's something to do with this hardcoded ViewMatrix.

Any help appreciated!

david w
20
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 4th Dec 2012 22:50
You need to do an orthograpic project and I think that is what is going on with hard coded view matrix. You can get rid of that if you make an orthograpic view matrix and then pass it into your shader. It should work.

I'm assuming that your shadow texture is a square and not a rectangle? This will make a difference when want to compare values, of course you can use a standard view and projection martix, but I think the results will be funny and your shadows will be stretched/elongated unaturally.
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 4th Dec 2012 23:27
Thanks David. Interestingly I got it working by getting the right combination of smoothstep ranges and camera ranges. I can't remember the exact relationship but it took me a while to figure it out. Thanks for giving me a bit of insight into the hard coded viewmatrix though.

Login to post a reply

Server time is: 2026-07-07 05:39:11
Your offset time is: 2026-07-07 05:39:11