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 | {
|
---|
11 |
|
---|
12 | VS_OUT OUT;
|
---|
13 |
|
---|
14 | OUT.hPosition = mul(worldViewProj, position);
|
---|
15 | OUT.Position = mul(worldViewProj, position);
|
---|
16 |
|
---|
17 | return OUT;
|
---|
18 | }
|
---|
19 |
|
---|
20 | //for directional light
|
---|
21 | float4 DepthPS(VS_OUT IN ):COLOR
|
---|
22 | {
|
---|
23 | //return 1;
|
---|
24 | float4 pos = (IN.Position / IN.Position.w);
|
---|
25 | //pos = (pos +1.0 ) / 2.0;
|
---|
26 | return float4(pos.z, pos.z * pos.z, 1, 1);
|
---|
27 | }
|
---|
28 |
|
---|
29 | ///////////////Shadow
|
---|
30 |
|
---|
31 | struct VS_OUT_SHADOW
|
---|
32 | {
|
---|
33 | float4 hPosition : POSITION;
|
---|
34 | float4 Position : TEXCOORD0;
|
---|
35 | float4 lPosition : TEXCOORD1;
|
---|
36 | };
|
---|
37 |
|
---|
38 | VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
|
---|
39 | uniform float4x4 worldViewProj,
|
---|
40 | uniform float4x4 world,
|
---|
41 | uniform float4x4 lightViewProj)
|
---|
42 | {
|
---|
43 | VS_OUT_SHADOW OUT;
|
---|
44 | OUT.hPosition = mul(worldViewProj, position);
|
---|
45 | float4 wPos = mul(world, position);
|
---|
46 | OUT.lPosition = mul(lightViewProj, wPos);
|
---|
47 | OUT.Position = position;
|
---|
48 | return OUT;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /*
|
---|
52 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
53 | uniform float4x4 lightViewProj,
|
---|
54 | uniform sampler2D depthShadowMap : register(s0)
|
---|
55 | ):COLOR
|
---|
56 | {
|
---|
57 | float bias = 0.0001;
|
---|
58 | float4 light = float4(1,1,1,1);
|
---|
59 | float4 shadow = float4(0.6,0.6,0.6,1);
|
---|
60 |
|
---|
61 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
62 | pos.xy = (pos.xy +1.0 ) / 2.0;
|
---|
63 | pos.y = 1.0 - pos.y;
|
---|
64 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
65 |
|
---|
66 | if(storedDepth.r + bias < pos.z)
|
---|
67 | {
|
---|
68 | float M1 = storedDepth.r;
|
---|
69 | float M2 = storedDepth.g;
|
---|
70 | float v2 = M2 - M1 * M1;
|
---|
71 | float pmax = v2 / (v2 + pow(M1 - pos.z, 2));
|
---|
72 | light = shadow + (1 - shadow) * pmax;
|
---|
73 |
|
---|
74 | //light = shadow;
|
---|
75 | }
|
---|
76 | //return abs( pos.z - storedDepth.z );
|
---|
77 | return light;
|
---|
78 | }
|
---|
79 | */
|
---|
80 |
|
---|
81 |
|
---|
82 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
83 | uniform float4x4 lightViewProj,
|
---|
84 | uniform sampler2D depthShadowMap : register(s0)
|
---|
85 | ):COLOR
|
---|
86 | {
|
---|
87 | float bias = 0.0001;
|
---|
88 | float4 light = float4(1,1,1,1);
|
---|
89 | float4 shadow = float4(0.65,0.65,0.65,1);
|
---|
90 | //float4 shadow = float4(0,0,0,1);
|
---|
91 |
|
---|
92 | if(IN.lPosition.z > 0.0)
|
---|
93 | {
|
---|
94 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
95 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
96 |
|
---|
97 |
|
---|
98 | pos.y = 1.0 - pos.y;
|
---|
99 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
100 |
|
---|
101 | if(storedDepth.r + bias < pos.z)
|
---|
102 | {
|
---|
103 | light = shadow;
|
---|
104 | }
|
---|
105 | //if(length(pos.xy)>1)
|
---|
106 | // light = shadow;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | return light;
|
---|
111 | }
|
---|