1 | float3 F0;
|
---|
2 |
|
---|
3 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
4 | {
|
---|
5 | float4 color = texCUBElod( cm, float4(coord.xy, - coord.z,0) );
|
---|
6 | return color;
|
---|
7 | }
|
---|
8 |
|
---|
9 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
10 | {
|
---|
11 | float dist = texCUBElod(dcm, float4(coord.xy, - coord.z,0)).a;
|
---|
12 | return dist;
|
---|
13 | }
|
---|
14 |
|
---|
15 | #define MAX_LIN_ITERATIONCOUNT 30 //80
|
---|
16 | #define MIN_LIN_ITERATIONCOUNT 25 //60
|
---|
17 | #define SECANT_ITERATIONCOUNT 1
|
---|
18 | #define MAX_RAY_DEPTH 2
|
---|
19 |
|
---|
20 | /*
|
---|
21 | void linearSearch( float3 x, float3 R, float3 N, samplerCUBE mp,
|
---|
22 | out float3 p,
|
---|
23 | out float dl,
|
---|
24 | out float dp,
|
---|
25 | out float llp,
|
---|
26 | out float ppp)
|
---|
27 | {
|
---|
28 | float3 Ra = abs(R), xa = abs(x);
|
---|
29 | float xm = max(max(xa.x,xa.y),xa.z);
|
---|
30 | float Rm = max(max(Ra.x,Ra.y),Ra.z);
|
---|
31 | float a = xm / Rm;
|
---|
32 |
|
---|
33 | bool undershoot = false, overshoot = false;
|
---|
34 |
|
---|
35 | float dt = length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT;
|
---|
36 | dt = max(dt, MIN_LIN_ITERATIONCOUNT);
|
---|
37 | dt = 1.0 / dt;
|
---|
38 |
|
---|
39 | float t = 0.01;//dt;
|
---|
40 | float pa;
|
---|
41 | bool first = true;
|
---|
42 | //Linear iteration
|
---|
43 | while(t <= 1.0 && !overshoot)
|
---|
44 | {
|
---|
45 | dp = a * t / (1 - t);
|
---|
46 | p = x + R * dp;
|
---|
47 | pa = readDistanceCubeMap(mp, p);
|
---|
48 | if(pa > 0)
|
---|
49 | {
|
---|
50 | ppp = length(p) / pa;
|
---|
51 | if(ppp < 1)
|
---|
52 | {
|
---|
53 | dl = dp;
|
---|
54 | llp = ppp;
|
---|
55 | undershoot = true;
|
---|
56 | }
|
---|
57 | else if(undershoot)
|
---|
58 | overshoot = true;
|
---|
59 | }
|
---|
60 | else
|
---|
61 | undershoot = false;
|
---|
62 | t += dt;
|
---|
63 | }
|
---|
64 |
|
---|
65 | if(!overshoot)
|
---|
66 | p = float3(0,0,0);
|
---|
67 | }
|
---|
68 | */
|
---|
69 |
|
---|
70 |
|
---|
71 | void linearSearch( float3 x, float3 R, float3 N, samplerCUBE mp,
|
---|
72 | float2 minMax,
|
---|
73 | out float3 p,
|
---|
74 | out float dl,
|
---|
75 | out float dp,
|
---|
76 | out float llp,
|
---|
77 | out float ppp)
|
---|
78 | {
|
---|
79 |
|
---|
80 | int shootL = 0, shootP = 0;
|
---|
81 | bool found = false;
|
---|
82 |
|
---|
83 | float min = minMax.x;
|
---|
84 | float max = minMax.y;
|
---|
85 |
|
---|
86 | float minT = 0;
|
---|
87 | float maxT = 0;
|
---|
88 | {
|
---|
89 | float a = dot(R,R);
|
---|
90 | float b = 2 * dot(R, x);
|
---|
91 | float c = dot(x,x) - min * min;
|
---|
92 | float c2 = dot(x,x) - max * max;
|
---|
93 |
|
---|
94 | float dis = b*b - 4*a*c;
|
---|
95 | float dis2 = b*b - 4*a*c2;
|
---|
96 |
|
---|
97 | float t1 = (-b + sqrt(dis))/ 2.0 * a;
|
---|
98 | float t2 = (-b - sqrt(dis))/ 2.0 * a;
|
---|
99 | float t3 = (-b + sqrt(dis2))/ 2.0 * a;
|
---|
100 | float t4 = (-b - sqrt(dis2))/ 2.0 * a;
|
---|
101 |
|
---|
102 | int numvalids = 0;
|
---|
103 | if(t1 > 0)
|
---|
104 | {
|
---|
105 | numvalids++;
|
---|
106 | if(t1 > maxT)maxT = t1;
|
---|
107 | if(t1 < minT || minT == 0)minT = t1;
|
---|
108 | }
|
---|
109 | if(t2 > 0)
|
---|
110 | {
|
---|
111 | numvalids++;
|
---|
112 | if(t2 > maxT)maxT = t2;
|
---|
113 | if(t2 < minT || minT == 0)minT = t2;
|
---|
114 | }
|
---|
115 | if(t3 > 0)
|
---|
116 | {
|
---|
117 | numvalids++;
|
---|
118 | if(t3 > maxT)maxT = t3;
|
---|
119 | if(t3 < minT || minT == 0)minT = t3;
|
---|
120 | }
|
---|
121 | if(t4 > 0)
|
---|
122 | {
|
---|
123 | numvalids++;
|
---|
124 | if(t4 > maxT)maxT = t4;
|
---|
125 | if(t4 < minT || minT == 0)minT = t4;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if(numvalids % 2 == 1)
|
---|
129 | minT = 0.01;
|
---|
130 | if(numvalids == 0)
|
---|
131 | minT = maxT + 1.0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | int iterationNeeded = MIN_LIN_ITERATIONCOUNT;// + (dot(normalize(x), R) + 1.0) / 2.0 * (MAX_LIN_ITERATIONCOUNT - MIN_LIN_ITERATIONCOUNT);
|
---|
135 | float pa;
|
---|
136 | float dt = (maxT - minT) / (float) iterationNeeded ;
|
---|
137 | dp = minT;
|
---|
138 | //Linear iteration
|
---|
139 | while(dp <= maxT && !found)
|
---|
140 | {
|
---|
141 | p = x + R * dp;
|
---|
142 | pa = readDistanceCubeMap(mp, p);
|
---|
143 |
|
---|
144 | if(pa > 0)
|
---|
145 | {
|
---|
146 | ppp = length(p) / pa;
|
---|
147 | if(ppp < 1)
|
---|
148 | shootP = -1;
|
---|
149 | else
|
---|
150 | shootP = 1;
|
---|
151 | if(shootL * shootP == -1)
|
---|
152 | found = true;
|
---|
153 | else
|
---|
154 | {
|
---|
155 | shootL = shootP;
|
---|
156 | dl = dp;
|
---|
157 | llp = ppp;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | else
|
---|
161 | shootL = 0;
|
---|
162 |
|
---|
163 | dp += dt;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if(!found)
|
---|
167 | p = float3(0,0,0);
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | void secantSearch(float3 x, float3 R, samplerCUBE mp,
|
---|
172 | float dl,
|
---|
173 | float dp,
|
---|
174 | float llp,
|
---|
175 | float ppp,
|
---|
176 | out float3 p)
|
---|
177 | {
|
---|
178 | for(int i= 0; i < SECANT_ITERATIONCOUNT; i++)
|
---|
179 | {
|
---|
180 | float dnew;
|
---|
181 | dnew = dl + (dp - dl) * (1 - llp) / (ppp - llp);
|
---|
182 | p = x + R * dnew;
|
---|
183 | half pppnew = length(p) / readDistanceCubeMap(mp, p);
|
---|
184 | if(pppnew < 1)
|
---|
185 | {
|
---|
186 | llp = pppnew;
|
---|
187 | dl = dnew;
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | ppp = pppnew;
|
---|
192 | dp = dnew;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | float3 Hit(float3 x, float3 R, float3 N,
|
---|
198 | samplerCUBE mp1,
|
---|
199 | samplerCUBE mp2,
|
---|
200 | samplerCUBE mp3Color,
|
---|
201 | samplerCUBE mp3Dist,
|
---|
202 | float2 minMax,
|
---|
203 | float2 minMax1,
|
---|
204 | float2 minMax2,
|
---|
205 | out float4 Il, out float3 Nl)
|
---|
206 | {
|
---|
207 | float dl1 = 0, dp1, llp1, ppp1;
|
---|
208 | float3 p1;
|
---|
209 | linearSearch(x, R, N, mp1, minMax1, p1, dl1, dp1, llp1, ppp1);
|
---|
210 | float dl2 = 0, dp2, llp2, ppp2;
|
---|
211 | float3 p2;
|
---|
212 | linearSearch(x, R, N, mp2, minMax2, p2, dl2, dp2, llp2, ppp2);
|
---|
213 |
|
---|
214 | bool valid1 = dot(p1,p1) != 0;
|
---|
215 | bool valid2 = dot(p2,p2) != 0;
|
---|
216 |
|
---|
217 | float dl, dp, llp, ppp;
|
---|
218 | float3 p;
|
---|
219 |
|
---|
220 | if(!valid1 && ! valid2)
|
---|
221 | {
|
---|
222 | linearSearch(x, R, N, mp3Dist, minMax, p, dl, dp, llp, ppp);
|
---|
223 | if(dot(p,p) != 0)
|
---|
224 | {
|
---|
225 | Il.a = 1;
|
---|
226 | secantSearch(x, R, mp3Dist, dl, dp, llp, ppp, p);
|
---|
227 | Il.rgb = Nl.rgb = readCubeMap(mp3Color, p).rgb;
|
---|
228 | }
|
---|
229 | else
|
---|
230 | Il = float4(0,0,0,0);
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | if( !valid2 || (valid1 && dp1 < dp2))
|
---|
235 | {
|
---|
236 | secantSearch(x, R, mp1, dl1, dp1, llp1, ppp1, p1);
|
---|
237 | Il.rgb = Nl.rgb = readCubeMap(mp1, p1).rgb;
|
---|
238 | p = p1;
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 | secantSearch(x, R, mp2, dl2, dp2, llp2, ppp2, p2);
|
---|
243 | Il.rgb = Nl.rgb = readCubeMap(mp2, p2).rgb;
|
---|
244 | p = p2;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | return p;
|
---|
248 | }
|
---|
249 |
|
---|
250 | struct VertOut
|
---|
251 | {
|
---|
252 | float3 wPos : TEXCOORD1;
|
---|
253 | float3 cPos : TEXCOORD3;
|
---|
254 | float2 texCoord : TEXCOORD0;
|
---|
255 | float3 mNormal : TEXCOORD2;
|
---|
256 | float4 hPos : POSITION;
|
---|
257 | };
|
---|
258 |
|
---|
259 | VertOut DefaultVS(float4 position : POSITION,
|
---|
260 | float2 texCoord : TEXCOORD0,
|
---|
261 | float3 normal : NORMAL,
|
---|
262 | uniform float4x4 worldViewProj,
|
---|
263 | uniform float4x4 world,
|
---|
264 | uniform float4x4 worldview,
|
---|
265 | uniform float4x4 worldI )
|
---|
266 | {
|
---|
267 | VertOut OUT;
|
---|
268 | OUT.hPos = mul(worldViewProj, position);
|
---|
269 | OUT.wPos = mul(world, position).xyz;
|
---|
270 | OUT.cPos = mul(worldview, position).xyz;
|
---|
271 | OUT.mNormal = mul(normal, worldI);
|
---|
272 | //OUT.mNormal = normal;
|
---|
273 | OUT.texCoord = texCoord;
|
---|
274 | return OUT;
|
---|
275 | }
|
---|
276 |
|
---|
277 | float4 SingleReflectionPS(VertOut IN,
|
---|
278 | uniform float3 cameraPos,
|
---|
279 | uniform samplerCUBE CubeMap : register(s0),
|
---|
280 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
281 | uniform samplerCUBE NormDistMap1 : register(s2),
|
---|
282 | uniform samplerCUBE NormDistMap2 : register(s3),
|
---|
283 | uniform float3 lastCenter,
|
---|
284 | uniform float refIndex,
|
---|
285 | float2 minMax,
|
---|
286 | float2 minMax1,
|
---|
287 | float2 minMax2
|
---|
288 | )
|
---|
289 | {
|
---|
290 | float4 Color = float4(1,1,1,1);
|
---|
291 |
|
---|
292 | float3 N = normalize(IN.mNormal);
|
---|
293 | float3 newTexCoord;
|
---|
294 | float3 x = IN.wPos - lastCenter;
|
---|
295 | float3 V = (IN.wPos - cameraPos);
|
---|
296 |
|
---|
297 | V = normalize(V);
|
---|
298 |
|
---|
299 | float3 R;
|
---|
300 |
|
---|
301 | R = normalize(reflect( V, N));
|
---|
302 |
|
---|
303 | float3 Nl;
|
---|
304 | float4 Il;
|
---|
305 | float3 l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, minMax, minMax1, minMax2, Il, Nl);
|
---|
306 | if(Il.a == 0)
|
---|
307 | Il = readCubeMap(CubeMap, l);
|
---|
308 |
|
---|
309 | return Il;
|
---|
310 | }
|
---|
311 |
|
---|
312 | #define REFRACTION 0
|
---|
313 |
|
---|
314 | float4 MultipleReflectionPS(VertOut IN,
|
---|
315 | uniform float3 cameraPos,
|
---|
316 | uniform samplerCUBE CubeMap : register(s0),
|
---|
317 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
318 | uniform samplerCUBE NormDistMap1 : register(s2),
|
---|
319 | uniform samplerCUBE NormDistMap2 : register(s3),
|
---|
320 | uniform float3 lastCenter,
|
---|
321 | uniform float refIndex,
|
---|
322 | float2 minMax,
|
---|
323 | float2 minMax1,
|
---|
324 | float2 minMax2)
|
---|
325 | {
|
---|
326 | float4 I = float4(0,0,0,0);
|
---|
327 |
|
---|
328 | float3 N = normalize(IN.mNormal);
|
---|
329 | float3 x = IN.wPos - lastCenter;
|
---|
330 | float3 V = (IN.wPos - cameraPos);
|
---|
331 |
|
---|
332 | float Fp = 0.04;
|
---|
333 | float F = Fp + pow(1 - dot(N, -V), 5) * (1 - Fp);
|
---|
334 |
|
---|
335 | V = normalize(V);
|
---|
336 | float3 l;
|
---|
337 | float4 Irefl;
|
---|
338 | int depth = 0;
|
---|
339 |
|
---|
340 | while(depth < MAX_RAY_DEPTH)
|
---|
341 | {
|
---|
342 | float3 R;
|
---|
343 | R = normalize(reflect( V, N));
|
---|
344 |
|
---|
345 | float3 Nl;
|
---|
346 | float4 Il;
|
---|
347 | l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, minMax, minMax1, minMax2, Il, Nl);
|
---|
348 | if(Il.a == 0)
|
---|
349 | {
|
---|
350 | depth += 1;
|
---|
351 | }
|
---|
352 | else
|
---|
353 | {
|
---|
354 | I = Il;
|
---|
355 | depth = MAX_RAY_DEPTH;
|
---|
356 | }
|
---|
357 | x = l;
|
---|
358 | N = Nl;
|
---|
359 | V = R;
|
---|
360 |
|
---|
361 | }
|
---|
362 | if(I.a == 0)
|
---|
363 | I = readCubeMap(CubeMap, l);
|
---|
364 | Irefl = I;
|
---|
365 |
|
---|
366 | if(REFRACTION)
|
---|
367 | {
|
---|
368 | float4 Irefr;
|
---|
369 | N = normalize(IN.mNormal);
|
---|
370 | x = IN.wPos - lastCenter;
|
---|
371 | V = (IN.wPos - cameraPos);
|
---|
372 | depth = 0;
|
---|
373 | while(depth < MAX_RAY_DEPTH)
|
---|
374 | {
|
---|
375 | float3 R;
|
---|
376 | //R = normalize(reflect( V, N));
|
---|
377 |
|
---|
378 |
|
---|
379 | float ri = refIndex;
|
---|
380 |
|
---|
381 | if(dot(V,N) > 0)
|
---|
382 | {
|
---|
383 | ri = 1.0 / ri;
|
---|
384 | N = -N;
|
---|
385 | }
|
---|
386 | R = refract( V, N, ri);
|
---|
387 | if(dot(R,R) == 0) R = reflect( V, N);
|
---|
388 |
|
---|
389 |
|
---|
390 | float3 Nl;
|
---|
391 | float4 Il;
|
---|
392 | l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, minMax, minMax1, minMax2, Il, Nl);
|
---|
393 | if(Il.a == 0)
|
---|
394 | {
|
---|
395 | depth += 1;
|
---|
396 | }
|
---|
397 | else
|
---|
398 | {
|
---|
399 | I = Il;
|
---|
400 | depth = MAX_RAY_DEPTH;
|
---|
401 | }
|
---|
402 | x = l;
|
---|
403 | N = normalize(Nl);
|
---|
404 | V = R;
|
---|
405 | }
|
---|
406 |
|
---|
407 | if(I.a == 0)
|
---|
408 | I = readCubeMap(CubeMap, l);
|
---|
409 | Irefr = I;
|
---|
410 |
|
---|
411 | I = Irefl * F + (1.0 - F) * Irefr;
|
---|
412 | }
|
---|
413 | return I;
|
---|
414 | }
|
---|
415 | /*
|
---|
416 | float4 mainPS(VertOut IN,
|
---|
417 | uniform float3 cameraPos,
|
---|
418 | uniform samplerCUBE CubeMap : register(s0),
|
---|
419 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
420 | uniform samplerCUBE NormDistMap1 : register(s2),
|
---|
421 | uniform samplerCUBE NormDistMap2 : register(s3),
|
---|
422 | uniform float3 lastCenter,
|
---|
423 | uniform float SingleBounce,
|
---|
424 | uniform float refIndex,
|
---|
425 | uniform float4 minmax,
|
---|
426 | uniform float4 minmax1,
|
---|
427 | uniform float4 minmax2
|
---|
428 | ):COLOR
|
---|
429 | {
|
---|
430 | float2 miniMaxi1;
|
---|
431 | miniMaxi1.x = 1.0 / minmax.x / 1.8;
|
---|
432 | miniMaxi1.y = minmax.y * 1.8;
|
---|
433 |
|
---|
434 | float2 miniMaxi2;
|
---|
435 | miniMaxi2.x = 1.0 / minmax1.x / 1.8;
|
---|
436 | miniMaxi2.y = minmax1.y * 1.8;
|
---|
437 |
|
---|
438 | float2 miniMaxi3;
|
---|
439 | miniMaxi3.x = 1.0 / minmax2.x / 1.8;
|
---|
440 | miniMaxi3.y = minmax2.y * 1.8;
|
---|
441 |
|
---|
442 | float4 Color = 1.0;
|
---|
443 | if(SingleBounce == 1)
|
---|
444 | Color = SingleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, miniMaxi1, miniMaxi2, miniMaxi3);
|
---|
445 | else
|
---|
446 | Color = MultipleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, miniMaxi1, miniMaxi2, miniMaxi3);
|
---|
447 |
|
---|
448 | return Color;
|
---|
449 | }
|
---|
450 | */
|
---|
451 |
|
---|
452 | float4 mainPS(VertOut IN,
|
---|
453 | uniform float3 cameraPos,
|
---|
454 | uniform samplerCUBE CubeMap : register(s0),
|
---|
455 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
456 | uniform samplerCUBE NormDistMap1 : register(s2),
|
---|
457 | uniform samplerCUBE NormDistMap2 : register(s3),
|
---|
458 | uniform float3 lastCenter,
|
---|
459 | uniform float SingleBounce,
|
---|
460 | uniform float refIndex,
|
---|
461 | uniform float4 min,
|
---|
462 | uniform float4 min1,
|
---|
463 | uniform float4 min2,
|
---|
464 | uniform float4 max,
|
---|
465 | uniform float4 max1,
|
---|
466 | uniform float4 max2
|
---|
467 | ):COLOR
|
---|
468 | {
|
---|
469 | float2 miniMaxi1;
|
---|
470 | miniMaxi1.x = min.a;
|
---|
471 | miniMaxi1.y = max.a;
|
---|
472 |
|
---|
473 | float2 miniMaxi2;
|
---|
474 | miniMaxi2.x = min1.a;
|
---|
475 | miniMaxi2.y = max1.a;
|
---|
476 |
|
---|
477 | float2 miniMaxi3;
|
---|
478 | miniMaxi3.x = min2.a;
|
---|
479 | miniMaxi3.y = max2.a;
|
---|
480 |
|
---|
481 | float4 Color = 1.0;
|
---|
482 | if(SingleBounce == 1)
|
---|
483 | Color = SingleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, miniMaxi1, miniMaxi2, miniMaxi3);
|
---|
484 | else
|
---|
485 | Color = MultipleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, miniMaxi1, miniMaxi2, miniMaxi3);
|
---|
486 |
|
---|
487 | return Color;
|
---|
488 | } |
---|