1 | ///////depth map
|
---|
2 | struct VS_OUT
|
---|
3 | {
|
---|
4 | float4 hPosition : POSITION;
|
---|
5 | float4 Position : TEXCOORD0;
|
---|
6 | };
|
---|
7 |
|
---|
8 | VS_OUT hDepthVS(float4 position : POSITION,
|
---|
9 | uniform float4x4 worldView,
|
---|
10 | uniform float4x4 worldViewProj)
|
---|
11 | {
|
---|
12 |
|
---|
13 | VS_OUT OUT;
|
---|
14 | OUT.hPosition = mul(worldViewProj, position);
|
---|
15 | OUT.Position = mul(worldView, position);
|
---|
16 | OUT.Position.z *= -1.0;
|
---|
17 | return OUT;
|
---|
18 | }
|
---|
19 |
|
---|
20 |
|
---|
21 |
|
---|
22 | float4 DepthPS(VS_OUT IN ):COLOR
|
---|
23 | {
|
---|
24 | float dist = length(IN.Position.xyz);
|
---|
25 | return float4(dist, dist, dist, 1);
|
---|
26 | }
|
---|
27 |
|
---|
28 | ///////////////Shadow
|
---|
29 |
|
---|
30 | struct VS_OUT_SHADOW
|
---|
31 | {
|
---|
32 | float4 hPosition : POSITION;
|
---|
33 | float4 Position : TEXCOORD0;
|
---|
34 | };
|
---|
35 |
|
---|
36 | VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
|
---|
37 | uniform float4x4 world,
|
---|
38 | uniform float4x4 worldViewProj)
|
---|
39 | {
|
---|
40 | VS_OUT_SHADOW OUT;
|
---|
41 | OUT.hPosition = mul(worldViewProj, position);
|
---|
42 | OUT.Position = mul(world, position);
|
---|
43 | return OUT;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
48 | uniform float4x4 lightView,
|
---|
49 | uniform float4x4 lightViewProj,
|
---|
50 | uniform sampler2D depthShadowMap ):COLOR
|
---|
51 | {
|
---|
52 | float bias = 0.0;
|
---|
53 | float4 shadowColor = float4(0.4, 0.4, 0.4, 1.0);
|
---|
54 |
|
---|
55 | float4 light = 1;
|
---|
56 |
|
---|
57 | float4 lightCamPos = mul(lightView, IN.Position);
|
---|
58 | float d = length(lightCamPos.xyz);
|
---|
59 |
|
---|
60 | float4 lightScreenPos = mul(lightViewProj, IN.Position);
|
---|
61 | lightScreenPos = lightScreenPos / lightScreenPos.w ;
|
---|
62 | float dist = length(lightScreenPos.xy);
|
---|
63 |
|
---|
64 | if(dist < 1.0 && lightCamPos.z < 0)
|
---|
65 | {
|
---|
66 | lightScreenPos = ( lightScreenPos + 1.0 ) / 2.0;
|
---|
67 | lightScreenPos.y = 1.0 - lightScreenPos.y;
|
---|
68 | float4 storedDepth = tex2D(depthShadowMap, lightScreenPos.xy);
|
---|
69 |
|
---|
70 |
|
---|
71 | if(storedDepth.r + bias < d)
|
---|
72 | light = shadowColor;
|
---|
73 | if(storedDepth.r == 0)
|
---|
74 | light = 1;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | light = 1;
|
---|
78 |
|
---|
79 | return light;
|
---|
80 | }
|
---|
81 |
|
---|