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.001;
|
---|
58 | float4 light = float4(1,1,1,1);
|
---|
59 | float4 shadow = float4(0.85,0.85,0.85,1);
|
---|
60 | //float4 shadow = float4(0,0,0,1);
|
---|
61 |
|
---|
62 | if(IN.lPosition.z > 0.0)
|
---|
63 | {
|
---|
64 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
65 |
|
---|
66 | if(length(pos.xy)>1)
|
---|
67 | light = shadow;
|
---|
68 | else
|
---|
69 | {
|
---|
70 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
71 | pos.y = 1.0 - pos.y;
|
---|
72 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
73 |
|
---|
74 | if(storedDepth.r + bias < pos.z)
|
---|
75 | {
|
---|
76 | float M1 = storedDepth.r;
|
---|
77 | float M2 = storedDepth.g;
|
---|
78 | float v2 = M2 - M1 * M1;
|
---|
79 | float pmax = v2 / (v2 + pow(M1 - pos.z, 2));
|
---|
80 | light = saturate(shadow + (1 - shadow) * pmax);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | }
|
---|
85 | else
|
---|
86 | light = shadow;
|
---|
87 |
|
---|
88 | return light;
|
---|
89 | }
|
---|
90 | */
|
---|
91 |
|
---|
92 |
|
---|
93 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
94 | uniform float4x4 lightViewProj,
|
---|
95 | uniform sampler2D depthShadowMap : register(s0)
|
---|
96 | ):COLOR
|
---|
97 | {
|
---|
98 | float bias = 0.0001;
|
---|
99 | float4 light = float4(1,1,1,1);
|
---|
100 | float4 shadow = float4(0.95,0.95,0.95,1);
|
---|
101 | //float4 shadow = float4(0,0,0,1);
|
---|
102 |
|
---|
103 | if(IN.lPosition.z > 0.0)
|
---|
104 | {
|
---|
105 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
106 |
|
---|
107 | if(length(pos.xy)>1)
|
---|
108 | light = shadow;
|
---|
109 | else
|
---|
110 | {
|
---|
111 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
112 | pos.y = 1.0 - pos.y;
|
---|
113 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
114 |
|
---|
115 | if(storedDepth.r + bias < pos.z)
|
---|
116 | {
|
---|
117 | light = shadow;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | }
|
---|
122 | else
|
---|
123 | light = shadow;
|
---|
124 |
|
---|
125 |
|
---|
126 | return light;
|
---|
127 | }
|
---|