1 | struct fragment
|
---|
2 | {
|
---|
3 | // normalized screen position
|
---|
4 | float4 pos: WPOS;
|
---|
5 | float4 texCoord: TEXCOORD0;
|
---|
6 | float3 view: COLOR0;
|
---|
7 | };
|
---|
8 |
|
---|
9 |
|
---|
10 | struct pixel
|
---|
11 | {
|
---|
12 | float4 color: COLOR0;
|
---|
13 | };
|
---|
14 |
|
---|
15 | #define NUM_SAMPLES 16
|
---|
16 |
|
---|
17 |
|
---|
18 | float2 myreflect(float2 pt, float2 n)
|
---|
19 | {
|
---|
20 | // distance to plane
|
---|
21 | float d = dot(n, pt);
|
---|
22 | // reflect around plane
|
---|
23 | float2 rpt = pt - d * 2.0f * n;
|
---|
24 |
|
---|
25 | return rpt;
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | /** function for standard deferred shading
|
---|
30 | */
|
---|
31 | float4 shade(fragment IN,
|
---|
32 | uniform float4 color,
|
---|
33 | uniform float4 position,
|
---|
34 | uniform float3 normal,
|
---|
35 | uniform float amb)
|
---|
36 | {
|
---|
37 | const float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f);
|
---|
38 | const float3 light = normalize(lightDir.xyz);
|
---|
39 | const float diffuseLight = saturate(dot(normal, light));
|
---|
40 |
|
---|
41 | /*
|
---|
42 | float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
|
---|
43 | float3 light2 = normalize(lightDir2.xyz);
|
---|
44 | float diffuseLight2 = saturate(dot(normal, light2));
|
---|
45 | */
|
---|
46 | float diffuse = diffuseLight;// + diffuseLight2;
|
---|
47 |
|
---|
48 | // global ambient
|
---|
49 | const float4 ambient = 0.25f;
|
---|
50 | //const float4 ambient = 0.0f;
|
---|
51 |
|
---|
52 | return (ambient + diffuse) * color * (1.0f - amb) + amb * color;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | /** The mrt shader for standard rendering
|
---|
58 | */
|
---|
59 | pixel main(fragment IN,
|
---|
60 | uniform sampler2D colors,
|
---|
61 | uniform sampler2D positions,
|
---|
62 | uniform sampler2D normals
|
---|
63 | )
|
---|
64 | {
|
---|
65 | pixel OUT;
|
---|
66 |
|
---|
67 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
68 | float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
69 | float4 position = tex2D(positions, IN.texCoord.xy);
|
---|
70 |
|
---|
71 | // an ambient color term
|
---|
72 | float amb = norm.w;
|
---|
73 |
|
---|
74 | float3 normal = normalize(norm.xyz);
|
---|
75 |
|
---|
76 | float4 col = shade(IN, color, position, normal, amb);
|
---|
77 |
|
---|
78 | //OUT.color = float4(1.0f);
|
---|
79 | OUT.color = col;
|
---|
80 | OUT.color.w = color.w;
|
---|
81 |
|
---|
82 | return OUT;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | float CalcShadowTerm(fragment IN,
|
---|
87 | uniform sampler2D shadowMap,
|
---|
88 | uniform float w,
|
---|
89 | uniform float2 lightSpacePos,
|
---|
90 | float depth,
|
---|
91 | uniform float2 samples[NUM_SAMPLES],
|
---|
92 | uniform sampler2D noiseTexture
|
---|
93 | )
|
---|
94 | {
|
---|
95 | float total_d = 0.0;
|
---|
96 |
|
---|
97 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
98 | {
|
---|
99 | const float2 offset = samples[i];
|
---|
100 |
|
---|
101 | #if 1
|
---|
102 | ////////////////////
|
---|
103 | //-- add random noise: reflect around random normal vector (warning: slow!)
|
---|
104 |
|
---|
105 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
|
---|
106 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
107 | #else
|
---|
108 | const float2 offsetTransformed = offset;
|
---|
109 | #endif
|
---|
110 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
111 | float2 texcoord = lightSpacePos + offsetTransformed * w;
|
---|
112 |
|
---|
113 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
114 |
|
---|
115 | total_d += step(depth, shadowDepth);
|
---|
116 | }
|
---|
117 |
|
---|
118 | total_d /= (float)NUM_SAMPLES;
|
---|
119 |
|
---|
120 | return total_d;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | #if 0
|
---|
125 | /** The mrt shader for standard rendering
|
---|
126 | */
|
---|
127 | pixel main_shadow(fragment IN,
|
---|
128 | uniform sampler2D colors,
|
---|
129 | uniform sampler2D positions,
|
---|
130 | uniform sampler2D normals,
|
---|
131 | uniform sampler2D shadowMap,
|
---|
132 | uniform float4x4 shadowMatrix,
|
---|
133 | uniform float maxDepth,
|
---|
134 | uniform float sampleWidth
|
---|
135 | )
|
---|
136 | {
|
---|
137 | pixel OUT;
|
---|
138 |
|
---|
139 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
140 | float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
141 | float4 position = tex2D(positions, IN.texCoord.xy);
|
---|
142 |
|
---|
143 |
|
---|
144 | // an ambient color term
|
---|
145 | float amb = norm.w;
|
---|
146 |
|
---|
147 | float3 normal = normalize(norm.xyz);
|
---|
148 | float4 col = shade(IN, color, position, normal, amb);
|
---|
149 |
|
---|
150 | position *= maxDepth;
|
---|
151 | position.w = 1.0f;
|
---|
152 |
|
---|
153 | float4 lightSpacePos = mul(shadowMatrix, position);
|
---|
154 | lightSpacePos /= lightSpacePos.w;
|
---|
155 |
|
---|
156 | OUT.color = col;
|
---|
157 |
|
---|
158 | float shadowDepth[9];
|
---|
159 |
|
---|
160 | float w = sampleWidth;
|
---|
161 |
|
---|
162 | // pcf sampling using 3 x 3 tab
|
---|
163 | shadowDepth[0] = tex2D(shadowMap, lightSpacePos.xy).x;
|
---|
164 |
|
---|
165 | shadowDepth[1] = tex2D(shadowMap, lightSpacePos.xy + float2( w, w)).x;
|
---|
166 | shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy + float2( w, -w)).x;
|
---|
167 | shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy + float2(-w, -w)).x;
|
---|
168 | shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy + float2(-w, w)).x;
|
---|
169 |
|
---|
170 | shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy + float2( w, 0)).x;
|
---|
171 | shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy + float2( 0, w)).x;
|
---|
172 | shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy + float2(-w, 0)).x;
|
---|
173 | shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy + float2( 0, -w)).x;
|
---|
174 |
|
---|
175 |
|
---|
176 | float depth = lightSpacePos.z;
|
---|
177 |
|
---|
178 | float d = 0.0f;
|
---|
179 |
|
---|
180 | for (int i = 0; i < 9; ++ i)
|
---|
181 | {
|
---|
182 | d += step(shadowDepth[i], depth);
|
---|
183 | }
|
---|
184 |
|
---|
185 | d /= 9.0f;
|
---|
186 |
|
---|
187 | if (amb < 0.9f) // hack: prevent shadowing the sky
|
---|
188 | {
|
---|
189 | // base lighting
|
---|
190 | const float x = 0.4f;
|
---|
191 | OUT.color *= x + (1.0f - x) * (1.0f - d);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /*
|
---|
195 | // hard shadows
|
---|
196 | float shadowDepth = tex2D(shadowMap, lightSpacePos.xy).x;
|
---|
197 |
|
---|
198 | if ((amb < 0.9f) && // hack: prevent shadowing the sky
|
---|
199 | (lightSpacePos.z > shadowDepth))
|
---|
200 | {
|
---|
201 | OUT.color *= 0.1f;
|
---|
202 | }*/
|
---|
203 |
|
---|
204 | OUT.color.w = color.w;
|
---|
205 |
|
---|
206 | return OUT;
|
---|
207 | }
|
---|
208 |
|
---|
209 | #else
|
---|
210 |
|
---|
211 |
|
---|
212 | pixel main_shadow(fragment IN,
|
---|
213 | uniform sampler2D colors,
|
---|
214 | uniform sampler2D positions,
|
---|
215 | uniform sampler2D normals,
|
---|
216 | uniform sampler2D shadowMap,
|
---|
217 | uniform float4x4 shadowMatrix,
|
---|
218 | uniform float maxDepth,
|
---|
219 | uniform float sampleWidth,
|
---|
220 | uniform sampler2D noiseTexture,
|
---|
221 | uniform float2 samples[NUM_SAMPLES]
|
---|
222 | )
|
---|
223 | {
|
---|
224 | pixel OUT;
|
---|
225 |
|
---|
226 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
227 | float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
228 | float4 position = tex2D(positions, IN.texCoord.xy);
|
---|
229 |
|
---|
230 | const float3 normal = normalize(norm.xyz);
|
---|
231 | const float3 lightDir = normalize(float3(0.8f, -1.0f, 0.7f));
|
---|
232 |
|
---|
233 | // hack: an emmisive color term
|
---|
234 | float emmisive = norm.w;
|
---|
235 |
|
---|
236 | // diffuse intensity
|
---|
237 | const float angle = saturate(dot(normal, lightDir));
|
---|
238 | float diffuse = angle;
|
---|
239 |
|
---|
240 | // calc diffuse illumination + shadow term
|
---|
241 | if ((emmisive < 0.95f) // hack: prevent shadowing the sky
|
---|
242 | && (angle > 1e-3f)
|
---|
243 | )
|
---|
244 | {
|
---|
245 | position *= maxDepth;
|
---|
246 | position.w = 1.0f;
|
---|
247 |
|
---|
248 | float4 lightSpacePos = mul(shadowMatrix, position);
|
---|
249 | lightSpacePos /= lightSpacePos.w;
|
---|
250 |
|
---|
251 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
|
---|
252 |
|
---|
253 | diffuse *= shadowTerm;
|
---|
254 | }
|
---|
255 |
|
---|
256 | // global ambient
|
---|
257 | const float4 ambient = 0.25f;
|
---|
258 |
|
---|
259 | // base lighting
|
---|
260 | OUT.color = (ambient + diffuse) * color * (1.0f - emmisive) + emmisive * color;
|
---|
261 |
|
---|
262 | // also write out depth component
|
---|
263 | OUT.color.w = color.w;
|
---|
264 |
|
---|
265 | return OUT;
|
---|
266 | }
|
---|
267 |
|
---|
268 | #endif
|
---|