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 | VS_OUT DepthDistVS(float4 position : POSITION,
|
---|
30 | uniform float4x4 worldViewProj,
|
---|
31 | uniform float4x4 worldView)
|
---|
32 | {
|
---|
33 |
|
---|
34 | VS_OUT OUT;
|
---|
35 |
|
---|
36 | OUT.hPosition = mul(worldViewProj, position);
|
---|
37 | OUT.Position = mul(worldView, position);
|
---|
38 |
|
---|
39 | return OUT;
|
---|
40 | }
|
---|
41 |
|
---|
42 | float4 DepthDistPS(VS_OUT IN,
|
---|
43 | uniform float farPlane ):COLOR
|
---|
44 | {
|
---|
45 | float dist = length(IN.Position.xyz) / farPlane;
|
---|
46 | return float4(dist, dist * dist, 1, 1);
|
---|
47 | //return farPlane;
|
---|
48 | }
|
---|
49 |
|
---|
50 | ///////////////Shadow
|
---|
51 |
|
---|
52 | struct VS_OUT_SHADOW
|
---|
53 | {
|
---|
54 | float4 hPosition : POSITION;
|
---|
55 | float4 Position : TEXCOORD0;
|
---|
56 | float4 lPosition : TEXCOORD1;
|
---|
57 | float4 lVPosition : TEXCOORD2;
|
---|
58 | };
|
---|
59 |
|
---|
60 | VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
|
---|
61 | uniform float4x4 worldViewProj,
|
---|
62 | uniform float4x4 world,
|
---|
63 | uniform float4x4 lightViewProj)
|
---|
64 | {
|
---|
65 | VS_OUT_SHADOW OUT;
|
---|
66 | OUT.hPosition = mul(worldViewProj, position);
|
---|
67 | float4 wPos = mul(world, position);
|
---|
68 | OUT.lPosition = mul(lightViewProj, wPos);
|
---|
69 | OUT.lVPosition = 0;
|
---|
70 | OUT.Position = position;
|
---|
71 | return OUT;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
76 | uniform float4x4 lightViewProj,
|
---|
77 | uniform sampler2D depthShadowMap : register(s0)
|
---|
78 | ):COLOR
|
---|
79 | {
|
---|
80 | float bias = 0.001;
|
---|
81 | float4 light = float4(1,1,1,1);
|
---|
82 | float4 shadow = float4(0.25,0.25,0.25,1);
|
---|
83 | //float4 shadow = float4(0,0,0,1);
|
---|
84 |
|
---|
85 | if(IN.lPosition.z > 0.0)
|
---|
86 | {
|
---|
87 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
88 |
|
---|
89 | if(length(pos.xy)>1)
|
---|
90 | light = shadow;
|
---|
91 | else
|
---|
92 | {
|
---|
93 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
94 | pos.y = 1.0 - pos.y;
|
---|
95 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
96 |
|
---|
97 |
|
---|
98 | float M1 = storedDepth.r;
|
---|
99 | float M2 = storedDepth.g;
|
---|
100 | float v2 = M2 - M1 * M1;
|
---|
101 | float pmax = v2 / (v2 + pow(M1 - pos.z, 2));
|
---|
102 | light = saturate(shadow + (1 - shadow) * pmax);
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|
107 | else
|
---|
108 | light = shadow;
|
---|
109 |
|
---|
110 | return light;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 | VS_OUT_SHADOW distShadowVS(float4 position : POSITION,
|
---|
117 | uniform float4x4 worldViewProj,
|
---|
118 | uniform float4x4 world,
|
---|
119 | uniform float4x4 lightView,
|
---|
120 | uniform float4x4 lightViewProj)
|
---|
121 | {
|
---|
122 | VS_OUT_SHADOW OUT;
|
---|
123 | OUT.hPosition = mul(worldViewProj, position);
|
---|
124 | float4 wPos = mul(world, position);
|
---|
125 | OUT.lPosition = mul(lightViewProj, wPos);
|
---|
126 | OUT.lVPosition = mul(lightView, wPos);
|
---|
127 | OUT.Position = position;
|
---|
128 | return OUT;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | float4 distShadowPS(VS_OUT_SHADOW IN,
|
---|
133 | uniform float4x4 lightViewProj,
|
---|
134 | uniform float lightFarPlane,
|
---|
135 | uniform sampler2D depthShadowMap : register(s0)
|
---|
136 | ):COLOR
|
---|
137 | {
|
---|
138 | float bias = 0.001;
|
---|
139 | float epsilon = 0.001;
|
---|
140 | float4 light = float4(1,1,1,1);
|
---|
141 | float4 shadow = float4(0.85,0.85,0.85,1);
|
---|
142 |
|
---|
143 | if(IN.lPosition.z > 0.0)
|
---|
144 | {
|
---|
145 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
146 |
|
---|
147 | float d = length(pos.xy);
|
---|
148 |
|
---|
149 | light = saturate((1.0 - d)/0.05);
|
---|
150 |
|
---|
151 | if(d <= 1.0)
|
---|
152 | {
|
---|
153 | float dist = length(IN.lVPosition.xyz) / lightFarPlane;
|
---|
154 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
155 | pos.y = 1.0 - pos.y;
|
---|
156 | float4 storedDist = tex2D(depthShadowMap, pos.xy);
|
---|
157 | dist -= bias;
|
---|
158 | float lit_factor = light * (dist <= storedDist.r);
|
---|
159 |
|
---|
160 | float M1 = storedDist.r;
|
---|
161 | float M2 = storedDist.g;
|
---|
162 | float v2 = min(max(M2 - M1 * M1, 0.0) + epsilon, 1.0);
|
---|
163 | float m_d = M1 - dist;
|
---|
164 | float pmax = v2 / (v2 + m_d * m_d);
|
---|
165 |
|
---|
166 | // Adjust the light color based on the shadow attenuation
|
---|
167 | light = max(lit_factor, pmax);
|
---|
168 |
|
---|
169 | }
|
---|
170 |
|
---|
171 | }
|
---|
172 | else
|
---|
173 | light = 0;
|
---|
174 |
|
---|
175 | return shadow + (1 - shadow) * light;
|
---|
176 | }
|
---|
177 | /*
|
---|
178 | float4 distShadowPS(VS_OUT_SHADOW IN,
|
---|
179 | uniform float4x4 lightViewProj,
|
---|
180 | uniform float lightFarPlane,
|
---|
181 | uniform sampler2D depthShadowMap : register(s0)
|
---|
182 | ):COLOR
|
---|
183 | {
|
---|
184 | float bias = 0.001;
|
---|
185 | float epsilon = 0.001;
|
---|
186 | float4 light = float4(1,1,1,1);
|
---|
187 | float4 shadow = float4(0.65,0.65,0.65,1);
|
---|
188 |
|
---|
189 | if(IN.lPosition.z > 0.0)
|
---|
190 | {
|
---|
191 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
192 |
|
---|
193 | float d = length(pos.xy);
|
---|
194 |
|
---|
195 | light = saturate((1.0 - d)/0.05);
|
---|
196 |
|
---|
197 | if(d <= 1.0)
|
---|
198 | {
|
---|
199 | float dist = length(IN.lVPosition.xyz) / lightFarPlane;
|
---|
200 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
201 | pos.y = 1.0 - pos.y;
|
---|
202 | float4 storedDist = tex2D(depthShadowMap, pos.xy);
|
---|
203 |
|
---|
204 | light = storedDist.r + dist*0.000000000001;
|
---|
205 |
|
---|
206 | }
|
---|
207 |
|
---|
208 | }
|
---|
209 | else
|
---|
210 | light = 0;
|
---|
211 |
|
---|
212 | return shadow + (1 - shadow) * light;
|
---|
213 | }
|
---|
214 | */
|
---|
215 | /*
|
---|
216 | float4 depthShadowPS(VS_OUT_SHADOW IN,
|
---|
217 | uniform float4x4 lightViewProj,
|
---|
218 | uniform sampler2D depthShadowMap : register(s0)
|
---|
219 | ):COLOR
|
---|
220 | {
|
---|
221 | float bias = 0.0001;
|
---|
222 | float4 light = float4(1,1,1,1);
|
---|
223 | float4 shadow = float4(0.95,0.95,0.95,1);
|
---|
224 | //float4 shadow = float4(0,0,0,1);
|
---|
225 |
|
---|
226 | if(IN.lPosition.z > 0.0)
|
---|
227 | {
|
---|
228 | float4 pos = (IN.lPosition / IN.lPosition.w);
|
---|
229 |
|
---|
230 | if(length(pos.xy)>1)
|
---|
231 | light = shadow;
|
---|
232 | else
|
---|
233 | {
|
---|
234 | pos.xy = (pos.xy + 1.0) / 2.0;
|
---|
235 | pos.y = 1.0 - pos.y;
|
---|
236 | float4 storedDepth = tex2D(depthShadowMap, pos.xy);
|
---|
237 |
|
---|
238 | if(storedDepth.r + bias < pos.z)
|
---|
239 | {
|
---|
240 | light = shadow;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | }
|
---|
245 | else
|
---|
246 | light = shadow;
|
---|
247 |
|
---|
248 |
|
---|
249 | return light;
|
---|
250 | }
|
---|
251 | */ |
---|