1 | ///////depth map
|
---|
2 | struct VS_OUT
|
---|
3 | {
|
---|
4 | float4 hPosition : POSITION;
|
---|
5 | float4 Position : TEXCOORD0;
|
---|
6 | };
|
---|
7 |
|
---|
8 | VS_OUT DepthVS(float4 position : POSITION,
|
---|
9 | uniform float4x4 worldViewProj,
|
---|
10 | uniform float4x4 viewProj,
|
---|
11 | uniform float4x4 cameraMatrix)
|
---|
12 | {
|
---|
13 |
|
---|
14 | VS_OUT OUT;
|
---|
15 |
|
---|
16 | OUT.hPosition = mul(worldViewProj, position);
|
---|
17 | OUT.Position = mul(viewProj, position);
|
---|
18 |
|
---|
19 | return OUT;
|
---|
20 | }
|
---|
21 |
|
---|
22 | //for directional light
|
---|
23 | float4 DepthPS(VS_OUT IN ):COLOR
|
---|
24 | {
|
---|
25 | float4 pos = (IN.Position / IN.Position.w);
|
---|
26 | pos = (pos +1.0 ) / 2.0;
|
---|
27 | return pos.z;
|
---|
28 | }
|
---|
29 |
|
---|
30 | ///////////////Shadow
|
---|
31 |
|
---|
32 | struct VS_OUT_SHADOW
|
---|
33 | {
|
---|
34 | float4 hPosition : POSITION;
|
---|
35 | float4 Position : TEXCOORD0;
|
---|
36 | float4 lPosition : TEXCOORD1;
|
---|
37 | };
|
---|
38 |
|
---|
39 | VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
|
---|
40 | uniform float4x4 worldViewProj,
|
---|
41 | uniform float4x4 lightViewProj)
|
---|
42 | {
|
---|
43 | VS_OUT_SHADOW OUT;
|
---|
44 | OUT.hPosition = mul(worldViewProj, position);
|
---|
45 | OUT.lPosition = mul(lightViewProj, position);
|
---|
46 | OUT.Position = position;
|
---|
47 | return OUT;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
52 | uniform float4x4 lightViewProj,
|
---|
53 | uniform sampler2D depthShadowMap : register(s0)
|
---|
54 | ):COLOR
|
---|
55 | {
|
---|
56 | float bias = 0.01;
|
---|
57 | float4 light = float4(1,1,1,1);
|
---|
58 | float4 shadow = float4(0.3,0.3,0.3,1);
|
---|
59 |
|
---|
60 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
61 | pos = (pos +1.0 ) / 2.0;
|
---|
62 | pos.y = 1.0 - pos.y;
|
---|
63 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
64 | if(storedDepth.r + bias < pos.z)
|
---|
65 | light = shadow;
|
---|
66 | //return abs( pos.z - storedDepth.z );
|
---|
67 | return light;
|
---|
68 | }
|
---|
69 |
|
---|